`
iamzhongyong
  • 浏览: 796353 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

CPU上下文切换的次数和时间(context switch)

 
阅读更多

        什么是CPU上下文切换?

        现在linux是大多基于抢占式,CPU给每个任务一定的服务时间,当时间片轮转的时候,需要把当前状态保存下来,同时加载下一个任务,这个过程叫做上下文切换。时间片轮转的方式,使得多个任务利用一个CPU执行成为可能,但是保存现场和加载现场,也带来了性能消耗。  那线程上下文切换的次数和时间以及性能消耗如何看呢?  

        

        

 

    ​    ​如何获得上下文切换的次数?

         vmstat直接运行即可,在最后几列,有CPU的context switch次数。 这个是系统层面的,加入想看特定进程的情况,可以使用pidstat。     

1
2
3
4
5
6
7
$ vmstat 1 100
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0     88 233484 288756 1784744    0    0     0    23    0    0  4  1 94  0  0
 4  0     88 233236 288756 1784752    0    0     0     0 6202 7880  4  1 96  0  0
 2  0     88 233360 288756 1784800    0    0     0   112 6277 7612  4  1 95  0  0
 0  0     88 232864 288756 1784804    0    0     0   644 5747 6593  6  0 92  2  0

    ​     ​执行pidstat,将输出系统启动后所有活动进程的cpu统计信息:       ​    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
linux:~ # pidstat
Linux 2.6.32.12-0.7-default (linux)             06/18/12        _x86_64_
 
11:37:19          PID    %usr %system  %guest    %CPU   CPU  Command
……
11:37:19        11452    0.00    0.00    0.00    0.00     2  bash
11:37:19        11509    0.00    0.00    0.00    0.00     3  dd
11:37:19: pidstat获取信息时间点
PID: 进程pid
%usr: 进程在用户态运行所占cpu时间比率
%system: 进程在内核态运行所占cpu时间比率
%CPU: 进程运行所占cpu时间比率
CPU: 指示进程在哪个核运行
Command: 拉起进程对应的命令
备注:执行pidstat默认输出信息为系统启动后到执行时间点的统计信息,因而即使当前某进程的cpu占用率很高,输出中的值有可能仍为0

​    ​    

    ​    ​​上下文切换的性能消耗在哪里呢?

    ​    ​context switch过高,会导致CPU像个搬运工,频繁在寄存器和运行队列直接奔波  ,更多的时间花在了线程切换,而不是真正工作的线程上。直接的消耗包括CPU寄存器需要保存和加载,系统调度器的代码需要执行。间接消耗在于多核cache之间的共享数据。 

    ​

    ​    ​引起上下文切换的原因有哪些?

    ​    ​对于抢占式操作系统而言, 大体有几种:

    ​    ​1、当前任务的时间片用完之后,系统CPU正常调度下一个任务;

    ​    ​2、当前任务碰到IO阻塞,调度线程将挂起此任务,继续下一个任务;

    ​    ​3、多个任务抢占锁资源,当前任务没有抢到,被调度器挂起,继续下一个任务;

    ​    ​4、用户代码挂起当前任务,让出CPU时间;

    ​    ​5、硬件中断;

    ​   ​

    ​    ​如何测试上下文切换的时间消耗?

        LMbench,知道这个工具,是在霸爷的博客上面(http://blog.yufeng.info/archives/753),然后就开始在测试环境下搞了一把,一会就出结果了。然后就搞了台线上机器安装这个工具,然后测试,后面在测试Memory的时候,直接导致Load飙升,还好没人发现,机器java进程重启就好了。这方面纯粹是业务选手。霸爷说分析的结果对于高性能C的开发同学来说,是需要熟记的,没办法,咱是搞java的,只能每个指标逐个看一下了。

       ​ 

    ​    ​LMbench的简单介绍?

    ​    首先看英文介绍:LMbench -Tools for Performance Analysis,微观性能分析工具。

     ​   官方地址:http://www.bitmover.com/lmbench/

        ​下载地址:http://www.bitmover.com/lmbench/lmbench3.tar.gz 

        

    ​    ​LMbench​主要能干啥?

        ​主要是带宽(读取缓存文件、内存拷贝、读写内存、管道等)和反应时间(上下文切换、网路、进程创建等)的评测工具。

       

    ​    ​ LMbench ​安装?

1
2
3
4
#wget http://www.bitmover.com/lmbench/lmbench3.tar.gz
#tar -zxvf lmbench3.tar.gz
#cd lmbench3
#make

        ​中间遇到一个问题,就是报错,在CSDN上面找到了答案,这这里贴一下。

1
2
3
4
5
6
7
8
9
10
11
此时会报错:
   make[2]: *** 没有规则可以创建“bk.ver”需要的目标“../SCCS/s.ChangeSet”。 停止。
   make[2]:正在离开目录 `/home/hero/lmbench3/src'
   make[1]: *** [lmbench] 错误 2
   make[1]:正在离开目录 `/home/hero/lmbench3/src'
   make: *** [build] 错误 2
解决办法:
lmbench3目录下
   #mkdir SCCS
   #touch  ./SCCS/s.ChangeSet
   #make

       

    ​    ​ LMbench关于结果解释(这次主要关注线程切换信息)

        在网上找了半天,信息很少,只能看doc下面的英文解释了。    

        测试上下文切换的时间,一个上下文切换,包括保存一个进程状态的保存和恢复另外一个进程的时间。

        典型的上下文切换性能,仅仅是测量最小的线程切换时间。仅仅是做进程切换,任何实质的任务都不做。

1
2
3
4
5
6
Context switching - times in microseconds - smaller is better
-------------------------------------------------------------------------
Host                 OS  2p/02p/162p/648p/168p/6416p/1616p/64K
                         ctxsw  ctxsw  ctxsw ctxsw  ctxsw   ctxsw   ctxsw
--------- ------------- ------ ------ ------ ------ ------ ------- -------
commonway Linux 2.6.189.2400 4.0200 9.0300 7.5600 8.3800    11.6 6.28000
时间的单位是微秒。

    ​

        LMbench是如何来测量进程切换的时间的?  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
The benchmark is a ring of two to twenty processes that are connected
with Unix pipes.  A token is passed from process to process, forcing
context switches.  The benchmark measures the time it takes to pass
the token two thousand times from process to process.  Each hand off
of the token has two costs: (a) the context switch, and (b) the cost
of passing the token.  In order to get just the context switching time,the benchmark first measures the cost of passing the token through a
ring of pipes in a single process.  This time is defined as the cost
of passing the token and is not included in the reported context switch
time.
.PP
    When the processes are larger than the default baseline of ``zero''
(where zero means just big enough to do the benchmark), the cost
of the context switch includes the cost of restoring user level
state (cache lines).  This is accomplished by having the process
allocate an array of data and sum it as a series of integers
after receiving the token but before passing the token to the
next process.  Note that the overhead mentioned above includes
the cost of accessing the data but because it is measured in
just one address space, the cost is typically the cost with hot
caches.  So the context switch time does not include anything
other than the context switch provided that all the processes
fit in the cache.  If there are cache misses (as is common), the
cost of the context switch includes the cost of those cache misses.
.PP
    首先是看任务处理的时间(通过一次任务处理,这个任务处理的时间被定义为token时间,不包括线程切换的)。
    然后多次执行,排除任务执行的时间,然后计算出CS的时间(如果有cache miss,则CS的时间也包括cache misses的时间)。

 

文章参考:    

霸爷和周忱的博客

http://www.bitmover.com/lmbench/

https://www.usenix.org/legacy/publications/library/proceedings/sd96/full_papers/mcvoy.pdf

http://blog.csdn.net/taozi343805436/article/details/7876087 

http://blog.yufeng.info/archives/753

http://rdc.taobao.com/team/jm/archives/1706

 

2
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics