Beispiel #1
0
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;
}
Beispiel #2
0
/**
 * Returns the amount of used RAM in GiB.
 */
double get_used_memory() {
  glibtop_mem mem;

  glibtop_get_mem(&mem);

  return (double)mem.user / ((double)(G_GUINT64_CONSTANT(1) << 30));
}
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;
}
Beispiel #4
0
static void pprint_get_mem(void)
{
  glibtop_mem buf;

  glibtop_get_mem(&buf);

  HEADER_PPRINT(glibtop_get_mem);
  PPRINT(flags, "%#llx");
  PPRINT(total, "%llu");
  PPRINT(used, "%llu");
  PPRINT(free, "%llu");
  PPRINT(shared, "%llu");
  PPRINT(buffer, "%llu");
  PPRINT(cached, "%llu");
  PPRINT(user, "%llu");
  PPRINT(locked, "%llu");
  FOOTER_PPRINT();
}
Beispiel #5
0
void
GetMemory (int Maximum, int data [5], LoadGraph *g)
{
    int user, shared, buffer, cached;
    
    glibtop_mem mem;
	
    glibtop_get_mem (&mem);
	
    g_return_if_fail ((mem.flags & needed_mem_flags) == needed_mem_flags);

    user    = rint (Maximum * (float)mem.user / (float)mem.total);
    shared  = rint (Maximum * (float)mem.shared / (float)mem.total);
    buffer  = rint (Maximum * (float)mem.buffer / (float)mem.total);
    cached = rint (Maximum * (float)mem.cached / (float)mem.total);
    
    data [0] = user;
    data [1] = shared;
    data [2] = buffer;
    data[3] = cached;
    data [4] = Maximum-user-shared-buffer-cached;
}
Beispiel #6
0
static PyObject* gtop_mem(PyObject *self, PyObject *args)
{
	glibtop_mem buf;
	PyObject *d;

	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	glibtop_get_mem(&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, "shared", PyL_ULL(buf.shared));
	my_dict_add_and_decref(d, "buffer", PyL_ULL(buf.buffer));
	my_dict_add_and_decref(d, "cached", PyL_ULL(buf.cached));
	my_dict_add_and_decref(d, "user",   PyL_ULL(buf.user));
	my_dict_add_and_decref(d, "locked", PyL_ULL(buf.locked));

	return _struct_new(d);
}
Beispiel #7
0
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;
}
Beispiel #8
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);
}
Beispiel #9
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);
}