xref: /netbsd-src/lib/libkvm/kvm_getloadavg.c (revision e1a2f47f1264dd9755e9e747d45ba016dff2334f)
1*e1a2f47fSmatt /*	$NetBSD: kvm_getloadavg.c,v 1.11 2012/03/21 10:10:36 matt Exp $	*/
2346e67f8Sthorpej 
30215cc7dScgd /*-
40215cc7dScgd  * Copyright (c) 1993
50215cc7dScgd  *	The Regents of the University of California.  All rights reserved.
60215cc7dScgd  *
70215cc7dScgd  * Redistribution and use in source and binary forms, with or without
80215cc7dScgd  * modification, are permitted provided that the following conditions
90215cc7dScgd  * are met:
100215cc7dScgd  * 1. Redistributions of source code must retain the above copyright
110215cc7dScgd  *    notice, this list of conditions and the following disclaimer.
120215cc7dScgd  * 2. Redistributions in binary form must reproduce the above copyright
130215cc7dScgd  *    notice, this list of conditions and the following disclaimer in the
140215cc7dScgd  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
160215cc7dScgd  *    may be used to endorse or promote products derived from this software
170215cc7dScgd  *    without specific prior written permission.
180215cc7dScgd  *
190215cc7dScgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
200215cc7dScgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
210215cc7dScgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220215cc7dScgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
230215cc7dScgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240215cc7dScgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250215cc7dScgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260215cc7dScgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
270215cc7dScgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280215cc7dScgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290215cc7dScgd  * SUCH DAMAGE.
300215cc7dScgd  */
310215cc7dScgd 
32b4119f6bSmikel #include <sys/cdefs.h>
330215cc7dScgd #if defined(LIBC_SCCS) && !defined(lint)
34346e67f8Sthorpej #if 0
350215cc7dScgd static char sccsid[] = "@(#)kvm_getloadavg.c	8.1 (Berkeley) 6/4/93";
36346e67f8Sthorpej #else
37*e1a2f47fSmatt __RCSID("$NetBSD: kvm_getloadavg.c,v 1.11 2012/03/21 10:10:36 matt Exp $");
38346e67f8Sthorpej #endif
390215cc7dScgd #endif /* LIBC_SCCS and not lint */
400215cc7dScgd 
410215cc7dScgd #include <sys/param.h>
420215cc7dScgd #include <sys/time.h>
430215cc7dScgd #include <sys/resource.h>
44c62a74e6Sthorpej #include <sys/lwp.h>
450215cc7dScgd #include <sys/proc.h>
460215cc7dScgd #include <sys/sysctl.h>
470eac4bfcSmrg #include <uvm/uvm_param.h>
480215cc7dScgd 
490215cc7dScgd #include <db.h>
500215cc7dScgd #include <fcntl.h>
510215cc7dScgd #include <limits.h>
520215cc7dScgd #include <nlist.h>
530215cc7dScgd #include <kvm.h>
54ef9b3c57Smikel #include <stdlib.h>
550215cc7dScgd 
560215cc7dScgd #include "kvm_private.h"
570215cc7dScgd 
580215cc7dScgd static struct nlist nl[] = {
59*e1a2f47fSmatt 	{ .n_name = "_averunnable" },
600215cc7dScgd #define	X_AVERUNNABLE	0
61*e1a2f47fSmatt 	{ .n_name = "_fscale" },
620215cc7dScgd #define	X_FSCALE	1
63*e1a2f47fSmatt 	{ .n_name = "" },
640215cc7dScgd };
650215cc7dScgd 
660215cc7dScgd /*
670215cc7dScgd  * kvm_getloadavg() -- Get system load averages, from live or dead kernels.
680215cc7dScgd  *
690215cc7dScgd  * Put `nelem' samples into `loadavg' array.
700215cc7dScgd  * Return number of samples retrieved, or -1 on error.
710215cc7dScgd  */
720215cc7dScgd int
kvm_getloadavg(kvm_t * kd,double loadavg[],int nelem)736dc46b92Sjym kvm_getloadavg(kvm_t *kd, double loadavg[], int nelem)
740215cc7dScgd {
750215cc7dScgd 	struct loadavg loadinfo;
760215cc7dScgd 	struct nlist *p;
770215cc7dScgd 	int fscale, i;
780215cc7dScgd 
790215cc7dScgd 	if (ISALIVE(kd))
800215cc7dScgd 		return (getloadavg(loadavg, nelem));
810215cc7dScgd 
820215cc7dScgd 	if (kvm_nlist(kd, nl) != 0) {
830215cc7dScgd 		for (p = nl; p->n_type != 0; ++p);
840215cc7dScgd 		_kvm_err(kd, kd->program,
850215cc7dScgd 		    "%s: no such symbol", p->n_name);
860215cc7dScgd 		return (-1);
870215cc7dScgd 	}
880215cc7dScgd 
890215cc7dScgd 	if (KREAD(kd, nl[X_AVERUNNABLE].n_value, &loadinfo)) {
900215cc7dScgd 		_kvm_err(kd, kd->program, "can't read averunnable");
910215cc7dScgd 		return (-1);
920215cc7dScgd 	}
930215cc7dScgd 
940215cc7dScgd 	/*
950215cc7dScgd 	 * Old kernels have fscale separately; if not found assume
960215cc7dScgd 	 * running new format.
970215cc7dScgd 	 */
980215cc7dScgd 	if (!KREAD(kd, nl[X_FSCALE].n_value, &fscale))
990215cc7dScgd 		loadinfo.fscale = fscale;
1000215cc7dScgd 
1010215cc7dScgd 	nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t));
1020215cc7dScgd 	for (i = 0; i < nelem; i++)
1030215cc7dScgd 		loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale;
1040215cc7dScgd 	return (nelem);
1050215cc7dScgd }
106