2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <mach/mach.h>
33 static void usage(void);
34 static void do_print(void);
35 static void do_difftime(bool usepercent
, uint64_t olduser
, uint64_t oldsystem
, uint64_t oldidle
);
36 static kern_return_t
get_processor_time(uint64_t *user
, uint64_t *sys
, uint64_t *idle
);
37 static kern_return_t
get_processor_count(int *ncpu
);
40 main(int argc
, char *argv
[])
43 const char *optu
= NULL
;
44 const char *opts
= NULL
;
45 const char *opti
= NULL
;
48 uint64_t olduser
, oldsystem
, oldidle
;
50 bool usepercent
= false;
52 while ((ch
= getopt(argc
, argv
, "Ppu:s:i:")) != -1) {
76 if (optu
|| opts
|| opti
) {
81 olduser
= strtoull(optu
, &endstr
, 0);
82 if (optu
[0] == '\0' || endstr
[0] != '\0')
87 oldsystem
= strtoull(opts
, &endstr
, 0);
88 if (opts
[0] == '\0' || endstr
[0] != '\0')
93 oldidle
= strtoull(opti
, &endstr
, 0);
94 if (opti
[0] == '\0' || endstr
[0] != '\0')
97 do_difftime(usepercent
, olduser
, oldsystem
, oldidle
);
107 kret
= get_processor_time(&olduser
, &oldsystem
, &oldidle
);
109 errx(1, "Error getting processor time: %s (%d)", mach_error_string(kret
), kret
);
111 switch(pid
= vfork()) {
119 _exit((errno
== ENOENT
) ? 127 : 126);
124 (void)signal(SIGINT
, SIG_IGN
);
125 (void)signal(SIGQUIT
, SIG_IGN
);
126 while (wait(&status
) != pid
);
128 do_difftime(usepercent
, olduser
, oldsystem
, oldidle
);
130 exit (WIFEXITED(status
) ? WEXITSTATUS(status
) : EXIT_FAILURE
);
138 fprintf(stderr
, "usage: systime [-P] utility [argument ...]\n"
140 " systime [-P] -u user -s sys -i idle\n");
147 uint64_t user
, system
, idle
;
150 kret
= get_processor_time(&user
, &system
, &idle
);
152 errx(1, "Error getting processor time: %s (%d)", mach_error_string(kret
), kret
);
154 printf("systime_user=%llu\n", user
);
155 printf("systime_sys=%llu\n", system
);
156 printf("systime_idle=%llu\n", idle
);
160 do_difftime(bool usepercent
, uint64_t olduser
, uint64_t oldsystem
, uint64_t oldidle
)
162 uint64_t user
, system
, idle
;
163 uint64_t userelapsed
, systemelapsed
, idleelapsed
, totalelapsed
;
166 kret
= get_processor_time(&user
, &system
, &idle
);
168 errx(1, "Error getting processor time: %s (%d)", mach_error_string(kret
), kret
);
170 userelapsed
= user
- olduser
;
171 systemelapsed
= system
- oldsystem
;
172 idleelapsed
= idle
- oldidle
;
173 totalelapsed
= userelapsed
+ systemelapsed
+ idleelapsed
;
176 fprintf(stderr
, "%1.02f%% user %1.02f%% sys %1.02f%% idle\n",
177 ((double)userelapsed
* 100)/totalelapsed
,
178 ((double)systemelapsed
* 100)/totalelapsed
,
179 ((double)idleelapsed
* 100)/totalelapsed
);
183 kret
= get_processor_count(&ncpu
);
185 errx(1, "Error getting processor count: %s (%d)", mach_error_string(kret
), kret
);
187 fprintf(stderr
, "%1.02f real %1.02f user %1.02f sys\n",
188 ((double)totalelapsed
) / 1000 /* ms per sec */ / ncpu
,
189 ((double)userelapsed
) / 1000,
190 ((double)systemelapsed
) / 1000);
195 get_processor_time(uint64_t *user
, uint64_t *sys
, uint64_t *idle
)
197 host_name_port_t host
;
199 host_cpu_load_info_data_t host_load
;
200 mach_msg_type_number_t count
;
202 host
= mach_host_self();
204 count
= HOST_CPU_LOAD_INFO_COUNT
;
206 kret
= host_statistics(host
, HOST_CPU_LOAD_INFO
, (host_info_t
)&host_load
, &count
);
210 *user
= ((uint64_t)host_load
.cpu_ticks
[CPU_STATE_USER
]) * 10 /* ms per tick */;
211 *sys
= ((uint64_t)host_load
.cpu_ticks
[CPU_STATE_SYSTEM
]) * 10;
212 *idle
= ((uint64_t)host_load
.cpu_ticks
[CPU_STATE_IDLE
]) * 10;
218 get_processor_count(int *ncpu
)
220 host_name_port_t host
;
222 host_basic_info_data_t hi
;
223 mach_msg_type_number_t count
;
225 host
= mach_host_self();
227 count
= HOST_BASIC_INFO_COUNT
;
229 kret
= host_info(host
, HOST_BASIC_INFO
, (host_info_t
)&hi
, &count
);
233 *ncpu
= hi
.avail_cpus
;