Page 1 of 1

Get CPU Activity

Posted: Thu Dec 15, 2011 5:15 am
by eltomato
Hi,

I'm just wanted to implement some eye candy ui things and show a cpu activity (In a blinking dot).

How do I get the current state of my cpu? I don't really know what to search for. :P

Any hints?

Re: Get CPU Activity

Posted: Thu Dec 15, 2011 5:36 am
by bluemoon
If it's your own OS, you usually record such activity in the scheduler.

If you're talking about geting such information on specific platform like windows or linux, check the manual.

Re: Get CPU Activity

Posted: Thu Dec 15, 2011 7:07 am
by turdus
I've a so called TCB (thread control block) mapped at a fixed location for every thread that holds tick counters. When my timer isr gets called, it checks the code segment on the stack, and increases user tick counter or kernel tick counter for the current thread accordingly. This is not 100% accurate, but close enough.
Before displaying, I sum all the ticks, so I can calculate the percentage of cpu usage in userspace as well as in kernelspace for every thread.
That's what you can do about it. If you want a led style indicator, I'm with Berkus, never turn it off.

Re: Get CPU Activity

Posted: Thu Dec 15, 2011 11:14 am
by Brendan
Hi,

I'd do time accounting during task switches. For example, imagine something like this at the start of the task switch:

Code: Select all

    used += get_time() - time_of_last_task_switch;
    time_of_last_task_switch = now;
    current_task->used_time[current_task->priority_group] += used;
    this_cpu->used_time[current_task->priority_group] += used;
Of course this works better if "get_time()" is fast and precise (e.g. use the TSC or maybe HPET's main counter if you can). It still works if "get_time()" is returning "ticks_since_boot" (with 10ms between ticks or something) though.

From that you can find out how much time each CPU has spent running high priority tasks, medium priority tasks, low priority tasks, etc. You could monitor these values and calculate averages and draw pretty graphs of CPU load; or figure out how much CPU time each task has consumed.

For your "activity light", maybe you could do something like this:

Code: Select all

    for each CPU {
        last_total += cpu->used_time[HIGH_PRIORITY] + cpu->used_time[MEDIUM_PRIORITY];
    }
    for(;;) {
        sleep(1);
        total = 0;
        for each CPU {
             total += cpu->used_time[HIGH_PRIORITY] + cpu->used_time[MEDIUM_PRIORITY];
        }
        used = total - last_total;
        last_total = total;
        if(used / number_of_cpus >= 0.5) turn_light_on();
        else turn_light_off();
    }
If the light is on, then CPUs are spending at least 50% of their time running medium priority or high priority tasks...


Cheers,

Brendan

Re: Get CPU Activity

Posted: Thu Dec 15, 2011 2:47 pm
by turdus
Brendan wrote:If the light is on, then CPUs are spending at least 50% of their time running medium priority or high priority tasks...
That gave me an idea. You could turn off the light if you schedule idle thread, turn it on otherwise. On a normal desktop it will light about 2-3% of the time. If load increases, time grows, and blinking will be more rapid; on an overloaded machine it will light continously.

Re: Get CPU Activity

Posted: Thu Dec 15, 2011 2:51 pm
by rdos
Brendan wrote:Of course this works better if "get_time()" is fast and precise (e.g. use the TSC or maybe HPET's main counter if you can). It still works if "get_time()" is returning "ticks_since_boot" (with 10ms between ticks or something) though.
I use my GetSystemTime syscall which returns number of (PIT, 1.193MHz) tics (8 bytes) since boot regardless of if this derives from PIT, HPET or something else. That means I record execution with sub-microsecond precision per thread. Since each core also has a null-thread, which accumulates time, I can compare the load between cores with good precision, and see how much the cores are loaded.

Re: Get CPU Activity

Posted: Thu Dec 15, 2011 4:44 pm
by Brendan
Hi,
turdus wrote:
Brendan wrote:If the light is on, then CPUs are spending at least 50% of their time running medium priority or high priority tasks...
That gave me an idea. You could turn off the light if you schedule idle thread, turn it on otherwise. On a normal desktop it will light about 2-3% of the time. If load increases, time grows, and blinking will be more rapid; on an overloaded machine it will light continously.
With only one CPU, I'm going to write a task that sleeps for 8.333 ms and then runs for 8.333 ms (then sleep, then run, etc). If the video mode is 60 frames per second (very likely) then the activity light will look like it's permanently on or permanently off.

With 16 CPUs all turning the same activity light on and off, you'd probably get more accurate results from "rand()"... ;)


Cheers,

Brendan

Re: Get CPU Activity

Posted: Fri Dec 16, 2011 2:25 am
by Combuster
Actually, the result is a led with PWM dimmer. Half load means half brightness.

Re: Get CPU Activity

Posted: Fri Dec 16, 2011 4:24 am
by CrypticalCode0
Combuster your in the right direction but with PWM half cycle time on half off != half brightness.

now back to topic, Brendan thank you for your code example you solved one of my misunderstandings by it.
I was under the impression it could be done by some ACPI voodoo. ;)