Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(934)

Unified Diff: common/cmd_time.c

Issue 6670118: Add time measurement command to u-boot (Closed) Base URL: ssh://gitrw.chromium.org:9222/u-boot-next.git@chromeos-v2010.09
Patch Set: Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: common/cmd_time.c
diff --git a/common/cmd_time.c b/common/cmd_time.c
new file mode 100644
index 0000000000000000000000000000000000000000..51fb3b30e1529c34b3d7af3585def57090566759
--- /dev/null
+++ b/common/cmd_time.c
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL") version 2 as published by the Free
+ * Software Foundation.
+ */
+
+/* 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.
+
+#include <common.h>
+#include <command.h>
+
+static void report_time(unsigned long int cycles)
+{
+#ifdef CONFIG_SYS_HZ
+ unsigned long int minutes, seconds, milliseconds;
+ unsigned long int r1000, ms, mshz, diff, min_diff;
+
+ seconds = cycles / CONFIG_SYS_HZ;
+ minutes = seconds / 60;
+ seconds %= 60;
+
+ /*
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 =
+ * We approxmate milliseconds value by picking the value that
+ * the difference
+ * remainder / CONFIG_SYS_HZ - milliseconds / 1000
+ * is minimal, where
+ * remainder = cycles % CONFIG_SYS_HZ.
+ *
+ * But, to avoid floating-point arithmetic or truncation of
+ * integer arithmetic, we compute
+ * remainder * 1000 - milliseconds * CONFIG_SYS_HZ
+ * instead.
+ */
+
+ /*
+ * TODO This is a simple linear search. We should make it faster
+ * if do_time() is time critical (maybe use a binary search?).
+ */
+ r1000 = (cycles % CONFIG_SYS_HZ) * 1000; /* r1000 = remainder * 1000 */
+ milliseconds = 0;
+ min_diff = r1000; /* r1000 - 0 * CONFIG_SYS_HZ = r1000 */
+ for (ms = 1; ms < 1000; ms++) {
+ mshz = ms * CONFIG_SYS_HZ;
+ diff = r1000 - mshz;
+ if (diff < min_diff) {
+ milliseconds = ms;
+ min_diff = diff;
+ }
+ }
+
+ 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
+#else
+ printf("CONFIG_SYS_HZ not defined\n");
+#endif
+
+ 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.
+}
+
+int do_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ const int target_argc = argc - 1;
+
sjg 2011/03/31 13:45:45 remove blank line
Che-Liang Chiou 2011/04/04 09:24:41 Done.
+ int retval = 0;
+ unsigned long int cycles = 0;
+ cmd_tbl_t *target_cmdtp = NULL;
+
+ if (argc == 1) {
+ 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'
+ return 128;
+ }
+
+ /* parse command */
+
sjg 2011/03/31 13:45:45 remove blank line
Che-Liang Chiou 2011/04/04 09:24:41 Done.
+ target_cmdtp = find_cmd(argv[1]);
+ if (!target_cmdtp) {
+ printf("command not found: %s\n", argv[1]);
+ return 128;
+ }
+
+ if (target_argc > target_cmdtp->maxargs) {
+ printf("maxarags exceeded: %d > %d\n", target_argc,
+ target_cmdtp->maxargs);
+ return 128;
+ }
+
+ /* run command and report run time */
+
sjg 2011/03/31 13:45:45 remove blank line
Che-Liang Chiou 2011/04/04 09:24:41 Done.
+ 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
+ retval = target_cmdtp->cmd(target_cmdtp, 0, target_argc, argv + 1);
+ cycles = get_timer_masked() - cycles;
+
+ putc('\n');
+ report_time(cycles);
+
+ return retval;
+}
+
+U_BOOT_CMD(time, CONFIG_SYS_MAXARGS, 0, do_time,
+ "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.
+ "command [args...]\n"
+ "the return value of time is the return value of "
+ "the command it executed and measured.");
« no previous file with comments | « common/Makefile ('k') | include/config_cmd_all.h » ('j') | include/configs/chromeos/developer.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698