V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
dielianxiang
V2EX  ›  Linux

Linux 求教:多核 CPU 电脑,指定不同的进程(应用)使用不同的 CPU 运行。

  •  
  •   dielianxiang · 2018-07-11 16:22:28 +08:00 · 4042 次点击
    这是一个创建于 2087 天前的主题,其中的信息可能已经有所发展或是发生改变。

    事情是这样的:

    公司有一个项目,需要实时传输视频,然后对视频的每一帧进行图像处理。而图像处理或者其他程序运行后,cpu 会突然高一阵子,这就导致视频传输的进程的时间片被抢占,导致丢包。

    故想如何避免这个问题?

    目前我的想法是视频传输进程使用单独的 CPU,其他程序使用其他的 CPU,应该就不会发生这样的问题了。那么问题来了,linux 里面如何实现呢?

    19 条回复    2018-07-12 16:50:02 +08:00
    coderfox
        1
    coderfox  
       2018-07-11 16:45:02 +08:00 via Android
    限制一下视频处理的线程数更简单吧。
    dielianxiang
        2
    dielianxiang  
    OP
       2018-07-11 16:46:32 +08:00
    @coderfox 这两个程序都是必要的。改动的可能性不大。
    ryd994
        3
    ryd994  
       2018-07-11 16:51:45 +08:00   ❤️ 2
    taskset
    但是我觉得你这种情况用 nice 和或 chrt 更好,因为 taskset 只能保证这个进程不抢其他人,不代表其他人不抢它。
    虽然也可以用 isolcpu,但是你这种情况应该还用不上
    privil
        4
    privil  
       2018-07-11 17:26:58 +08:00   ❤️ 3
    cpulimit cgroups 了解一下
    danc
        5
    danc  
       2018-07-11 17:27:09 +08:00
    一定是你的机器不信,加机器配置呗
    lihongjie0209
        6
    lihongjie0209  
       2018-07-11 17:29:04 +08:00
    如果可以的话, 把图像处理和视频传输分散到两台机器上, 中间用消息队列

    关于丢包, 这个要具体分析了:
    1. 什么协议
    2. 什么情况下丢包
    lihongjie0209
        7
    lihongjie0209  
       2018-07-11 17:29:40 +08:00
    @privil #4 这个不错
    linyinma
        8
    linyinma  
       2018-07-11 17:32:03 +08:00
    CPU 亲和性设置

    /* API */
    /* Set the CPU affinity for a task */
    int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
    /* Get the CPU affinity for a task */
    int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);

    /* set CPU affinity attribute in thread attributes object */
    int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset);
    /* get CPU affinity attribute in thread attributes object */
    int pthread_attr_getaffinity_np(const pthread_attr_t *attr, size_t cpusetsize, cpu_set_t *cpuset);

    /* set CPU affinity of a thread */
    int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset);
    /* get CPU affinity of a thread */
    int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset);
    a7a2
        9
    a7a2  
       2018-07-11 17:32:15 +08:00
    直接 使用命令 nice 修改改变进程优先级 即可
    sleeperqp
        10
    sleeperqp  
       2018-07-11 17:44:45 +08:00 via iPhone
    cpu 亲和性+1
    henglinli
        11
    henglinli  
       2018-07-11 21:17:46 +08:00 via iPhone
    估计是监控视频的人脸识别
    tempdban
        12
    tempdban  
       2018-07-12 00:29:10 +08:00 via Android
    补充一点 内核 boot 参数要设好核隔离。
    你这个稍微有点性能优化的意思,提醒一句 SIMD 用了没?
    msg7086
        13
    msg7086  
       2018-07-12 04:08:13 +08:00
    X 问题:如何防止重要进程的 CPU 被抢占。
    Y 问题:如何指定进程使用不同的 CPU 运行。

    XY 问题了解一下。
    guanhui07
        14
    guanhui07  
       2018-07-12 08:33:11 +08:00
    cpulimit
    defel
        15
    defel  
       2018-07-12 09:38:19 +08:00 via iPhone
    slurm 可以解决,直接指定计算节点。
    dielianxiang
        16
    dielianxiang  
    OP
       2018-07-12 09:38:41 +08:00
    我都试试 谢谢各位,结果我后面根据测试结论告知大家
    dielianxiang
        17
    dielianxiang  
    OP
       2018-07-12 09:39:13 +08:00
    @henglinli 比这个高级多了
    checgg
        18
    checgg  
       2018-07-12 10:34:46 +08:00
    docker 直接跑 docker 里面满足你的要求吗
    freemon
        19
    freemon  
       2018-07-12 16:50:02 +08:00
    cpu 亲和性,可以先做 cpu isolate,然后设置 affinity 指定进程绑定一个或者一组 cpu
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1043 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:27 · PVG 06:27 · LAX 15:27 · JFK 18:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.