From b6a89d8bac142d63927a842dbd7e470e8260e495 Mon Sep 17 00:00:00 2001 From: Marc Teitelbaum Date: Fri, 29 Jun 1990 07:15:34 -0800 Subject: [PATCH] use kvm library SCCS-vsn: lib/libc/gen/getloadavg.c 6.2 --- usr/src/lib/libc/gen/getloadavg.c | 57 +++++++++++-------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/usr/src/lib/libc/gen/getloadavg.c b/usr/src/lib/libc/gen/getloadavg.c index 5bbaaf8769..cd43944d0e 100644 --- a/usr/src/lib/libc/gen/getloadavg.c +++ b/usr/src/lib/libc/gen/getloadavg.c @@ -1,33 +1,18 @@ -/* +/*- * Copyright (c) 1989 The Regents of the University of California. * All rights reserved. * - * Redistribution and use in source and binary forms are permitted - * provided that the above copyright notice and this paragraph are - * duplicated in all such forms and that any documentation, - * advertising materials, and other materials related to such - * distribution and use acknowledge that the software was developed - * by the University of California, Berkeley. The name of the - * University may not be used to endorse or promote products derived - * from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * %sccs.include.redist.c% */ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)getloadavg.c 6.1 (Berkeley) %G%"; -#endif LIBC_SCCS and not lint +static char sccsid[] = "@(#)getloadavg.c 6.2 (Berkeley) %G%"; +#endif /* LIBC_SCCS and not lint */ #include #include #include - #include -#include - -static char *kmem = _PATH_KMEM; -static char *vmunix = _PATH_UNIX; static struct nlist nl[] = { { "_averunnable" }, @@ -47,38 +32,36 @@ getloadavg(loadavg, nelem) double loadavg[]; int nelem; { - off_t lseek(); static int need_nlist = 1; fixpt_t averunnable[3]; int fscale, kmemfd, i; + int alreadyopen; - /* nlist() is slow; cache result */ + if ((alreadyopen = kvm_openfiles(NULL, NULL, NULL)) == -1) + return (-1); + /* + * cache nlist + */ if (need_nlist) { - if (nlist(vmunix, nl) != 0) - return (-1); - if (nl[X_AVERUNNABLE].n_type == 0 || nl[X_FSCALE].n_type == 0) - return (-1); + if (kvm_nlist(nl) != 0) + goto bad; need_nlist = 0; } - - if ((kmemfd = open(kmem, O_RDONLY, 0)) < 0) - return (-1); - if (lseek(kmemfd, (off_t)nl[X_AVERUNNABLE].n_value, L_SET) == -1) - goto bad; - if (read(kmemfd, (char *)averunnable, sizeof(averunnable)) < 0) + if (kvm_read((off_t)nl[X_AVERUNNABLE].n_value, (char *)averunnable, + sizeof(averunnable)) != sizeof(averunnable)) goto bad; - if (lseek(kmemfd, (off_t)nl[X_FSCALE].n_value, L_SET) == -1) + if (kvm_read( (off_t)nl[X_FSCALE].n_value, (char *)&fscale, + sizeof(fscale)) != sizeof(fscale)) goto bad; - if (read(kmemfd, (char *)&fscale, sizeof(fscale)) < 0) - goto bad; - (void) close(kmemfd); - nelem = MIN(nelem, sizeof(averunnable) / sizeof(averunnable[0])); for (i = 0; i < nelem; i++) loadavg[i] = (double) averunnable[i] / fscale; + if (!alreadyopen) + kvm_close(); return (nelem); bad: - (void) close(kmemfd); + if (!alreadyopen) + kvm_close(); return (-1); } -- 2.20.1