1 /*- 2 * Copyright (c) 2008 Yahoo!, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the author nor the names of any co-contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/pcpu.h> 36 #include <sys/resource.h> 37 #include <sys/sysctl.h> 38 #include <errno.h> 39 #include <kvm.h> 40 #include <limits.h> 41 #include <stdlib.h> 42 43 #include "kvm_private.h" 44 45 static struct nlist kvm_cp_time_nl[] = { 46 { "_cp_time" }, /* (deprecated) */ 47 { NULL }, 48 }; 49 50 #define NL_CP_TIME 0 51 52 static int kvm_cp_time_cached; 53 54 static int 55 _kvm_cp_time_init(kvm_t *kd) 56 { 57 58 if (kvm_nlist(kd, kvm_cp_time_nl) < 0) 59 return (-1); 60 kvm_cp_time_cached = 1; 61 } 62 63 static int 64 getsysctl(kvm_t *kd, const char *name, void *buf, size_t len) 65 { 66 size_t nlen; 67 68 nlen = len; 69 if (sysctlbyname(name, buf, &nlen, NULL, 0) < 0) { 70 _kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name, 71 strerror(errno)); 72 return (-1); 73 } 74 if (nlen != len) { 75 _kvm_err(kd, kd->program, "sysctl %s has unexpected size", 76 name); 77 return (-1); 78 } 79 return (0); 80 } 81 82 int 83 kvm_getcptime(kvm_t *kd, long *cp_time) 84 { 85 struct pcpu *pc; 86 int i, j, maxcpu; 87 88 if (kd == NULL) { 89 kvm_cp_time_cached = 0; 90 return (0); 91 } 92 93 if (ISALIVE(kd)) 94 return (getsysctl(kd, "kern.cp_time", cp_time, sizeof(long) * 95 CPUSTATES)); 96 97 if (kvm_cp_time_cached == 0) { 98 if (_kvm_cp_time_init(kd) < 0) 99 return (-1); 100 } 101 102 /* If this kernel has a "cp_time[]" symbol, then just read that. */ 103 if (kvm_cp_time_nl[NL_CP_TIME].n_value != 0) { 104 if (kvm_read(kd, kvm_cp_time_nl[NL_CP_TIME].n_value, cp_time, 105 sizeof(long) * CPUSTATES) != sizeof(long) * CPUSTATES) { 106 _kvm_err(kd, kd->program, "cannot read cp_time array"); 107 return (-1); 108 } 109 return (0); 110 } 111 112 /* 113 * If we don't have that symbol, then we have to simulate 114 * "cp_time[]" by adding up the individual times for each CPU. 115 */ 116 maxcpu = kvm_getmaxcpu(kd); 117 if (maxcpu < 0) 118 return (-1); 119 for (i = 0; i < CPUSTATES; i++) 120 cp_time[i] = 0; 121 for (i = 0; i < maxcpu; i++) { 122 pc = kvm_getpcpu(kd, i); 123 if (pc == NULL) 124 continue; 125 if (pc == (void *)-1) 126 return (-1); 127 for (j = 0; j < CPUSTATES; j++) 128 cp_time[j] += pc->pc_cp_time[j]; 129 free(pc); 130 } 131 return (0); 132 } 133