int main(int argc, char **argv) { int i; struct glibtop_netlist netlist; char **ifnames = glibtop_get_netlist(&netlist); for (i = 0; i < netlist.number; i++) { struct glibtop_netload netload; glibtop_get_netload(&netload, ifnames[i]); printf("%s\n", ifnames[i]); printf(" %lu, %lu\n", netload.bytes_in, netload.bytes_out); } struct glibtop_cpu cpu; glibtop_get_cpu(&cpu); printf("%lu, %lu, %lu\n", cpu.user, cpu.sys, cpu.nice); struct glibtop_mem mem; glibtop_get_mem(&mem); printf("%lu, %lu\n", mem.total, mem.used); struct glibtop_swap swap; glibtop_get_swap(&swap); printf("%lu, %lu\n", swap.total, swap.used); struct glibtop_conntrack conntrack; glibtop_get_conntrack(&conntrack); printf("%lu, %lu\n", conntrack.tcp_conns, conntrack.udp_conns); struct glibtop_wireless wireless; glibtop_get_wireless(&wireless, "wlan0"); printf("%ld, %ld\n", wireless.signal_dbm, wireless.noise_dbm); return 0; }
static void pprint_get_cpu(void) { glibtop_cpu buf; glibtop_get_cpu(&buf); HEADER_PPRINT(glibtop_get_cpu); PPRINT(flags, "%#llx"); PPRINT(total, "%llu"); PPRINT(user, "%llu"); PPRINT(nice, "%llu"); PPRINT(sys, "%llu"); PPRINT(idle, "%llu"); PPRINT(iowait, "%llu"); PPRINT(irq, "%llu"); PPRINT(softirq, "%llu"); PPRINT(frequency, "%llu"); PPRINT_ARRAY(xcpu_total, GLIBTOP_NCPU, "%llu"); PPRINT_ARRAY(xcpu_user, GLIBTOP_NCPU, "%llu"); PPRINT_ARRAY(xcpu_nice, GLIBTOP_NCPU, "%llu"); PPRINT_ARRAY(xcpu_sys, GLIBTOP_NCPU, "%llu"); PPRINT_ARRAY(xcpu_idle, GLIBTOP_NCPU, "%llu"); PPRINT_ARRAY(xcpu_iowait, GLIBTOP_NCPU, "%llu"); PPRINT_ARRAY(xcpu_irq, GLIBTOP_NCPU, "%llu"); PPRINT_ARRAY(xcpu_softirq, GLIBTOP_NCPU, "%llu"); PPRINT(xcpu_flags, "%llx"); FOOTER_PPRINT(); }
static void get_load (LoadGraph *g) { guint i; glibtop_cpu cpu; glibtop_get_cpu (&cpu); #undef NOW #undef LAST #define NOW (g->cpu.times[g->cpu.now]) #define LAST (g->cpu.times[g->cpu.now ^ 1]) if (g->n == 1) { NOW[0][CPU_TOTAL] = cpu.total; NOW[0][CPU_USED] = cpu.user + cpu.nice + cpu.sys; } else { for (i = 0; i < g->n; i++) { NOW[i][CPU_TOTAL] = cpu.xcpu_total[i]; NOW[i][CPU_USED] = cpu.xcpu_user[i] + cpu.xcpu_nice[i] + cpu.xcpu_sys[i]; } } // on the first call, LAST is 0 // which means data is set to the average load since boot // that value has no meaning, we just want all the // graphs to be aligned, so the CPU graph needs to start // immediately for (i = 0; i < g->n; i++) { float load; float total, used; gchar *text; total = NOW[i][CPU_TOTAL] - LAST[i][CPU_TOTAL]; used = NOW[i][CPU_USED] - LAST[i][CPU_USED]; load = used / MAX(total, 1.0f); g->data[0][i] = load; /* Update label */ text = g_strdup_printf("%.1f%%", load * 100.0f); gtk_label_set_text(GTK_LABEL(g->labels.cpu[i]), text); g_free(text); } g->cpu.now ^= 1; #undef NOW #undef LAST }
void SmoothRefresh::reset() { glibtop_cpu cpu; glibtop_proc_time proctime; glibtop_get_cpu(&cpu); glibtop_get_proc_time(&proctime, getpid()); this->interval = ProcData::get_instance()->config.update_interval; this->last_pcpu = PCPU_LO; this->last_total_time = cpu.total; this->last_cpu_time = proctime.rtime; }
/** * Returns the CPU load of each core as a percentage. */ std::vector<double> get_cpu_usage() { glibtop_cpu cpu; glibtop_get_cpu(&cpu); std::vector<double> percentages; for(guint i = 0; i < GLIBTOP_NCPU; i++) { if(cpu.xcpu_user[i] > 0) { auto used = cpu.xcpu_user[i] + cpu.xcpu_nice[i] + cpu.xcpu_sys[i]; percentages.push_back(((double)used / (double)cpu.xcpu_total[i]) * 100.0); } } return percentages; }
void GetLoad (int Maximum, int data [5], LoadGraph *g) { int usr, nice, sys, iowait, free; int total; glibtop_cpu cpu; glibtop_get_cpu (&cpu); g_return_if_fail ((cpu.flags & needed_cpu_flags) == needed_cpu_flags); g->cpu_time [0] = cpu.user; g->cpu_time [1] = cpu.nice; g->cpu_time [2] = cpu.sys; g->cpu_time [3] = cpu.iowait + cpu.irq + cpu.softirq; g->cpu_time [4] = cpu.idle; if (!g->cpu_initialized) { memcpy (g->cpu_last, g->cpu_time, sizeof (g->cpu_last)); g->cpu_initialized = 1; } usr = g->cpu_time [0] - g->cpu_last [0]; nice = g->cpu_time [1] - g->cpu_last [1]; sys = g->cpu_time [2] - g->cpu_last [2]; iowait = g->cpu_time [3] - g->cpu_last [3]; free = g->cpu_time [4] - g->cpu_last [4]; total = usr + nice + sys + free + iowait; memcpy(g->cpu_last, g->cpu_time, sizeof g->cpu_last); usr = rint (Maximum * (float)(usr) / total); nice = rint (Maximum * (float)(nice) / total); sys = rint (Maximum * (float)(sys) / total); iowait = rint (Maximum * (float)(iowait) / total); free = Maximum - usr - nice - sys - iowait; data [0] = usr; data [1] = sys; data [2] = nice; data [3] = iowait; data [4] = free; }
static PyObject* gtop_cpu(PyObject *self, PyObject *args) { glibtop_cpu buf; PyObject *d; PyObject *smp; unsigned i; if(!PyArg_ParseTuple(args, "")) return NULL; glibtop_get_cpu(&buf); d = PyDict_New(); my_dict_add_and_decref(d, "total", PyL_ULL(buf.total)); my_dict_add_and_decref(d, "user", PyL_ULL(buf.user)); my_dict_add_and_decref(d, "nice", PyL_ULL(buf.nice)); my_dict_add_and_decref(d, "sys", PyL_ULL(buf.sys)); my_dict_add_and_decref(d, "idle", PyL_ULL(buf.idle)); my_dict_add_and_decref(d, "frequency", PyL_ULL(buf.frequency)); my_dict_add_and_decref(d, "iowait", PyL_ULL(buf.iowait)); my_dict_add_and_decref(d, "irq", PyL_ULL(buf.irq)); my_dict_add_and_decref(d, "sofr_irq", PyL_ULL(buf.softirq)); smp = PyTuple_New(1 + glibtop_global_server->ncpu); for(i = 0; i <= glibtop_global_server->ncpu; ++i) { PyObject *a; a = get_smp_cpu(&buf, i); PyTuple_SET_ITEM(smp, i, a); } my_dict_add_and_decref(d, "cpus", smp); Py_INCREF(smp); my_dict_add_and_decref(d, "xcpu", smp); my_dict_add_and_decref(d, "xcpu_flags", PyL_ULL(buf.xcpu_flags)); return _struct_new(d); }
unsigned SmoothRefresh::get_own_cpu_usage() { glibtop_cpu cpu; glibtop_proc_time proctime; guint64 elapsed; unsigned usage = PCPU_LO; glibtop_get_cpu (&cpu); elapsed = cpu.total - this->last_total_time; if (elapsed) { // avoid division by 0 glibtop_get_proc_time(&proctime, getpid()); usage = (proctime.rtime - this->last_cpu_time) * 100 / elapsed; } usage = CLAMP(usage, 0, 100); this->last_total_time = cpu.total; this->last_cpu_time = proctime.rtime; return usage; }
int ztask_mon_proc_test (bool verbose) { printf (" * ztask_mon_proc: "); if (verbose) printf ("\n"); // @selftest // Simple create/destroy test ztask_mon_proc_t *self = ztask_mon_proc_new (0); assert (self); // FIXME memory leak (report bug to glibtop) glibtop_init(); glibtop_cpu cpu; glibtop_mem memory; for (int i = 0; i < 4; i++) { glibtop_get_cpu (&cpu); glibtop_get_mem (&memory); ztask_mon_proc_update (self, &cpu, &memory, 0); if (verbose) ztask_mon_proc_dump (self); zclock_sleep (500); /* setting current values for total and used cpu */ ztask_mon_proc_set_cpu_total_last (self, cpu.total); ztask_mon_proc_set_cpu_used_last (self, cpu.user + cpu.nice + cpu.sys); } glibtop_close(); ztask_mon_proc_destroy (&self); if (verbose) printf (" * ztask_mon_proc: "); printf ("OK\n"); return 0; }
int main (int argc, char *argv []) { glibtop_union data; unsigned c; struct rusage total_start, total_end; struct rusage rusage_start, rusage_end; struct timeval elapsed_utime, elapsed_stime; pid_t pid; pid = getpid (); setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, GTOPLOCALEDIR); textdomain (GETTEXT_PACKAGE); glibtop_init_r (&glibtop_global_server, 0, 0); display_self_times(); printf ("%-12s (%-10s): %7s - %9s - %9s\n", "Feature", "Flags", "Count", "utime", "stime"); printf ("-------------------------------------------" "---------------\n"); getrusage (RUSAGE_SELF, &total_start); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_cpu (&data.cpu); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("CPU (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.cpu.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) glibtop_get_mem (&data.mem); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Memory (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.mem.flags, PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) glibtop_get_swap (&data.swap); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Swap (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.swap.flags, PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_uptime (&data.uptime); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Uptime (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.uptime.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_loadavg (&data.loadavg); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Loadavg (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.loadavg.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) { pid_t* ptr = glibtop_get_proclist (&data.proclist, 0, 0); g_free (ptr); } getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proclist (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proclist.flags, PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) { char** ptr = glibtop_get_netlist (&data.netlist); g_strfreev (ptr); } getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Netlist (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proclist.flags, PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_proc_state (&data.proc_state, pid); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_State (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_state.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_proc_uid (&data.proc_uid, pid); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Uid (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_uid.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_proc_mem (&data.proc_mem, pid); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Mem (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_mem.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) { glibtop_map_entry* entries; entries = glibtop_get_proc_map (&data.proc_map, pid); g_free (entries); } getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Map (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_map.flags, PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE, (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_proc_segment (&data.proc_segment, pid); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Segment (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_segment.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) { char** argv; argv = glibtop_get_proc_argv (&data.proc_args, pid, 0); g_strfreev(argv); } getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Args (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_args.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_proc_time (&data.proc_time, pid); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Time (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_time.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_proc_signal (&data.proc_signal, pid); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Signal (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_signal.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &rusage_start); for (c = 0; c < PROFILE_COUNT; c++) glibtop_get_proc_kernel (&data.proc_kernel, pid); getrusage (RUSAGE_SELF, &rusage_end); libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime, &elapsed_utime); libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime, &elapsed_stime); printf ("Proc_Kernel (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n", (unsigned long) data.proc_kernel.flags, PROFILE_COUNT, (long double) ELAPSED_UTIME / PROFILE_COUNT, (long double) ELAPSED_STIME / PROFILE_COUNT); getrusage (RUSAGE_SELF, &total_end); libgtop_timersub (&total_end.ru_utime, &total_start.ru_utime, &elapsed_utime); libgtop_timersub (&total_end.ru_stime, &total_start.ru_stime, &elapsed_stime); printf ("-------------------------------------------" "---------------\n"); printf ("%-36s %9lu - %9lu\n\n", "TOTAL", ELAPSED_UTIME, ELAPSED_STIME); printf ("All timings are in clock ticks " "(1000000 ticks per second).\n\n"); display_self_times(); glibtop_close (); exit (0); }
static gboolean render(GtkWidget ** pwidget, gint interval, CPU_plug_data **p) { CPU_plug_data * data = *p; char buf[256]; glibtop_cpu cpu; static gdouble old_time = -1; float old_user, old_sys, old_idle, old_iowait; dashboard_cairo_widget c_widge; char * content[2][4]; int i; cairo_text_extents_t extents; double x, y; float cpu_mult; float tot = 0; GTimeVal timeval; if (old_time < 0) { old_time = time(NULL); return FALSE; } data->timer = data->timer - interval; if (data->timer <= 0) { gdouble newtime; g_get_current_time (&timeval); newtime = timeval.tv_sec + timeval.tv_usec / 1000000.0; cpu_mult = (1.0 / (newtime - old_time) ); old_time = newtime; glibtop_get_cpu(&cpu); //could as easily do this in render icon. seems more appropriate here. FIXME old_user = data->user; data->user = (cpu.user - data->accum_user) * cpu_mult; data->accum_user = cpu.user; old_sys = data->sys; data->sys = (cpu.sys - data->accum_sys) * cpu_mult; data->accum_sys = cpu.sys; old_idle = data->idle; data->idle = (cpu.idle - data->accum_idle) * cpu_mult; data->accum_idle = cpu.idle; old_iowait = data->iowait; data->iowait = (cpu.iowait - data->accum_iowait) * cpu_mult ; data->accum_iowait = cpu.iowait; data->user = (data->user > 100) ? (100 + old_user) / 2 : (data->user + old_user) / 2; data->sys = (data->sys > 100) ? (100 + old_sys) / 2 : (data->sys + old_sys) / 2; data->idle = (data->idle > 100) ? (100 + old_idle) / 2 : (data->idle + old_idle) / 2; data->iowait = (data->iowait > 100) ? (100 + old_iowait) / 2 : (data->iowait + old_iowait) / 2; tot = data->user + data->sys + data->idle + data->iowait; if (data->user + data->sys + data->idle + data->iowait > 100) { data->idle = data->idle * 100.0 / tot; data->sys = data->sys * 100.0 / tot; data->user = data->user* 100.0 / tot; data->iowait = data->iowait * 100.0 / tot; } content[0][0] = strdup("User"); content[0][1] = strdup("Sys"); content[0][2] = strdup("Wait"); content[0][3] = strdup("Idle"); snprintf(buf, sizeof(buf), "%3.1f%%", data->user); content[1][0] = strdup(buf); snprintf(buf, sizeof(buf), "%3.1f%%", data->sys); content[1][1] = strdup(buf); snprintf(buf, sizeof(buf), "%3.1f%%", data->iowait); content[1][2] = strdup(buf); snprintf(buf, sizeof(buf), "%3.1f%%", data->idle); content[1][3] = strdup(buf); goto_bad_heheh: if (data->max_width_left < 0) { *pwidget = get_cairo_widget(&c_widge, dashboard_get_font_size(DASHBOARD_FONT_SMALL) * 9 * data->size_mult, dashboard_get_font_size(DASHBOARD_FONT_SMALL) * 4 * data->size_mult); use_bg_rgba_colour(c_widge.cr); cairo_set_operator(c_widge.cr, CAIRO_OPERATOR_SOURCE); cairo_paint(c_widge.cr); } else { *pwidget = get_cairo_widget(&c_widge, data->width, data->height); cairo_rectangle(c_widge.cr, 0, 0, data->width, data->height); cairo_set_source_rgba(c_widge.cr, data->bg.red, data->bg.green, data->bg.blue, data->bg.alpha); cairo_fill(c_widge.cr); } cairo_select_font_face(c_widge.cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size(c_widge.cr, dashboard_get_font_size(DASHBOARD_FONT_SMALL)*data->size_mult); cairo_set_source_rgba(c_widge.cr, data->fg.red, data->fg.green, data->fg.blue, data->fg.alpha); cairo_move_to(c_widge.cr, 10.0*data->size_mult, 15.0*data->size_mult); if (data->max_width_left < 0) { for (i = 0;i < 4;i++) { cairo_text_extents(c_widge.cr, content[0][i], &extents); if (extents.width > data->max_width_left) { data->max_width_left = extents.width; } } data->move_down = extents.height * 1.4; for (i = 0;i < 4;i++) { cairo_text_extents(c_widge.cr, content[1][i], &extents); if (extents.width > data->max_width_right) { data->max_width_right = extents.width; } } data->width = (data->max_width_right + data->max_width_left) * 1.25 + 100 * data->size_mult; data->height = extents.height * 6.8; del_cairo_widget(&c_widge); y = dashboard_get_font_size(DASHBOARD_FONT_SMALL) * data->size_mult ; data->pats[0] = cairo_pattern_create_linear(data->width - 100 * data->size_mult, y - data->move_down * 0.8, data->width, data->move_down * 0.6); cairo_pattern_add_color_stop_rgba(data->pats[0], 0, data->colour_meter_start.red , data->colour_meter_start.green, data->colour_meter_start.blue, data->colour_meter_start.alpha); if (!data->two_colour_gradient) cairo_pattern_add_color_stop_rgba(data->pats[0], 0.5, data->colour_meter_middle.red , data->colour_meter_middle.green, data->colour_meter_middle.blue, data->colour_meter_middle.alpha); cairo_pattern_add_color_stop_rgba(data->pats[0], 1, data->colour_meter_end.red , data->colour_meter_end.green, data->colour_meter_end.blue, data->colour_meter_end.alpha); data->pats[1] = cairo_pattern_create_linear(data->width - 100 * data->size_mult, y - data->move_down * 0.8, data->width, data->move_down * 0.6); cairo_pattern_add_color_stop_rgba(data->pats[1], 0, data->colour_meter_start.red , data->colour_meter_start.green, data->colour_meter_start.blue, data->colour_meter_start.alpha); if (!data->two_colour_gradient) cairo_pattern_add_color_stop_rgba(data->pats[1], 0.5, data->colour_meter_middle.red , data->colour_meter_middle.green, data->colour_meter_middle.blue, data->colour_meter_middle.alpha); cairo_pattern_add_color_stop_rgba(data->pats[1], 1, data->colour_meter_end.red , data->colour_meter_end.green, data->colour_meter_end.blue, data->colour_meter_end.alpha); data->pats[2] = cairo_pattern_create_linear(data->width - 100 * data->size_mult, y - data->move_down * 0.8, data->width, data->move_down * 0.6); cairo_pattern_add_color_stop_rgba(data->pats[2], 0, data->colour_meter_start.red , data->colour_meter_start.green, data->colour_meter_start.blue, data->colour_meter_start.alpha); if (!data->two_colour_gradient) cairo_pattern_add_color_stop_rgba(data->pats[2], 0.5, data->colour_meter_middle.red , data->colour_meter_middle.green, data->colour_meter_middle.blue, data->colour_meter_middle.alpha); cairo_pattern_add_color_stop_rgba(data->pats[2], 1, data->colour_meter_end.red , data->colour_meter_end.green, data->colour_meter_end.blue, data->colour_meter_end.alpha); data->pats[3] = cairo_pattern_create_linear(data->width - 100 * data->size_mult, y - data->move_down * 0.8 , data->width, data->move_down * 0.6); cairo_pattern_add_color_stop_rgba(data->pats[3], 0, data->colour_meter_end.red , data->colour_meter_end.green, data->colour_meter_end.blue, data->colour_meter_end.alpha); if (!data->two_colour_gradient) cairo_pattern_add_color_stop_rgba(data->pats[3], 0.5, data->colour_meter_middle.red , data->colour_meter_middle.green, data->colour_meter_middle.blue, data->colour_meter_middle.alpha); cairo_pattern_add_color_stop_rgba(data->pats[3], 1, data->colour_meter_start.red , data->colour_meter_start.green, data->colour_meter_start.blue, data->colour_meter_start.alpha); goto goto_bad_heheh; } cairo_move_to(c_widge.cr, x = dashboard_get_font_size(DASHBOARD_FONT_SMALL) * data->size_mult, y = dashboard_get_font_size(DASHBOARD_FONT_SMALL) * data->size_mult); for (i = 0;i < 4;i++) { cairo_show_text(c_widge.cr, content[0][i]); x = x + data->max_width_left * 1.1; cairo_text_extents(c_widge.cr, content[1][i], &extents); if (data->emotive_text) { switch (i) { case 0: cairo_set_source_rgb(c_widge.cr, data->user / 100.0, 1.0 - data->user / 100.0, 0.4); break; case 1: cairo_set_source_rgb(c_widge.cr, (data->sys) / 100.0, 1.0 - (data->sys) / 100.0, 0.4); break; case 2: cairo_set_source_rgb(c_widge.cr, (data->iowait) / 100.0, 1.0 - (data->iowait) / 100.0, 0.4); break; case 3: cairo_set_source_rgb(c_widge.cr, 1.0 - data->idle / 100.0, data->idle / 100.0, 0.4); break; } } else { cairo_set_source_rgba(c_widge.cr, data->fg.red, data->fg.green, data->fg.blue, data->fg.alpha); } cairo_move_to(c_widge.cr, x + (data->max_width_right - extents.width) , y); cairo_show_text(c_widge.cr, content[1][i]); // cairo_pattern_t *pat; // pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, 256.0); switch (i) { case 0: if (data->user) { cairo_rectangle(c_widge.cr, data->width - 100*data->size_mult, y - data->move_down*0.6, data->user*data->size_mult, data->move_down*0.6); if (data->shiny_graphs) cairo_set_source(c_widge.cr, data->pats[i]); cairo_fill(c_widge.cr); } break; case 1: if (data->sys) { cairo_rectangle(c_widge.cr, data->width - 100*data->size_mult, y - data->move_down*0.6, data->sys*data->size_mult, data->move_down*0.6); if (data->shiny_graphs) cairo_set_source(c_widge.cr, data->pats[i]); cairo_fill(c_widge.cr); } break; case 2: if (data->iowait) { cairo_rectangle(c_widge.cr, data->width - 100*data->size_mult, y - data->move_down*0.6, data->iowait*data->size_mult, data->move_down*0.6); if (data->shiny_graphs) cairo_set_source(c_widge.cr, data->pats[i]); cairo_fill(c_widge.cr); } break; case 3: if (data->user) { cairo_rectangle(c_widge.cr, data->width - 100*data->size_mult, y - data->move_down*0.6, data->idle*data->size_mult, data->move_down*0.6); if (data->shiny_graphs) cairo_set_source(c_widge.cr, data->pats[i]); cairo_fill(c_widge.cr); } break; } cairo_set_source_rgba(c_widge.cr, data->fg.red, data->fg.green, data->fg.blue, data->fg.alpha); x = dashboard_get_font_size(DASHBOARD_FONT_SMALL) * data->size_mult; y = y + data->move_down; cairo_move_to(c_widge.cr, x, y); } del_cairo_widget(&c_widge); data->timer = data->refresh; for (i = 0;i < 4;i++) { free(content[0][i]); free(content[1][i]); } return TRUE; } else { return FALSE; } }
void update_process_info (void) { static guint64 old_total_jiffies = 0; if (!awn_proc_users) { g_debug ("%s: no users",__func__); return; } pid_t * p; gint y ; glibtop_proclist proclist; GList * old_awn_proc_info=awn_proc_info; guint64 total_jiffies; glibtop_cpu cpu; gdouble percent; gdouble total_per = 0; glibtop_get_cpu (&cpu); total_jiffies = cpu.total; awn_proc_info = NULL; /* p = glibtop_get_proclist(&proclist, GLIBTOP_KERN_PROC_RUID, getuid());*/ p = glibtop_get_proclist(&proclist, GLIBTOP_KERN_PROC_ALL, -1); // g_debug ("number of entries = %d",proclist.number); for (y = 0;y < proclist.number;y++) { AwnProcInfo * data = g_malloc (sizeof(AwnProcInfo)); GList * search; data->pid = p[y]; glibtop_get_proc_state(&data->proc_state, p[y]); glibtop_get_proc_time(&data->proc_time, p[y]); search = g_list_find_custom (old_awn_proc_info,GINT_TO_POINTER(p[y]), (GCompareFunc)_cmp_find_pid); if (search) { AwnProcInfo * search_data = search->data; long time_diff; double jiffies; jiffies = total_jiffies - old_total_jiffies; // g_debug ("%d jiffies = %lf",p[y],jiffies); time_diff = (data->proc_time.utime + data->proc_time.stime) - (search_data->proc_time.utime+search_data->proc_time.stime); // g_debug ("%d time diff = %ld",p[y],time_diff); percent = time_diff / (jiffies / cpu.frequency) ; // g_debug ("percent for %d = %lf",p[y],percent); } else { percent = 0; } data->percent_cpu = percent; total_per = total_per + percent; awn_proc_info = g_list_prepend (awn_proc_info,data); } g_list_foreach (old_awn_proc_info,(GFunc)g_free,NULL); g_list_free (old_awn_proc_info); g_free (p); old_total_jiffies = total_jiffies; }
static void append_sysinfo() { struct glibtop_cpu cpu; struct glibtop_mem mem; struct glibtop_swap swap; struct glibtop_conntrack conntrack; #ifndef NETLOAD_IFACE struct glibtop_netlist netlist; int i; char **ifnames; #endif const time_t timestamp = time(NULL); if (timestamp == data_history[last_index].timestamp) return; glibtop_get_cpu(&cpu); glibtop_get_mem(&mem); glibtop_get_swap(&swap); glibtop_get_conntrack(&conntrack); uint64_t in = 0; uint64_t out = 0; #ifndef NETLOAD_IFACE ifnames = glibtop_get_netlist(&netlist); for (i = 0; i < netlist.number; ++i) #endif { struct glibtop_netload netload; #ifndef NETLOAD_IFACE glibtop_get_netload (&netload, ifnames[i]); if (netload.is_loopback) continue; /* Skip interfaces without any IPv4/IPv6 address (or those with only a LINK ipv6 addr) However we need to be able to exclude these while still keeping the value so when they get online (with NetworkManager for example) we don't get a suddent peak. Once we're able to get this, ignoring down interfaces will be possible too. */ if (!netload.has_addr || (netload.has_addr && netload.is_linklocal)) continue; #else glibtop_get_netload (&netload, NETLOAD_IFACE); #endif /* Don't skip interfaces that are down (GLIBTOP_IF_FLAGS_UP) to avoid spikes when they are brought up */ in += netload.bytes_in; out += netload.bytes_out; #ifndef NETLOAD_IFACE free(ifnames[i]); #endif } #ifndef NETLOAD_IFACE free(ifnames); #endif #ifdef IW_IFACE struct glibtop_wireless wireless; glibtop_get_wireless(&wireless, IW_IFACE); #endif pthread_mutex_lock(&info_mutex); last_index = (last_index+1) % MAX_HISTORY; data_history[last_index].cpu_total = (float)cpu.total; data_history[last_index].cpu_used = (float)(cpu.user + cpu.nice + cpu.sys); data_history[last_index].memory_usage = ((float)mem.used) / ((float)mem.total); data_history[last_index].swap_usage = swap.total == 0 ? 0.0f : ((float)swap.used) / ((float)swap.total); data_history[last_index].timestamp = timestamp; data_history[last_index].network_in = (float) in; data_history[last_index].network_out = (float) out; data_history[last_index].tcp_conns = (float) conntrack.tcp_conns; data_history[last_index].udp_conns = (float) conntrack.udp_conns; #ifdef IW_IFACE data_history[last_index].iw_signal = (float) wireless.signal_dbm; data_history[last_index].iw_noise = (float) wireless.noise_dbm; #else data_history[last_index].iw_signal = 0.0f; data_history[last_index].iw_noise = 0.0f; #endif pthread_mutex_unlock(&info_mutex); }
static AwnGraphSinglePoint awn_CPUicon_get_load(AwnCPUicon *self) { guint i; glibtop_cpu cpu; AwnCPUiconPrivate *priv; float total, used; gdouble load; AwnGraphSinglePoint point; gdouble new_time; priv = AWN_CPUICON_GET_PRIVATE (self); new_time = get_double_time (); glibtop_get_cpu(&cpu); #undef NOW #undef LAST #define NOW (priv->times[priv->now]) #define LAST (priv->times[priv->now ^ 1]) if (priv->num_cpus == 1) { NOW[0][CPU_TOTAL] = cpu.total; NOW[0][CPU_USED] = cpu.user + cpu.nice + cpu.sys; } else { for (i = 0; i < priv->num_cpus; i++) { NOW[i][CPU_TOTAL] = cpu.xcpu_total[i]; NOW[i][CPU_USED] = cpu.xcpu_user[i] + cpu.xcpu_nice[i] + cpu.xcpu_sys[i]; } } load = total = used = 0.0; for (i = 0; i < priv->num_cpus; i++) { total = total + NOW[i][CPU_TOTAL] - LAST[i][CPU_TOTAL]; used = used + NOW[i][CPU_USED] - LAST[i][CPU_USED]; } load = used / MAX(total, (float)priv->num_cpus * 1.0f); point.value = load * 100.0; if (point.value>100.0) { point.value = 100.0; } point.points = (new_time - priv->prev_time) * 1000.0 / get_conf_value_int (G_OBJECT(self), "update-timeout"); priv->prev_time = new_time; // toggle the buffer index. priv->now ^= 1; #undef NOW #undef LAST return point; }
static void awn_CPUicon_constructed (GObject *object) { /*FIXME*/ AwnCPUiconPrivate * priv; glibtop_cpu cpu; int i = 0; AwnApplet * applet; g_assert (G_OBJECT_CLASS ( awn_CPUicon_parent_class) ); if (G_OBJECT_CLASS ( awn_CPUicon_parent_class)->constructed) { G_OBJECT_CLASS ( awn_CPUicon_parent_class)->constructed(object); } g_object_get (object, "applet",&applet, NULL); g_assert (applet); g_assert (AWN_IS_APPLET (applet)); priv = AWN_CPUICON_GET_PRIVATE (object); /* this will choose add_seconds in a relatively conservative manner. Note that the timer is assumed to be incorrect and time elapsed is actually measured accurately when the timer fires. Area graph can be informed that the measurement contains a partial point and it will average things out. */ priv->dialog = awn_cpu_dialog_new_with_applet(GTK_WIDGET(object),applet); gtk_window_set_title (GTK_WINDOW (priv->dialog),_("CPU")); g_signal_connect(object, "button-press-event", G_CALLBACK(_awn_cpu_icon_clicked), priv->dialog); priv->num_cpus = 0; priv->prev_time = get_double_time(); glibtop_get_cpu(&cpu); while (i < GLIBTOP_NCPU && cpu.xcpu_total[i] != 0) { priv->num_cpus++; i++; } priv->now = 0; connect_notify (object, "graph-type", G_CALLBACK (_graph_type_change),applet); connect_notify (object, "update-timeout", G_CALLBACK (_update_timeout_change),object); set_timeout (AWN_CPUICON(object)); priv->text_overlay = AWN_OVERLAY(awn_overlay_text_new()); g_object_set (priv->text_overlay, "align", AWN_OVERLAY_ALIGN_RIGHT, "gravity", GDK_GRAVITY_SOUTH, "x-adj", 0.3, "y-adj", 0.0, "text", "0.0", NULL); awn_overlayable_add_overlay (AWN_OVERLAYABLE(object), priv->text_overlay); do_bridge ( applet,object, "icon","update_timeout","update-timeout"); }
static gboolean _awn_CPUicon_update_icon(gpointer object) { AwnCPUiconPrivate * priv; AwnSysmoniconPrivate * sysmonicon_priv=NULL; AwnCPUicon * icon = object; AwnGraphSinglePoint *point; GList * list = NULL; gchar *text; priv = AWN_CPUICON_GET_PRIVATE (object); sysmonicon_priv = AWN_SYSMONICON_GET_PRIVATE (object); g_object_set (object, "invalidate",TRUE, NULL); /*FIXME change this to some type of graph_type thing */ if ( (AWN_IS_AREAGRAPH(sysmonicon_priv->graph)) || (AWN_IS_CIRCLEGRAPH(sysmonicon_priv->graph) )) { point = g_new0 (AwnGraphSinglePoint,1); *point = awn_CPUicon_get_load (object); /* Translators: %2.0lf is a number, %% is a percent sign, do not change them */ text = g_strdup_printf (_("CPU: %2.0lf%%"),point->value); // awn_tooltip_set_text (AWN_TOOLTIP(sysmonicon_priv->tooltip),text); awn_icon_set_tooltip_text (AWN_ICON(object),text); g_free (text); text = g_strdup_printf("%.0lf%%",point->value); g_object_set (priv->text_overlay, "text", text, NULL); g_free (text); list = g_list_prepend (list,point); awn_graph_add_data (sysmonicon_priv->graph,list); awn_sysmonicon_update_icon (AWN_SYSMONICON (icon)); g_free (point); g_list_free (list); } else if ( AWN_IS_BARGRAPH(sysmonicon_priv->graph)) { #undef NOW #undef LAST #define LAST (priv->times[priv->now]) #define NOW (priv->times[priv->now ^ 1]) AwnGraphSinglePoint avg_point = awn_CPUicon_get_load (object); gint i; GList * iter; glibtop_cpu cpu; glibtop_get_cpu(&cpu); for (i = 0; i < priv->num_cpus; i++) { gint64 total; gint64 total_used; gdouble percent_used; total = NOW[i][CPU_TOTAL] - LAST[i][CPU_TOTAL]; total_used = NOW[i][CPU_USED] - LAST[i][CPU_USED]; percent_used = total_used / (gdouble) total * 100.0; point = g_new0 (AwnGraphSinglePoint,1); point->value = percent_used; list = g_list_prepend (list,point); } text = g_strdup_printf (_("CPU: %2.0lf%%"), avg_point.value ); // awn_tooltip_set_text (AWN_TOOLTIP(sysmonicon_priv->tooltip),text); awn_icon_set_tooltip_text (AWN_ICON(object),text); g_free (text); text = g_strdup_printf("%.0lf%%",avg_point.value); g_object_set (priv->text_overlay, "text", text, NULL); g_free (text); awn_graph_add_data (sysmonicon_priv->graph,list); awn_sysmonicon_update_icon (AWN_SYSMONICON (icon)); for (iter = list; iter; iter=g_list_next(iter)) { g_free(iter->data); } g_list_free (list); #undef NOW #undef LAST } return TRUE; }