49

Question:

  • How is a CPU tick calculated and what does it represent?
  • Does a single tick equate to 10 miliseconds thus if some thread reported not called for (5 * 10 ticks = 500 ticks) does this mean the CPU was perhaps too busy to schedule the aforementioned thread to work?
Aaron
  • 1,488

3 Answers3

42

A tick is an arbitrary unit for measuring internal system time. There is usually an OS-internal counter for ticks; the current time and date used by various functions of the OS are derived from that counter.

How many milliseconds a tick represents depends on the OS, and may even vary between installations. Use the OS's mechanisms to convert ticks into seconds.

As to why a thread reports it's not being called: That will depend on whether the thread is blocking somewhere (waiting, I/O etc.). If it is not blocking, then yes, the OS's scheduler will decide when it gets to run, which may be a long time if the system is busy.

Edit:

Note that, perhaps unfortunately, some authors also use tick as a synonym for processor clock cycle (e.g. this text). I believe this usage is less widespread, but still, best to find out first what people are talking about.

sleske
  • 23,525
3

Edit: Taken from PC Hardware in a Nutshell:

"The processor clock coordinates all CPU and memory operations by periodically generating a time reference signal called a clock cycle or tick. Clock frequency is specified in gigahertz (GHz), which specifies billions of ticks per second. Clock speed determines how fast instructions execute. Some instructions require one tick, others multiple ticks, and some processors execute multiple instructions during one tick."


The time between ticks is determined by your clock speed, and it takes one to many ticks depending on the OP being performed. For example, a 286 class CPU needs 20 ticks to multiply two numbers.

If you need high performance timers, then I don't think you can rely on ticks being constant across all systems.

The CPU scheduler could have delayed the thread, especially if there was another thread with a higher priority. So yes, the CPU could've been too busy.

invert
  • 4,996
-1

In addition to sleske's answer I wrote a C program that will print the "OS ticks per second" ft:

// C headers
#include <stdio.h>

// Posix headers #include <unistd.h>

int main(int argc, char * argv[]){

long ft; // OS ticks per second

t = sysconf(_SC_CLK_TCK);
printf(&quot;ft = %ld\n\n&quot;, ft);

return 0;

}

In my case I am on OS Linux Debian system and when I compile this C program and run it, it will always print ft = 100. Note that here unit is not written but it is "ticks per second". Therefore this is a frequency! We can write it with a unit:

ft = 100 1/s

If we want to get a period t out of frequency ft this equation is the missing link.

t = 1/ftt = 1/(100 1/s)t = 1/100 s

Let's convert this in ˙ms˙:

t = 1/100 s = (1 * 10^(-3)) s / (100 * 10^(-3)) = 1 * m s / (100 * 10^(-3)) = 1 * m s * 10^3 * 10^(-2) = 10^4 ms = 10 ms.

So tick happens every 10 ms on OS Linux Debian.

71GA
  • 140
  • 1
  • 12