Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 * | |
| 6 * Alternatively, this software may be distributed under the terms of the | |
| 7 * GNU General Public License ("GPL") version 2 as published by the Free | |
| 8 * Software Foundation. | |
| 9 */ | |
| 10 | |
| 11 /* time - run command and report its run time */ | |
|
sjg
2011/03/31 13:45:45
run a command and ...
Che-Liang Chiou
2011/04/04 09:24:41
Done.
| |
| 12 | |
| 13 #include <common.h> | |
| 14 #include <command.h> | |
| 15 | |
| 16 static void report_time(unsigned long int cycles) | |
| 17 { | |
| 18 #ifdef CONFIG_SYS_HZ | |
| 19 unsigned long int minutes, seconds, milliseconds; | |
| 20 unsigned long int r1000, ms, mshz, diff, min_diff; | |
| 21 | |
| 22 seconds = cycles / CONFIG_SYS_HZ; | |
| 23 minutes = seconds / 60; | |
| 24 seconds %= 60; | |
| 25 | |
| 26 /* | |
|
sjg
2011/03/31 13:45:45
Can you just use something like:
milliseconds = c
Che-Liang Chiou
2011/04/04 09:24:41
I guess you were trying to write
milliseconds =
| |
| 27 * We approxmate milliseconds value by picking the value that | |
| 28 * the difference | |
| 29 * remainder / CONFIG_SYS_HZ - milliseconds / 1000 | |
| 30 * is minimal, where | |
| 31 * remainder = cycles % CONFIG_SYS_HZ. | |
| 32 * | |
| 33 * But, to avoid floating-point arithmetic or truncation of | |
| 34 * integer arithmetic, we compute | |
| 35 * remainder * 1000 - milliseconds * CONFIG_SYS_HZ | |
| 36 * instead. | |
| 37 */ | |
| 38 | |
| 39 /* | |
| 40 * TODO This is a simple linear search. We should make it faster | |
| 41 * if do_time() is time critical (maybe use a binary search?). | |
| 42 */ | |
| 43 r1000 = (cycles % CONFIG_SYS_HZ) * 1000; /* r1000 = remainder * 1000 */ | |
| 44 milliseconds = 0; | |
| 45 min_diff = r1000; /* r1000 - 0 * CONFIG_SYS_HZ = r1000 */ | |
| 46 for (ms = 1; ms < 1000; ms++) { | |
| 47 mshz = ms * CONFIG_SYS_HZ; | |
| 48 diff = r1000 - mshz; | |
| 49 if (diff < min_diff) { | |
| 50 milliseconds = ms; | |
| 51 min_diff = diff; | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 printf("time:\t%lum%lu.%03lus\n", minutes, seconds, milliseconds); | |
|
sjg
2011/03/31 13:45:45
It might be better to omit minutes if it is 0, and
Che-Liang Chiou
2011/04/04 09:24:41
Nice. I think it is a good idea to output differen
| |
| 56 #else | |
| 57 printf("CONFIG_SYS_HZ not defined\n"); | |
| 58 #endif | |
| 59 | |
| 60 printf("cycles:\t%lu\n", cycles); | |
|
sjg
2011/03/31 13:45:45
Perhaps put this on the same line as the 'time' ou
Che-Liang Chiou
2011/04/04 09:24:41
Done.
| |
| 61 } | |
| 62 | |
| 63 int do_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
| 64 { | |
| 65 const int target_argc = argc - 1; | |
| 66 | |
|
sjg
2011/03/31 13:45:45
remove blank line
Che-Liang Chiou
2011/04/04 09:24:41
Done.
| |
| 67 int retval = 0; | |
| 68 unsigned long int cycles = 0; | |
| 69 cmd_tbl_t *target_cmdtp = NULL; | |
| 70 | |
| 71 if (argc == 1) { | |
| 72 printf("no command provided\n"); | |
|
sjg
2011/03/31 13:45:45
Linux time prints elapsed time anyway in this case
Che-Liang Chiou
2011/04/04 09:24:41
I think we are not making a exact replica of Unix'
| |
| 73 return 128; | |
| 74 } | |
| 75 | |
| 76 /* parse command */ | |
| 77 | |
|
sjg
2011/03/31 13:45:45
remove blank line
Che-Liang Chiou
2011/04/04 09:24:41
Done.
| |
| 78 target_cmdtp = find_cmd(argv[1]); | |
| 79 if (!target_cmdtp) { | |
| 80 printf("command not found: %s\n", argv[1]); | |
| 81 return 128; | |
| 82 } | |
| 83 | |
| 84 if (target_argc > target_cmdtp->maxargs) { | |
| 85 printf("maxarags exceeded: %d > %d\n", target_argc, | |
| 86 target_cmdtp->maxargs); | |
| 87 return 128; | |
| 88 } | |
| 89 | |
| 90 /* run command and report run time */ | |
| 91 | |
|
sjg
2011/03/31 13:45:45
remove blank line
Che-Liang Chiou
2011/04/04 09:24:41
Done.
| |
| 92 cycles = get_timer_masked(); | |
|
sjg
2011/03/31 13:45:45
cycles should perhaps be time_ms?
Che-Liang Chiou
2011/04/04 09:24:41
cycles will be identical to milliseconds only if C
| |
| 93 retval = target_cmdtp->cmd(target_cmdtp, 0, target_argc, argv + 1); | |
| 94 cycles = get_timer_masked() - cycles; | |
| 95 | |
| 96 putc('\n'); | |
| 97 report_time(cycles); | |
| 98 | |
| 99 return retval; | |
| 100 } | |
| 101 | |
| 102 U_BOOT_CMD(time, CONFIG_SYS_MAXARGS, 0, do_time, | |
| 103 "run command and report its run time", | |
|
sjg
2011/03/31 13:45:45
run a command...
Che-Liang Chiou
2011/04/04 09:24:41
Done.
| |
| 104 "command [args...]\n" | |
| 105 "the return value of time is the return value of " | |
| 106 "the command it executed and measured."); | |
| OLD | NEW |