05-08-2013 01:13 AM
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
Solved! Go to Solution.
05-08-2013 01:50 AM
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.
05-08-2013 05:02 AM - edited 05-08-2013 05:09 AM
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; }
05-08-2013 06:21 AM
Thanks for Quick Reply..
I will try this code and let you know...
05-08-2013 06:24 AM
Thanks for reply...
GUI is not there in my application..So I am looking for any API / system Call which will give direct results..
05-08-2013 06:37 AM
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.
05-08-2013 07:11 AM
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.....
05-08-2013 07:26 AM
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...
05-09-2013 05:07 AM
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.
05-09-2013 10:35 AM
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