Context Switching Cost
Posted 冰花ぃ雪魄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Context Switching Cost相关的知识,希望对你有一定的参考价值。
锁为什么耗性能:
1. 每个线程在被cpu执行的时候, 都会给这个线程分配一定的时间让这个线程执行, 这段时间不会被信号中断.
2. 在获取锁的时候, 如果竞争很激烈, 这个线程会被挂起, 所以, 如果获取锁的操作很多, 这个线程就会频繁被挂起, 无法利用完cpu分配给它的时间, 时间都浪费
在了contextSwitch 上, 所以说, 获取锁是比较费时的操作
3. contextSwith 的耗时大概是5,000 to 10,000 clock cycles or several microseconds on most current processors.
The vmstat command on Unix systems and the perfmon tool on Windows systems report the number of context
switches and the percentage of time spent in the kernel. High kernel usage (over 10%) often indicates heavy scheduling
activity, which may be caused by blocking due to I/O or lock contention.
用top也可以
锁的消耗也分竞争激烈的锁和竞争不激烈的锁, 竞争不激烈的锁消耗会比较小. 竞争太激烈的话, 会导致很多线程会被os挂里, 有相应的contextSwitch 的操作, cache miss的操作, os的算法,对os的通知等等 所以竞争激烈不激烈也比较重要
There are three ways to reduce lock contention:
? Reduce the duration for which locks are held;
? Reduce the frequency with which locks are requested; or
? Replace exclusive locks with coordination mechanisms that permit greater concurrency.
以上是关于Context Switching Cost的主要内容,如果未能解决你的问题,请参考以下文章
WebDriver switching to new window
P2846 [USACO08NOV]光开关Light Switching
Luogu P2826 [USACO08NOV]光开关Light Switching