xref: /netbsd-src/usr.bin/systat/fetch.c (revision e7a39c4fb61273a5692b460db4ace56c8c83a5da)
1*e7a39c4fSlukem /*	$NetBSD: fetch.c,v 1.13 2009/04/13 23:20:27 lukem Exp $	*/
22f3cb7aeSjtc 
312749a2aSjtc /*-
412749a2aSjtc  * Copyright (c) 1980, 1992, 1993
512749a2aSjtc  *	The Regents of the University of California.  All rights reserved.
612749a2aSjtc  *
712749a2aSjtc  * Redistribution and use in source and binary forms, with or without
812749a2aSjtc  * modification, are permitted provided that the following conditions
912749a2aSjtc  * are met:
1012749a2aSjtc  * 1. Redistributions of source code must retain the above copyright
1112749a2aSjtc  *    notice, this list of conditions and the following disclaimer.
1212749a2aSjtc  * 2. Redistributions in binary form must reproduce the above copyright
1312749a2aSjtc  *    notice, this list of conditions and the following disclaimer in the
1412749a2aSjtc  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1612749a2aSjtc  *    may be used to endorse or promote products derived from this software
1712749a2aSjtc  *    without specific prior written permission.
1812749a2aSjtc  *
1912749a2aSjtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2012749a2aSjtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2112749a2aSjtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2212749a2aSjtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2312749a2aSjtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2412749a2aSjtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2512749a2aSjtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2612749a2aSjtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2712749a2aSjtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2812749a2aSjtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2912749a2aSjtc  * SUCH DAMAGE.
3012749a2aSjtc  */
3112749a2aSjtc 
3259413993Smrg #include <sys/cdefs.h>
3312749a2aSjtc #ifndef lint
342f3cb7aeSjtc #if 0
3512749a2aSjtc static char sccsid[] = "@(#)fetch.c	8.1 (Berkeley) 6/6/93";
362f3cb7aeSjtc #endif
37*e7a39c4fSlukem __RCSID("$NetBSD: fetch.c,v 1.13 2009/04/13 23:20:27 lukem Exp $");
3812749a2aSjtc #endif /* not lint */
3912749a2aSjtc 
4013ba787eSthorpej #include <sys/param.h>
4113ba787eSthorpej #include <sys/sched.h>
4213ba787eSthorpej #include <sys/sysctl.h>
43828483a7Ssimonb 
4413ba787eSthorpej #include <string.h>
45676abde7Schristos #include <errno.h>
46828483a7Ssimonb 
4712749a2aSjtc #include "systat.h"
4812749a2aSjtc #include "extern.h"
4912749a2aSjtc 
50ef4ee3d4Sross ssize_t
kvm_ckread(const void * a,void * b,size_t l,const char * name)51676abde7Schristos kvm_ckread(const void *a, void *b, size_t l, const char *name)
5212749a2aSjtc {
53*e7a39c4fSlukem 	if ((size_t)kvm_read(kd, (u_long)a, b, l) != l) {
5412749a2aSjtc 		if (verbose)
55676abde7Schristos 			error("error reading kmem for %s at %p (%s)\n", name,
56676abde7Schristos 			    a, strerror(errno));
5712749a2aSjtc 		return (0);
5847abaab8Smrg 	} else
5912749a2aSjtc 		return (1);
6012749a2aSjtc }
6113ba787eSthorpej 
6213ba787eSthorpej int
fetch_cptime(u_int64_t * cptime)63fc391547Sad fetch_cptime(u_int64_t *cptime)
6413ba787eSthorpej {
6513ba787eSthorpej 	size_t ssize;
6613ba787eSthorpej 	int mib[2];
6713ba787eSthorpej 
6813ba787eSthorpej 	/*
6913ba787eSthorpej 	 * XXX Need to locate the `correct' CPU when looking for this
7013ba787eSthorpej 	 * XXX in crash dumps.  Just don't report it for now, in that
7113ba787eSthorpej 	 * XXX case.
7213ba787eSthorpej 	 */
7313ba787eSthorpej 	ssize = CPUSTATES * sizeof(u_int64_t);
7413ba787eSthorpej 	memset(cptime, 0, ssize);
7513ba787eSthorpej 	if (memf == NULL) {
7613ba787eSthorpej 		mib[0] = CTL_KERN;
7713ba787eSthorpej 		mib[1] = KERN_CP_TIME;
7813ba787eSthorpej 		if (sysctl(mib, 2, cptime, &ssize, NULL, 0) < 0) {
7913ba787eSthorpej 			if (verbose)
8013ba787eSthorpej 				error("error fetching cp_time\n");
8113ba787eSthorpej 			return (0);
8213ba787eSthorpej 		} else
8313ba787eSthorpej 			return (1);
8413ba787eSthorpej 	}
8513ba787eSthorpej 	return (1);
8613ba787eSthorpej }
87