* Compute number of ticks in the specified amount of time. */
| 529 | * Compute number of ticks in the specified amount of time. |
| 530 | */ |
| 531 | int |
| 532 | tvtohz(struct timeval *tv) |
| 533 | { |
| 534 | unsigned long ticks; |
| 535 | long sec, usec; |
| 536 | |
| 537 | /* |
| 538 | * If the number of usecs in the whole seconds part of the time |
| 539 | * difference fits in a long, then the total number of usecs will |
| 540 | * fit in an unsigned long. Compute the total and convert it to |
| 541 | * ticks, rounding up and adding 1 to allow for the current tick |
| 542 | * to expire. Rounding also depends on unsigned long arithmetic |
| 543 | * to avoid overflow. |
| 544 | * |
| 545 | * Otherwise, if the number of ticks in the whole seconds part of |
| 546 | * the time difference fits in a long, then convert the parts to |
| 547 | * ticks separately and add, using similar rounding methods and |
| 548 | * overflow avoidance. This method would work in the previous |
| 549 | * case but it is slightly slower and assumes that hz is integral. |
| 550 | * |
| 551 | * Otherwise, round the time difference down to the maximum |
| 552 | * representable value. |
| 553 | * |
| 554 | * If ints have 32 bits, then the maximum value for any timeout in |
| 555 | * 10ms ticks is 248 days. |
| 556 | */ |
| 557 | sec = tv->tv_sec; |
| 558 | usec = tv->tv_usec; |
| 559 | if (usec < 0) { |
| 560 | sec--; |
| 561 | usec += 1000000; |
| 562 | } |
| 563 | if (sec < 0) { |
| 564 | #ifdef DIAGNOSTIC |
| 565 | if (usec > 0) { |
| 566 | sec++; |
| 567 | usec -= 1000000; |
| 568 | } |
| 569 | printf("tvotohz: negative time difference %ld sec %ld usec\n", |
| 570 | sec, usec); |
| 571 | #endif |
| 572 | ticks = 1; |
| 573 | } else if (sec <= LONG_MAX / 1000000) |
| 574 | ticks = howmany(sec * 1000000 + (unsigned long)usec, tick) + 1; |
| 575 | else if (sec <= LONG_MAX / hz) |
| 576 | ticks = sec * hz |
| 577 | + howmany((unsigned long)usec, tick) + 1; |
| 578 | else |
| 579 | ticks = LONG_MAX; |
| 580 | if (ticks > INT_MAX) |
| 581 | ticks = INT_MAX; |
| 582 | return ((int)ticks); |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Start profiling on a process. |
no test coverage detected