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