Get CPU Activity

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
eltomato
Posts: 7
Joined: Wed Oct 05, 2011 9:49 am
Location: Bonn, Germany

Get CPU Activity

Post 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?
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Get CPU Activity

Post 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.
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: Get CPU Activity

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Get CPU Activity

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: Get CPU Activity

Post 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.
rdos
Member
Member
Posts: 3308
Joined: Wed Oct 01, 2008 1:55 pm

Re: Get CPU Activity

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Get CPU Activity

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Get CPU Activity

Post by Combuster »

Actually, the result is a led with PWM dimmer. Half load means half brightness.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
CrypticalCode0
Member
Member
Posts: 81
Joined: Wed Nov 09, 2011 2:21 am
Location: Behind a keyboard located in The Netherlands

Re: Get CPU Activity

Post 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. ;)
Post Reply