Fossil SCM
Add <nowiki>[utime] and [stime]</nowiki> commands to TH1.
Commit
3d50bdcb9a9e5bcfcb362548ebe88390e4d2c952
Parent
80b602069298d5e…
1 file changed
+83
+83
| --- src/th_main.c | ||
| +++ src/th_main.c | ||
| @@ -420,10 +420,91 @@ | ||
| 420 | 420 | if( openRepository ) db_find_and_open_repository(OPEN_OK_NOT_FOUND, 0); |
| 421 | 421 | } |
| 422 | 422 | Th_SetResult(interp, g.zRepositoryName, -1); |
| 423 | 423 | return TH_OK; |
| 424 | 424 | } |
| 425 | + | |
| 426 | +#ifdef _WIN32 | |
| 427 | +# include <windows.h> | |
| 428 | +#else | |
| 429 | +# include <sys/time.h> | |
| 430 | +# include <sys/resource.h> | |
| 431 | +#endif | |
| 432 | + | |
| 433 | +/* | |
| 434 | +** Get user and kernel times in microseconds. | |
| 435 | +*/ | |
| 436 | +static void getCpuTimes(sqlite3_uint64 *piUser, sqlite3_uint64 *piKernel){ | |
| 437 | +#ifdef _WIN32 | |
| 438 | + FILETIME not_used; | |
| 439 | + FILETIME kernel_time; | |
| 440 | + FILETIME user_time; | |
| 441 | + GetProcessTimes(GetCurrentProcess(), ¬_used, ¬_used, | |
| 442 | + &kernel_time, &user_time); | |
| 443 | + if( piUser ){ | |
| 444 | + *piUser = ((((sqlite3_uint64)user_time.dwHighDateTime)<<32) + | |
| 445 | + (sqlite3_uint64)user_time.dwLowDateTime + 5)/10; | |
| 446 | + } | |
| 447 | + if( piKernel ){ | |
| 448 | + *piKernel = ((((sqlite3_uint64)kernel_time.dwHighDateTime)<<32) + | |
| 449 | + (sqlite3_uint64)kernel_time.dwLowDateTime + 5)/10; | |
| 450 | + } | |
| 451 | +#else | |
| 452 | + struct rusage s; | |
| 453 | + getrusage(RUSAGE_SELF, &s); | |
| 454 | + if( piUser ){ | |
| 455 | + *piUser = ((sqlite3_uint64)s.ru_utime.tv_sec)*1000000 + s.ru_utime.tv_usec; | |
| 456 | + } | |
| 457 | + if( piKernel ){ | |
| 458 | + *piKernel = | |
| 459 | + ((sqlite3_uint64)s.ru_stime.tv_sec)*1000000 + s.ru_stime.tv_usec; | |
| 460 | + } | |
| 461 | +#endif | |
| 462 | +} | |
| 463 | + | |
| 464 | +/* | |
| 465 | +** TH1 command: utime | |
| 466 | +** | |
| 467 | +** Return the number of microseconds of CPU time consumed by the current | |
| 468 | +** process in user space. | |
| 469 | +*/ | |
| 470 | +static int utimeCmd( | |
| 471 | + Th_Interp *interp, | |
| 472 | + void *p, | |
| 473 | + int argc, | |
| 474 | + const char **argv, | |
| 475 | + int *argl | |
| 476 | +){ | |
| 477 | + sqlite3_uint64 x; | |
| 478 | + char zUTime[50]; | |
| 479 | + getCpuTimes(&x, 0); | |
| 480 | + sqlite3_snprintf(sizeof(zUTime), zUTime, "%llu", x); | |
| 481 | + Th_SetResult(interp, zUTime, -1); | |
| 482 | + return TH_OK; | |
| 483 | +} | |
| 484 | + | |
| 485 | +/* | |
| 486 | +** TH1 command: stime | |
| 487 | +** | |
| 488 | +** Return the number of microseconds of CPU time consumed by the current | |
| 489 | +** process in systsem space. | |
| 490 | +*/ | |
| 491 | +static int stimeCmd( | |
| 492 | + Th_Interp *interp, | |
| 493 | + void *p, | |
| 494 | + int argc, | |
| 495 | + const char **argv, | |
| 496 | + int *argl | |
| 497 | +){ | |
| 498 | + sqlite3_uint64 x; | |
| 499 | + char zUTime[50]; | |
| 500 | + getCpuTimes(0, &x); | |
| 501 | + sqlite3_snprintf(sizeof(zUTime), zUTime, "%llu", x); | |
| 502 | + Th_SetResult(interp, zUTime, -1); | |
| 503 | + return TH_OK; | |
| 504 | +} | |
| 505 | + | |
| 425 | 506 | |
| 426 | 507 | /* |
| 427 | 508 | ** Make sure the interpreter has been initialized. Initialize it if |
| 428 | 509 | ** it has not been already. |
| 429 | 510 | ** |
| @@ -445,10 +526,12 @@ | ||
| 445 | 526 | {"date", dateCmd, 0}, |
| 446 | 527 | {"html", putsCmd, 0}, |
| 447 | 528 | {"puts", putsCmd, (void*)1}, |
| 448 | 529 | {"wiki", wikiCmd, 0}, |
| 449 | 530 | {"repository", repositoryCmd, 0}, |
| 531 | + {"utime", utimeCmd, 0}, | |
| 532 | + {"stime", stimeCmd, 0}, | |
| 450 | 533 | {0, 0, 0} |
| 451 | 534 | }; |
| 452 | 535 | if( g.interp==0 ){ |
| 453 | 536 | int i; |
| 454 | 537 | g.interp = Th_CreateInterp(&vtab); |
| 455 | 538 |
| --- src/th_main.c | |
| +++ src/th_main.c | |
| @@ -420,10 +420,91 @@ | |
| 420 | if( openRepository ) db_find_and_open_repository(OPEN_OK_NOT_FOUND, 0); |
| 421 | } |
| 422 | Th_SetResult(interp, g.zRepositoryName, -1); |
| 423 | return TH_OK; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | ** Make sure the interpreter has been initialized. Initialize it if |
| 428 | ** it has not been already. |
| 429 | ** |
| @@ -445,10 +526,12 @@ | |
| 445 | {"date", dateCmd, 0}, |
| 446 | {"html", putsCmd, 0}, |
| 447 | {"puts", putsCmd, (void*)1}, |
| 448 | {"wiki", wikiCmd, 0}, |
| 449 | {"repository", repositoryCmd, 0}, |
| 450 | {0, 0, 0} |
| 451 | }; |
| 452 | if( g.interp==0 ){ |
| 453 | int i; |
| 454 | g.interp = Th_CreateInterp(&vtab); |
| 455 |
| --- src/th_main.c | |
| +++ src/th_main.c | |
| @@ -420,10 +420,91 @@ | |
| 420 | if( openRepository ) db_find_and_open_repository(OPEN_OK_NOT_FOUND, 0); |
| 421 | } |
| 422 | Th_SetResult(interp, g.zRepositoryName, -1); |
| 423 | return TH_OK; |
| 424 | } |
| 425 | |
| 426 | #ifdef _WIN32 |
| 427 | # include <windows.h> |
| 428 | #else |
| 429 | # include <sys/time.h> |
| 430 | # include <sys/resource.h> |
| 431 | #endif |
| 432 | |
| 433 | /* |
| 434 | ** Get user and kernel times in microseconds. |
| 435 | */ |
| 436 | static void getCpuTimes(sqlite3_uint64 *piUser, sqlite3_uint64 *piKernel){ |
| 437 | #ifdef _WIN32 |
| 438 | FILETIME not_used; |
| 439 | FILETIME kernel_time; |
| 440 | FILETIME user_time; |
| 441 | GetProcessTimes(GetCurrentProcess(), ¬_used, ¬_used, |
| 442 | &kernel_time, &user_time); |
| 443 | if( piUser ){ |
| 444 | *piUser = ((((sqlite3_uint64)user_time.dwHighDateTime)<<32) + |
| 445 | (sqlite3_uint64)user_time.dwLowDateTime + 5)/10; |
| 446 | } |
| 447 | if( piKernel ){ |
| 448 | *piKernel = ((((sqlite3_uint64)kernel_time.dwHighDateTime)<<32) + |
| 449 | (sqlite3_uint64)kernel_time.dwLowDateTime + 5)/10; |
| 450 | } |
| 451 | #else |
| 452 | struct rusage s; |
| 453 | getrusage(RUSAGE_SELF, &s); |
| 454 | if( piUser ){ |
| 455 | *piUser = ((sqlite3_uint64)s.ru_utime.tv_sec)*1000000 + s.ru_utime.tv_usec; |
| 456 | } |
| 457 | if( piKernel ){ |
| 458 | *piKernel = |
| 459 | ((sqlite3_uint64)s.ru_stime.tv_sec)*1000000 + s.ru_stime.tv_usec; |
| 460 | } |
| 461 | #endif |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | ** TH1 command: utime |
| 466 | ** |
| 467 | ** Return the number of microseconds of CPU time consumed by the current |
| 468 | ** process in user space. |
| 469 | */ |
| 470 | static int utimeCmd( |
| 471 | Th_Interp *interp, |
| 472 | void *p, |
| 473 | int argc, |
| 474 | const char **argv, |
| 475 | int *argl |
| 476 | ){ |
| 477 | sqlite3_uint64 x; |
| 478 | char zUTime[50]; |
| 479 | getCpuTimes(&x, 0); |
| 480 | sqlite3_snprintf(sizeof(zUTime), zUTime, "%llu", x); |
| 481 | Th_SetResult(interp, zUTime, -1); |
| 482 | return TH_OK; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | ** TH1 command: stime |
| 487 | ** |
| 488 | ** Return the number of microseconds of CPU time consumed by the current |
| 489 | ** process in systsem space. |
| 490 | */ |
| 491 | static int stimeCmd( |
| 492 | Th_Interp *interp, |
| 493 | void *p, |
| 494 | int argc, |
| 495 | const char **argv, |
| 496 | int *argl |
| 497 | ){ |
| 498 | sqlite3_uint64 x; |
| 499 | char zUTime[50]; |
| 500 | getCpuTimes(0, &x); |
| 501 | sqlite3_snprintf(sizeof(zUTime), zUTime, "%llu", x); |
| 502 | Th_SetResult(interp, zUTime, -1); |
| 503 | return TH_OK; |
| 504 | } |
| 505 | |
| 506 | |
| 507 | /* |
| 508 | ** Make sure the interpreter has been initialized. Initialize it if |
| 509 | ** it has not been already. |
| 510 | ** |
| @@ -445,10 +526,12 @@ | |
| 526 | {"date", dateCmd, 0}, |
| 527 | {"html", putsCmd, 0}, |
| 528 | {"puts", putsCmd, (void*)1}, |
| 529 | {"wiki", wikiCmd, 0}, |
| 530 | {"repository", repositoryCmd, 0}, |
| 531 | {"utime", utimeCmd, 0}, |
| 532 | {"stime", stimeCmd, 0}, |
| 533 | {0, 0, 0} |
| 534 | }; |
| 535 | if( g.interp==0 ){ |
| 536 | int i; |
| 537 | g.interp = Th_CreateInterp(&vtab); |
| 538 |