Changeset 1883 in kBuild for trunk/src/misc
- Timestamp:
- Oct 19, 2008 8:06:44 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/misc/kmk_time.c
r1796 r1883 45 45 # include <unistd.h> 46 46 # include <sys/times.h> 47 # include <signal.h> 47 48 #endif 48 49 … … 55 56 #endif 56 57 58 #ifndef _MSC_VER 59 static const char *my_strsignal(int signo) 60 { 61 #define CASE_SIG_RET_STR(sig) if (signo == SIG##sig) return #sig 62 #ifdef SIGHUP 63 CASE_SIG_RET_STR(HUP); 64 #endif 65 #ifdef SIGINT 66 CASE_SIG_RET_STR(INT); 67 #endif 68 #ifdef SIGQUIT 69 CASE_SIG_RET_STR(QUIT); 70 #endif 71 #ifdef SIGILL 72 CASE_SIG_RET_STR(ILL); 73 #endif 74 #ifdef SIGTRAP 75 CASE_SIG_RET_STR(TRAP); 76 #endif 77 #ifdef SIGABRT 78 CASE_SIG_RET_STR(ABRT); 79 #endif 80 #ifdef SIGIOT 81 CASE_SIG_RET_STR(IOT); 82 #endif 83 #ifdef SIGBUS 84 CASE_SIG_RET_STR(BUS); 85 #endif 86 #ifdef SIGFPE 87 CASE_SIG_RET_STR(FPE); 88 #endif 89 #ifdef SIGKILL 90 CASE_SIG_RET_STR(KILL); 91 #endif 92 #ifdef SIGUSR1 93 CASE_SIG_RET_STR(USR1); 94 #endif 95 #ifdef SIGSEGV 96 CASE_SIG_RET_STR(SEGV); 97 #endif 98 #ifdef SIGUSR2 99 CASE_SIG_RET_STR(USR2); 100 #endif 101 #ifdef SIGPIPE 102 CASE_SIG_RET_STR(PIPE); 103 #endif 104 #ifdef SIGALRM 105 CASE_SIG_RET_STR(ALRM); 106 #endif 107 #ifdef SIGTERM 108 CASE_SIG_RET_STR(TERM); 109 #endif 110 #ifdef SIGSTKFLT 111 CASE_SIG_RET_STR(STKFLT); 112 #endif 113 #ifdef SIGCHLD 114 CASE_SIG_RET_STR(CHLD); 115 #endif 116 #ifdef SIGCONT 117 CASE_SIG_RET_STR(CONT); 118 #endif 119 #ifdef SIGSTOP 120 CASE_SIG_RET_STR(STOP); 121 #endif 122 #ifdef SIGTSTP 123 CASE_SIG_RET_STR(TSTP); 124 #endif 125 #ifdef SIGTTIN 126 CASE_SIG_RET_STR(TTIN); 127 #endif 128 #ifdef SIGTTOU 129 CASE_SIG_RET_STR(TTOU); 130 #endif 131 #ifdef SIGURG 132 CASE_SIG_RET_STR(URG); 133 #endif 134 #ifdef SIGXCPU 135 CASE_SIG_RET_STR(XCPU); 136 #endif 137 #ifdef SIGXFSZ 138 CASE_SIG_RET_STR(XFSZ); 139 #endif 140 #ifdef SIGVTALRM 141 CASE_SIG_RET_STR(VTALRM); 142 #endif 143 #ifdef SIGPROF 144 CASE_SIG_RET_STR(PROF); 145 #endif 146 #ifdef SIGWINCH 147 CASE_SIG_RET_STR(WINCH); 148 #endif 149 #ifdef SIGIO 150 CASE_SIG_RET_STR(IO); 151 #endif 152 #ifdef SIGPWR 153 CASE_SIG_RET_STR(PWR); 154 #endif 155 #ifdef SIGSYS 156 CASE_SIG_RET_STR(SYS); 157 #endif 158 #ifdef SIGBREAK 159 CASE_SIG_RET_STR(BREAK); 160 #endif 161 #undef CASE_SIG_RET_STR 162 return "???"; 163 } 164 #endif /* unix */ 57 165 58 166 static const char *name(const char *pszName) … … 84 192 int main(int argc, char **argv) 85 193 { 86 int i; 194 int i, j; 195 int cTimes = 3; 87 196 #if defined(_MSC_VER) 88 FILETIME ftStart, ft; 89 intptr_t rc; 197 FILETIME ftStart, ft; 198 unsigned _int64 usMin, usMax, usAvg, usTotal, usCur; 199 unsigned _int64 iStart; 200 intptr_t rc; 90 201 #else 91 struct timeval tvStart, tv; 92 pid_t pid; 93 int rc; 202 struct timeval tvStart, tv; 203 unsigned long long usMin, usMax, usAvg, usTotal, usCur; 204 pid_t pid; 205 int rc; 94 206 #endif 95 207 … … 119 231 else if (!strcmp(psz, "-version")) 120 232 psz = "V"; 233 else if (!strcmp(psz, "-iterations")) 234 psz = "i"; 121 235 } 122 236 … … 133 247 KBUILD_SVN_REV); 134 248 return 0; 249 250 case 'i': 251 if (i + 1 >= argc) 252 { 253 fprintf(stderr, "%s: syntax error: missing iteration count\n", name(argv[0])); 254 return 1; 255 } 256 cTimes = atoi(argv[++i]); 257 if (cTimes <= 0) 258 { 259 fprintf(stderr, "%s: error: invalid interation count '%s'.\n", name(argv[0]), argv[i]); 260 return 1; 261 } 262 break; 135 263 136 264 default: … … 150 278 151 279 /* 152 * Execute the program (it's actually supposed to be a command I think, but wtf).280 * Execute the program the specified number of times. 153 281 */ 282 usMax = usMin = usTotal = 0; 283 usMin--; /* wraps to max value */ 284 for (j = 0; j < cTimes; j++) 285 { 286 /* 287 * Execute the program (it's actually supposed to be a command I think, but wtf). 288 */ 289 154 290 #if defined(_MSC_VER) 155 /** @todo 156 * We'll have to find the '--' in the commandline and pass that 157 * on to CreateProcess or spawn. Otherwise, the argument qouting 158 * is gonna be messed up. 159 */ 160 GetSystemTimeAsFileTime(&ftStart); 161 rc = _spawnvp(_P_WAIT, argv[i], &argv[i]); 162 if (rc != -1) 163 { 164 unsigned _int64 iStart, iElapsed; 291 /** @todo 292 * We'll have to find the '--' in the commandline and pass that 293 * on to CreateProcess or spawn. Otherwise, the argument qouting 294 * is gonna be messed up. 295 */ 296 GetSystemTimeAsFileTime(&ftStart); 297 rc = _spawnvp(_P_WAIT, argv[i], &argv[i]); 298 if (rc != -1) 299 { 300 fprintf(stderr, "%s: error: _spawnvp(_P_WAIT, \"%s\", ...) failed: %s\n", name(argv[0]), argv[i], strerror(errno)); 301 return 8; 302 } 303 165 304 GetSystemTimeAsFileTime(&ft); 166 305 167 306 iStart = ftStart.dwLowDateTime | ((unsigned _int64)ftStart.dwHighDateTime << 32); 168 iElapsed = ft.dwLowDateTime | ((unsigned _int64)ft.dwHighDateTime << 32); 169 iElapsed -= iStart; 170 iElapsed /= 10; /* to usecs */ 171 172 printf("%s: %um%u.%06us\n", name(argv[0]), 173 (unsigned)(iElapsed / (60 * 1000000)), 174 (unsigned)(iElapsed % (60 * 1000000)) / 1000000, 175 (unsigned)(iElapsed % 1000000)); 176 } 177 else 178 { 179 fprintf(stderr, "%s: error: _spawnvp(_P_WAIT, \"%s\", ...) failed: %s\n", name(argv[0]), argv[i], strerror(errno)); 307 usCur = ft.dwLowDateTime | ((unsigned _int64)ft.dwHighDateTime << 32); 308 usCur -= iStart; 309 usCur /= 10; /* to usecs */ 310 311 printf("%s: ", name(argv[0])); 312 if (cTimes != 1) 313 printf("#%u ", j + 1); 314 printf("%um%u.%06us - exit code: %d\n", 315 (unsigned)(usCur / (60 * 1000000)), 316 (unsigned)(usCur % (60 * 1000000)) / 1000000, 317 (unsigned)(usCur % 1000000), 318 rc); 319 320 #else /* unix: */ 180 321 rc = 1; 181 } 182 return rc; 183 184 #else 185 rc = 1; 186 gettimeofday(&tvStart, NULL); 187 pid = fork(); 188 if (pid > 0) 189 { 190 waitpid(pid, &rc, 0); 322 gettimeofday(&tvStart, NULL); 323 pid = fork(); 324 if (!pid) 325 { 326 /* child */ 327 execvp(argv[i], &argv[i]); 328 fprintf(stderr, "%s: error: _execvp(\"%s\", ...) failed: %s\n", name(argv[0]), argv[i], strerror(errno)); 329 return 8; 330 } 331 if (pid < 0) 332 { 333 fprintf(stderr, "%s: error: fork() failed: %s\n", name(argv[0]), strerror(errno)); 334 return 8; 335 } 336 337 /* parent, wait for child. */ 338 rc = 9; 339 while (waitpid(pid, &rc, 0) == -1 && errno == EINTR) 340 /* nothing */; 191 341 gettimeofday(&tv, NULL); 192 342 … … 200 350 tv.tv_usec = tv.tv_usec + 1000000 - tvStart.tv_usec; 201 351 } 202 203 printf("%s: %um%u.%06us\n", name(argv[0]), 352 usCur = tv.tv_sec * 1000000ULL 353 + tv.tv_usec; 354 355 printf("%s: ", name(argv[0])); 356 if (cTimes != 1) 357 printf("#%u ", j + 1); 358 printf("%um%u.%06us", 204 359 (unsigned)(tv.tv_sec / 60), 205 360 (unsigned)(tv.tv_sec % 60), 206 361 (unsigned)tv.tv_usec); 362 if (WIFEXITED(rc)) 363 printf(" - normal exit: %d\n", WEXITSTATUS(rc)); 364 else if (WIFSIGNALED(rc) && WCOREDUMP(rc)) 365 printf(" - dumped core: %s (%d)\n", my_strsignal(WTERMSIG(rc)), WTERMSIG(rc)); 366 else if (WIFSIGNALED(rc)) 367 printf(" - killed by: %s (%d)\n", my_strsignal(WTERMSIG(rc)), WTERMSIG(rc)); 368 else if (WIFSTOPPED(rc)) 369 printf(" - stopped by: %s (%d)\n", my_strsignal(WSTOPSIG(rc)), WSTOPSIG(rc)); 370 else 371 printf(" unknown exit status %#x (%d)\n", rc, rc); 372 #endif /* unix */ 373 374 /* calc min/max/avg */ 375 usTotal += usCur; 376 if (usMax < usCur) 377 usMax = usCur; 378 if (usMin > usCur) 379 usMin = usCur; 207 380 } 208 else if (!pid) 381 382 /* 383 * Summary if more than one run. 384 */ 385 if (cTimes != 1) 209 386 { 210 execvp(argv[i], &argv[i]); 211 fprintf(stderr, "%s: error: _execvp(\"%s\", ...) failed: %s\n", name(argv[0]), argv[i], strerror(errno)); 387 usAvg = usTotal / cTimes; 388 389 printf("%s: avg %um%u.%06us\n", name(argv[0]), (unsigned)(usAvg / 60000000), (unsigned)(usAvg % 60000000) / 1000000, (unsigned)(usAvg % 1000000)); 390 printf("%s: min %um%u.%06us\n", name(argv[0]), (unsigned)(usMin / 60000000), (unsigned)(usMin % 60000000) / 1000000, (unsigned)(usMin % 1000000)); 391 printf("%s: max %um%u.%06us\n", name(argv[0]), (unsigned)(usMax / 60000000), (unsigned)(usMax % 60000000) / 1000000, (unsigned)(usMax % 1000000)); 212 392 } 213 else214 fprintf(stderr, "%s: error: fork() failed: %s\n", name(argv[0]), strerror(errno));215 393 216 394 return rc; 217 #endif218 395 } 219 396
Note:
See TracChangeset
for help on using the changeset viewer.