Blame sysdeps/sun4/cpu.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.h>
Packit d37888
#include <glibtop/cpu.h>
Packit d37888
Packit d37888
#include <glibtop_suid.h>
Packit d37888
Packit d37888
static const unsigned long _glibtop_sysdeps_cpu =
Packit d37888
(1 << GLIBTOP_CPU_TOTAL) + (1 << GLIBTOP_CPU_USER) +
Packit d37888
(1 << GLIBTOP_CPU_NICE) + (1 << GLIBTOP_CPU_SYS) +
Packit d37888
(1 << GLIBTOP_CPU_IDLE) + (1 << GLIBTOP_CPU_FREQUENCY);
Packit d37888
Packit d37888
/* Provides information about cpu usage. */
Packit d37888
Packit d37888
void
Packit d37888
glibtop_get_cpu_p (glibtop *server, glibtop_cpu *buf)
Packit d37888
{
Packit d37888
	long cp_time [CPUSTATES], mp_time [NCPU][CPUSTATES];
Packit d37888
	int i;
Packit d37888
Packit d37888
	glibtop_init_p (server, (1 << GLIBTOP_SYSDEPS_CPU), 0);
Packit d37888
Packit d37888
	memset (buf, 0, sizeof (glibtop_cpu));
Packit d37888
Packit d37888
	/* !!! THE FOLLOWING CODE RUNS SGID KMEM - CHANGE WITH CAUTION !!! */
Packit d37888
Packit d37888
	glibtop_suid_enter (server);
Packit d37888
Packit d37888
	/* get the cp_time array */
Packit d37888
Packit d37888
	(void) _glibtop_getkval (server, _glibtop_nlist [X_CP_TIME].n_value,
Packit d37888
				 (int *) cp_time, sizeof (cp_time),
Packit d37888
				 _glibtop_nlist [X_CP_TIME].n_name);
Packit d37888
Packit d37888
#ifdef MULTIPROCESSOR
Packit d37888
	/* get the mp_time array as well */
Packit d37888
Packit d37888
	if (server->machine->ncpu > 1) {
Packit d37888
		(void) _glibtop_getkval (server, _glibtop_nlist [X_MP_TIME].n_value,
Packit d37888
					 (int *) mp_time, sizeof (mp_time),
Packit d37888
					 _glibtop_nlist [X_MP_TIME].n_name);
Packit d37888
	}
Packit d37888
#endif
Packit d37888
Packit d37888
	glibtop_suid_leave (server);
Packit d37888
Packit d37888
	/* !!! END OF SUID ROOT PART !!! */
Packit d37888
Packit d37888
#ifdef MULTIPROCESSOR
Packit d37888
	/* If we have multiple processors, we add the times for each of them
Packit d37888
	 * and set frequency to 100 times the number of the processors. */
Packit d37888
Packit d37888
	/* [FIXME]: I had no machine with more than one processor to test
Packit d37888
	 *          this code !!! */
Packit d37888
Packit d37888
	if (server->machine->ncpu > 1) {
Packit d37888
		for (i = 0; i < server->machine->ncpu; i++) {
Packit d37888
			buf->user += mp_time [i][CP_USER];
Packit d37888
			buf->nice += mp_time [i][CP_NICE];
Packit d37888
			buf->sys  += mp_time [i][CP_SYS];
Packit d37888
			buf->idle += mp_time [i][CP_IDLE];
Packit d37888
			buf->frequency += 100;
Packit d37888
		}
Packit d37888
	} else {
Packit d37888
		buf->user = cp_time [CP_USER];
Packit d37888
		buf->nice = cp_time [CP_NICE];
Packit d37888
		buf->sys  = cp_time [CP_SYS];
Packit d37888
		buf->idle = cp_time [CP_IDLE];
Packit d37888
		buf->frequency = 100;
Packit d37888
	}
Packit d37888
#else
Packit d37888
	buf->user = cp_time [CP_USER];
Packit d37888
	buf->nice = cp_time [CP_NICE];
Packit d37888
	buf->sys  = cp_time [CP_SYS];
Packit d37888
	buf->idle = cp_time [CP_IDLE];
Packit d37888
	buf->frequency = 100;
Packit d37888
#endif
Packit d37888
Packit d37888
	/* Calculate total time. */
Packit d37888
Packit d37888
	buf->total = buf->user + buf->nice + buf->sys + buf->idle;
Packit d37888
Packit d37888
	/* Now we can set the flags. */
Packit d37888
Packit d37888
	buf->flags = _glibtop_sysdeps_cpu;
Packit d37888
}