LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Get CPU Usage in LabWindows using C program

Solved!
Go to solution

Hello All,

 

Please Help me..

How to Get CPU Usage and RAM Memory Usage in LabWindows CVI 12.0 using C.

 

Any help with this will be very appreciated.

Thanks In advance

0 Kudos
Message 1 of 18
(5,620 Views)

You can have a look at the function GetMemoryInfo (, , , , , , ); from the Programmer's Toolbox.

 

To my knowledge there is no function (not even using the Win32 API) that retrieves the CPU usage.

 

0 Kudos
Message 2 of 18
(5,617 Views)

This code determines some sort of CPU usage using Win32API functions. I've taken it from the internet some time ago, don't remember where, and adapted to CVI. It can be executed in the Interactive Window. It gives results more or less comparable with task monitor but I really don't know how reliable it is.

 

#include <windows.h>
#include <utility.h>

static FILETIME	lpIdleTime;
static FILETIME	lpKernelTime;
static FILETIME	lpUserTime;
static ULARGE_INTEGER	ulIdleTime, ulIdleTimeOld;
static ULARGE_INTEGER	ulKernelTime, ulKernelTimeOld;
static ULARGE_INTEGER	ulUserTime, ulUserTimeOld;
static ULARGE_INTEGER	TotalTime, IdleTime;
static int		i;

//BOOL WINAPI GetSystemTimes(
//  __out         LPFILETIME lpIdleTime,
//  __out         LPFILETIME lpKernelTime,
//  __out         LPFILETIME lpUserTime
//);
GetSystemTimes (&lpIdleTime, &lpKernelTime, &lpUserTime);
memcpy (&ulIdleTimeOld,   &lpIdleTime,   sizeof (ULARGE_INTEGER)); 
memcpy (&ulKernelTimeOld, &lpKernelTime, sizeof (ULARGE_INTEGER)); 
memcpy (&ulUserTimeOld,   &lpUserTime,   sizeof (ULARGE_INTEGER)); 

for (i = 0; i < 10; i++) {
	Sleep (200);

	GetSystemTimes (&lpIdleTime, &lpKernelTime, &lpUserTime);
	memcpy (&ulIdleTime,   &lpIdleTime,   sizeof (ULARGE_INTEGER)); 
	memcpy (&ulKernelTime, &lpKernelTime, sizeof (ULARGE_INTEGER)); 
	memcpy (&ulUserTime,   &lpUserTime,   sizeof (ULARGE_INTEGER)); 

	TotalTime.QuadPart = (ulKernelTime.QuadPart - ulKernelTimeOld.QuadPart) + (ulUserTime.QuadPart - ulUserTimeOld.QuadPart);
	IdleTime.QuadPart = ulIdleTime.QuadPart - ulIdleTimeOld.QuadPart;
	DebugPrintf ("CPU Usage (%%): %lld\n", (TotalTime.QuadPart - IdleTime.QuadPart) * 100 / TotalTime.QuadPart);

	ulKernelTimeOld = ulKernelTime;
	ulUserTimeOld =   ulUserTime;
	ulIdleTimeOld =   ulIdleTime;
}

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 3 of 18
(5,608 Views)

Thanks for Quick Reply..

I will try this code and let you know...

0 Kudos
Message 4 of 18
(5,602 Views)

Thanks for reply...

 

GUI is not there in my application..So I am looking for any API / system Call which will give direct results..

0 Kudos
Message 5 of 18
(5,601 Views)

I don't understand your answer: neither the function proposed by Wolfgang nor my code make use of a user interface.

Maybe you must give us some more details on your needs and application.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 18
(5,597 Views)

I have tried code you have suggested... It is Working fine in windows enviornment...

But my Application is in Real Time Enviornment ..

When am compiling it is showing undefined error for 'GetSystemTimes'

So i have included "KERNEL32.dll" with project

After that i have downloaded to my target..

It is throwing error like " Error loading "test.dll": Missing export 'GetSystemTimes' from 'KERNEL32.dll'  "

 

Kindly Help to Solve this issue.....

0 Kudos
Message 7 of 18
(5,594 Views)

Info On My Project

 

am working on LabWindows CVI 12.0 for development

This project is a real time application for hardware. which is having Phar Lap ETS as RTOS...

 

My requirement is to find CPU usage and RAM Memory Usage of the Target System...

 

0 Kudos
Message 8 of 18
(5,590 Views)

I can well understand why Win APIs don't work on RT target. Unfortunately I have no suggestions for you on this matter as I have no experience on them.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 18
(5,563 Views)

Yes - RT is a very different animal.  

 

Memory usage is pretty easy - dynamically load the following from the ni_emb.* library (ni_emb.dll on PharLap-based targets, ni_emb.out on VxWorks though VxWorks has a flat hierarchy):

struct RTMemStats {
   unsigned int numBytesAllocated;
   unsigned int numBytesFree;
   unsigned int maxFreeBlockSize;
};

unsigned int GetRTMemStats( RTMemStats *memstats );

The return value is always 0 - this call cannot fail.

 

CPU statistics is very similar.  This API could disappear in future releases, but it's the only one externally available right now:

void ThreadTimeGetPrioritizedCPUUsage(double* usageIdle, double* usageLow, double* usageNormal, double* usageHigh, double* usageTimeCritical);

Remember, these are all cdecl calling conventions.

 

-Danny

Message 10 of 18
(5,544 Views)