2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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
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>
55 static int numcpus = 0;
60 cputime_get(struct kinfo_cputime **percpu)
63 size_t len = sizeof(struct kinfo_cputime) * numcpus;
65 if ((*percpu = malloc(len)) == NULL) {
69 /* retrieve per-cpu statistics from kernel */
72 error = sysctlbyname("kern.cputime", *percpu, &len, NULL, 0);
74 warn("sysctl: kern.cputime");
79 /* cross-check size */
80 if (error == 0 && (len / sizeof(struct kinfo_cputime)) != numcpus) {
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)
106 cpt->cp_user + cpt->cp_nice + cpt->cp_sys + cpt->cp_intr + cpt->cp_idle);
114 /* get number of cpus */
115 if ( kinfo_get_cpus(&numcpus) )
118 printf("%d cpus\n", numcpus);
121 struct kinfo_cputime *old, *new, delta;
123 error = cputime_get(&old);
129 error = cputime_get(&new);
133 for (i = 0; i < numcpus; ++i) {
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),
143 pct(delta.cp_intr), pct(delta.cp_idle));