1 /* 2 * Copyright (c) 2004, Intel Corporation. All rights reserved. 3 * This file is licensed under the GPL license. For the full content 4 * of this license, see the COPYING file at the top level of this 5 * source tree. 6 * 7 * If pid=0, then clock_getcpuclockid() will return the CPU-time clock of 8 * the calling process. 9 * 10 */ 11 12 #define _XOPEN_SOURCE 600 13 14 #include <errno.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <time.h> 19 #include <unistd.h> 20 #include "posixtest.h" 21 #include "timespec.h" 22 23 int main(void) 24 { 25 #if !defined(_POSIX_CPUTIME) || _POSIX_CPUTIME == -1 26 printf("_POSIX_CPUTIME unsupported\n"); 27 return PTS_UNSUPPORTED; 28 #else 29 clockid_t clockid_1, clockid_2; 30 struct timespec t1, t2, t3; 31 32 if (sysconf(_SC_CPUTIME) == -1) { 33 printf("_POSIX_CPUTIME unsupported\n"); 34 return PTS_UNSUPPORTED; 35 } 36 37 if (clock_getcpuclockid(getpid(), &clockid_1) != 0) { 38 printf("clock_getcpuclockid(getpid(),) failed\n"); 39 return PTS_FAIL; 40 } 41 42 if (clock_getcpuclockid(0, &clockid_2) != 0) { 43 printf("clock_getcpuclockid(0,) failed\n"); 44 return PTS_FAIL; 45 } 46 47 /* if ids are the same, we are done */ 48 if (clockid_1 == clockid_2 && clockid_2 == CLOCK_PROCESS_CPUTIME_ID) { 49 printf("Test PASSED\n"); 50 return PTS_PASS; 51 } 52 53 /* otherwise get cputimes and check that they differs only a little */ 54 if (clock_gettime(clockid_1, &t1) != 0) { 55 printf("clock_gettime(clockid_1,) failed\n"); 56 return PTS_FAIL; 57 } 58 59 if (clock_gettime(clockid_2, &t2) != 0) { 60 printf("clock_gettime(clockid_2,) failed\n"); 61 return PTS_FAIL; 62 } 63 64 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t3) != 0) { 65 printf("clock_gettime(CLOCK_PROCESS_CPUTIME_ID,) failed\n"); 66 return PTS_FAIL; 67 } 68 69 if (timespec_nsec_diff(&t1, &t2) > NSEC_IN_SEC / 2 || 70 timespec_nsec_diff(&t2, &t3) > NSEC_IN_SEC / 2) { 71 printf("reported times differ too much\n"); 72 return PTS_FAIL; 73 } 74 75 printf("Test PASSED\n"); 76 return PTS_PASS; 77 #endif 78 } 79