OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 2:28 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Get CPU Activity
PostPosted: Thu Dec 15, 2011 5:15 am 
Offline

Joined: Wed Oct 05, 2011 9:49 am
Posts: 7
Location: Bonn, Germany
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?


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Thu Dec 15, 2011 5:36 am 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1761
Location: Hong Kong
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.


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Thu Dec 15, 2011 7:07 am 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Thu Dec 15, 2011 11:14 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

I'd do time accounting during task switches. For example, imagine something like this at the start of the task switch:
Code:
    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:
    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.


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Thu Dec 15, 2011 2:47 pm 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Thu Dec 15, 2011 2:51 pm 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Thu Dec 15, 2011 4:44 pm 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Fri Dec 16, 2011 2:25 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
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 ]


Top
 Profile  
 
 Post subject: Re: Get CPU Activity
PostPosted: Fri Dec 16, 2011 4:24 am 
Offline
Member
Member

Joined: Wed Nov 09, 2011 2:21 am
Posts: 81
Location: Behind a keyboard located in The Netherlands
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. ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: deblokc, Google [Bot], Majestic-12 [Bot], MichaelPetch and 167 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group