什么是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 _x 86 _ 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 2 p/ 0 K 2 p/ 16 K 2 p/ 64 K 8 p/ 16 K 8 p/ 64 K 16 p/ 16 K 16 p/ 64 K
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 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
相关推荐
1. CPU上下文切换到底是个什么东西 文章目录1. CPU上下文切换到底是个什么东西1.1. CPU上下文1.2. CPU上下文切换1.2.1. 进程上下文切换1.2.2. 线程上下文切换1.2.3. 中断上下文切换 第一节,我们了解到了平均负载是...
Linux操作系统通过实战理解CPU上下文切换.pdf
一种任意时间片长度对CPU上下文切换代价影响消除的新方法.pdf
怎么查看系统的上下文切换情况通过前面学习我们知道,过多的上下文切换,会把 CPU 时间消耗在寄存器、内核栈以及虚拟内存等数据的保存和恢复上,缩短进程真正运行的时
非自愿上下文切换:是指由于时间片的时间到了,然后被系统强制调用,进而发生上下文切换。 pidstat -w -u 5 -w:进程上下文切换情况。 -u :进程cpu使用情况 5:是每五秒输出一组数据。 pidstat -wt 1 -wt :输出...
利用Spring Context上下文创建自定义对象,学习Spring的IoC容器如何使用控制反转创建一个对象,了解Spring控制反转原理
rlwrap sqlplus 上下文切换,不错的东西,需要配置/home/oracle/.bash_profile
轻量级内联分析器,用于测量线程的挂起时间,cpu时间和premptive上下文切换
当UE开机并附着上网络时,... 为了减少E-UTRAN的开销和UE的处理负担,当UE进入ECM-IDLE状态时,所有UE相关的信息(包括UE上下文)都会被接入网和UE清除。只有MME依然保留UE上下文(包括已经为该UE创建的bearer信息)。
UCOS时钟中断与上下文切换情景分析[整理].pdf
Linux进程上下文切换和中断上下文切换Linux缺页中断和中断区别。
Rust异步和Linux线程上下文切换时间和内存使用情况的比较 这是一些试图以各种方式衡量上下文切换时间和任务内存使用情况的程序。 总之: 异步任务之间的上下文切换大约需要0.2µs,而内核线程之间的切换大约需要1.7...
Linux上下文切换性能测试的一种新方法.pdf
针对嵌入式系统实时控制和信号处理的需求,建立了一种基于DSP架构的快速上下文切换机制,为实时处理提供了有力支持。机制采用两条独立的总线,分别用来传送地址和数据信息,实现地址和数据信息的并行传输,增加了上...
考虑中断和上下文切换开销的响应时间分析.pdf
经典原文文献shape Matching and object Recognition Using shape的代码,代码有中文注释,易懂
查看指定进程的cpu和内存使用情况(与任务管理器查看一样)
jQuery Context Menu - jQuery上下文菜单插件
css3上下文过渡切换 css3上下文过渡切换网页特效.zip