Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Programmatically read CPU load and RAM usage: (5 Items)
   
Programmatically read CPU load and RAM usage  
Dear community,
tools like 'top', 'sac' or 'pidin' can be used to display CPU load and RAM usage, so there must be a way to program this
.
Could somebody please tell me, how this can be done in a C(++) program (QNX7)?

Thanks in advance,
Michael.
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
Re: Programmatically read CPU load and RAM usage  
here's one way of reading free mem of the system (or at least a start):


http://www.qnx.com/developers/docs/7.1/#com.qnx.doc.neutrino.lib_ref/topic/p/procmgr_value_notify_add.html


you can also follow this guide to get mapping information and then sum it up:

http://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.neutrino.cookbook/topic/s3_procfs_DCMD_PROC_MAPINFO.html


Note though that esp DCMD_PROC_PAGEDATA is *very* expensive, you don't want to run that often.


regards

-- Michael


________________________________
From: Albrecht Uhlmann <community-noreply@qnx.com>
Sent: Thursday, February 2, 2023 15:08
To: ostech-core_os
Subject: 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



_______________________________________________

OSTech
http://community.qnx.com/sf/go/post122126
To cancel your subscription to this discussion, please e-mail ostech-core_os-unsubscribe@community.qnx.com
Attachment: HTML sf-attachment-mime40890 3.61 KB
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
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.