使用taskset来进行多核系统调优

上传人:新** 文档编号:467345333 上传时间:2023-08-10 格式:DOCX 页数:8 大小:117.58KB
返回 下载 相关 举报
使用taskset来进行多核系统调优_第1页
第1页 / 共8页
使用taskset来进行多核系统调优_第2页
第2页 / 共8页
使用taskset来进行多核系统调优_第3页
第3页 / 共8页
使用taskset来进行多核系统调优_第4页
第4页 / 共8页
使用taskset来进行多核系统调优_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《使用taskset来进行多核系统调优》由会员分享,可在线阅读,更多相关《使用taskset来进行多核系统调优(8页珍藏版)》请在金锄头文库上搜索。

1、查看pid和那个cpu亲和taskset -pc 3687返回pid 3687s current affinity list: 0,1,2,3表示3687和0,1两个cpu内核比较亲和taskset -pc 0-13687设置线程3678和0,1两个cpu内核亲和top -p3687查看此线程具体执行情况*利用taskset可以充分利用多核cpu的好处,可以让某个程序或脚本,运行在某个具体的cpu上。这个工具系统可能没有默认安装:,rpm包名util-linux#taskset -helptaskset (util-linux 2.13-pre7)usage: taskset options

2、mask | cpu-list pid | cmd args.set or get the affinity of a process-p, pid operate on existing given pid-c, cpu-list display and specify cpus in list format-h, help display this help-v, version output version information1:让某个程序运行在特定cpu上面taskset -c 0 sh wade.sh2:切换某个进程到特定的cpu上。taskset -pc 0 12345比如你有

3、一个cpu是4 core你可以这样写你的脚本#!/bin/bashtaskset -c 0 sh a.sh &taskset -c 1 sh b.sh &taskset -c 2 sh c.sh &taskset -c 3 sh d.sh &应该可以充分利用你的cpu了。*我的Linode十分繁忙,在跑一些密集操作数据库的Rake任务时尤其如此。但我观察发现,Linode服务器的4核CPU,只有第1个核心(CPU#0)非常忙,其他都处于idle状态。不了解Linux是如何调度的,但目前显然有优化的余地。除了处理正常任务,CPU#0还需要处理每秒网卡中断。因此,若能将CPU#0分担的任务摊派到其

4、他CPU核心上,可以预见,系统的处理能力将有更大的提升。两个名词SMP(Symmetrical Multi-Processing):指在一个计算机上汇集了一组处理器(多CPU),各CPU之间共享内存子系统以及总线结构。 更多.CPU affinity:中文唤作“CPU亲和力”,是指在CMP架构下,能够将一个或多个进程绑定到一个或多个处理器上运行。更多.一、在Linux上修改进程的“CPU亲和力”在Linux上,可以通过taskset命令进行修改。以Ubuntu为例,运行如下命令可以安装taskset工具。# apt-get install schedutils对运行中的进程,文档上说可以用下面

5、的命令,把CPU#1 #2 #3分配给PID为2345的进程:# taskset -cp 1,2,3 2345但我尝试没奏效,于是我关掉了MySQL,并用taskset将它启动:# taskset -c 1,2,3 /etc/init.d/mysql start对于其他进程,也可如此处理(nginx除外,详见下文)。之后用top查看CPU的使用情况,原来空闲的#1 #2 #3,已经在辛勤工作了。二、配置nginx绑定CPU刚才说nginx除外,是因为nginx提供了更精确的控制。在conf/nginx.conf中,有如下一行:worker_processes 1;这是用来配置nginx启动几个

6、工作进程的,默认为1。而nginx还支持一个名为worker_cpu_affinity的配置项,也就是说,nginx可以为每个工作进程绑定CPU。我做了如下配置:worker_processes 3;worker_cpu_affinity 0010 0100 1000;这里0010 0100 1000是掩码,分别代表第2、3、4颗cpu核心。重启nginx后,3个工作进程就可以各自用各自的CPU了。三、刨根问底1. 如果自己写代码,要把进程绑定到CPU,该怎么做?可以用sched_setaffinity函数。在Linux上,这会触发一次系统调用。2. 如果父进程设置了affinity,之后其创

7、建的子进程是否会有同样的属性?我发现子进程确实继承了父进程的affinity属性。四、Windows?在Windows上修改“CPU亲和力”,可以通过任务管理器搞定。* 个人感觉,Windows系统中翻译的“处理器关系”比“CPU亲和力”容易理解点儿进行了这样的修改后,即使系统负载达到3以上,不带缓存打开首页(有40多次查询)依然顺畅;以前一旦负载超过了1.5,响应就很慢了。效果很明显。linux taskset命令详解SYNOPSIS taskset options mask | list pid | command arg.OPTIONS -p, -pid operate on an ex

8、isting PID and not launch a new task -c, -cpu-list specifiy 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. -h, -help display usage information and exit -V, -version output version information and exit

9、*现 在多核的CPU已经相当普遍了,那么这种多核的服务器如何让CPU得到充分利用,可以靠应用自己来定义,或者依赖操作系统来调度。根据红帽的说法 RHEL5有一个很强壮的CPU调度机制,RHEL6就更强壮了,所以看起来跑在LINUX下面的应用应该都不用去管该用哪个CPU。首先我们来看看CPU中断请求的统计:CentOS release 5.2 (Final)从图上看,CPU的使用基本上还是均匀的。不过CPU0负载还是最大的。所有在某种情况下可能会需要手工来设置进程使用CPU核的优先级。下面是一个操作的例子:postgres 6457 1 0 May05 ? 00:00:00 /app/pgsql

10、/bin/postgres -D /database/pgdata -p 1921rootdevelop1 # taskset -pc 6457pid 6457s current affinity list: 0-3这个进程目前是默认与0-3 这4个核心亲和的。也就是说会在0-3这几个核心调度。rootdevelop1 # taskset -pc 0-1 6457pid 6457s current affinity list: 0-3pid 6457s new affinity list: 0,1修改之后我们看到,已经修改为0,1的范围了。可以通过top -p 6457 f - j查看P列可以

11、看到当前运行的核心号。如果该成在单个CORE上跑的话,马上就能看到CORE的变化。下面是taskset的MAN PAGE:从描述上来看的话,只要taskset返回结果了,那LINUX肯定是确保得到了你想要的结果。DESCRIPTIONtaskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COM-MAND with a given CPU affinity. CPU affinity is a scheduler property that

12、 “bonds” a process to a given set ofCPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on anyother CPUs. Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keepprocesses on the same CPU as long as practical

13、for performance reasons. Therefore, forcing a specific CPUaffinity is useful only in certain applications.The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPUand the highest order bit corresponding to the last logical CPU. Not all CPUs may exist on a given system buta mask may specify more CPUs than are present. A retrieved mask will reflect only the bits that correspond toCPUs physically on the system. If an invalid mask is given (i.e., one that corres

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 机械/制造/汽车 > 汽车技术

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号