Skip to content

Commit

Permalink
refactor: replace wd_sleep function with nanosleep for improved accur…
Browse files Browse the repository at this point in the history
…acy in CPU usage calculation

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Nov 20, 2024
1 parent 042af9c commit 8edefb4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
23 changes: 8 additions & 15 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,20 +344,24 @@ read_cpu_fields (FILE *fp, unsigned long *fields)
return total_tick?1:0; // in case of total_tick is zero, as some platform reporting, I dont know why.
}

/**
* @brief Function prototype for a method returning a floating-point value
* @return float value
*/
float
get_cpu_usage()
{
FILE *fp;
unsigned long fields[10], total_tick, total_tick_old, idle, idle_old, del_total_tick, del_idle;
int i;
float percent_usage;
struct timespec sleep_time = {1, 0}; // 1 second sleep

fp = fopen ("/proc/stat", "r");
if (!fp) {
return 0.f;
return 0.f;
}


if (!read_cpu_fields (fp, fields)) {
fclose (fp);
return 0.f;
Expand All @@ -368,7 +372,7 @@ get_cpu_usage()
}
idle = fields[3]; /* idle ticks index */

wd_sleep(1, 0);
nanosleep(&sleep_time, NULL);
total_tick_old = total_tick;
idle_old = idle;

Expand All @@ -388,19 +392,8 @@ get_cpu_usage()
del_total_tick = total_tick - total_tick_old;
del_idle = idle - idle_old;

percent_usage = ((del_total_tick - del_idle) / (float) del_total_tick) * 100; /* 3 is index of idle time */
percent_usage = ((del_total_tick - del_idle) / (float) del_total_tick) * 100;
debug (LOG_DEBUG, "Total CPU Usage: %3.2lf%%\n", percent_usage);

return percent_usage;
}

// wd_sleep using select timeout method to instead of sleep-func
// s: second, u: usec 10^6usec = 1s
void
wd_sleep(unsigned int s, unsigned int u){
struct timeval timeout;
timeout.tv_sec = s;
timeout.tv_usec = u;

select(0, NULL, NULL, NULL, &timeout);
}
2 changes: 0 additions & 2 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ int wd_connect(int, const struct sockaddr *, socklen_t, int);

float get_cpu_usage();

void wd_sleep(unsigned, unsigned );

#endif /* _UTIL_H_ */

0 comments on commit 8edefb4

Please sign in to comment.