Albrecht Uhlmann
|
Re: Programmatically read CPU load and RAM usage
|
Albrecht Uhlmann
02/02/2023 9:08 AM
post122126
|
Re: Programmatically read CPU load and RAM usage
Hi Michael,
for total RAM usage, there is a simple command, I think you need to do a stat on /proc and read the filesize member. The
knowledge base contains sample code: https://www.qnx.com/support/knowledgebase.html?id=50130000000nbHX
As to CPU load, I am very much interested myself but I do not know a solution. My idea was to open process #1 and use
the devctl's to query its individual threads. Threads #1...through to #cpus are the idle threads (one per core), so
getting their time must be "Idle time per core". The CPU time of all other threads in procnto I guess is what would be
called "Kernel Time". And the remaining time (per core) is user time.
What I do not know is interrupt time.
Any of the QNX gurus comment on this?
Regards,
-Al
|
|
|
Elad Lahav
|
Re: Programmatically read CPU load and RAM usage
|
Elad Lahav
02/02/2023 9:59 AM
post122128
|
Re: Programmatically read CPU load and RAM usage
There are various ways to query total and used memory. The cleanest in my opinion is to use the _MEM_INFO message, e.g.:
mem_info_t msg = { .i.type = _MEM_INFO, .i.fd = -1 };
if (MsgSend(MEMMGR_COID, &msg.i, sizeof(msg.i), &msg.o, sizeof(msg.o))
== -1) {
perror("MsgSend");
return 1;
}
printf("Total memory=%lu Free memory=%lu\n",
msg.o.info.__posix_tmi_total, msg.o.info.posix_tmi_length);
To track CPU usage you do the opposite and look at idle time. This can be done using the runtime clock of the idle
threads. For example, for core 1:
uint64_t idle_1_time;
clock_t idle_1_clock_id = ClockId(1, 1);
ClockTime(idle_1_clock_id, NULL, &idle_1_time);
The value in idle_1_time is the total run time of the idle thread on core 1. By sampling this value periodically you can
calculate the percentage of idle time and, from that, the percentage of CPU usage.
--Elad
|
|
|
Michael Kurt
|
Re: Programmatically read CPU load and RAM usage
|
Michael Kurt
02/03/2023 1:23 AM
post122130
|
Re: Programmatically read CPU load and RAM usage
Thank You all for the quick response and simple solution.
I think that's all I need to know (and Albrecht also got an answer to his question).
Regards,
Michael.
|
|
|