support cputime clocks for processes/threads other than self
authorRich Felker <dalias@aerifal.cx>
Sat, 8 Jun 2013 15:36:41 +0000 (11:36 -0400)
committerRich Felker <dalias@aerifal.cx>
Sat, 8 Jun 2013 15:36:41 +0000 (11:36 -0400)
apparently these features have been in Linux for a while now, so it
makes sense to support them. the bit twiddling seems utterly illogical
and wasteful, especially the negation, but that's how the kernel folks
chose to encode pids/tids into the clock id.

src/thread/pthread_getcpuclockid.c
src/time/clock_getcpuclockid.c

index cf3d2b8..9df14fb 100644 (file)
@@ -2,5 +2,6 @@
 
 int pthread_getcpuclockid(pthread_t t, clockid_t *clockid)
 {
 
 int pthread_getcpuclockid(pthread_t t, clockid_t *clockid)
 {
-       return ENOSYS;
+       *clockid = (-t->tid-1)*8U + 6;
+       return 0;
 }
 }
index 723840b..8a0e2d4 100644 (file)
@@ -5,7 +5,10 @@
 
 int clock_getcpuclockid(pid_t pid, clockid_t *clk)
 {
 
 int clock_getcpuclockid(pid_t pid, clockid_t *clk)
 {
-       if (pid && pid != getpid()) return EPERM;
-       *clk = CLOCK_PROCESS_CPUTIME_ID;
+       struct timespec ts;
+       clockid_t id = (-pid-1)*8U + 2;
+       int ret = __syscall(SYS_clock_getres, id, &ts);
+       if (ret) return -ret;
+       *clk = id;
        return 0;
 }
        return 0;
 }