Main Site Documentation

CPU utilization


#1

Is there a way to see how much cpu time my programm consums?

At the moment I have 5 threads running and its going complex, so is there a way to see if Iam on the limit of the emx dev board?

best regards


#2

Good question. Nothing built in but let me ask the experts here


#3

Did anyone ever get you an answer on this? Is there a way to get status of memory useage as well?


#4

Andrew, memory is easy. In debugging mode in VS you can see the GC messages when cleanup happens.


#5

If your thinking of how much time each thread is consuming then it would be interesting.

If your thinking in terms of ‘idle time like from the task manager on your PC then there should be next to none if youre not using lots of thread.sleep.

As far as I understand this system there would be no limit processor vise, just all threads will execute slower the more you pack on to the system.


#6

I was thinkng more in terms of when I’m not debugging. I would have the free memory and CPU utilization as part of the status that my cobra app sends back to a host so I can keep tabs on it.

On the CPU I was in fact thinking about how much time each thread consumes. I can probably come up with something if it becomes absolutly necessary. I was envisioning that some threads could thrash because of type of processing I’m doing. With some info sent back to the host I could see this and possibly address it by canceling the thread etc.

Thanks -AP


#7

Memory usage is easy to get through
memory = GC.Enable(false);
which is what is really important

Now, for the processor utilization, we have discussed this internally and it looks like something can be be done but since this is not more important than the other things we are working on, it is going to be a while before we do anything.

As always, if you have serious commercial needs then you can contact GHI directly to discuss


#8

I’m thinking of a lowest priority thread that increments a number. A slightly higher priority thread can then read out the number each second and analyse it.


#9

I thought about it and it will not work. Threads on NETMF are very nicely done so each thread will run sooner or later even if a thread is low priority. In our case, we want the idle thread to run ONLY when system is completely idle.

Do not misunderstand this. NETMF threading system is done very very well so no bad-high-thread can lockup the whole system. In our case of reading idle time, this will not work for us!


#10

Ok, it will not work to create an Idle thread but it might work to measure the CPU utilization.
The lowest priority thread will run for sure, but is will run much more (and increment the number faster) when higher priority threads are in a Sleep state.


#11

I did a quick test and the results are as expected.

I have this lowest priority thread:


private static void SlowThread()
{
    while (true) idle++;
}

I also have a thread that answers to web-requests.

In the main program loop I used this code:


int prevIdle = 0;
while (true)
{
    Thread.Sleep(2000);

    int i = idle;
    Debug.Print((i - prevIdle).ToString());
    prevIdle = i;
}

And this is the output:

51592
51085
51089
51082
51077
51079
45925
51077
51084
51084
43335
51081
35092
42830
51080
51087

The highest number ~51000 is my application running in an Idle state. The number drops to ~45000 when I send a webrequest to the FEZ and to ~35000 when I send more then one webrequest.

But it might not be possible to convert this number to a value in %


#12

Do this, add a thread with while(true){}; in it. You should get 100% utilization but you will not because even though you have a dead lock in a high priority thread, the idle thread will still run.

But, if what you did is giving you enough idea of utilization then that maybe good enough for your needs.


#13

I agree make one or more highest priority thread(s) and record the lowest number. Use that as your base 0 to calculate % usage roughly