什么是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. C语言中动态内存的分配(malloc,realloc)

    动态内存分配:根据需要随时开辟,随时释放的内存分配方式.分配时机和释放时机完全由程序员决定,由于没有数据声明,这部分空间没有名字.无法像使用变量或数组那样通过变量名或数组名引用其中的数据,只能通过指针 ...

  2. springboot 整合apache shiro

    这几天因为项目需要,学习了下shiro,由此留下一些记录,也希望对初学shiro的朋友有帮助. springboot 是这两年新兴起来的一个项目,它的出现是为了减少springmvc开发过程中需要引入 ...

  3. 重构 之 总结代码的坏味道 Bad Smell (一) 重复代码 过长函数 过大的类 过长参数列 发散式变化 霰弹式修改

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  4. P4语法(1)基础数据类型和Header

    文章学习自:P4语言编程详解 由于原文有一点的年份,所以也继续阅读了相关的最新规范. P4语言规范 基础数据类型 布尔型(bool) 运算符 描述 and 双目运算符,结果为布尔型 or 双目运算符, ...

  5. Tomcat服务器学习和使用(一)

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  6. Swift-创建UIButton(其他UI组件雷同)

    let button = UIButton.init(frame: CGRectMake(, , , )) button.setTitle("按钮", forState: UICo ...

  7. js控制input只能输入数字和小数点后两位,输入其他自动清除方法。

    工作中input='text'总会遇到要控制输入数字,或者是输入中文,输入电话,输入身份证号,邮箱等.今天我遇到的是要输入数字并且只能小数点后面两位的数字,还不能为负数.废话不多说上代码: <i ...

  8. 语音信号处理之动态时间规整(DTW)(转)

    这学期有<语音信号处理>这门课,快考试了,所以也要了解了解相关的知识点.呵呵,平时没怎么听课,现在只能抱佛脚了.顺便也总结总结,好让自己的知识架构清晰点,也和大家分享下.下面总结的是第一个 ...

  9. IIS部署网部常用问题汇总

    1.unrecognized attribute 'targetframework' A: 需要注册.net framework到iis.步骤如下: (1)'Start' -> 'CMD' (2 ...

  10. timer实现

    实现一个 timer 前段时间写过一篇 blog 谈到 用 timer 驱动游戏 的一个想法.当 timer 被大量使用之后,似乎自己实现一个 timer 比用系统提供的要放心一些.最近在重构以前的代 ...