6396 remove SVM
[unleashed.git] / usr / src / cmd / stat / common / acquire.c
blob99e9ac4f179fc0a6c2ed0b856ead2d5963a621a9
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
26 #include "statcommon.h"
27 #include "dsr.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <strings.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <poll.h>
36 #define ARRAY_SIZE(a) (sizeof (a) / sizeof (*a))
39 * The time we delay before retrying after an allocation
40 * failure, in milliseconds
42 #define RETRY_DELAY 200
44 static char *cpu_states[] = {
45 "cpu_ticks_idle",
46 "cpu_ticks_user",
47 "cpu_ticks_kernel",
48 "cpu_ticks_wait"
51 static kstat_t *
52 kstat_lookup_read(kstat_ctl_t *kc, char *module,
53 int instance, char *name)
55 kstat_t *ksp = kstat_lookup(kc, module, instance, name);
56 if (ksp == NULL)
57 return (NULL);
58 if (kstat_read(kc, ksp, NULL) == -1)
59 return (NULL);
60 return (ksp);
64 * Note: the following helpers do not clean up on the failure case,
65 * because it is left to the free_snapshot() in the acquire_snapshot()
66 * failure path.
69 static int
70 acquire_cpus(struct snapshot *ss, kstat_ctl_t *kc)
72 size_t i;
74 ss->s_nr_cpus = sysconf(_SC_CPUID_MAX) + 1;
75 ss->s_cpus = calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot));
76 if (ss->s_cpus == NULL)
77 goto out;
79 for (i = 0; i < ss->s_nr_cpus; i++) {
80 kstat_t *ksp;
82 ss->s_cpus[i].cs_id = ID_NO_CPU;
83 ss->s_cpus[i].cs_state = p_online(i, P_STATUS);
84 /* If no valid CPU is present, move on to the next one */
85 if (ss->s_cpus[i].cs_state == -1)
86 continue;
87 ss->s_cpus[i].cs_id = i;
89 if ((ksp = kstat_lookup_read(kc, "cpu_info", i, NULL)) == NULL)
90 goto out;
92 (void) pset_assign(PS_QUERY, i, &ss->s_cpus[i].cs_pset_id);
93 if (ss->s_cpus[i].cs_pset_id == PS_NONE)
94 ss->s_cpus[i].cs_pset_id = ID_NO_PSET;
96 if (!CPU_ACTIVE(&ss->s_cpus[i]))
97 continue;
99 if ((ksp = kstat_lookup_read(kc, "cpu", i, "vm")) == NULL)
100 goto out;
102 if (kstat_copy(ksp, &ss->s_cpus[i].cs_vm))
103 goto out;
105 if ((ksp = kstat_lookup_read(kc, "cpu", i, "sys")) == NULL)
106 goto out;
108 if (kstat_copy(ksp, &ss->s_cpus[i].cs_sys))
109 goto out;
112 errno = 0;
113 out:
114 return (errno);
117 static int
118 acquire_psets(struct snapshot *ss)
120 psetid_t *pids = NULL;
121 struct pset_snapshot *ps;
122 size_t pids_nr;
123 size_t i, j;
126 * Careful in this code. We have to use pset_list
127 * twice, but inbetween pids_nr can change at will.
128 * We delay the setting of s_nr_psets until we have
129 * the "final" value of pids_nr.
132 if (pset_list(NULL, &pids_nr) < 0)
133 return (errno);
135 if ((pids = calloc(pids_nr, sizeof (psetid_t))) == NULL)
136 goto out;
138 if (pset_list(pids, &pids_nr) < 0)
139 goto out;
141 ss->s_psets = calloc(pids_nr + 1, sizeof (struct pset_snapshot));
142 if (ss->s_psets == NULL)
143 goto out;
144 ss->s_nr_psets = pids_nr + 1;
146 /* CPUs not in any actual pset */
147 ps = &ss->s_psets[0];
148 ps->ps_id = 0;
149 ps->ps_cpus = calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot *));
150 if (ps->ps_cpus == NULL)
151 goto out;
153 /* CPUs in a a pset */
154 for (i = 1; i < ss->s_nr_psets; i++) {
155 ps = &ss->s_psets[i];
157 ps->ps_id = pids[i - 1];
158 ps->ps_cpus =
159 calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot *));
160 if (ps->ps_cpus == NULL)
161 goto out;
164 for (i = 0; i < ss->s_nr_psets; i++) {
165 ps = &ss->s_psets[i];
167 for (j = 0; j < ss->s_nr_cpus; j++) {
168 if (!CPU_ACTIVE(&ss->s_cpus[j]))
169 continue;
170 if (ss->s_cpus[j].cs_pset_id != ps->ps_id)
171 continue;
173 ps->ps_cpus[ps->ps_nr_cpus++] = &ss->s_cpus[j];
177 errno = 0;
178 out:
179 free(pids);
180 return (errno);
183 static int
184 acquire_intrs(struct snapshot *ss, kstat_ctl_t *kc)
186 kstat_t *ksp;
187 size_t i = 0;
188 kstat_t *sys_misc;
189 kstat_named_t *clock;
191 /* clock interrupt */
192 ss->s_nr_intrs = 1;
194 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
195 if (ksp->ks_type == KSTAT_TYPE_INTR)
196 ss->s_nr_intrs++;
199 ss->s_intrs = calloc(ss->s_nr_intrs, sizeof (struct intr_snapshot));
200 if (ss->s_intrs == NULL)
201 return (errno);
203 sys_misc = kstat_lookup_read(kc, "unix", 0, "system_misc");
204 if (sys_misc == NULL)
205 goto out;
207 clock = (kstat_named_t *)kstat_data_lookup(sys_misc, "clk_intr");
208 if (clock == NULL)
209 goto out;
211 (void) strlcpy(ss->s_intrs[0].is_name, "clock", KSTAT_STRLEN);
212 ss->s_intrs[0].is_total = clock->value.ui32;
214 i = 1;
216 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
217 kstat_intr_t *ki;
218 int j;
220 if (ksp->ks_type != KSTAT_TYPE_INTR)
221 continue;
222 if (kstat_read(kc, ksp, NULL) == -1)
223 goto out;
225 ki = KSTAT_INTR_PTR(ksp);
227 (void) strlcpy(ss->s_intrs[i].is_name, ksp->ks_name,
228 KSTAT_STRLEN);
229 ss->s_intrs[i].is_total = 0;
231 for (j = 0; j < KSTAT_NUM_INTRS; j++)
232 ss->s_intrs[i].is_total += ki->intrs[j];
234 i++;
237 errno = 0;
238 out:
239 return (errno);
243 acquire_sys(struct snapshot *ss, kstat_ctl_t *kc)
245 size_t i;
246 kstat_named_t *knp;
247 kstat_t *ksp;
249 if ((ksp = kstat_lookup(kc, "unix", 0, "sysinfo")) == NULL)
250 return (errno);
252 if (kstat_read(kc, ksp, &ss->s_sys.ss_sysinfo) == -1)
253 return (errno);
255 if ((ksp = kstat_lookup(kc, "unix", 0, "vminfo")) == NULL)
256 return (errno);
258 if (kstat_read(kc, ksp, &ss->s_sys.ss_vminfo) == -1)
259 return (errno);
261 if ((ksp = kstat_lookup(kc, "unix", 0, "dnlcstats")) == NULL)
262 return (errno);
264 if (kstat_read(kc, ksp, &ss->s_sys.ss_nc) == -1)
265 return (errno);
267 if ((ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == NULL)
268 return (errno);
270 if (kstat_read(kc, ksp, NULL) == -1)
271 return (errno);
273 knp = (kstat_named_t *)kstat_data_lookup(ksp, "clk_intr");
274 if (knp == NULL)
275 return (errno);
277 ss->s_sys.ss_ticks = knp->value.l;
279 knp = (kstat_named_t *)kstat_data_lookup(ksp, "deficit");
280 if (knp == NULL)
281 return (errno);
283 ss->s_sys.ss_deficit = knp->value.l;
285 for (i = 0; i < ss->s_nr_cpus; i++) {
286 if (!CPU_ACTIVE(&ss->s_cpus[i]))
287 continue;
289 if (kstat_add(&ss->s_cpus[i].cs_sys, &ss->s_sys.ss_agg_sys))
290 return (errno);
291 if (kstat_add(&ss->s_cpus[i].cs_vm, &ss->s_sys.ss_agg_vm))
292 return (errno);
293 ss->s_nr_active_cpus++;
296 return (0);
299 struct snapshot *
300 acquire_snapshot(kstat_ctl_t *kc, int types, struct iodev_filter *iodev_filter)
302 struct snapshot *ss = NULL;
303 int err;
305 retry:
306 err = 0;
307 /* ensure any partial resources are freed on a retry */
308 free_snapshot(ss);
310 ss = safe_alloc(sizeof (struct snapshot));
312 (void) memset(ss, 0, sizeof (struct snapshot));
314 ss->s_types = types;
316 /* wait for a possibly up-to-date chain */
317 while (kstat_chain_update(kc) == -1) {
318 if (errno == EAGAIN)
319 (void) poll(NULL, 0, RETRY_DELAY);
320 else
321 fail(1, "kstat_chain_update failed");
324 if (!err && (types & SNAP_INTERRUPTS))
325 err = acquire_intrs(ss, kc);
327 if (!err && (types & (SNAP_CPUS | SNAP_SYSTEM | SNAP_PSETS)))
328 err = acquire_cpus(ss, kc);
330 if (!err && (types & SNAP_PSETS))
331 err = acquire_psets(ss);
333 if (!err && (types & (SNAP_IODEVS | SNAP_CONTROLLERS |
334 SNAP_IOPATHS_LI | SNAP_IOPATHS_LTI)))
335 err = acquire_iodevs(ss, kc, iodev_filter);
337 if (!err && (types & SNAP_SYSTEM))
338 err = acquire_sys(ss, kc);
340 switch (err) {
341 case 0:
342 break;
343 case EAGAIN:
344 (void) poll(NULL, 0, RETRY_DELAY);
345 /* a kstat disappeared from under us */
346 /*FALLTHRU*/
347 case ENXIO:
348 case ENOENT:
349 goto retry;
350 default:
351 fail(1, "acquiring snapshot failed");
354 return (ss);
357 void
358 free_snapshot(struct snapshot *ss)
360 size_t i;
362 if (ss == NULL)
363 return;
365 while (ss->s_iodevs) {
366 struct iodev_snapshot *tmp = ss->s_iodevs;
367 ss->s_iodevs = ss->s_iodevs->is_next;
368 free_iodev(tmp);
371 if (ss->s_cpus) {
372 for (i = 0; i < ss->s_nr_cpus; i++) {
373 free(ss->s_cpus[i].cs_vm.ks_data);
374 free(ss->s_cpus[i].cs_sys.ks_data);
376 free(ss->s_cpus);
379 if (ss->s_psets) {
380 for (i = 0; i < ss->s_nr_psets; i++)
381 free(ss->s_psets[i].ps_cpus);
382 free(ss->s_psets);
385 free(ss->s_sys.ss_agg_sys.ks_data);
386 free(ss->s_sys.ss_agg_vm.ks_data);
387 free(ss);
390 kstat_ctl_t *
391 open_kstat(void)
393 kstat_ctl_t *kc;
395 while ((kc = kstat_open()) == NULL) {
396 if (errno == EAGAIN)
397 (void) poll(NULL, 0, RETRY_DELAY);
398 else
399 fail(1, "kstat_open failed");
402 return (kc);
405 void *
406 safe_alloc(size_t size)
408 void *ptr;
410 while ((ptr = malloc(size)) == NULL) {
411 if (errno == EAGAIN)
412 (void) poll(NULL, 0, RETRY_DELAY);
413 else
414 fail(1, "malloc failed");
416 return (ptr);
419 char *
420 safe_strdup(char *str)
422 char *ret;
424 if (str == NULL)
425 return (NULL);
427 while ((ret = strdup(str)) == NULL) {
428 if (errno == EAGAIN)
429 (void) poll(NULL, 0, RETRY_DELAY);
430 else
431 fail(1, "malloc failed");
433 return (ret);
436 uint64_t
437 kstat_delta(kstat_t *old, kstat_t *new, char *name)
439 kstat_named_t *knew = kstat_data_lookup(new, name);
440 if (old && old->ks_data) {
441 kstat_named_t *kold = kstat_data_lookup(old, name);
442 return (knew->value.ui64 - kold->value.ui64);
444 return (knew->value.ui64);
448 kstat_copy(const kstat_t *src, kstat_t *dst)
450 *dst = *src;
452 if (src->ks_data != NULL) {
453 if ((dst->ks_data = malloc(src->ks_data_size)) == NULL)
454 return (-1);
455 bcopy(src->ks_data, dst->ks_data, src->ks_data_size);
456 } else {
457 dst->ks_data = NULL;
458 dst->ks_data_size = 0;
460 return (0);
464 kstat_add(const kstat_t *src, kstat_t *dst)
466 size_t i;
467 kstat_named_t *from;
468 kstat_named_t *to;
470 if (dst->ks_data == NULL)
471 return (kstat_copy(src, dst));
473 from = src->ks_data;
474 to = dst->ks_data;
476 for (i = 0; i < src->ks_ndata; i++) {
477 /* "addition" makes little sense for strings */
478 if (from->data_type != KSTAT_DATA_CHAR &&
479 from->data_type != KSTAT_DATA_STRING)
480 (to)->value.ui64 += (from)->value.ui64;
481 from++;
482 to++;
485 return (0);
488 uint64_t
489 cpu_ticks_delta(kstat_t *old, kstat_t *new)
491 uint64_t ticks = 0;
492 size_t i;
493 for (i = 0; i < ARRAY_SIZE(cpu_states); i++)
494 ticks += kstat_delta(old, new, cpu_states[i]);
495 return (ticks);
499 nr_active_cpus(struct snapshot *ss)
501 size_t i;
502 int count = 0;
503 for (i = 0; i < ss->s_nr_cpus; i++) {
504 if (CPU_ACTIVE(&ss->s_cpus[i]))
505 count++;
508 return (count);
512 * Return the number of ticks delta between two hrtime_t
513 * values. Attempt to cater for various kinds of overflow
514 * in hrtime_t - no matter how improbable.
516 uint64_t
517 hrtime_delta(hrtime_t old, hrtime_t new)
519 uint64_t del;
521 if ((new >= old) && (old >= 0L))
522 return (new - old);
523 else {
525 * We've overflowed the positive portion of an
526 * hrtime_t.
528 if (new < 0L) {
530 * The new value is negative. Handle the
531 * case where the old value is positive or
532 * negative.
534 uint64_t n1;
535 uint64_t o1;
537 n1 = -new;
538 if (old > 0L)
539 return (n1 - old);
540 else {
541 o1 = -old;
542 del = n1 - o1;
543 return (del);
545 } else {
547 * Either we've just gone from being negative
548 * to positive *or* the last entry was positive
549 * and the new entry is also positive but *less*
550 * than the old entry. This implies we waited
551 * quite a few days on a very fast system between
552 * iostat displays.
554 if (old < 0L) {
555 uint64_t o2;
557 o2 = -old;
558 del = UINT64_MAX - o2;
559 } else {
560 del = UINT64_MAX - old;
562 del += new;
563 return (del);