xref: /freebsd/lib/libkvm/kvm_cptime.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2008 Yahoo!, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/pcpu.h>
38 #include <sys/resource.h>
39 #include <sys/sysctl.h>
40 #include <errno.h>
41 #include <kvm.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "kvm_private.h"
47 
48 static struct nlist kvm_cp_time_nl[] = {
49 	{ .n_name = "_cp_time" },		/* (deprecated) */
50 	{ .n_name = NULL },
51 };
52 
53 #define	NL_CP_TIME		0
54 
55 static int kvm_cp_time_cached;
56 
57 static int
58 _kvm_cp_time_init(kvm_t *kd)
59 {
60 
61 	if (kvm_nlist(kd, kvm_cp_time_nl) < 0)
62 		return (-1);
63 	kvm_cp_time_cached = 1;
64 	return (0);
65 }
66 
67 static int
68 getsysctl(kvm_t *kd, const char *name, void *buf, size_t len)
69 {
70 	size_t nlen;
71 
72 	nlen = len;
73 	if (sysctlbyname(name, buf, &nlen, NULL, 0) < 0) {
74 		_kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name,
75 		    strerror(errno));
76 		return (-1);
77 	}
78 	if (nlen != len) {
79 		_kvm_err(kd, kd->program, "sysctl %s has unexpected size",
80 		    name);
81 		return (-1);
82 	}
83 	return (0);
84 }
85 
86 int
87 kvm_getcptime(kvm_t *kd, long *cp_time)
88 {
89 	struct pcpu *pc;
90 	int i, j, maxcpu;
91 
92 	if (kd == NULL) {
93 		kvm_cp_time_cached = 0;
94 		return (0);
95 	}
96 
97 	if (ISALIVE(kd))
98 		return (getsysctl(kd, "kern.cp_time", cp_time, sizeof(long) *
99 		    CPUSTATES));
100 
101 	if (!kd->arch->ka_native(kd)) {
102 		_kvm_err(kd, kd->program,
103 		    "cannot read cp_time from non-native core");
104 		return (-1);
105 	}
106 
107 	if (kvm_cp_time_cached == 0) {
108 		if (_kvm_cp_time_init(kd) < 0)
109 			return (-1);
110 	}
111 
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");
117 			return (-1);
118 		}
119 		return (0);
120 	}
121 
122 	/*
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.
125 	 */
126 	maxcpu = kvm_getmaxcpu(kd);
127 	if (maxcpu < 0)
128 		return (-1);
129 	for (i = 0; i < CPUSTATES; i++)
130 		cp_time[i] = 0;
131 	for (i = 0; i < maxcpu; i++) {
132 		pc = kvm_getpcpu(kd, i);
133 		if (pc == NULL)
134 			continue;
135 		if (pc == (void *)-1)
136 			return (-1);
137 		for (j = 0; j < CPUSTATES; j++)
138 			cp_time[j] += pc->pc_cp_time[j];
139 		free(pc);
140 	}
141 	return (0);
142 }
143