linux:如何指定进程运行的CPU
coolshell最新的文章《性能调优攻略》在“多核CPU调优”章节,提到“我们不能任由操作系统负载均衡,因为我们自己更了解自己的程序,所以,我们可以手动地为其分配CPU核,而不会过多地占用CPU0,或是让我们关键进程和一堆别的进程挤在一起。”。在文章中提到了Linux下的一个工具,taskset,可以设定单个进程运行的CPU。
同时,因为最近在看redis的相关资料,redis作为单进程模型的程序,为了充分利用多核CPU,常常在一台server上会启动多个实例。而为了减少切换的开销,有必要为每个实例指定其所运行的CPU。
下文,将会介绍taskset命令,以及sched_setaffinity系统调用,两者均可以指定进程运行的CPU实例。
1.taskset
taskset是LINUX提供的一个命令(ubuntu系统可能需要自行安装,schedutils package)。他可以让某个程序运行在某个(或)某些CPU上。
以下均以redis-server举例。
1)显示进程运行的CPU
命令taskset -p 21184
显示结果:
pid 21184's current affinity mask: ffffff
注:21184是redis-server运行的pid
显示结果的ffffff实际上是二进制24个低位均为1的bitmask,每一个1对应于1个CPU,表示该进程在24个CPU上运行
2)指定进程运行在某个特定的CPU上
命令taskset -pc 3 21184
显示结果:
pid 21184's current affinity list: 0-23
pid 21184's new affinity list: 3
注:3表示CPU将只会运行在第4个CPU上(从0开始计数)。
3)进程启动时指定CPU
命令taskset -c 1 ./redis-server ../redis.conf
结合这上边三个例子,再看下taskset的manual,就比较清楚了。
OPTIONS
-p, --pid
operate on an existing PID and not launch a new task
-c, --cpu-list
specify a numerical list of processors instead of a bitmask. The list may contain multiple items, separated by comma, and ranges. For example, 0,5,7,9-11.
2.sched_setaffinity系统调用
问题描述
sched_setaffinity可以将某个进程绑定到一个特定的CPU。你比操作系统更了解自己的程序,为了避免调度器愚蠢的调度你的程序,或是为了在多线程程序中避免缓存失效造成的开销,你可能会希望这样做。如下是sched_setaffinity的例子:
/* Short test program to test sched_setaffinity
* (which sets the affinity of processes to processors).
* Compile: gcc sched_setaffinity_test.c
* -o sched_setaffinity_test -lm
* Usage: ./sched_setaffinity_test
*
* Open a "top"-window at the same time and see all the work
* being done on CPU 0 first and after a short wait on CPU 1.
* Repeat with different numbers to make sure, it is not a
* coincidence.
*/ #include <stdio.h>
#include <math.h>
#include <sched.h> double waste_time(long n)
{
double res = 0;
long i = 0;
while(i <n * 200000) {
i++;
res += sqrt (i);
}
return res;
} int main(int argc, char **argv)
{
unsigned long mask = 1; /* processor 0 */ /* bind process to processor 0 */
if (sched_setaffinity(0, sizeof(mask), &mask) <0) {
perror("sched_setaffinity");
} /* waste some time so the work is visible with "top" */
printf ("result: %f\n", waste_time (2000)); mask = 2; /* process switches to processor 1 now */
if (sched_setaffinity(0, sizeof(mask), &mask) <0) {
perror("sched_setaffinity");
} /* waste some more time to see the processor switch */
printf ("result: %f\n", waste_time (2000));
}
根据你CPU的快慢,调整waste_time的参数。然后使用top命令,就可以看到进程在不同CPU之间的切换。(启动top命令后按“1”,可以看到各个CPU的情况)。
父进程和子进程之间会继承对affinity的设置。因此,大胆猜测,taskset实际上是首先执行了sched_setaffinity系统调用,然后fork+exec用户指定的进程。
from:http://www.cnblogs.com/liuhao/archive/2012/06/21/2558069.html
linux:如何指定进程运行的CPU的更多相关文章
- [转] 指定进程运行的CPU
转自:https://www.cnblogs.com/liuhao/archive/2012/06/21/2558069.html coolshell最新的文章<性能调优攻略>在“多核CP ...
- linux下查看进程运行的时间
原文链接:http://www.centoscn.com/CentOS/2014/0403/2724.html 可通过ps 来查看,通过参数 -o 来查看 例: ps -eo pid,tty,user ...
- [Linux] cronjob指定用户运行脚本,并按日期区分输出日志
废话不多说,直接上代码,在root的cronjob,指定nginx用户跑cronjob */1 * * * * su nginx -c "/usr/local/scripts/goods.s ...
- linux 查找指定进程并kill
ps -ef | grep php | grep -v 'grep' | awk '{print $2}'| xargs kill -9
- Linux环境下进程的CPU占用率
阿里云服务器网站:https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=qqwovx6h 文字来源:http://www.s ...
- Nginx 关于进程数 与CPU核心数相等时,进程间切换的代价是最小的-- 绑定CPU核心
在阅读Nginx模块开发与架构模式一书时: "Nginx 上的进程数 与CPU核心数相等时(最好每个worker进程都绑定特定的CPU核心),进程间切换的代价是最小的;" &am ...
- 深入Linux内核架构——进程管理和调度(下)
五.调度器的实现 调度器的任务是在程序之间共享CPU时间,创造并行执行的错觉.该任务可分为调度策略和上下文切换两个不同部分. 1.概观 暂时不考虑实时进程,只考虑CFS调度器.经典的调度器对系统中的进 ...
- 在Linux中通过Top运行进程查找最高内存和CPU使用率
按内存使用情况查找前15个进程,在批处理模式下为"top" 使用top命令查看有关当前状态,系统使用情况的更详细信息:正常运行时间,负载平均值和进程总数. 分类:Linux命令操作 ...
- Linux 有问必答:如何知道进程运行在哪个 CPU 内核上?
问题:我有个 Linux 进程运行在多核处理器系统上.怎样才能找出哪个 CPU 内核正在运行该进程? 当你在 多核 NUMA 处理器上运 行需要较高性能的 HPC(高性能计算)程序或非常消耗网络资源的 ...
随机推荐
- 换个视角来看git命令与代码库发生网络交互报错事件
git的一系列命令中像 clone.pull.push等与代码库发生网络交互时,可能报下面的错误信息 fatal: remote error: CAPTCHA required Your Stash ...
- SQL Server 查询性能优化——创建索引原则(一)(转载)
索引是什么?索引是提高查询性能的一个重要工具,索引就是把查询语句所需要的少量数据添加到索引分页中,这样访问数据时只要访问少数索引的分页就可以.但是索引对于提高查询性能也不是万能的,也不是建立越多的索引 ...
- .NET MD5 加密
using System; using System.Security.Cryptography; using System.Text; namespace Md5Demo { /// <sum ...
- [LeetCode] Binary Tree Tilt 二叉树的坡度
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- python2.7-巡风源码阅读
推荐个脚本示例网站:https://www.programcreek.com/python/example/404/thread.start_new_thread,里面可以搜索函数在代码中的写法,只有 ...
- poj 3525 凸多边形多大内切圆
Most Distant Point from the Sea Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4758 ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Codeforces Round#433 简要题解
来自FallDream的博客,未经允许,请勿转载,谢谢. 我的号自从几个月前姿势水平过低疯狂掉分之后就没动过了 突然想上点分 就打了一场Div1 没想到打到了rank5 一发上橙了,可还行. ...
- [bzoj4881][Lydsy2017年5月月赛]线段游戏
来自FallDream的博客,未经允许,请勿转载,谢谢. quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐标分别为(0,i)和(1, ...
- 毕业设计-JSP论文盲审系统
之前做的一款jsp的论文盲审系统,ssh框架的,学生提交论文,系统管理员将论文分配给教员,教员在不知学员是谁的情况之下,对论文进行打分,然后提交给系统,最后系统发布成绩,供学员查看. 整体做的还不错, ...