Monitoring execution times

QNX Neutrino includes some special CPU-time clocks that you can use to monitor the execution times for processes and threads.

This mechanism is implemented following the POSIX "Execution Time Monitoring" option, which defines special clock IDs that represent the execution time of a thread or a process:

To obtain: Call: Specifying: Classification
A process CPU-time clock ID clock_getcpuclockid() A process ID, or 0 to get the clock ID for the calling process POSIX
A thread CPU-time clock ID pthread_getcpuclockid() A thread ID in the calling process POSIX
Either of the above ClockId() A process ID and a thread ID QNX Neutrino

A process has permission to get the CPU-time clock ID of any process.

To get the execution time for the process or thread, call clock_gettime() or ClockTime(), passing a process or thread CPU-time clock ID. POSIX also defines the following, which you can use instead of calling clock_getcpuclockid() or pthread_getcpuclockid():

CLOCK_PROCESS_CPUTIME_ID
The CPU-time clock ID for the calling process.
CLOCK_THREAD_CPUTIME_ID
The CPU-time clock ID for the calling thread.

QNX Neutrino calculates the execution times for all threads and processes on every clock tick. The full tick interval is added to the running time of the active threads, so the times are approximate.

Here's an example:

int process_clock_id, ret;
struct timespec process_time;

ret = clock_getcpuclockid(0, &process_clock_id);
if (ret != 0) {
    perror ("clock_getcpuid()");
    return (EXIT_FAILURE);
}

printf ("Process clock ID: %d\n", process_clock_id);
  
ret = clock_gettime (process_clock_id, &process_time);
if (ret != 0) {
    perror ("clock_gettime()");
    return (EXIT_FAILURE);
}
printf ("Process clock: %ld sec, %ld nsec\n", process_time.tv_sec,
        process_time.tv_nsec);

Given that we're getting the execution time for the calling process, we could use CLOCK_PROCESS_CPUTIME_ID instead of clock_getcpuclockid():

int process_clock_id, ret;
struct timespec process_time;

ret = clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &process_time);
if (ret != 0) {
    perror ("clock_gettime()");
    return (EXIT_FAILURE);
}
printf ("Process clock: %ld sec, %ld nsec\n", process_time.tv_sec,
        process_time.tv_nsec);
Note: You can't use clock_settime() or ClockTime() to set a process or thread CPU-time clock.