您好,欢迎来到榕意旅游网。
搜索
您的当前位置:首页使用taskset来进行多核系统调优

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

来源:榕意旅游网


查看pid和那个cpu亲和

taskset -pc 3687

返回pid 3687's current affinity list: 0,1,2,3

表示3687和0,1两个cpu内核比较亲和

taskset -pc 0-1 3687

设置线程3678和0,1两个cpu内核亲和

top -p 3687

查看此线程具体执行情况

******************************************************************************************************************************

1 / 15

利用taskset可以充分利用多核cpu的好处,可以让某个程序或脚本,运行在某个具体的cpu上。

这个工具系统可能没有默认安装:,rpm包名util-linux

#taskset --help

taskset (util-linux 2.13-pre7)

usage: taskset [options] [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 information

1:让某个程序运行在特定cpu上面

taskset -c 0 sh wade.sh

2:切换某个进程到特定的cpu上。

taskset -pc 0 12345

比如你有一个cpu是4 core你可以这样写你的脚本

#!/bin/bash

taskset -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分担的任务摊派到其他CPU核心上,可以预见,系统的处理能力将有更大的提升。

两个名词

SMP (Symmetrical Multi-Processing):指在一个计算机上汇集了一组处理器(多CPU),各CPU之间共享内存子系统以及总线结构。 [更多...]

CPU affinity:中文唤作“CPU亲和力”,是指在CMP架构下,能够将一个或多个进程绑定到一个或多个处理器上运行。[更多...]

一、在Linux上修改进程的“CPU亲和力”

在Linux上,可以通过 taskset 命令进行修改。以Ubuntu为例,运行如下命令可以安装taskset工具。

# apt-get install schedutils

对运行中的进程,文档上说可以用下面的命令,把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启动几个工作进程的,默认为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,之后其创建的子进程是否会有同样的属性?我发现子进程确实继承了父进程的affinity属性。

四、Windows?

在Windows上修改“CPU亲和力”,可以通过任务管理器搞定。

* 个人感觉,Windows系统中翻译的“处理器关系”比“CPU亲和力”容易理解点儿

—————–

进行了这样的修改后,即使系统负载达到3以上,不带缓存打开blogkid.net首页(有40多次查询)依然顺畅;以前一旦负载超过了1.5,响应就很慢了。效果很明显。

linux taskset命令详解

SYNOPSIS

taskset [options] [mask | list ] [pid | command [arg]...]

OPTIONS

-p, --pid

operate on an existing 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

******************************************************************************************************************************

现 在多核的CPU已经相当普遍了,那么这种多核的服务器如何让CPU得到充分利用,可以靠应用自己来定义,或者依赖操作系统来调度。根据红帽的说法 RHEL5有一个很强壮的CPU调度机制,RHEL6就更强壮了,所以看起来跑在LINUX下面的应用应该都不用去管该用哪个CPU。

首先我们来看看CPU中断请求的统计:

CentOS release 5.2 (Final)

从图上看,CPU的使用基本上还是均匀的。不过CPU0负载还是最大的。

所有在某种情况下可能会需要手工来设置进程使用CPU核的优先级。

下面是一个操作的例子:

postgres 57 1 0 May05 ? 00:00:00 /app/pgsql/bin/postgres -D /database/pgdata -p 1921

[root@develop1 ~]# taskset -pc 57

pid 57’s current affinity list: 0-3

这个进程目前是默认与0-3 这4个核心亲和的。也就是说会在0-3这几个核心调度。

[root@develop1 ~]# taskset -pc 0-1 57

pid 57’s current affinity list: 0-3

pid 57’s new affinity list: 0,1

修改之后我们看到,已经修改为0,1的范围了。

可以通过top -p 57 [f -> j]

查看P列可以看到当前运行的核心号。

如果该成在单个CORE上跑的话,马上就能看到CORE的变化。

下面是taskset的MAN PAGE:

从描述上来看的话,只要taskset返回结果了,那LINUX肯定是确保得到了你想要的结果。

DESCRIPTION

taskset 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 “bonds” a process to a given set of

CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any

other CPUs. Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep

processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU

affinity is useful only in certain applications.

The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU

and the highest order bit corresponding to the last logical CPU. Not all CPUs may exist on a given system but

a mask may specify more CPUs than are present. A retrieved mask will reflect only the bits that correspond to

CPUs physically on the system. If an invalid mask is given (i.e., one that corresponds to no valid CPUs on the

current system) an error is returned. The masks are typically given in hexadecimal. For example,

0×00000001

is processor #0

0×00000003

is processors #0 and #1

0xFFFFFFFF

is all processors (#0 through #31)

When taskset returns, it is guaranteed that the given program has been scheduled to a legal CPU.

友情提示:范文可能无法思考和涵盖全面,供参考!最好找专业人士起草或审核后使用,感谢您的下载!

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- nryq.cn 版权所有 赣ICP备2024042798号-6

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务