2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008 Yahoo!, Inc.
6 * Written by: John Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/resource.h>
39 #include <sys/sysctl.h>
46 #include "kvm_private.h"
48 static struct nlist kvm_cp_time_nl[] = {
49 { .n_name = "_cp_time" }, /* (deprecated) */
55 static int kvm_cp_time_cached;
58 _kvm_cp_time_init(kvm_t *kd)
61 if (kvm_nlist(kd, kvm_cp_time_nl) < 0)
63 kvm_cp_time_cached = 1;
68 getsysctl(kvm_t *kd, const char *name, void *buf, size_t len)
73 if (sysctlbyname(name, buf, &nlen, NULL, 0) < 0) {
74 _kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name,
79 _kvm_err(kd, kd->program, "sysctl %s has unexpected size",
87 kvm_getcptime(kvm_t *kd, long *cp_time)
93 kvm_cp_time_cached = 0;
98 return (getsysctl(kd, "kern.cp_time", cp_time, sizeof(long) *
101 if (!kd->arch->ka_native(kd)) {
102 _kvm_err(kd, kd->program,
103 "cannot read cp_time from non-native core");
107 if (kvm_cp_time_cached == 0) {
108 if (_kvm_cp_time_init(kd) < 0)
112 /* If this kernel has a "cp_time[]" symbol, then just read that. */
113 if (kvm_cp_time_nl[NL_CP_TIME].n_value != 0) {
114 if (kvm_read(kd, kvm_cp_time_nl[NL_CP_TIME].n_value, cp_time,
115 sizeof(long) * CPUSTATES) != sizeof(long) * CPUSTATES) {
116 _kvm_err(kd, kd->program, "cannot read cp_time array");
123 * If we don't have that symbol, then we have to simulate
124 * "cp_time[]" by adding up the individual times for each CPU.
126 maxcpu = kvm_getmaxcpu(kd);
129 for (i = 0; i < CPUSTATES; i++)
131 for (i = 0; i < maxcpu; i++) {
132 pc = kvm_getpcpu(kd, i);
135 if (pc == (void *)-1)
137 for (j = 0; j < CPUSTATES; j++)
138 cp_time[j] += pc->pc_cp_time[j];