Tuesday, October 1, 2013

Negative clockid when using clock_getcpuclockid()

Negative clockid when using clock_getcpuclockid()

I am trying to understand clock_getres() and clock_getcpuclockid(). I
wrote the example snippet below. I was expecting to get a clockid of 2
(CLOCK_PROCESS_CPUTIME_ID), based on the code in glibc:
https://github.com/lattera/glibc/blob/master/rt/clock_getcpuclockid.c .
However, the results printed out give a clockid of -6 (err is 0). ts.sec
is 0 and ts.nsec is 1, which seems reasonable. However, I don't understand
why the clockid is -6. What am I missing?
#include <time.h>
#include <iostream>
using namespace std;
int main(void)
{
struct timespec ts;
int clockid;
int err = clock_getcpuclockid(0, &clockid);
clock_getres(clockid, &ts);
cout << "err: " << err << endl;
cout << "clockid: " << clockid << " sec: " << ts.tv_sec
<< " nsec: " << ts.tv_nsec << endl;
return 0;
}

No comments:

Post a Comment