CPU上下文切换的次数和时间(context switch)
什么是CPU上下文切换?
现在linux是大多基于抢占式,CPU给每个任务一定的服务时间,当时间片轮转的时候,需要把当前状态保存下来,同时加载下一个任务,这个过程叫做上下文切换。时间片轮转的方式,使得多个任务利用一个CPU执行成为可能,但是保存现场和加载现场,也带来了性能消耗。 那线程上下文切换的次数和时间以及性能消耗如何看呢?
如何获得上下文切换的次数?
vmstat直接运行即可,在最后几列,有CPU的context switch次数。 这个是系统层面的,加入想看特定进程的情况,可以使用pidstat。
|
1
2
3
4
5
6
7
|
$ vmstat 1 100procs -----------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:~ # pidstatLinux 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 bash11:37:19 11509 0.00 0.00 0.00 0.00 3 dd11: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/0K 2p/16K 2p/64K 8p/16K 8p/64K 16p/16K 16p/64K ctxsw ctxsw ctxsw ctxsw ctxsw ctxsw ctxsw--------- ------------- ------ ------ ------ ------ ------ ------- -------commonway Linux 2.6.18- 9.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 connectedwith Unix pipes. A token is passed from process to process, forcingcontext switches. The benchmark measures the time it takes to passthe token two thousand times from process to process. Each hand offof the token has two costs: (a) the context switch, and (b) the costof passing the token. In order to get just the context switching time,the benchmark first measures the cost of passing the token through aring of pipes in a single process. This time is defined as the costof passing the token and is not included in the reported context switchtime..PP When the processes are larger than the default baseline of ``zero''(where zero means just big enough to do the benchmark), the costof the context switch includes the cost of restoring user levelstate (cache lines). This is accomplished by having the processallocate an array of data and sum it as a series of integersafter receiving the token but before passing the token to thenext process. Note that the overhead mentioned above includesthe cost of accessing the data but because it is measured injust one address space, the cost is typically the cost with hotcaches. So the context switch time does not include anythingother than the context switch provided that all the processesfit in the cache. If there are cache misses (as is common), thecost 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)的更多相关文章
- 【转】CPU上下文切换的次数和时间(context switch)
http://iamzhongyong.iteye.com/blog/1895728 什么是CPU上下文切换? 现在linux是大多基于抢占式,CPU给每个任务一定的服务时间,当时间片轮转的时候,需要 ...
- 压力测试衡量CPU的三个指标:CPU Utilization、Load Average和Context Switch Rate
分类: 4.软件设计/架构/测试 2010-01-12 19:58 34241人阅读 评论(4) 收藏 举报 测试loadrunnerlinux服务器firebugthread 上篇讲如何用LoadR ...
- Linux性能优化从入门到实战:03 CPU篇:CPU上下文切换
linux操作系统是将CPU轮流分配给任务,分时执行的.而每次执行任务时,CPU需要知道CPU寄存器(CPU内置的内存)和程序计数器PC(CPU正在执行指令和下一条指令的位置)值,这些值是CPU执 ...
- 从Java视角理解CPU上下文切换(Context Switch)
从Java视角理解系统结构连载, 关注我的微博(链接)了解最新动态 在高性能编程时,经常接触到多线程. 起初我们的理解是, 多个线程并行地执行总比单个线程要快, 就像多个人一起干活总比一个人干要快 ...
- 操作系统重点双语阅读 - 上下文切换 Context Switch
The context is represented in the PCB of the process. It includes the value of the CPU registers, th ...
- [CareerCup] 16.2 Measure Time in a Context Switch 测量上下文转换的时间
16.2 How would you measure the time spent in a context switch? 上下文转换发生在两个进程之间,比如让一个等待进程进入执行和让一个运行进程进 ...
- CPU上下文切换
CPU上下文切换包括进程上下文切换.线程上下文切换及中断上下文切换,当任务进行io或发生时间片事件及发生中断(如硬件读取完成)时,就会进入内核态,发生CPU上下文切换. 进程上下文切换,进程的上下文信 ...
- cpu上下文切换(下)
--怎么查看系统的上下文切换情况 过多的上下文切换,会把cpu时间消耗在寄存器.内核栈以及虚拟内存等数据的保存和恢复上,缩短进程真正运行的时间,成了系统性能大幅下降的一个元凶. 查看,使用vmstat ...
- CPU 上下文切换及案例分析
什么是CPU 上下文 我们都知道,Linux是一个多任务操作系统,它远支持大于CPU数量的任务同时运行,当然,这些任务实际上并不是真的在同时运行,而是因为系统在很短时间内,将CPU轮流分配给他们,造成 ...
随机推荐
- java知乎爬虫
好久没写博客了,前阵子项目忙着上线,现在有点空闲,就把最近写的一个爬虫和大家分享下,统计结果放在了自己买的阿里云服务器上(点此查看效果),效果如下: 程序是在工作之余写的,用了java 的webmgi ...
- Java中的死锁问题
死锁问题: 例如有两个线程, 线程1与线程2. 线程1在执行的过程中, 要锁定对象1, 2才能完成整个操作, 首先锁定对象1, 再锁定对象2. 线程2在执行的过程中, 要锁定对象2, 1才能完成整个操 ...
- TCP系列24—重传—14、F-RTO虚假重传探测
一.虚假重传 在一些情况下,TCP可能会在没有数据丢失的情况下初始化一个重传,这种重传就叫做虚假重传(Spurious retransmission).发生虚假重传的原因可能是包传输中重排序.传输中发 ...
- TCP系列05—连接管理—4、TCP连接的ISN、连接建立超时及TCP的长短连接
一.TCP连接的ISN 之前我们说过初始建立TCP连接的时候的系列号(ISN)是随机选择的,那么这个系列号为什么不采用一个固定的值呢?主要有两方面的原因 防止同一个连接的不同实例(di ...
- 3dContactPointAnnotationTool开发日志(二二)
昨天是实现了显示GameObject子GameObject的选项卡功能,今天就是要让statusPanel可以控制它们的位置.旋转和缩放了. 没什么难的,对应选项卡绑定上对应的物体或子物体即可 ...
- 菜鸟的飞翔日记-os篇
一轮王道os复习感想 1概述 虽然去年有上操作系统这门必修课,考的成绩也算理想,本来还有点沾沾自喜,嗯,觉得自己学的还不错,知道有一天我拿起了王道,(没给王道打广告)看王道的原因完全在于为考研做准备, ...
- python爬虫:爬取网站视频
python爬取百思不得姐网站视频:http://www.budejie.com/video/ 新建一个py文件,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ...
- Tomcat启动报错ERROR:transport error 202:bind failed:Address already
昨天在服务器上拷贝了一个tomcat项目,修改了server.xml之后启动居然报错ERROR:transport error 202:bind failed:Address already,应该是远 ...
- 在Delphi中如何获得SQL中存储过程的返回值?
示例存储过程:create procedure proc_loginusername varchar(20),password varchar(20)asdeclare @result intsele ...
- C++基础知识(一)
C++中头文件中class的两个花括号后面要加上分号,否则会出现很多的莫名奇妙的错误. 一. 每一个C++程序(或者由多个源文件组成的C++项目)都必须包含且只有一个main()函数.对于预处理指令, ...