kvm_getloadavg.c revision 84768
119370Spst/*-
219370Spst * Copyright (c) 1993
398948Sobrien *	The Regents of the University of California.  All rights reserved.
4130809Smarcel *
5130809Smarcel * Redistribution and use in source and binary forms, with or without
619370Spst * modification, are permitted provided that the following conditions
798948Sobrien * are met:
819370Spst * 1. Redistributions of source code must retain the above copyright
998948Sobrien *    notice, this list of conditions and the following disclaimer.
1098948Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1198948Sobrien *    notice, this list of conditions and the following disclaimer in the
1298948Sobrien *    documentation and/or other materials provided with the distribution.
1319370Spst * 3. All advertising materials mentioning features or use of this software
1498948Sobrien *    must display the following acknowledgement:
1598948Sobrien *	This product includes software developed by the University of
1698948Sobrien *	California, Berkeley and its contributors.
1798948Sobrien * 4. Neither the name of the University nor the names of its contributors
1819370Spst *    may be used to endorse or promote products derived from this software
1998948Sobrien *    without specific prior written permission.
2098948Sobrien *
2198948Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2298948Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2319370Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2498948Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2519370Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2619370Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2719370Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2846289Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2919370Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3019370Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3119370Spst * SUCH DAMAGE.
3219370Spst */
3319370Spst
3498948Sobrien#include <sys/cdefs.h>
3519370Spst__FBSDID("$FreeBSD: head/lib/libkvm/kvm_getloadavg.c 84768 2001-10-10 17:48:44Z bde $");
3619370Spst
3719370Spst#if defined(LIBC_SCCS) && !defined(lint)
3846289Sdfr#if 0
3998948Sobrienstatic char sccsid[] = "@(#)kvm_getloadavg.c	8.1 (Berkeley) 6/4/93";
4098948Sobrien#endif
4198948Sobrien#endif /* LIBC_SCCS and not lint */
4298948Sobrien
4319370Spst#include <sys/param.h>
4498948Sobrien#include <sys/time.h>
4598948Sobrien#include <sys/resource.h>
4619370Spst#include <sys/proc.h>
4719370Spst#include <sys/sysctl.h>
4819370Spst#include <vm/vm_param.h>
4919370Spst
5098948Sobrien#include <stdlib.h>
5198948Sobrien#include <fcntl.h>
5298948Sobrien#include <limits.h>
5398948Sobrien#include <nlist.h>
5419370Spst#include <kvm.h>
5519370Spst
5619370Spst#include "kvm_private.h"
5798948Sobrien
5898948Sobrienstatic struct nlist nl[] = {
59130809Smarcel	{ "_averunnable" },
60130809Smarcel#define	X_AVERUNNABLE	0
6119370Spst	{ "_fscale" },
6298948Sobrien#define	X_FSCALE	1
6398948Sobrien	{ "" },
6498948Sobrien};
6519370Spst
6698948Sobrien/*
6798948Sobrien * kvm_getloadavg() -- Get system load averages, from live or dead kernels.
6898948Sobrien *
6998948Sobrien * Put `nelem' samples into `loadavg' array.
7019370Spst * Return number of samples retrieved, or -1 on error.
7198948Sobrien */
7219370Spstint
7398948Sobrienkvm_getloadavg(kd, loadavg, nelem)
7419370Spst	kvm_t *kd;
7598948Sobrien	double loadavg[];
7698948Sobrien	int nelem;
7798948Sobrien{
7898948Sobrien	struct loadavg loadinfo;
7919370Spst	struct nlist *p;
8098948Sobrien	int fscale, i;
8119370Spst
8298948Sobrien	if (ISALIVE(kd))
8319370Spst		return (getloadavg(loadavg, nelem));
8498948Sobrien
8598948Sobrien	if (kvm_nlist(kd, nl) != 0) {
8698948Sobrien		for (p = nl; p->n_type != 0; ++p);
8798948Sobrien		_kvm_err(kd, kd->program,
88130809Smarcel		    "%s: no such symbol", p->n_name);
8919370Spst		return (-1);
9098948Sobrien	}
9198948Sobrien
9219370Spst#define KREAD(kd, addr, obj) \
9398948Sobrien	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
9498948Sobrien	if (KREAD(kd, nl[X_AVERUNNABLE].n_value, &loadinfo)) {
9519370Spst		_kvm_err(kd, kd->program, "can't read averunnable");
96130809Smarcel		return (-1);
97130809Smarcel	}
9819370Spst
9998948Sobrien	/*
10019370Spst	 * Old kernels have fscale separately; if not found assume
10198948Sobrien	 * running new format.
10219370Spst	 */
10398948Sobrien	if (!KREAD(kd, nl[X_FSCALE].n_value, &fscale))
10498948Sobrien		loadinfo.fscale = fscale;
10519370Spst
10698948Sobrien	nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t));
10719370Spst	for (i = 0; i < nelem; i++)
10898948Sobrien		loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale;
10919370Spst	return (nelem);
11098948Sobrien}
11198948Sobrien