static void query_cpu_load(struct hud_graph *gr) { struct cpu_info *info = gr->query_data; uint64_t now = os_time_get(); if (info->last_time) { if (info->last_time + gr->pane->period <= now) { uint64_t cpu_busy, cpu_total, cpu_load; get_cpu_stats(info->cpu_index, &cpu_busy, &cpu_total); cpu_load = (cpu_busy - info->last_cpu_busy) * 100 / (double)(cpu_total - info->last_cpu_total); hud_graph_add_value(gr, cpu_load); info->last_cpu_busy = cpu_busy; info->last_cpu_total = cpu_total; info->last_time = now; } } else { /* initialize */ info->last_time = now; get_cpu_stats(info->cpu_index, &info->last_cpu_busy, &info->last_cpu_total); } }
int hud_get_num_cpus(void) { uint64_t busy, total; int i = 0; while (get_cpu_stats(i, &busy, &total)) i++; return i; }
void hud_cpu_graph_install(struct hud_pane *pane, unsigned cpu_index) { struct hud_graph *gr; struct cpu_info *info; uint64_t busy, total; /* see if the cpu exists */ if (cpu_index != ALL_CPUS && !get_cpu_stats(cpu_index, &busy, &total)) { return; } gr = CALLOC_STRUCT(hud_graph); if (!gr) return; if (cpu_index == ALL_CPUS) strcpy(gr->name, "cpu"); else sprintf(gr->name, "cpu%u", cpu_index); gr->query_data = CALLOC_STRUCT(cpu_info); if (!gr->query_data) { FREE(gr); return; } gr->query_new_value = query_cpu_load; /* Don't use free() as our callback as that messes up Gallium's * memory debugger. Use simple free_query_data() wrapper. */ gr->free_query_data = free_query_data; info = gr->query_data; info->cpu_index = cpu_index; hud_graph_set_dump_file(gr); hud_pane_add_graph(pane, gr); hud_pane_set_max_value(pane, 100); }
int main(void) { char status[STATUS_SIZ]; struct sysinfo sys; int i; for(i = 0; i < CPUSTATES; i++) sys.cpu_cycles[i] = .0; if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "Cannot open display.\n"); return 1; } for (;;sleep(UPDATE_INTERVAL)) { snprintf(status, sizeof status, "%s :: %s :: %s" , get_cpu_stats(&sys), get_load_average(), getdatetime()); setstatus(status); } XCloseDisplay(dpy); return 0; }