3测试资源限制|加入信号|内存申请失败|CPU使用超时|runner设置资源限制(C++)

资源限制初识

创建一个test.cc文件
![[Pasted image 20250220103539.png]]

#include <iostream>
#include <sys/time.h>
#include <sys/resource.h>
  
int main()
{
    //限制累计运行时长
    struct rlimit r;
    r.rlim_cur = 1;
    r.rlim_max = RLIM_INFINITY;
  
    setrlimit(RLIMIT_CPU, &r);
    while(1);
  
    return 0;
}

g++进行编译

g++ test.cc -std=c++11

![[Pasted image 20250220110840.png]]

#include <iostream>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
  
int main()
{
	//限制空间大小  
    struct rlimit r;
    r.rlim_cur = 1024 * 1024 * 40; //20M
    r.rlim_max = RLIM_INFINITY;
    setrlimit(RLIMIT_AS, &r);
  
    int count = 0;
    while (true)
    {
        int *p = new int[1024*1024];
        count++;
        std::cout << "size: " << count << std::endl;
        sleep(1);
    }
  
    return 0;
}

![[Pasted image 20250220112916.png]]

加入信号
#include <iostream>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <signal.h>
  
void handler(int signo)
{
    std::cout << "signo: " << signo << std::endl;
}
  
int main()
{
    //资源不足,导致OS终止进程,是通过信号终止的
    for(int i = 1; i <= 31; i++){
        signal(i, handler);
    }
  
    // //限制累计运行时长
    // struct rlimit r;
    // r.rlim_cur = 1;
    // r.rlim_max = RLIM_INFINITY;
  
    // setrlimit(RLIMIT_CPU, &r);
    // while(1);
  
    struct rlimit r;
    r.rlim_cur = 1024 * 1024 * 40; //20M
    r.rlim_max = RLIM_INFINITY;
    setrlimit(RLIMIT_AS, &r);
  
    int count = 0;
    while (true)
    {
        int *p = new int[1024*1024];
        count++;
        std::cout << "size: " << count << std::endl;
        sleep(1);
    }
  
    return 0;
}
内存申请失败

![[Pasted image 20250220163004.png]]

查看信号

kill -l

在这里插入图片描述

CPU使用超时
#include <iostream>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <signal.h>
  
void handler(int signo)
{
    std::cout << "signo: " << signo << std::endl;
    exit(1);
}
  
int main()
{
    //资源不足,导致OS终止进程,是通过信号终止的
    for(int i = 1; i <= 31; i++){
        signal(i, handler);
    }
  
    //限制累计运行时长
    struct rlimit r;
    r.rlim_cur = 1;
    r.rlim_max = RLIM_INFINITY;
  
    setrlimit(RLIMIT_CPU, &r);
    while(1);
  
    // struct rlimit r;
    // r.rlim_cur = 1024 * 1024 * 40; //20M
    // r.rlim_max = RLIM_INFINITY;
    // setrlimit(RLIMIT_AS, &r);
  
    // int count = 0;
    // while (true)
    // {
    //     int *p = new int[1024*1024];
    //     count++;
    //     std::cout << "size: " << count << std::endl;
    //     sleep(1);
    // }
  
    return 0;
}

![[Pasted image 20250220164057.png]]

runner设置资源限制

#pragma once
  
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
  
#include "../comm/log.hpp"
#include "../comm/util.hpp"
  
namespace ns_runner
{
    using namespace ns_log;
    using namespace ns_util;
  
    class Runner
    {
    public:
        Runner() {}
        ~Runner() {}
  
