Blame sysdeps/solaris/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/error.h>
Packit d37888
#include <glibtop/cpu.h>
Packit d37888
Packit d37888
#include <assert.h>
Packit d37888
#include <sys/processor.h>
Packit d37888
Packit d37888
#include <glibtop_private.h>
Packit d37888
Packit d37888
static const unsigned long _glibtop_sysdeps_cpu_freq =
Packit d37888
(1L << GLIBTOP_CPU_FREQUENCY);
Packit d37888
Packit d37888
static const unsigned long _glibtop_sysdeps_cpu_all =
Packit d37888
(1L << GLIBTOP_CPU_TOTAL) + (1L << GLIBTOP_CPU_USER) +
Packit d37888
(1L << GLIBTOP_CPU_SYS) + (1L << GLIBTOP_CPU_IDLE) +
Packit d37888
(1L << GLIBTOP_XCPU_TOTAL) + (1L << GLIBTOP_XCPU_USER) +
Packit d37888
(1L << GLIBTOP_XCPU_SYS) + (1L << GLIBTOP_XCPU_IDLE) +
Packit d37888
(1L << GLIBTOP_XCPU_FLAGS) +
Packit d37888
(1L << GLIBTOP_CPU_FREQUENCY);
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_all;
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
    kstat_ctl_t * const kc = server->machine->kc;
Packit d37888
    cpu_stat_t cpu_stat;
Packit d37888
    processorid_t cpu;
Packit d37888
    int ncpu, found;
Packit d37888
Packit d37888
    memset (buf, 0, sizeof (glibtop_cpu));
Packit d37888
Packit d37888
    buf->frequency = server->machine->ticks;
Packit d37888
    buf->flags = _glibtop_sysdeps_cpu_freq;
Packit d37888
Packit d37888
    if(!kc)
Packit d37888
	return;
Packit d37888
Packit d37888
    switch(kstat_chain_update(kc))
Packit d37888
    {
Packit d37888
	case -1: assert(0); /* Debugging purposes, shouldn't happen */
Packit d37888
	case 0:  break;
Packit d37888
	default: glibtop_get_kstats(server);
Packit d37888
    }
Packit d37888
Packit d37888
    ncpu = MIN(GLIBTOP_NCPU, server->ncpu);
Packit d37888
Packit d37888
    for (cpu = 0, found = 0; cpu < GLIBTOP_NCPU && found != ncpu; cpu++)
Packit d37888
    {
Packit d37888
	kstat_t * const ksp = server->machine->cpu_stat_kstat [cpu];
Packit d37888
	if (!ksp) continue;;
Packit d37888
Packit d37888
	++found;
Packit d37888
Packit d37888
	if(p_online(cpu, P_STATUS) == P_ONLINE)
Packit d37888
	    buf->xcpu_flags |= (1L << cpu);
Packit d37888
	else
Packit d37888
	    continue;
Packit d37888
Packit d37888
	if (kstat_read (kc, ksp, &cpu_stat) == -1) {
Packit d37888
	    glibtop_warn_io_r (server, "kstat_read (cpu_stat%d)", cpu);
Packit d37888
	    continue;
Packit d37888
	}
Packit d37888
Packit d37888
	buf->xcpu_idle [cpu] = cpu_stat.cpu_sysinfo.cpu [CPU_IDLE];
Packit d37888
	buf->xcpu_user [cpu] = cpu_stat.cpu_sysinfo.cpu [CPU_USER];
Packit d37888
	buf->xcpu_sys [cpu] = cpu_stat.cpu_sysinfo.cpu [CPU_KERNEL];
Packit d37888
	buf->xcpu_total [cpu] = buf->xcpu_idle [cpu] + buf->xcpu_user [cpu] +
Packit d37888
	    buf->xcpu_sys [cpu];
Packit d37888
Packit d37888
	buf->idle += cpu_stat.cpu_sysinfo.cpu [CPU_IDLE];
Packit d37888
	buf->user += cpu_stat.cpu_sysinfo.cpu [CPU_USER];
Packit d37888
	buf->sys  += cpu_stat.cpu_sysinfo.cpu [CPU_KERNEL];
Packit d37888
    }
Packit d37888
Packit d37888
    if(!found)
Packit d37888
	return;
Packit d37888
Packit d37888
    buf->total = buf->idle + buf->user + buf->sys;
Packit d37888
    buf->flags = _glibtop_sysdeps_cpu_all;
Packit d37888
}