xref: /illumos-gate/usr/src/cmd/mdb/common/modules/genunix/cpupart.c (revision 71269a2275bf5a143dad6461eee2710a344e7261)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 
30 #include <mdb/mdb_param.h>
31 #include <mdb/mdb_modapi.h>
32 #include <mdb/mdb_ks.h>
33 
34 #include "lgrp.h"
35 #include "cpupart_mdb.h"
36 
37 #include <sys/cpuvar.h>
38 #include <sys/cpupart.h>
39 
40 /* ARGSUSED */
41 static int
42 cpupart_cpulist_callback(uintptr_t addr, const void *arg, void *cb_data)
43 {
44 	cpu_t *cpu = (cpu_t *)arg;
45 
46 	ulong_t *cpuset = cb_data;
47 
48 	BT_SET(cpuset, cpu->cpu_id);
49 
50 	return (WALK_NEXT);
51 }
52 
53 #define	CPUPART_IDWIDTH		3
54 
55 #ifdef _LP64
56 #define	CPUPART_CPUWIDTH	21
57 #if defined(__amd64)
58 #define	CPUPART_TWIDTH		16
59 #else
60 #define	CPUPART_TWIDTH		11
61 #endif
62 #else
63 #define	CPUPART_CPUWIDTH	13
64 #define	CPUPART_TWIDTH		8
65 #endif
66 
67 
68 #define	CPUPART_THRDELT		(CPUPART_IDWIDTH + CPUPART_CPUWIDTH)
69 #define	CPUPART_INDENT		mdb_printf("%*s", CPUPART_THRDELT, "")
70 
71 int
72 cpupart_disp_threads(disp_t *disp)
73 {
74 	dispq_t	*dq;
75 	int i, npri = disp->disp_npri;
76 	proc_t p;
77 	kthread_t t;
78 
79 	dq = mdb_alloc(sizeof (dispq_t) * npri, UM_SLEEP | UM_GC);
80 
81 	if (mdb_vread(dq, sizeof (dispq_t) * npri,
82 	    (uintptr_t)disp->disp_q) == -1) {
83 		mdb_warn("failed to read dispq_t at %p", disp->disp_q);
84 		return (DCMD_ERR);
85 	}
86 
87 	CPUPART_INDENT;
88 	mdb_printf("|\n");
89 	CPUPART_INDENT;
90 	mdb_printf("+-->  %3s %-*s %s\n", "PRI", CPUPART_TWIDTH, "THREAD",
91 	    "PROC");
92 
93 	for (i = npri - 1; i >= 0; i--) {
94 		uintptr_t taddr = (uintptr_t)dq[i].dq_first;
95 
96 		while (taddr != NULL) {
97 			if (mdb_vread(&t, sizeof (t), taddr) == -1) {
98 				mdb_warn("failed to read kthread_t at %p",
99 				    taddr);
100 				return (DCMD_ERR);
101 			}
102 
103 			if (mdb_vread(&p, sizeof (p),
104 			    (uintptr_t)t.t_procp) == -1) {
105 				mdb_warn("failed to read proc_t at %p",
106 				    t.t_procp);
107 				return (DCMD_ERR);
108 			}
109 
110 			CPUPART_INDENT;
111 			mdb_printf("%9d %0*p %s\n", t.t_pri, CPUPART_TWIDTH,
112 			    taddr, p.p_user.u_comm);
113 
114 			taddr = (uintptr_t)t.t_link;
115 		}
116 	}
117 
118 	return (DCMD_OK);
119 }
120 
121 /* ARGSUSED */
122 int
123 cpupart(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
124 {
125 	cpupart_t cpupart;
126 	int cpusetsize;
127 	int _ncpu;
128 	ulong_t *cpuset;
129 	uint_t verbose = FALSE;
130 
131 	if (mdb_getopts(argc, argv,
132 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
133 		return (DCMD_USAGE);
134 
135 	if (!(flags & DCMD_ADDRSPEC)) {
136 		if (mdb_walk_dcmd("cpupart_walk", "cpupart", argc, argv)
137 		    == -1) {
138 			mdb_warn("can't walk 'cpupart'");
139 			return (DCMD_ERR);
140 		}
141 		return (DCMD_OK);
142 	}
143 
144 	if (DCMD_HDRSPEC(flags)) {
145 		mdb_printf("%3s %?s %4s %4s %4s\n",
146 		    "ID",
147 		    "ADDR",
148 		    "NRUN",
149 		    "#CPU",
150 		    "CPUS");
151 	}
152 
153 	if (mdb_vread(&cpupart, sizeof (cpupart_t), addr) == -1) {
154 		mdb_warn("unable to read 'cpupart_t' at %p", addr);
155 		return (DCMD_ERR);
156 	}
157 
158 	mdb_printf("%3d %?p %4d %4d ",
159 	    cpupart.cp_id,
160 	    addr,
161 	    cpupart.cp_kp_queue.disp_nrunnable,
162 	    cpupart.cp_ncpus);
163 
164 	if (cpupart.cp_ncpus == 0) {
165 		mdb_printf("\n");
166 		return (DCMD_OK);
167 	}
168 
169 	/*
170 	 * figure out what cpus we've got
171 	 */
172 	if (mdb_readsym(&_ncpu, sizeof (int), "_ncpu") == -1) {
173 		mdb_warn("symbol '_ncpu' not found");
174 		return (DCMD_ERR);
175 	}
176 
177 	/*
178 	 * allocate enough space for set of longs to hold cpuid bitfield
179 	 */
180 
181 	cpusetsize = BT_BITOUL(_ncpu) * sizeof (ulong_t);
182 	cpuset = mdb_zalloc(cpusetsize, UM_SLEEP | UM_GC);
183 
184 	if (mdb_pwalk("cpupart_cpulist", cpupart_cpulist_callback, cpuset,
185 	    addr) == -1) {
186 		mdb_warn("unable to walk cpupart_cpulist");
187 		return (DCMD_ERR);
188 	}
189 
190 	print_cpuset_range(cpuset, cpusetsize/sizeof (ulong_t), 0);
191 
192 	mdb_printf("\n");
193 	/*
194 	 * If there are any threads on kp queue and -v is specified
195 	 */
196 	if (verbose && cpupart.cp_kp_queue.disp_nrunnable) {
197 		if (cpupart_disp_threads(&cpupart.cp_kp_queue) != DCMD_OK)
198 			return (DCMD_ERR);
199 	}
200 
201 	return (DCMD_OK);
202 }
203 
204 typedef struct cpupart_cpulist_walk {
205 	uintptr_t	ccw_firstcpu;
206 	int 		ccw_cpusleft;
207 } cpupart_cpulist_walk_t;
208 
209 int
210 cpupart_cpulist_walk_init(mdb_walk_state_t *wsp)
211 {
212 	cpupart_cpulist_walk_t *ccw;
213 	cpupart_t cpupart;
214 
215 	ccw = mdb_alloc(sizeof (cpupart_cpulist_walk_t), UM_SLEEP | UM_GC);
216 
217 	if (mdb_vread(&cpupart, sizeof (cpupart_t), wsp->walk_addr) == -1) {
218 		mdb_warn("couldn't read 'cpupart' at %p", wsp->walk_addr);
219 		return (WALK_ERR);
220 	}
221 
222 	ccw->ccw_firstcpu = (uintptr_t)cpupart.cp_cpulist;
223 	ccw->ccw_cpusleft = cpupart.cp_ncpus;
224 
225 	wsp->walk_data = ccw;
226 	wsp->walk_addr = ccw->ccw_firstcpu;
227 
228 	return (WALK_NEXT);
229 }
230 
231 int
232 cpupart_cpulist_walk_step(mdb_walk_state_t *wsp)
233 {
234 	cpupart_cpulist_walk_t *ccw = (cpupart_cpulist_walk_t *)
235 	    wsp->walk_data;
236 	uintptr_t addr = wsp->walk_addr;
237 	cpu_t cpu;
238 	int status;
239 
240 	if (mdb_vread(&cpu, sizeof (cpu_t), addr) == -1) {
241 		mdb_warn("couldn't read 'cpupart' at %p", addr);
242 		return (WALK_ERR);
243 	}
244 
245 	status = wsp->walk_callback(addr, &cpu, wsp->walk_cbdata);
246 
247 	if (status != WALK_NEXT)
248 		return (status);
249 
250 	addr = (uintptr_t)cpu.cpu_next_part;
251 	wsp->walk_addr = addr;
252 
253 	ccw->ccw_cpusleft--;
254 
255 	if (ccw->ccw_cpusleft < 0) {
256 		mdb_warn("cpu count doesn't match cpupart list");
257 		return (WALK_ERR);
258 	}
259 
260 	if (ccw->ccw_firstcpu == addr) {
261 		if (ccw->ccw_cpusleft != 0) {
262 			mdb_warn("cpu count doesn't match cpupart list");
263 			return (WALK_ERR);
264 		}
265 		return (WALK_DONE);
266 	}
267 
268 	return (WALK_NEXT);
269 }
270 
271 int
272 cpupart_walk_init(mdb_walk_state_t *wsp)
273 {
274 	GElf_Sym sym;
275 	uintptr_t addr;
276 
277 	if (mdb_lookup_by_name("cp_default", &sym) == -1) {
278 		mdb_warn("failed to find 'cp_default'\n");
279 		return (WALK_ERR);
280 	}
281 
282 	addr = (uintptr_t)sym.st_value;
283 	wsp->walk_data = (void *)addr;
284 	wsp->walk_addr = addr;
285 
286 	return (WALK_NEXT);
287 }
288 
289 int
290 cpupart_walk_step(mdb_walk_state_t *wsp)
291 {
292 	cpupart_t cpupart;
293 	int status;
294 
295 	if (mdb_vread(&cpupart, sizeof (cpupart_t),
296 	    wsp->walk_addr) == -1) {
297 		mdb_warn("unable to read cpupart at %p",
298 		    wsp->walk_addr);
299 		return (WALK_ERR);
300 	}
301 
302 	status = wsp->walk_callback(wsp->walk_addr, &cpupart,
303 	    wsp->walk_cbdata);
304 
305 	if (status != WALK_NEXT)
306 		return (status);
307 
308 	wsp->walk_addr = (uintptr_t)cpupart.cp_next;
309 
310 	if (wsp->walk_addr == (uintptr_t)wsp->walk_data)
311 		return (WALK_DONE);
312 
313 	return (WALK_NEXT);
314 
315 }
316