Home | History | Annotate | Download | only in clock_gettime
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8    Test that parameter CLOCK_PROCESS_CPUTIME_ID returns the CPU time of
      9    the calling process.
     10    Validity is checked by ensuring that the time returned is always
     11    increasing.
     12    This is only supported if _POSIX_CPUTIME is defined.
     13  */
     14 #include <stdio.h>
     15 #include <time.h>
     16 #include <errno.h>
     17 #include <sys/time.h>
     18 #include <unistd.h>
     19 #include "posixtest.h"
     20 
     21 #define LARGENUMBER 900000
     22 void dosomething()
     23 {
     24 	int i;
     25 	for (i = 0; i < LARGENUMBER; i++) {
     26 		clock();
     27 	}
     28 }
     29 
     30 int main(void)
     31 {
     32 #if _POSIX_CPUTIME == -1
     33 	printf("_POSIX_CPUTIME unsupported\n");
     34 	return PTS_UNSUPPORTED;
     35 #else
     36 #ifdef CLOCK_PROCESS_CPUTIME_ID
     37 	struct timespec ts1, ts2, ts3, ts4;
     38 
     39 	if (sysconf(_SC_CPUTIME) == -1) {
     40 		printf("_POSIX_CPUTIME unsupported\n");
     41 		return PTS_UNSUPPORTED;
     42 	}
     43 
     44 	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts1) != 0) {
     45 		printf("clock_gettime() failed: errno %d\n", errno);
     46 		return PTS_UNRESOLVED;
     47 	}
     48 
     49 	dosomething();
     50 
     51 	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts2) != 0) {
     52 		printf("clock_gettime() failed: errno %d\n", errno);
     53 		return PTS_UNRESOLVED;
     54 	}
     55 
     56 	dosomething();
     57 
     58 	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts3) != 0) {
     59 		printf("clock_gettime() failed: errno %d\n", errno);
     60 		return PTS_UNRESOLVED;
     61 	}
     62 
     63 	dosomething();
     64 
     65 	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts4) != 0) {
     66 		printf("clock_gettime() failed: errno %d\n", errno);
     67 		return PTS_UNRESOLVED;
     68 	}
     69 
     70 	if ((ts1.tv_sec <= ts2.tv_sec) &&
     71 	    (ts2.tv_sec <= ts3.tv_sec) && (ts3.tv_sec <= ts4.tv_sec)) {
     72 		printf("Test PASSED\n");
     73 		return PTS_PASS;
     74 	}
     75 
     76 	printf("Test FAILED - ts1=%ld,ts2=%ld,ts3=%ld,ts4=%ld\n",
     77 	       ts1.tv_sec, ts2.tv_sec, ts3.tv_sec, ts4.tv_sec);
     78 	return PTS_FAIL;
     79 #else
     80 	printf("CLOCK_PROCESS_CPUTIME_ID unsupported\n");
     81 	return PTS_UNSUPPORTED;
     82 #endif
     83 #endif
     84 }
     85