什么是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

CPU上下文切换的次数和时间(context switch)的更多相关文章

  1. 【转】CPU上下文切换的次数和时间(context switch)

    http://iamzhongyong.iteye.com/blog/1895728 什么是CPU上下文切换? 现在linux是大多基于抢占式,CPU给每个任务一定的服务时间,当时间片轮转的时候,需要 ...

  2. 压力测试衡量CPU的三个指标:CPU Utilization、Load Average和Context Switch Rate

    分类: 4.软件设计/架构/测试 2010-01-12 19:58 34241人阅读 评论(4) 收藏 举报 测试loadrunnerlinux服务器firebugthread 上篇讲如何用LoadR ...

  3. Linux性能优化从入门到实战:03 CPU篇:CPU上下文切换

      linux操作系统是将CPU轮流分配给任务,分时执行的.而每次执行任务时,CPU需要知道CPU寄存器(CPU内置的内存)和程序计数器PC(CPU正在执行指令和下一条指令的位置)值,这些值是CPU执 ...

  4. 从Java视角理解CPU上下文切换(Context Switch)

    从Java视角理解系统结构连载, 关注我的微博(链接)了解最新动态   在高性能编程时,经常接触到多线程. 起初我们的理解是, 多个线程并行地执行总比单个线程要快, 就像多个人一起干活总比一个人干要快 ...

  5. 操作系统重点双语阅读 - 上下文切换 Context Switch

    The context is represented in the PCB of the process. It includes the value of the CPU registers, th ...

  6. [CareerCup] 16.2 Measure Time in a Context Switch 测量上下文转换的时间

    16.2 How would you measure the time spent in a context switch? 上下文转换发生在两个进程之间,比如让一个等待进程进入执行和让一个运行进程进 ...

  7. CPU上下文切换

    CPU上下文切换包括进程上下文切换.线程上下文切换及中断上下文切换,当任务进行io或发生时间片事件及发生中断(如硬件读取完成)时,就会进入内核态,发生CPU上下文切换. 进程上下文切换,进程的上下文信 ...

  8. cpu上下文切换(下)

    --怎么查看系统的上下文切换情况 过多的上下文切换,会把cpu时间消耗在寄存器.内核栈以及虚拟内存等数据的保存和恢复上,缩短进程真正运行的时间,成了系统性能大幅下降的一个元凶. 查看,使用vmstat ...

  9. CPU 上下文切换及案例分析

    什么是CPU 上下文 我们都知道,Linux是一个多任务操作系统,它远支持大于CPU数量的任务同时运行,当然,这些任务实际上并不是真的在同时运行,而是因为系统在很短时间内,将CPU轮流分配给他们,造成 ...

随机推荐

  1. Python学习之路5 - 函数

    函数 定义方式: def func(): "这里面写函数的描述" 这里写代码 return x #如果没有返回值就叫"过程",函数和过程的区别就是有无返回值 实 ...

  2. iOS开发UIColor,CGColor,CIColor三者的区别和联系

    最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...

  3. [TUTORIAL]How to setup SP_Flash_Tool_Linux (MTK/MediaTek Soc)

    转自:https://forum.xda-developers.com/general/rooting-roms/tutorial-how-to-setup-spflashtoollinux-t316 ...

  4. 使用WCF上传数据

    通过传递Stream对象来传递大数据文件,但是有一些限制: 1.只有 BasicHttpBinding.NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据. 2. ...

  5. java数据结构-HashMap

    一直以来似乎都有一个错觉,认为map跟其他的集合类一样继承自Collection,其实不然,Map和Collection在结构层次上是没有任何关系的,通过查看源码可以发现map所有操作都是基于key- ...

  6. foreach循环2

    <select id="test" parameterType="java.util.List" resultType="user"& ...

  7. codesandbox

    codesandbox https://codesandbox.io https://codesandbox.io/dashboard https://codesandbox.io/dashboard ...

  8. RT-thread内核之系统时钟

    一.系统时钟 rt-thread的系统时钟模块采用全局变量rt_tick作为系统时钟节拍,该变量在系统时钟中断函数中不断加1.而系统时钟中断源和中断间隔一般由MCU硬件定时器(如stm32的嘀嗒定时器 ...

  9. javabean 参数收集 设置属性 设置不同级别的域对象的属性 默认存储在pagecontext中

    javabean 参数收集  设置属性 设置不同级别的域对象的属性  默认存储在pagecontext中

  10. bzoj3546[ONTAK2010]Life of the Party

    题意是裸的二分图关键点(必然在二分图最大匹配中出现的点).比较经典的做法在cyb15年的论文里有: 前几天写jzoj5007的时候脑补了一种基于最小割可行边的做法:考虑用最大流求解二分图匹配.如果某个 ...