tags:

views:

783

answers:

2

Hi

I am wondering if somebody knows some more details about the time stamp counter in Linux when a context switch occurs? Until now I had the opinion, that the TSC value is just increasing by 1 during each clock cycle, independent if in kernel or in user mode. I measured now the performance of an application using the TSC which yielded a performance result of 5 Mio Clock Cyles. Then, I made some changes to the scheduler which means that a context switch takes considerably longer, i.g. 2 Mio cycles instead of 500.000 cycles. The funny bit is, that when measuring the performance of the original application again it still takes 5 Mio cycles... So I am wondering why it did not take considerably longer as a context switch takes now almost 2 Mio clock cyles more? (and there occur at least 3 context during execution of the application).

Is the time stamp counter somehow deactivated during kernel mode? Or is the content of the TSC saved during contest switches? Thanks if someone could point me out what could be the problem!

A: 

I believe the TSC is actually a hardware construct of the processor you're using. IE: reading the TSC actually uses the RDTSC processor opcode. I don't even think there's a way for the OS to alter it's value, it just increases with each tick since the last power reset.

Regarding your modifications to the scheduler, is it possible that you're using a multi-core processor in a way that the OS is not switching out your running process? You might put a call to sched_yield() or sleep(0) in your program to see if your scheduler changes start taking effect.

bmdhacks
Thanks for your input. I thought the same, that the TSC is just a hardware counter that increases with each tick. I am running the application on a multicore, but I have disabled the second core by setting maxcpus=1 during boot, so that I just have one core. So, basically I have now running those 2 processes on the same core ( hopefully;) ), at the same time but still the difference in the TSC counter remains 5 Mio. I was guessing it should double or something like that when 2 processes share CPU time...
PS.: I should have mentioned the fact that I know the scheduler is called between 3- 10 times during program execution. However, it might be that it just schedules than immediatelly the same process again....
+1  A: 

As you can read on Wikipedia

With the advent of multi-core/hyperthreaded CPUs, systems with multiple CPUs, and "hibernating" operating systems, the TSC cannot be relied on to provide accurate results. The issue has two components: rate of tick and whether all cores (processors) have identical values in their time-keeping registers. There is no promise that the timestamp counters of multiple CPUs on a single motherboard will be synchronized. In such cases, programmers can only get reliable results by locking their code to a single CPU. Even then, the CPU speed may change due to power-saving measures taken by the OS or BIOS, or the system may be hibernated and later resumed (resetting the time stamp counter). Reliance on the time stamp counter also reduces portability, as other processors may not have a similar feature. Recent Intel processors include a constant rate TSC (identified by the constant_tsc flag in Linux's /proc/cpuinfo). With these processors the TSC reads at the processor's maximum rate regardless of the actual CPU running rate. While this makes time keeping more consistent, it can skew benchmarks, where a certain amount of spin-up time is spent at a lower clock rate before the OS switches the processor to the higher rate. This has the effect of making things seem like they require more processor cycles than they normally would.

lothar
I should have mentioned that I have disabled one of the cores, using the maxcpus=1. When using cat /proc/cpuinfo it tells me there is just one CPU, plus I have the constant_tsc flag set! That is, why that all is a bit suspicious...
From the above quote: "Even then, the CPU speed may change due to power-saving measures taken by the OS".
lothar