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 get_memory (LoadGraph *g) { float mempercent, swappercent; glibtop_mem mem; glibtop_swap swap; glibtop_get_mem (&mem); glibtop_get_swap (&swap); /* There's no swap on LiveCD : 0.0f is better than NaN :) */ swappercent = (swap.total ? (float)swap.used / (float)swap.total : 0.0f); mempercent = (float)mem.user / (float)mem.total; set_memory_label_and_picker(GTK_LABEL(g->labels.memory), GSM_COLOR_BUTTON(g->mem_color_picker), mem.user, mem.total, mempercent); set_memory_label_and_picker(GTK_LABEL(g->labels.swap), GSM_COLOR_BUTTON(g->swap_color_picker), swap.used, swap.total, swappercent); g->data[0][0] = mempercent; g->data[0][1] = swappercent; }
static void pprint_get_swap(void) { glibtop_swap buf; glibtop_get_swap(&buf); HEADER_PPRINT(glibtop_get_swap); PPRINT(flags, "%#llx"); PPRINT(total, "%llu"); PPRINT(used, "%llu"); PPRINT(free, "%llu"); PPRINT(pagein, "%llu"); PPRINT(pageout, "%llu"); FOOTER_PPRINT(); }
void GetPage (int Maximum, int data [3], LoadGraph *g) { static int max = 100; /* guess at maximum page rate (= in + out) */ static u_int64_t lastin = 0; static u_int64_t lastout = 0; int in, out, idle; glibtop_swap swap; glibtop_get_swap (&swap); assert ((swap.flags & needed_page_flags) == needed_page_flags); if ((lastin > 0) && (lastin < swap.pagein)) { in = swap.pagein - lastin; } else { in = 0; } lastin = swap.pagein; if ((lastout > 0) && (lastout < swap.pageout)) { out = swap.pageout - lastout; } else { out = 0; } lastout = swap.pageout; if ((in + out) > max) { /* Maximum page rate has increased. Change the scale without any indication whatsoever to the user (not a nice thing to do). */ max = in + out; } in = rint (Maximum * ((float)in / max)); out = rint (Maximum * ((float)out / max)); idle = Maximum - in - out; data [0] = in; data [1] = out; data [2] = idle; }
void GetSwap (int Maximum, int data [2], LoadGraph *g) { int used; glibtop_swap swap; glibtop_get_swap (&swap); g_return_if_fail ((swap.flags & needed_swap_flags) == needed_swap_flags); if (swap.total == 0) { used = 0; } else { used = rint (Maximum * (float)swap.used / swap.total); } data [0] = used; data [1] = Maximum - used; }
static PyObject* gtop_swap(PyObject *self, PyObject *args) { glibtop_swap buf; PyObject *d; if(!PyArg_ParseTuple(args, "")) return NULL; glibtop_get_swap(&buf); d = PyDict_New(); my_dict_add_and_decref(d, "total", PyL_ULL(buf.total)); my_dict_add_and_decref(d, "used", PyL_ULL(buf.used)); my_dict_add_and_decref(d, "free", PyL_ULL(buf.free)); my_dict_add_and_decref(d, "pagein", PyL_ULL(buf.pagein)); my_dict_add_and_decref(d, "pageout", PyL_ULL(buf.pageout)); return _struct_new(d); }
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 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); }