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

Danny,

 

Thanks for the reply and suggestion....

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

 

 

-Vaishakh

0 Kudos
Message 11 of 18
(2,059 Views)

Danny,


I have tried your suggesion.

When i am compiling Code it is throwing "Missing Prototype" Error.

So i tried to load library for the API ("ni_emb.dll " you have suggested).

But the library is only for LabView Not for LabWindows.It is in another format not as ' .lib ' format.

 

So which library i have to add for successful compilation?

 

For CPU Usage API also same error its throwing..

 

Please help to solve this problem...

0 Kudos
Message 12 of 18
(2,054 Views)
Solution
Accepted by topic author vaishakhak

I should have been more explicit.  You need to dynamically load this function in order for it to work - it is not included in any exported headers. This code is written for a C++ compiler, but it's an example of how to do what you want to do.

 

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

__declspec(dllexport) int __cdecl printFreeMemory( void )
{
   typedef unsigned int (*tGetRTMemStatsFunc)(RTMemStats *memStats);

   HMODULE handle = LoadLibrary( "ni_emb.dll" );
   if( !handle )
   {
       printf( "ni_emb.dll failed to load!\n" );
       return -1;
   }

   tGetRTMemStatsFunc myfunc = (tGetRTMemStatsFunc) GetProcAddress( handle, "GetRTMemStats" );
   if( !myfunc)
   {
       printf( "GetRTMemStats not found in ni_emb.dll!\n" );
       return -2;
   }

   struct RTMemStats myRTMemStats;
   myfunc( &myRTMemStats );

   printf( "The system has 0x%x bytes free!\n", myRTMemStats.numBytesFree );

   FreeLibrary( handle );
   return 0;
}

 

The process is the same for the CPU Utilization function, too. Remember that ni_emb.dll is a "ghost" DLL on PharLap-based targets; you won't find it in the file system, but if you attempt to load the library from within LoadLibrary it will succeed - this is because ni_emb is cleverly hidden within the ph_exec.exe component (required by the system to exist) so that it's always guaranteed to be present on the system.

 

-Danny

Message 13 of 18
(2,038 Views)

Danny,

 

Thanks for the help...

I have tried your suggestion ..

 

getting memory usage i have tested in my target ..

It is working fine..

Thanks once more for giving great help...

 

But CPU usage api is showing error.. like "General Protection Fault"

 

Please help to sort out this issue ..

0 Kudos
Message 14 of 18
(2,029 Views)

If you're getting a protection fault, that's probably because you used a NULL pointer as one of the input parameters.  All of the pointers have to be good, you cannot use NULL for any of the pointers.

 

-Danny

Message 15 of 18
(2,014 Views)

Danny,

 

Thanks for the help.. It is working fine...

 

Using this CPU time details .. how i can calculate CPU Usage in percentage??

 

0 Kudos
Message 16 of 18
(1,993 Views)

I'm sure if you thought about that for a few more minutes you'll come up with your answer - if you add up all the values, what do you get?  Remember, those values represent ALL of the CPUs in the system (if there are multiple cores) and in those values is an idle amount (how much of the CPU is not being used); the rest of the values represent components of the CPU "usage".  I'll leave the math as an exercise to the reader.  

 

-Danny

Message 17 of 18
(1,986 Views)

Danny,

 

Thanks for helping ...

 

 

Sorry am very new to processor level coding...

 

--Vaishakh

 

0 Kudos
Message 18 of 18
(1,973 Views)