
事情是这样的:
公司有一个项目,需要实时传输视频,然后对视频的每一帧进行图像处理。而图像处理或者其他程序运行后,cpu 会突然高一阵子,这就导致视频传输的进程的时间片被抢占,导致丢包。
故想如何避免这个问题?
目前我的想法是视频传输进程使用单独的 CPU,其他程序使用其他的 CPU,应该就不会发生这样的问题了。那么问题来了,linux 里面如何实现呢?
1 coderfox 2018-07-11 16:45:02 +08:00 via Android 限制一下视频处理的线程数更简单吧。 |
2 dielianxiang OP @coderfox 这两个程序都是必要的。改动的可能性不大。 |
3 ryd994 2018-07-11 16:51:45 +08:00 taskset 但是我觉得你这种情况用 nice 和或 chrt 更好,因为 taskset 只能保证这个进程不抢其他人,不代表其他人不抢它。 虽然也可以用 isolcpu,但是你这种情况应该还用不上 |
4 privil 2018-07-11 17:26:58 +08:00 cpulimit cgroups 了解一下 |
5 danc 2018-07-11 17:27:09 +08:00 一定是你的机器不信,加机器配置呗 |
6 lihongjie0209 2018-07-11 17:29:04 +08:00 如果可以的话, 把图像处理和视频传输分散到两台机器上, 中间用消息队列 关于丢包, 这个要具体分析了: 1. 什么协议 2. 什么情况下丢包 |
7 lihongjie0209 2018-07-11 17:29:40 +08:00 @privil #4 这个不错 |
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); |
9 a7a2 2018-07-11 17:32:15 +08:00 直接 使用命令 nice 修改改变进程优先级 即可 |
10 sleeperqp 2018-07-11 17:44:45 +08:00 via iPhone cpu 亲和性+1 |
11 henglinli 2018-07-11 21:17:46 +08:00 via iPhone 估计是监控视频的人脸识别 |
12 tempdban 2018-07-12 00:29:10 +08:00 via Android 补充一点 内核 boot 参数要设好核隔离。 你这个稍微有点性能优化的意思,提醒一句 SIMD 用了没? |
13 msg7086 2018-07-12 04:08:13 +08:00 X 问题:如何防止重要进程的 CPU 被抢占。 Y 问题:如何指定进程使用不同的 CPU 运行。 XY 问题了解一下。 |
14 guanhui07 2018-07-12 08:33:11 +08:00 cpulimit |
15 defel 2018-07-12 09:38:19 +08:00 via iPhone slurm 可以解决,直接指定计算节点。 |
16 dielianxiang OP 我都试试 谢谢各位,结果我后面根据测试结论告知大家 |
17 dielianxiang OP @henglinli 比这个高级多了 |
18 checgg 2018-07-12 10:34:46 +08:00 docker 直接跑 docker 里面满足你的要求吗 |
19 freemon 2018-07-12 16:50:02 +08:00 cpu 亲和性,可以先做 cpu isolate,然后设置 affinity 指定进程绑定一个或者一组 cpu |