Blame sysdeps/linux/sysinfo.c

Packit d37888
/* Copyright (C) 1998-99 Martin Baulig
Packit d37888
   This file is part of LibGTop 1.0.
Packit d37888
Packit d37888
   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Packit d37888
Packit d37888
   LibGTop is free software; you can redistribute it and/or modify it
Packit d37888
   under the terms of the GNU General Public License as published by
Packit d37888
   the Free Software Foundation; either version 2 of the License,
Packit d37888
   or (at your option) any later version.
Packit d37888
Packit d37888
   LibGTop is distributed in the hope that it will be useful, but WITHOUT
Packit d37888
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit d37888
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit d37888
   for more details.
Packit d37888
Packit d37888
   You should have received a copy of the GNU General Public License
Packit d37888
   along with LibGTop; see the file COPYING. If not, write to the
Packit d37888
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit d37888
   Boston, MA 02110-1301, USA.
Packit d37888
*/
Packit d37888
Packit d37888
#include <config.h>
Packit d37888
#include <glibtop/error.h>
Packit d37888
#include <glibtop/cpu.h>
Packit d37888
#include <glibtop/sysinfo.h>
Packit d37888
Packit d37888
#include "glibtop_private.h"
Packit d37888
Packit d37888
#define FILENAME "/proc/cpuinfo"
Packit d37888
Packit d37888
static const unsigned long _glibtop_sysdeps_sysinfo =
Packit d37888
(1L << GLIBTOP_SYSINFO_CPUINFO);
Packit d37888
Packit d37888
static glibtop_sysinfo sysinfo = { .flags = 0 };
Packit d37888
Packit d37888
static void
Packit d37888
init_sysinfo (glibtop *server)
Packit d37888
{
Packit d37888
	char* buffer;
Packit d37888
	gchar ** processors;
Packit d37888
Packit d37888
	if(G_LIKELY(sysinfo.flags)) return;
Packit d37888
Packit d37888
	if (!g_file_get_contents(FILENAME, &buffer, NULL, NULL)) {
Packit d37888
		glibtop_error_io_r(server, "g_file_get_contents(%s)", FILENAME);
Packit d37888
	}
Packit d37888
Packit d37888
	/* cpuinfo records are seperated by a blank line */
Packit d37888
	processors = g_strsplit(buffer, "\n\n", 0);
Packit d37888
Packit d37888
	g_free(buffer);
Packit d37888
Packit d37888
	sysinfo.ncpu = 0;
Packit d37888
	for (char** this_proc = &processors[0]; *this_proc && **this_proc; this_proc++) {
Packit d37888
Packit d37888
		if (sysinfo.ncpu >= GLIBTOP_NCPU) {
Packit d37888
			glibtop_warn_r(server, "Cannot deal with more than %d CPUs", GLIBTOP_NCPU);
Packit d37888
			break;
Packit d37888
		}
Packit d37888
Packit d37888
		gchar **parts, **p;
Packit d37888
		if (g_strrstr (*this_proc, "processor" ) == NULL) {
Packit d37888
		  /* skip unknown paragraph */
Packit d37888
		  continue;
Packit d37888
		}
Packit d37888
Packit d37888
		glibtop_entry * const cpuinfo = &sysinfo.cpuinfo[sysinfo.ncpu];
Packit d37888
Packit d37888
		cpuinfo->labels = g_ptr_array_new ();
Packit d37888
Packit d37888
		cpuinfo->values = g_hash_table_new_full(g_str_hash, g_str_equal,
Packit d37888
							g_free, g_free);
Packit d37888
Packit d37888
		cpuinfo->descriptions = g_hash_table_new_full(g_str_hash, g_str_equal,
Packit d37888
							g_free, g_free);
Packit d37888
Packit d37888
		/* "<key>    : <value>" */
Packit d37888
		parts = g_strsplit_set(*this_proc, ":\n", 0);
Packit d37888
Packit d37888
		for(p = parts; *p && *(p+1); p += 2) {
Packit d37888
Packit d37888
			/* stole the allocated memory */
Packit d37888
			gchar * const key   = g_strstrip(   *p   );
Packit d37888
			gchar * const value = g_strstrip( *(p+1) );
Packit d37888
Packit d37888
			g_ptr_array_add(cpuinfo->labels, key);
Packit d37888
			g_hash_table_insert(cpuinfo->values, key, value);
Packit d37888
		}
Packit d37888
Packit d37888
Packit d37888
		/* the last key has no value and has not been added */
Packit d37888
		if(*p) g_free(*p);
Packit d37888
Packit d37888
		/* just g_free instead of g_strvfree because we stole
Packit d37888
		   the memory*/
Packit d37888
Packit d37888
		g_free(parts);
Packit d37888
Packit d37888
		sysinfo.ncpu++;
Packit d37888
	}
Packit d37888
Packit d37888
	g_strfreev(processors);
Packit d37888
Packit d37888
	sysinfo.flags = _glibtop_sysdeps_sysinfo;
Packit d37888
}
Packit d37888
Packit d37888
const glibtop_sysinfo *
Packit d37888
glibtop_get_sysinfo_s (glibtop *server)
Packit d37888
{
Packit d37888
	init_sysinfo (server);
Packit d37888
	return &sysinfo;
Packit d37888
}