Blame sysdeps/openbsd/cpu.c

Packit d37888
/* Copyright (C) 1998 Joshua Sled
Packit d37888
   This file is part of LibGTop 1.0.
Packit d37888
Packit d37888
   Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 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/error.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
(1L << GLIBTOP_CPU_TOTAL) + (1L << GLIBTOP_CPU_USER) +
Packit d37888
(1L << GLIBTOP_CPU_NICE) + (1L << GLIBTOP_CPU_SYS) +
Packit d37888
(1L << GLIBTOP_CPU_IDLE) + (1L << GLIBTOP_CPU_FREQUENCY) +
Packit d37888
(1L << GLIBTOP_CPU_IRQ);
Packit d37888
Packit d37888
static const unsigned long _glibtop_sysdeps_cpu_smp =
Packit d37888
(1L << GLIBTOP_XCPU_TOTAL) + (1L << GLIBTOP_XCPU_USER) +
Packit d37888
(1L << GLIBTOP_XCPU_NICE) + (1L << GLIBTOP_XCPU_SYS) +
Packit d37888
(1L << GLIBTOP_XCPU_IDLE) + (1L << GLIBTOP_XCPU_IRQ);
Packit d37888
Packit d37888
/* MIB array for sysctl */
Packit d37888
static int mib_length=2;
Packit d37888
static int mib_cr [] = { CTL_KERN, KERN_CLOCKRATE };
Packit d37888
static int mib_cptime [] = { CTL_KERN, KERN_CPTIME };
Packit d37888
static int mib_cptime_s [] = { CTL_KERN, KERN_CPTIME2, 0 };
Packit d37888
Packit d37888
/* Init function. */
Packit d37888
Packit d37888
void
Packit d37888
_glibtop_init_cpu_s (glibtop *server)
Packit d37888
{
Packit d37888
	server->sysdeps.cpu = _glibtop_sysdeps_cpu;
Packit d37888
Packit d37888
	if (server->ncpu)
Packit d37888
		server->sysdeps.cpu |= _glibtop_sysdeps_cpu_smp;
Packit d37888
}
Packit d37888
Packit d37888
/* Provides information about cpu usage. */
Packit d37888
Packit d37888
void
Packit d37888
glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
Packit d37888
{
Packit d37888
	long cpts [CPUSTATES];
Packit d37888
	int64_t *cp_times = NULL;
Packit d37888
	struct clockinfo ci;
Packit d37888
	size_t length;
Packit d37888
	int ncpu, i;
Packit d37888
Packit d37888
	glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
Packit d37888
Packit d37888
	memset (buf, 0, sizeof (glibtop_cpu));
Packit d37888
Packit d37888
	/* If this fails, the nlist may not be valid. */
Packit d37888
	if (server->sysdeps.cpu == 0)
Packit d37888
		return;
Packit d37888
Packit d37888
	length = sizeof (cpts);
Packit d37888
	if (sysctl (mib_cptime, mib_length, cpts, &length, NULL, 0)) {
Packit d37888
		glibtop_warn_io_r (server, "sysctl (kern.cptime)");
Packit d37888
		return;
Packit d37888
	}
Packit d37888
Packit d37888
	/* Get the clockrate data */
Packit d37888
	length = sizeof (struct clockinfo);
Packit d37888
	if (sysctl (mib_cr, mib_length, &ci, &length, NULL, 0)) {
Packit d37888
		glibtop_warn_io_r (server, "sysctl (kern.clockrate)");
Packit d37888
		return;
Packit d37888
	}
Packit d37888
Packit d37888
	/* set user time */
Packit d37888
	buf->user = cpts [CP_USER];
Packit d37888
	/* set nice time */
Packit d37888
	buf->nice = cpts [CP_NICE];
Packit d37888
	/* set sys time */
Packit d37888
	buf->sys = cpts [CP_SYS];
Packit d37888
	/* set idle time */
Packit d37888
	buf->idle = cpts [CP_IDLE];
Packit d37888
	/* set irq */
Packit d37888
	buf->irq = cpts [CP_INTR];
Packit d37888
Packit d37888
	/* set frequency */
Packit d37888
	buf->frequency = (ci.stathz ? ci.stathz : ci.hz);
Packit d37888
	/* set total */
Packit d37888
	buf->total = cpts [CP_USER] + cpts [CP_NICE] \
Packit d37888
		+ cpts [CP_SYS] + cpts [CP_IDLE] + cpts [CP_INTR];
Packit d37888
Packit d37888
	ncpu = server->ncpu + 1;
Packit d37888
Packit d37888
	/*
Packit d37888
	if (!cp_times) {
Packit d37888
		if ((cp_times = calloc(sizeof(int64_t *), ncpu)) == NULL)
Packit d37888
			return;
Packit d37888
	}
Packit d37888
	*/
Packit d37888
Packit d37888
	length = CPUSTATES * sizeof(int64_t);
Packit d37888
	for (i = 0; i < ncpu; i++) {
Packit d37888
		mib_cptime_s[2] = i;
Packit d37888
		cp_times = g_malloc (length);
Packit d37888
		if (sysctl (mib_cptime_s, 3, cp_times, &length, NULL, 0) < 0)
Packit d37888
			free(cp_times);
Packit d37888
Packit d37888
		if (cp_times) {
Packit d37888
			buf->xcpu_user[i] = cp_times[CP_USER];
Packit d37888
			buf->xcpu_nice[i] = cp_times[CP_NICE];
Packit d37888
			buf->xcpu_sys[i] = cp_times[CP_SYS];
Packit d37888
			buf->xcpu_idle[i] = cp_times[CP_IDLE];
Packit d37888
			buf->xcpu_irq[i] = cp_times[CP_INTR];
Packit d37888
		} else {
Packit d37888
			buf->xcpu_user[i] = cpts [CP_USER] / ncpu;
Packit d37888
			buf->xcpu_nice[i] = cpts [CP_NICE] / ncpu;
Packit d37888
			buf->xcpu_sys[i] = cpts [CP_SYS] / ncpu;
Packit d37888
			buf->xcpu_idle[i] = cpts [CP_IDLE] / ncpu;
Packit d37888
			buf->xcpu_irq[i] = cpts [CP_INTR] / ncpu;
Packit d37888
		}
Packit d37888
		buf->xcpu_total[i] = buf->xcpu_user[i] + buf->xcpu_nice[i] \
Packit d37888
				     + buf->xcpu_sys[i] + buf->xcpu_idle[i] \
Packit d37888
				     + buf->xcpu_irq[i];
Packit d37888
	}
Packit d37888
Packit d37888
	g_free (cp_times);
Packit d37888
Packit d37888
	/* Set the flags last. */
Packit d37888
	buf->flags = _glibtop_sysdeps_cpu;
Packit d37888
Packit d37888
	if (ncpu > 1) {
Packit d37888
		buf->flags |= _glibtop_sysdeps_cpu_smp;
Packit d37888
	}
Packit d37888
}