Get CPU Activity
Get CPU Activity
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.
Any hints?
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.
Any hints?
Re: Get CPU Activity
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.
If you're talking about geting such information on specific platform like windows or linux, check the manual.
Re: Get CPU Activity
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.
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
Hi,
I'd do time accounting during task switches. For example, imagine something like this at the start of the task switch:
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:
If the light is on, then CPUs are spending at least 50% of their time running medium priority or high priority tasks...
Cheers,
Brendan
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;
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();
}
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.
Re: Get CPU Activity
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.Brendan wrote:If the light is on, then CPUs are spending at least 50% of their time running medium priority or high priority tasks...
Re: Get CPU Activity
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.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.
Re: Get CPU Activity
Hi,
With 16 CPUs all turning the same activity light on and off, you'd probably get more accurate results from "rand()"...
Cheers,
Brendan
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.turdus wrote: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.Brendan wrote:If the light is on, then CPUs are spending at least 50% of their time running medium priority or high priority tasks...
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.
- Combuster
- 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
Actually, the result is a led with PWM dimmer. Half load means half brightness.
-
- Member
- Posts: 81
- Joined: Wed Nov 09, 2011 2:21 am
- Location: Behind a keyboard located in The Netherlands
Re: Get CPU Activity
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.
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.