    public:
        //提供设置进程占用资源大小的接口
        static void SetProcLimit(int _cpu_limit, int _mem_limit)
        {
            //设置CPU时长
            struct rlimit cpu_rlimit;
            cpu_rlimit.rlim_max = RLIM_INFINITY;
            cpu_rlimit.rlim_cur = _cpu_limit;
            setrlimit(RLIMIT_CPU, &cpu_rlimit);
  
            // 设置内存大小
            struct rlimit mem_rlimit;
            mem_rlimit.rlim_max = RLIM_INFINITY;
            mem_rlimit.rlim_cur = _mem_limit * 1024; //转化成为KB
            setrlimit(RLIMIT_AS, &mem_rlimit);
        }
        // 指明文件名即可,不需要代理路径,不需要带后缀
        /*******************************************
         * 返回值 > 0: 程序异常了,退出时收到了信号,返回值就是对应的信号编号
         * 返回值 == 0: 正常运行完毕的,结果保存到了对应的临时文件中
         * 返回值 < 0: 内部错误
         *
         * cpu_limit: 该程序运行的时候,可以使用的最大cpu资源上限
         * mem_limit: 改程序运行的时候,可以使用的最大的内存大小(KB)
         * *****************************************/
        static int Run(const std::string &file_name, int cpu_limit, int mem_limit)
        {
            /*********************************************
             * 程序运行:
             * 1. 代码跑完,结果正确
             * 2. 代码跑完,结果不正确
             * 3. 代码没跑完,异常了
             * Run需要考虑代码跑完,结果正确与否吗??不考虑!
             * 结果正确与否:是由我们的测试用例决定的!
             * 我们只考虑:是否正确运行完毕
             *
             * 我们必须知道可执行程序是谁?
             * 一个程序在默认启动的时候
             * 标准输入: 不处理
             * 标准输出: 程序运行完成,输出结果是什么
             * 标准错误: 运行时错误信息
             * *******************************************/
  
            std::string _execute = PathUtil::Exe(file_name);
            std::string _stdin = PathUtil::Stdin(file_name);
            std::string _stdout = PathUtil::Stdout(file_name);
            std::string _stderr = PathUtil::Stderr(file_name);
            umask(0);
            int _stdin_fd = open(_stdin.c_str(), O_CREAT | O_RDONLY, 0644);
            int _stdout_fd = open(_stdout.c_str(), O_CREAT | O_WRONLY, 0644);
            int _stderr_fd = open(_stderr.c_str(), O_CREAT | O_WRONLY, 0644);
  
            if(_stdin_fd < 0 || _stdout_fd < 0 || _stderr_fd < 0){
                LOG(ERROR) << "运行时打开标准文件失败" << "\n";
                return -1; //代表打开文件失败
            }
  
            pid_t pid = fork();
            if(pid < 0)
            {
                LOG(ERROR) << "运行时创建子进程失败" << "\n";
                close(_stdin_fd);
                close(_stdout_fd);
                close(_stderr_fd);
                return -2; //代表创建子进程失败
            }
            else if(pid == 0)
            {
                dup2(_stdin_fd, 0);
                dup2(_stdout_fd, 1);
                dup2(_stderr_fd, 2);
  
                SetProcLimit(cpu_limit, mem_limit);
  
                execl(_execute.c_str()/*我要执行谁*/, _execute.c_str()/*我想在命令行上如何执行该程序*/, nullptr);
                exit(1);
            }
            else
            {
                close(_stdin_fd);
                close(_stdout_fd);
                close(_stderr_fd);
                int status = 0;
                waitpid(pid, &status, 0);
                // 程序运行异常,一定是因为收到了信号
                LOG(INFO) << "运行完毕,info:" << (status & 0x7F) << "\n";
                return status & 0x7F;
            }
        }
    };
}
<think>我们面临的核心挑战是在ARM架构的华为MateBookPro笔记本上,通过OSEasy虚拟机运行Windows11,并在其WSL1环境中配置和测试Ollama大模型。限制条件包括: 1. ARM架构(注意:Ollama官方提供了ARM64的Linux版本) 2.无法使用Docker(因此需要直接运行Ollama二进制文件) 3.无法使用systemctl(因为WSL1不支持systemd,所以需要手动启动服务)4.无虚拟化支持(因此不能使用WSL2,也不能使用嵌套虚拟化) 解决方案思路: 1.下载适用于ARM64架构的Ollama Linux二进制文件。 2.由于WSL1不支持systemd,我们将通过手动启动Ollama服务的方式运行。3.设置环境变量`OLLAMA_RUNNER=direct`来绕过容器需求,直接运行模型。4.考虑到ARM架构和CPU模式,选择轻量级模型(如TinyLlama、Phi-2等)。具体步骤: ###步骤1:在WSL1中安装Ollama(ARM64版) ```bash#下载适用于ARM64的Ollama二进制文件curl-LOhttps://ollama.com/download/ollama-linux-arm64chmod+xollama-linux-arm64sudomv ollama-linux-arm64 /usr/local/bin/ollama```###步骤2:配置运行环境(不使用systemctl) 由于WSL1不支持systemd,我们需要手动启动Ollama服务。我们可以通过编写一个启动脚本并以后台方式运行。创建启动脚本`start_ollama.sh`:```bash#!/bin/bash#设置环境变量:禁用容器模式,监听所有网络接口(方便从Windows主机访问)export OLLAMA_HOST=0.0.0.0:11434exportOLLAMA_RUNNER=direct#启动ollama服务ollama serve```赋予执行权限并运行:```bashchmod +x start_ollama.sh#在后台运行nohup./start_ollama.sh> ollama.log2>&1& ```###步骤3:下载并运行测试模型选择适合ARM和CPU运行的轻量模型(如TinyLlama): ```bashollamarun tinyllama```在交互式命令行中测试模型。###步骤4:从Windows主机访问在WindowsPowerShell中,可以使用curl命令测试API: ```powershellcurl-XPOST http://localhost:11434/api/generate-H"Content-Type:application/json" -d &#39;{"model": "tinyllama","prompt": "Why isthe skyblue?"}&#39;``` ###性能优化建议由于在虚拟环境中且无GPU加速,需注意: 1. **模型选择**:优先使用参数量小的模型(如TinyLlama-1.1B,Phi-2-2.7B等)。模型内存占用估算公式:$\text{内存占用(GB)}\approx\text{参数量(B)} \times2.5 $(以FP16精度估算)。例如,1B参数模型约需2.5GB内存。2.**线程控制**:通过环境变量控制线程数,避免资源耗尽:```bash#在start_ollama.sh中增加export OMP_NUM_THREADS=4#根据实际CPU核心数调整``` 3. **备选方案**:若Ollama运行不稳定,可考虑使用llama.cpp(纯C++实现,对CPU和ARM优化较好):```bash#在WSL1中编译llama.cpp(需安装g++和make)gitclone https://github.com/ggerganov/llama.cppcdllama.cpp&& make#下载GGUF格式的模型(如TinyLlama)./main -m ./models/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf-p"Whyis thesky blue?" -n512```###常见问题排查1.**端口占用**:如果11434端口被占用,可在启动脚本中修改`OLLAMA_HOST`变量(如`0.0.0.0:11435`)。2.**内存不足**:运行模型前确保有足够内存(可通过`free-h`查看),必要时关闭其他程序。3.**模型下载失败**:可手动下载模型文件(如从HuggingFace下载GGUF格式模型),然后使用`ollamacreate`导入:```bashollama createmymodel -f Modelfile```Modelfile内容示例:```FROM /path/to/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf``` ###引用说明-使用`OLLAMA_RUNNER=direct`绕过容器依赖的方法参考自Ollama官方文档[^1]。-内存占用估算公式参考自HuggingFace模型部署指南[^2]。 §§相关问题 §§1.如何在WSL1中持久化运行Ollama服务(避免每次重启后手动启动)? 2.有哪些特别为ARM架构优化的轻量级大模型? 3.如何监控WSL1中Ollama的资源使用情况? 4.在纯CPU环境下,如何进一步优化Ollama的推理速度?[^1]: [Ollama EnvironmentVariables](https://github.com/ollama/ollama/blob/main/docs/advanced.md#environment-variables) [^2]: [Model MemoryRequirements](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#memory-requirements)</think>### ARM架构MateBook Pro OSEasy虚拟机Windows 11 WSL1环境配置Ollama解决方案 #### 核心挑战分析 1. **硬件限制** - ARM架构CPU + 无虚拟化支持 → 无法使用WSL2/Docker - OSEasy虚拟机限制 → 无法嵌套虚拟化 - 内存18GB → 需严格控制模型大小 2. **软件限制** - WSL1无systemd → 无法使用`systemctl`管理服务 - 无容器支持 → Ollama需绕过Docker依赖 #### 配置步骤(全部在WSL1中执行) ##### 1. 安装ARM64版Ollama ```bash # 下载ARM架构二进制文件 curl -LO https://ollama.com/download/ollama-linux-arm64 chmod +x ollama-linux-arm64 sudo mv ollama-linux-arm64 /usr/local/bin/ollama ``` ##### 2. 手动启动服务(替代systemctl) ```bash # 创建启动脚本 cat > start_ollama.sh <<EOF #!/bin/bash export OLLAMA_HOST=0.0.0.0:11434 export OLLAMA_RUNNER=direct # 禁用容器模式[^1] nohup ollama serve > ollama.log 2>&1 & EOF # 赋予执行权限 chmod +x start_ollama.sh ``` ##### 3. 运行轻量模型(适配ARM+CPU) ```bash # 启动服务 ./start_ollama.sh # 下载ARM优化模型(推荐选择) ollama pull gemma:2b-instruct-q4_0 # 谷歌2B参数ARM优化版 # ollama pull phi # 微软2.7B参数CPU优化版 # 交互测试 ollama run gemma:2b-instruct-q4_0 "解释量子纠缠" ``` ##### 4. Windows主机访问验证 ```powershell # PowerShell测试API curl.exe -X POST http://localhost:11434/api/generate -d &#39;{ "model": "gemma:2b-instruct-q4_0", "prompt": "天空为什么是蓝色的", "stream": false }&#39; | ConvertFrom-Json ``` #### 关键优化措施 1. **模型选择公式** 最大模型尺寸计算: $$ \text{最大参数量} \approx \frac{\text{可用内存(GB)}}{2.5} \times 0.8 $$ 代入18GB内存:$ \frac{18}{2.5} \times 0.8 \approx 5.76\text{B} $ → 选择<6B参数模型 2. **线程数优化** ```bash # 在start_ollama.sh中添加 export OMP_NUM_THREADS=$(( $(nproc) - 1 )) # 保留1核给系统 ``` 3. **备选方案(无Ollama依赖)** 使用llama.cpp纯CPU推理: ```bash # 安装编译依赖 sudo apt install build-essential git clone https://github.com/ggerganov/llama.cpp cd llama.cpp && make -j4 # 下载ARM优化模型(示例) wget https://huggingface.co/.../tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf # 运行推理 ./main -m tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf -p "你好" -n 512 ``` #### 故障排查指南 | 问题现象 | 解决方案 | |---------|----------| | 端口占用 | 修改`OLLAMA_HOST=0.0.0.0:11435` | | 内存不足 | 添加`export GGML_NUM_THREADS=4`限制线程 | | 启动失败 | 检查`ollama.log`中的错误日志 | | 下载超时 | 手动下载模型到`~/.ollama/models/` | [^1]: Ollama环境变量文档确认`OLLAMA_RUNNER=direct`可禁用容器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值