Skip to content

Commit

Permalink
cpu_usage: On BSD use long to hold cpu usage to avoid overflow. (#439)
Browse files Browse the repository at this point in the history
Co-authored-by: Sepherosa Ziehau <[email protected]>
  • Loading branch information
sepherosa and Sepherosa Ziehau authored Dec 19, 2021
1 parent 61a2c42 commit f757ee4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/print_cpu_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
#include "i3status.h"

struct cpu_usage {
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
long user;
long nice;
long system;
long idle;
long total;
#else
int user;
int nice;
int system;
Expand All @@ -47,6 +54,7 @@ struct cpu_usage {
int spin;
#endif
int total;
#endif
};

#if defined(__linux__)
Expand All @@ -66,7 +74,12 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) {
const char *walk;
char *outwalk = ctx->buf;
struct cpu_usage curr_all = {0, 0, 0, 0, 0};
int diff_idle, diff_total, diff_usage;
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
long diff_idle, diff_total;
#else
int diff_idle, diff_total;
#endif
int diff_usage;
bool colorful_output = false;

#if defined(__linux__)
Expand Down Expand Up @@ -153,6 +166,9 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) {
curr_all.user = cp_time[CP_USER];
curr_all.nice = cp_time[CP_NICE];
curr_all.system = cp_time[CP_SYS];
#if defined(__DragonFly__)
curr_all.system += cp_time[CP_INTR];
#endif
curr_all.idle = cp_time[CP_IDLE];
#if defined(__OpenBSD__)
curr_all.spin = cp_time[CP_SPIN];
Expand Down

0 comments on commit f757ee4

Please sign in to comment.