msk(4): Unify PROMISC and ALLMULTI settings
[dragonfly.git] / test / pcpu / cpustat.c
blob8508575eb809877c301e292c806dbab329086cbe
1 /*
2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Hiten Pandya <hmp@dragonflybsd.org> and Matthew Dillon
6 * <dillon@backplane.com>
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:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * $DragonFly: src/test/pcpu/cpustat.c,v 1.1 2005/08/08 03:31:00 hmp Exp $
39 * CPUSTAT - Utility for displaying per-cpu cpu load statistics.
41 * NB: this program should be either made part of top(1) or part
42 * of a new accounting program that has the ability to display
43 * per-cpu break-up for statistics that support them.
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <kinfo.h>
53 #include <unistd.h>
55 static int numcpus = 0;
57 #define INTERVAL 1
59 static int
60 cputime_get(struct kinfo_cputime **percpu)
62 int error = 0;
63 size_t len = sizeof(struct kinfo_cputime) * numcpus;
65 if ((*percpu = malloc(len)) == NULL) {
66 error = ENOMEM;
69 /* retrieve per-cpu statistics from kernel */
70 if (error == 0) {
71 bzero(*percpu, len);
72 error = sysctlbyname("kern.cputime", *percpu, &len, NULL, 0);
73 if (error < 0) {
74 warn("sysctl: kern.cputime");
75 error = EINVAL;
79 /* cross-check size */
80 if (error == 0 && (len / sizeof(struct kinfo_cputime)) != numcpus) {
81 error = EINVAL;
84 if (error) {
85 free(*percpu);
86 *percpu = NULL;
88 return (error);
91 static void
92 cputime_get_diff(struct kinfo_cputime *old, struct kinfo_cputime *new,
93 struct kinfo_cputime *delta)
95 delta->cp_user = new->cp_user - old->cp_user;
96 delta->cp_nice = new->cp_nice - old->cp_nice;
97 delta->cp_sys = new->cp_sys - old->cp_sys;
98 delta->cp_intr = new->cp_intr - old->cp_intr;
99 delta->cp_idle = new->cp_idle - old->cp_idle;
102 static __inline uint64_t
103 cputime_get_total(struct kinfo_cputime *cpt)
105 return(
106 cpt->cp_user + cpt->cp_nice + cpt->cp_sys + cpt->cp_intr + cpt->cp_idle);
110 main(void)
112 int i, error = 0;
114 /* get number of cpus */
115 if ( kinfo_get_cpus(&numcpus) )
116 exit(-1);
118 printf("%d cpus\n", numcpus);
120 for (;;) {
121 struct kinfo_cputime *old, *new, delta;
123 error = cputime_get(&old);
124 if (error)
125 return -1;
127 sleep(INTERVAL);
129 error = cputime_get(&new);
130 if (error)
131 return -1;
133 for (i = 0; i < numcpus; ++i) {
134 uint64_t total = 0;
135 #define pct(t) (total == 0 ? 0.0 : ((double)t * 100.0 / (double)total))
136 cputime_get_diff(&old[i], &new[i], &delta);
137 total = cputime_get_total(&delta);
138 printf("CPU-%d state: ", i);
139 printf("%6.2f%% user, %6.2f%% nice, %6.2f%% sys, "
140 "%6.2f%% intr, %6.2f%% idle\n",
141 pct(delta.cp_user), pct(delta.cp_nice),
142 pct(delta.cp_sys),
143 pct(delta.cp_intr), pct(delta.cp_idle));
145 free(old);
146 free(new);