2 * cpu_solaric.c - module to get cpu usage, for Solaris
4 * Copyright (C) 2001 Jonathan Lang <lang@synopsys.com>
5 * Copyright (C) 2002 Seiichi SATO <ssato@sh.rim.or.jp>
7 * licensed under the GPL
21 #include <sys/cpuvar.h>
22 #include <sys/sysinfo.h>
30 /* You don't need initialization under solaris */
34 /* returns current CPU usage in percent */
36 cpu_get_usage(cpu_options
*opts
)
38 static long oldload
, oldtotal
;
39 long cpuload
, cputotal
;
51 ks
= kstat_lookup(kc
, "unix", 0, "system_misc");
52 if (kstat_read(kc
, ks
, NULL
) == -1) {
58 * Find out how many CPUs the machine has
60 kn
= kstat_data_lookup(ks
, "ncpus");
64 * Get CPU usage stats.
66 cpu_ks
= (kstat_t
**) realloc(cpu_ks
, ncpus
* sizeof(kstat_t
*));
68 (cpu_stat_t
*) realloc(cpu_stat
, ncpus
* sizeof(cpu_stat_t
));
70 for (i
= 0, ks
= kc
->kc_chain
; ks
; ks
= ks
->ks_next
) {
71 if (strncmp(ks
->ks_name
, "cpu_stat", 8) == 0)
75 for (i
= 0; i
< ncpus
; ++i
)
76 (void) kstat_read(kc
, cpu_ks
[i
], &cpu_stat
[i
]);
79 * Sum the times for the various non-idle CPU_STATES, for Solaris the
87 for (cpuload
= 0, i
= 0; i
< ncpus
; ++i
) {
88 cpuload
+= (long) cpu_stat
[i
].cpu_sysinfo
.cpu
[CPU_USER
];
89 cpuload
+= (long) cpu_stat
[i
].cpu_sysinfo
.cpu
[CPU_KERNEL
];
90 cpuload
+= (long) cpu_stat
[i
].cpu_sysinfo
.cpu
[CPU_WAIT
];
96 for (cputotal
= cpuload
, i
= 0; i
< ncpus
; ++i
)
97 cputotal
+= (long) cpu_stat
[i
].cpu_sysinfo
.cpu
[CPU_IDLE
];
103 } else if ((cputotal
- oldtotal
) > 0) {
104 result
= (100 * (double) (cpuload
- oldload
)) /
105 (double) (cputotal
- oldtotal
);
106 } else if (cputotal
== oldtotal
) {