1 /*- 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)getloadavg.c 6.2 (Berkeley) 06/29/90"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/param.h> 13 #include <sys/types.h> 14 #include <sys/file.h> 15 #include <nlist.h> 16 17 static struct nlist nl[] = { 18 { "_averunnable" }, 19 #define X_AVERUNNABLE 0 20 { "_fscale" }, 21 #define X_FSCALE 1 22 { "" }, 23 }; 24 25 /* 26 * getloadavg() -- Get system load averages. 27 * 28 * Put `nelem' samples into `loadavg' array. 29 * Return number of samples retrieved, or -1 on error. 30 */ 31 getloadavg(loadavg, nelem) 32 double loadavg[]; 33 int nelem; 34 { 35 static int need_nlist = 1; 36 fixpt_t averunnable[3]; 37 int fscale, kmemfd, i; 38 int alreadyopen; 39 40 if ((alreadyopen = kvm_openfiles(NULL, NULL, NULL)) == -1) 41 return (-1); 42 /* 43 * cache nlist 44 */ 45 if (need_nlist) { 46 if (kvm_nlist(nl) != 0) 47 goto bad; 48 need_nlist = 0; 49 } 50 if (kvm_read((off_t)nl[X_AVERUNNABLE].n_value, (char *)averunnable, 51 sizeof(averunnable)) != sizeof(averunnable)) 52 goto bad; 53 if (kvm_read( (off_t)nl[X_FSCALE].n_value, (char *)&fscale, 54 sizeof(fscale)) != sizeof(fscale)) 55 goto bad; 56 nelem = MIN(nelem, sizeof(averunnable) / sizeof(averunnable[0])); 57 for (i = 0; i < nelem; i++) 58 loadavg[i] = (double) averunnable[i] / fscale; 59 if (!alreadyopen) 60 kvm_close(); 61 return (nelem); 62 63 bad: 64 if (!alreadyopen) 65 kvm_close(); 66 return (-1); 67 } 68