Lines 65-78
Link Here
|
65 |
static void siginfo(int); |
65 |
static void siginfo(int); |
66 |
static void usage(void); |
66 |
static void usage(void); |
67 |
|
67 |
|
68 |
static char decimal_point; |
68 |
static char decimal_point, colon; |
69 |
static struct timeval before_tv; |
69 |
static struct timeval before_tv; |
70 |
static int hflag, pflag; |
70 |
static int aflag, cflag, hflag, lflag, pflag; |
71 |
|
71 |
|
72 |
int |
72 |
int |
73 |
main(int argc, char **argv) |
73 |
main(int argc, char **argv) |
74 |
{ |
74 |
{ |
75 |
int aflag, ch, lflag, status; |
75 |
int ch, status; |
76 |
int exitonsig; |
76 |
int exitonsig; |
77 |
pid_t pid; |
77 |
pid_t pid; |
78 |
struct rlimit rl; |
78 |
struct rlimit rl; |
Lines 80-95
Link Here
|
80 |
struct timeval after; |
80 |
struct timeval after; |
81 |
char *ofn = NULL; |
81 |
char *ofn = NULL; |
82 |
FILE *out = stderr; |
82 |
FILE *out = stderr; |
|
|
83 |
int hz; |
84 |
u_long ticks; |
83 |
|
85 |
|
|
|
86 |
|
84 |
(void) setlocale(LC_NUMERIC, ""); |
87 |
(void) setlocale(LC_NUMERIC, ""); |
85 |
decimal_point = localeconv()->decimal_point[0]; |
88 |
decimal_point = localeconv()->decimal_point[0]; |
|
|
89 |
colon = ':'; |
86 |
|
90 |
|
87 |
aflag = hflag = lflag = pflag = 0; |
91 |
aflag = cflag = hflag = lflag = pflag = 0; |
88 |
while ((ch = getopt(argc, argv, "ahlo:p")) != -1) |
92 |
while ((ch = getopt(argc, argv, "achlo:p")) != -1) |
89 |
switch((char)ch) { |
93 |
switch((char)ch) { |
90 |
case 'a': |
94 |
case 'a': |
91 |
aflag = 1; |
95 |
aflag = 1; |
92 |
break; |
96 |
break; |
|
|
97 |
case 'c': |
98 |
cflag = 1; |
99 |
break; |
93 |
case 'h': |
100 |
case 'h': |
94 |
hflag = 1; |
101 |
hflag = 1; |
95 |
break; |
102 |
break; |
Lines 137-155
Link Here
|
137 |
warnx("command terminated abnormally"); |
144 |
warnx("command terminated abnormally"); |
138 |
exitonsig = WIFSIGNALED(status) ? WTERMSIG(status) : 0; |
145 |
exitonsig = WIFSIGNALED(status) ? WTERMSIG(status) : 0; |
139 |
showtime(out, &before_tv, &after, &ru); |
146 |
showtime(out, &before_tv, &after, &ru); |
140 |
if (lflag) { |
|
|
141 |
int hz = getstathz(); |
142 |
u_long ticks; |
143 |
|
147 |
|
144 |
ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + |
148 |
/* get the number of ticks executed */ |
|
|
149 |
hz = getstathz(); |
150 |
ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + |
145 |
hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; |
151 |
hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; |
|
|
152 |
/* |
153 |
* If our round-off on the tick calculation still puts us at 0, |
154 |
* then always assume at least one tick. |
155 |
*/ |
156 |
if (ticks == 0) |
157 |
ticks = 1; |
146 |
|
158 |
|
147 |
/* |
159 |
if (lflag) { |
148 |
* If our round-off on the tick calculation still puts us at 0, |
|
|
149 |
* then always assume at least one tick. |
150 |
*/ |
151 |
if (ticks == 0) |
152 |
ticks = 1; |
153 |
|
160 |
|
154 |
fprintf(out, "%10ld %s\n", |
161 |
fprintf(out, "%10ld %s\n", |
155 |
ru.ru_maxrss, "maximum resident set size"); |
162 |
ru.ru_maxrss, "maximum resident set size"); |
Lines 179-184
Link Here
|
179 |
ru.ru_nvcsw, "voluntary context switches"); |
186 |
ru.ru_nvcsw, "voluntary context switches"); |
180 |
fprintf(out, "%10ld %s\n", |
187 |
fprintf(out, "%10ld %s\n", |
181 |
ru.ru_nivcsw, "involuntary context switches"); |
188 |
ru.ru_nivcsw, "involuntary context switches"); |
|
|
189 |
} else if (cflag) { |
190 |
int i; |
191 |
/* |
192 |
* csh prints: "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww"; |
193 |
* result in: 0.000u 0.000s 0:05.01 0.0% 0+0k 0+0io 0pf+0w |
194 |
*/ |
195 |
/* percent time spent running |
196 |
* %P The CPU percentage computed as (%U + %S) / %E. |
197 |
* do the math on ms. |
198 |
*/ |
199 |
i = ((intmax_t)ru.ru_utime.tv_sec * 1000 + ru.ru_utime.tv_usec/1000 |
200 |
+ (intmax_t)ru.ru_stime.tv_sec * 1000 + ru.ru_stime.tv_usec/1000) |
201 |
/ ((intmax_t)after.tv_sec + after.tv_usec/(1000*1000)); |
202 |
fprintf(out, "%d%c%1d%% ", i /10 , decimal_point, i % 10 ); |
203 |
/* |
204 |
* %X The average amount in (shared) text space used in Kbytes. |
205 |
* %D The average amount in (unshared) data/stack space used in |
206 |
* Kbytes. |
207 |
*/ |
208 |
fprintf(out, "%ld+%ldk ", ru.ru_ixrss / ticks, (ru.ru_idrss + ru.ru_isrss) / ticks); |
209 |
/* |
210 |
* %I The number of input operations. |
211 |
* %O The number of output operations. |
212 |
*/ |
213 |
fprintf(out, "%ld+%ldio ", ru.ru_inblock, ru.ru_oublock); |
214 |
/* |
215 |
* %W Number of times the process was swapped. |
216 |
* %F The number of major page faults (page needed to be brought |
217 |
* from disk). |
218 |
*/ |
219 |
fprintf(out, "%ldpf+%ldw", ru.ru_majflt, ru.ru_nswap); |
220 |
fprintf(out, "\n"); |
182 |
} |
221 |
} |
183 |
/* |
222 |
/* |
184 |
* If the child has exited on a signal, exit on the same |
223 |
* If the child has exited on a signal, exit on the same |
Lines 202-208
Link Here
|
202 |
usage(void) |
241 |
usage(void) |
203 |
{ |
242 |
{ |
204 |
fprintf(stderr, |
243 |
fprintf(stderr, |
205 |
"usage: time [-al] [-h | -p] [-o file] utility [argument ...]\n"); |
244 |
"usage: time [-acl] [-h | -p] [-o file] utility [argument ...]\n"); |
206 |
exit(1); |
245 |
exit(1); |
207 |
} |
246 |
} |
208 |
|
247 |
|
Lines 276-281
Link Here
|
276 |
fprintf(out, " user\t"); |
315 |
fprintf(out, " user\t"); |
277 |
humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000); |
316 |
humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000); |
278 |
fprintf(out, " sys\n"); |
317 |
fprintf(out, " sys\n"); |
|
|
318 |
} else if (cflag) { |
319 |
fprintf(out, "%jd%c%03ldu ", |
320 |
(intmax_t)ru->ru_utime.tv_sec, decimal_point, |
321 |
ru->ru_utime.tv_usec/1000); |
322 |
fprintf(out, "%jd%c%03lds ", |
323 |
(intmax_t)ru->ru_stime.tv_sec, decimal_point, |
324 |
ru->ru_stime.tv_usec/1000); |
325 |
/* realtime format is min:sec.dec */ |
326 |
fprintf(out, "%jd%c%02ld%c%02ld ", |
327 |
(intmax_t)after->tv_sec / 60, colon, |
328 |
(intmax_t)after->tv_sec % 60, decimal_point, |
329 |
after->tv_usec/1000); |
279 |
} else { |
330 |
} else { |
280 |
fprintf(out, "%9jd%c%02ld real ", |
331 |
fprintf(out, "%9jd%c%02ld real ", |
281 |
(intmax_t)after->tv_sec, decimal_point, |
332 |
(intmax_t)after->tv_sec, decimal_point, |