1 /* Test 75 - getrusage functionality test. 2 */ 3 4 #include <sys/resource.h> 5 #include <sys/time.h> 6 #include <stdio.h> 7 #include <assert.h> 8 #include <sys/types.h> 9 #include <sys/wait.h> 10 #include <unistd.h> 11 12 #include "common.h" 13 14 #define CHECK_ZERO_FIELD(rusage, field) \ 15 if (rusage.field != 0) \ 16 em(1, #field " must be zero"); 17 18 #define CHECK_NOT_ZERO_FIELD(rusage, field) \ 19 if (rusage.field == 0) \ 20 em(1, #field " can't be zero"); 21 22 #define CHECK_EQUAL_FIELD(rusage1, rusage2, field) \ 23 if (rusage1.field != rusage2.field) \ 24 em(1, #field " of " #rusage1 " doesn't equal to " \ 25 #field " of " #rusage2); 26 27 static void 28 spin(void) 29 { 30 struct timeval start_time; 31 struct timeval end_time; 32 unsigned int loop = 0; 33 if (gettimeofday(&start_time, NULL) == -1) { 34 e(1); 35 exit(1); 36 } 37 end_time = start_time; 38 do { 39 if ((++loop % 3000000000) == 0) { 40 if (gettimeofday(&end_time, NULL) == -1) { 41 e(1); 42 exit(1); 43 } 44 } 45 } while (start_time.tv_sec + 10 > end_time.tv_sec); 46 } 47 48 int 49 main(int argc, char *argv[]) 50 { 51 struct rusage r_usage1; 52 struct rusage r_usage2; 53 struct rusage r_usage3; 54 pid_t child; 55 int status = 0; 56 start(75); 57 if ((getrusage(RUSAGE_SELF + 1, &r_usage1) != -1 || errno != EINVAL) || 58 (getrusage(RUSAGE_CHILDREN - 1, &r_usage1) != -1 || 59 errno != EINVAL) || (getrusage(RUSAGE_SELF, NULL) != -1 || 60 errno != EFAULT)) { 61 e(1); 62 exit(1); 63 } 64 spin(); 65 if (getrusage(RUSAGE_SELF, &r_usage1) != 0) { 66 e(1); 67 exit(1); 68 } 69 CHECK_NOT_ZERO_FIELD(r_usage1, ru_utime.tv_sec); 70 CHECK_NOT_ZERO_FIELD(r_usage1, ru_maxrss); 71 CHECK_NOT_ZERO_FIELD(r_usage1, ru_ixrss); 72 CHECK_NOT_ZERO_FIELD(r_usage1, ru_idrss); 73 CHECK_NOT_ZERO_FIELD(r_usage1, ru_isrss); 74 if (getrusage(RUSAGE_CHILDREN, &r_usage2) != 0) { 75 e(1); 76 exit(1); 77 } 78 CHECK_NOT_ZERO_FIELD(r_usage2, ru_maxrss); 79 CHECK_NOT_ZERO_FIELD(r_usage2, ru_ixrss); 80 CHECK_NOT_ZERO_FIELD(r_usage2, ru_idrss); 81 CHECK_NOT_ZERO_FIELD(r_usage2, ru_isrss); 82 CHECK_EQUAL_FIELD(r_usage1, r_usage2, ru_ixrss); 83 CHECK_EQUAL_FIELD(r_usage1, r_usage2, ru_idrss); 84 CHECK_EQUAL_FIELD(r_usage1, r_usage2, ru_isrss); 85 if ((child = fork()) == 0) { 86 /* 87 * We cannot do this part of the test in the parent, since 88 * start() calls system() which spawns a child process. 89 */ 90 if (getrusage(RUSAGE_CHILDREN, &r_usage1) != 0) { 91 e(1); 92 exit(1); 93 } 94 CHECK_ZERO_FIELD(r_usage1, ru_utime.tv_sec); 95 CHECK_ZERO_FIELD(r_usage1, ru_utime.tv_usec); 96 spin(); 97 exit(0); 98 } else { 99 if (child != waitpid(child, &status, 0)) { 100 e(1); 101 exit(1); 102 } 103 if (WEXITSTATUS(status) != 0) { 104 e(1); 105 exit(1); 106 } 107 if (getrusage(RUSAGE_CHILDREN, &r_usage3) != 0) { 108 e(1); 109 exit(1); 110 } 111 CHECK_NOT_ZERO_FIELD(r_usage3, ru_utime.tv_sec); 112 CHECK_NOT_ZERO_FIELD(r_usage3, ru_maxrss); 113 CHECK_NOT_ZERO_FIELD(r_usage3, ru_ixrss); 114 CHECK_NOT_ZERO_FIELD(r_usage3, ru_idrss); 115 CHECK_NOT_ZERO_FIELD(r_usage3, ru_isrss); 116 CHECK_EQUAL_FIELD(r_usage1, r_usage3, ru_ixrss); 117 CHECK_EQUAL_FIELD(r_usage1, r_usage3, ru_idrss); 118 CHECK_EQUAL_FIELD(r_usage1, r_usage3, ru_isrss); 119 } 120 quit(); 121 122 return 0; 123 } 124