Skip to content

Commit

Permalink
Fix long/short cpu stat init
Browse files Browse the repository at this point in the history
  • Loading branch information
brndnmtthws committed Sep 30, 2023
1 parent 5397340 commit aac26b5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
12 changes: 4 additions & 8 deletions src/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -747,15 +747,14 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
END OBJ(diskiograph_write, &update_diskio) parse_diskiograph_arg(obj, arg);
obj->callbacks.graphval = &diskiographval_write;
#endif /* BUILD_GUI */
END OBJ(color, nullptr)
if (false
END OBJ(color, nullptr) if (false
#ifdef BUILD_GUI
|| out_to_gui(*state)
|| out_to_gui(*state)
#endif /* BUILD_GUI */
#ifdef BUILD_NCURSES
|| out_to_ncurses.get(*state)
|| out_to_ncurses.get(*state)
#endif /* BUILD_NCURSES */
) {
) {
Colour c = arg != nullptr ? parse_color(arg) : default_color.get(*state);
obj->data.l = c.to_argb32();
set_current_text_color(c);
Expand Down Expand Up @@ -1058,9 +1057,6 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
* nothing else than just that, using an ugly switch(). */
if (strncmp(s, "top", 3) == EQUAL) {
if (parse_top_args(s, arg, obj) != 0) {
#ifdef __linux__
determine_longstat_file();
#endif
obj->cb_handle = create_cb_handle(update_top);
} else {
free(obj);
Expand Down
9 changes: 7 additions & 2 deletions src/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,10 @@ void determine_longstat_file(void) {
FILE *stat_fp;
static int reported = 0;
char buf[MAX_PROCSTAT_LINELEN + 1];
static int stat_initialized = 0;

/* only execute once */
if (stat_initialized) return;

if (!(stat_fp = open_file("/proc/stat", &reported))) return;
while (!feof(stat_fp) &&
Expand All @@ -913,6 +917,7 @@ void determine_longstat_file(void) {
}
}
fclose(stat_fp);
stat_initialized = 1;
}

void get_cpu_count(void) {
Expand Down Expand Up @@ -1000,6 +1005,7 @@ int update_stat(void) {
}

if (!stat_template) {
determine_longstat_file();
stat_template =
KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT;
}
Expand Down Expand Up @@ -1216,8 +1222,7 @@ static void get_dev_path(const char *dir, const char *dev, char *out_buf) {
if (name_fd < 0) {
snprintf(path, 512, "%s%s/device/name", dir, namelist[i]->d_name);
name_fd = open(path, O_RDONLY);
if (name_fd < 0)
continue;
if (name_fd < 0) continue;
}
size = read(name_fd, name, strlen(dev));
if (size < strlen(dev)) {
Expand Down
2 changes: 0 additions & 2 deletions src/linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ int update_stat(void);

void print_distribution(struct text_object *, char *, unsigned int);

void determine_longstat_file(void);

extern char e_iface[64];
extern char interfaces_arr[MAX_NET_INTERFACES][64];

Expand Down

0 comments on commit aac26b5

Please sign in to comment.