File tree Expand file tree Collapse file tree 1 file changed +13
-8
lines changed
Expand file tree Collapse file tree 1 file changed +13
-8
lines changed Original file line number Diff line number Diff line change @@ -384,6 +384,10 @@ void daemonize(
384384 slog_setoutput (SLOG_OUTPUT_SYSLOG , "kcptun-libev" );
385385}
386386
387+ #define READ_TIMESPEC (ts ) \
388+ ((int_fast64_t)(ts).tv_sec * INT64_C(1000000000) + \
389+ (int_fast64_t)(ts).tv_nsec)
390+
387391double thread_load (void )
388392{
389393 static _Thread_local struct {
@@ -399,17 +403,18 @@ double thread_load(void)
399403 return load ;
400404 }
401405 if (last .set ) {
402- const double total =
403- (double )(monotime .tv_sec - last .monotime .tv_sec ) +
404- (double )(monotime .tv_nsec - last .monotime .tv_nsec ) *
405- 1e-9 ;
406- const double busy =
407- (double )(cputime .tv_sec - last .cputime .tv_sec ) +
408- (double )(cputime .tv_nsec - last .cputime .tv_nsec ) * 1e-9 ;
409- load = busy / total ;
406+ const int_fast64_t total =
407+ READ_TIMESPEC (monotime ) - READ_TIMESPEC (last .monotime );
408+ const int_fast64_t busy =
409+ READ_TIMESPEC (cputime ) - READ_TIMESPEC (last .cputime );
410+ if (busy > 0 && total > 0 && busy <= total ) {
411+ load = (double )busy / (double )total ;
412+ }
410413 }
411414 last .monotime = monotime ;
412415 last .cputime = cputime ;
413416 last .set = true;
414417 return load ;
415418}
419+
420+ #undef READ_TIMESPEC
You can’t perform that action at this time.
0 commit comments