-1

I want to calculate idle CPU cycles. I have tried to find out answer of this question on the internet. But the answers were not satisfactory. I queried for calculating idle CPU cycles but answers were for CPU utilization/CPU usage.

Please tell me how to calculate idle CPU cycles for a given time interval in c language? I am working on speed setting algorithm Scheduling for Reduced CPU Energy

idle_cycles = hard_idle + soft_idle;
run_cycles += excess_cycles;
run_percent = run_cycles /
(idle_cycles + run_cycles);
next_excess = run_cycles -
speed * (run_cycles + soft_idle)
IF excess_cycles < 0. THEN
excess_cycles = 0.
energy = (run_cycles - excess_cycles) *
speed * speed;
IF excess_cycles > idle_cycles THEN
newspeed = 1.0;
ELSEIF run_percent > 0.7 THEN
newspeed = speed + 0.2;
ELSEIF run_percent < 0.5 THEN
newspeed = speed -
(0.6 - run_percent);
IF newspeed > 1.0 THEN
newspeed = 1.0;
IF newspeed < min_speed THEN
newspeed = min_speed;
speed = newspeed;
excess_cycles = next_excess;

In this algorithm I came across the term idle_cycles which I want to calculate using c.

Yunis Khan
  • 41
  • 8
  • 3
    What do you call "idle CPU cycles" ? –  Dec 06 '15 at 16:08
  • What environment? For Windows and Linux http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process, otherwise more information required. – Clifford Dec 06 '15 at 16:23
  • I mean cycles where cpu is idle.For linux environment. – Yunis Khan Dec 06 '15 at 16:39
  • 3
    Many OS 'park' unuseable cores, (temporarily unuseable because there is no threads for them to run), in a 'halt' instruction. That core is then stopped. There are no cycles, no instruction fetches, no data reads, no data writes, no bus activity from that core. Hence, there are no idle cycles. – Martin James Dec 06 '15 at 17:42
  • 1
    What @MartinJames says is basically correct; you need to define what you mean. Do you mean 'clock cycles spent whilst the CPU is executing the `halt` instruction' or similar? The links give you mechanisms to determine CPU utilization / usage precisely because those terms have meanings. If you mean clock cycles spent in idle, just multiply the idle percentage by the number of CPU cycles per second and the number of seconds (assuming a single CPU). – abligh Dec 06 '15 at 18:18
  • Assuming you want to do what @MartinJames says, your process must run in a higher privilege level. – edmz Dec 06 '15 at 19:05
  • actually I am working on speed setting algorithm(Scheduling for Reduced CPU Energy published by Mark Weiser) – Yunis Khan Dec 06 '15 at 20:29

1 Answers1

1

/proc/uptime variable file

There is a variable file, located at /proc/uptime, containing only two values:

  1. Uptime in seconds

  2. Idle time in seconds

Note: if you use more than one core, second value is a counter of idle jiffies against all cores.

I wrote a demo using endless html there:

or simple monitor:

Please, rtfm: man proc

/proc/stat variable file

The first lines in /proc/stat hold counters for cpu and each cores.

head -n3 /proc/stat
cpu  1500160 13226 337809 16064648 1475420 34 16142 0 0 0
cpu0 747501 6569 168513 8022626 742061 25 14478 0 0 0
cpu1 752659 6656 169296 8042022 733359 9 1664 0 0 0

Where 4th counter is idle time counter.

in man proc:

man proc | sed  '/proc\/stat/,+19p;d'
   /proc/stat
          kernel/system statistics.   Varies  with  architecture.   Common
          entries include:

          cpu  3357 0 4313 1362393
                 The   amount  of  time,  measured  in  units  of  USER_HZ
                 (1/100ths  of  a  second  on  most   architectures,   use
                 sysconf(_SC_CLK_TCK) to obtain the right value), that the
                 system spent in various states:

                 user   (1) Time spent in user mode.

                 nice   (2) Time spent in  user  mode  with  low  priority
                        (nice).

                 system (3) Time spent in system mode.

                 idle   (4)  Time  spent  in  the  idle  task.  This value
                        should be USER_HZ times the second  entry  in  the
                        /proc/uptime pseudo-file.

For this, there is another endless html script:

F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137