How many Context Switches is “normal”?

This depends very much on the type of application you run. If you've got applications which are very trigger-happy WRT syscalls you can expect to see high amounts of context switching. If most of your applications idle around and only wake up when there's stuff happening on a socket, you can expect to see low context switch rates.

System calls

System calls cause context switches by their very own nature. When a process does a system call, it basically tells the kernel to take over from it's current point in time and memory to do stuff the process isn't privileged to do, and return to the same spot when it's done.

When we look at the definition of the write(2) syscall from Linux, this becomes very clear:

NAME
write - write to a file descriptor SYNOPSIS
#include ssize_t write(int fd, const void *buf, size_t count); DESCRIPTION
write() writes up to count bytes from the buffer pointed buf to the file
referred to by the file descriptor fd. [..] RETURN VALUE
On success, the number of bytes written is returned (zero indicates
nothing was written). On error, -1 is returned, and errno is set
appropriately.
[..]

This basically tells the kernel to take over operation from the process, move up to count bytes, starting from the memory address pointed at by *buf to file descriptor fd of the current process and then return back to the process and tell him how it went.

A nice example to show this is the dedicated game server for Valve Source based games, hlds.http://nopaste.narf.at/f1b22dbc9 shows one second worth of syscalls done by a single instance of a game server which had no players on it. This process takes about 3% CPU time on a Xeon X3220 (2.4Ghz), just to give you a feeling for how expensive this is.

Multi-Tasking

Another source of context switching might be processes which don't do syscalls, but need to get moved off a given CPU to make room for other processes.

A nice way to visualize this is cpuburn. cpuburn doesn't do any syscalls itself, it just iterates over it's own memory, so it shouldn't cause any context switching.

Take an idle machine, start vmstat and then run a burnMMX (or any different test from the cpuburn package) for every CPU core the system has. You should have full system utilization by then but hardly any increased context switching. Then try to start a few more processes. You'll see that the context switching rate increases as the processes begin to compete over CPU cores. The amount of switching depends on the processes/core ratio and the multitasking resolution of your kernel.

Further reading

linfo.org has a nice writeup on what context switches and system calls are. Wikipedia has generic information and a nice link collection on System calls.

Here is a blog abount how long will constext switches do: http://blog.tsunanet.net/2010/11/how-long-does-it-take-to-make-context.html

Context Switch and System Call的更多相关文章

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

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

  2. Context Switch Definition

    A context switch (also sometimes referred to as a process switch or a task switch) is the switching ...

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

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

  4. CPU上下文切换的次数和时间(context switch)

    什么是CPU上下文切换? 现在linux是大多基于抢占式,CPU给每个任务一定的服务时间,当时间片轮转的时候,需要把当前状态保存下来,同时加载下一个任务,这个过程叫做上下文切换.时间片轮转的方式,使得 ...

  5. context switch

    In computing, a context switch is the process of storing and restoring the state (more specifically, ...

  6. [转帖]How long does it take to make a context switch?

    How long does it take to make a context switch?   FROM: http://blog.tsunanet.net/2010/11/how-long-do ...

  7. How long does it take to make a context switch?

    FROM: http://blog.tsunanet.net/2010/11/how-long-does-it-take-to-make-context.html That's a interesti ...

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

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

  9. [Chapter 3 Process]Practice 3.4 Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets.

    3.4 The Sun UltraSPARC processor has multiple register sets. Describe what happens when a context sw ...

随机推荐

  1. ClusterWare 服务介绍

    一.ClusterWare启动流程图 二.Clusterware启动的代理进程 ­­­­­­­­­­­­­­­­­层次        代理进程        进程                    ...

  2. MFC界面相关(彩色工具栏)

    MFC工具栏控件 创建工具栏步骤: (1)在Resource View中插入新toolbar (2)在toolbar上双击增加按钮,更改ID为ID_BUTTON,编译后在resource.h中即可看到 ...

  3. poj1066 Jugs

    poj1066 Jugs http://poj.org/problem?id=1606 解题思路:本题可以用数学方法解得,最易理解,常规的解法是搜索.直接用接近模拟的广度优先搜索即可过. 给两个容器, ...

  4. javascript学习笔记--迭代函数

    概要 这里的迭代函数指的是对数组对象的操作方法,js数组共有五个迭代函数:every.fifter.forEach.map.some. 1.every every方法,返回值为Boolean类型,tr ...

  5. 大话项目管理工具之Confluence篇

    前言 前面的两篇文章,讲的都是代码层次上的项目管理工具.这篇文章打算介绍一下关于知识管理的工具,当然,它也属于项目管理中的一部分.说到知识管理,你想到了什么呢?织网?找节点?不错,这是个人的知识管理. ...

  6. InputStream中read()与read(byte[] b)(转)

    read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK API中是这样描述两者的: 1:read() : 从输入流中读取数据的下 ...

  7. (萌O(∩_∩)O)哈希知识点小结

    噶呜~先来了解一下什么是哈希吧? 当我们要在一堆东西中找到想要的那一个东西,我们常常通过比较来找,理想的情况是不经过任何比较,一次就能找到,怎么才能做到这样呢?那就在记录的储存位置和他的关键字之间建立 ...

  8. Android---OpenGL ES之添加动作

    本文译自:http://developer.android.com/training/graphics/opengl/motion.html 在屏幕上绘制对象是OpenGL的最基本功能,你可以使用其他 ...

  9. spring中bean的设计模式

    默认的是单例的. 如果不想单例需要如下配置: <bean id="user" class="..." singleton="false" ...

  10. Flume+LOG4J+Kafka

    基于Flume+LOG4J+Kafka的日志采集架构方案 本文将会介绍如何使用 Flume.log4j.Kafka进行规范的日志采集. Flume 基本概念 Flume是一个完善.强大的日志采集工具, ...