Che-liang Chiou | fd76300 | 2011-10-06 23:40:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 3 | * See file CREDITS for list of people who contributed to this |
| 4 | * project. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | * MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <common.h> |
| 23 | #include <command.h> |
| 24 | |
| 25 | /* |
| 26 | * TODO(clchiou): This function actually minics the bottom-half of the |
| 27 | * run_command() function. Since this function has ARM-dependent timer |
| 28 | * codes, we cannot merge it with the run_command() for now. |
| 29 | */ |
| 30 | static int run_command_and_time_it(int flag, int argc, char * const argv[], |
| 31 | ulong *cycles) |
| 32 | { |
| 33 | cmd_tbl_t *cmdtp = find_cmd(argv[0]); |
| 34 | int retval = 0; |
| 35 | |
| 36 | if (!cmdtp) { |
| 37 | printf("%s: command not found\n", argv[0]); |
| 38 | return 1; |
| 39 | } |
| 40 | if (argc > cmdtp->maxargs) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 41 | return CMD_RET_USAGE; |
Che-liang Chiou | fd76300 | 2011-10-06 23:40:48 +0000 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * TODO(clchiou): get_timer_masked() is only defined in certain ARM |
| 45 | * boards. We could use the new timer API that Graeme is proposing |
| 46 | * so that this piece of code would be arch-independent. |
| 47 | */ |
| 48 | *cycles = get_timer_masked(); |
| 49 | retval = cmdtp->cmd(cmdtp, flag, argc, argv); |
| 50 | *cycles = get_timer_masked() - *cycles; |
| 51 | |
| 52 | return retval; |
| 53 | } |
| 54 | |
| 55 | static void report_time(ulong cycles) |
| 56 | { |
| 57 | ulong minutes, seconds, milliseconds; |
| 58 | ulong total_seconds, remainder; |
| 59 | |
| 60 | total_seconds = cycles / CONFIG_SYS_HZ; |
| 61 | remainder = cycles % CONFIG_SYS_HZ; |
| 62 | minutes = total_seconds / 60; |
| 63 | seconds = total_seconds % 60; |
| 64 | /* approximate millisecond value */ |
| 65 | milliseconds = (remainder * 1000 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ; |
| 66 | |
| 67 | printf("\ntime:"); |
| 68 | if (minutes) |
| 69 | printf(" %lu minutes,", minutes); |
| 70 | printf(" %lu.%03lu seconds, %lu ticks\n", |
| 71 | seconds, milliseconds, cycles); |
| 72 | } |
| 73 | |
| 74 | static int do_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 75 | { |
| 76 | ulong cycles = 0; |
| 77 | int retval = 0; |
| 78 | |
| 79 | if (argc == 1) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 80 | return CMD_RET_USAGE; |
Che-liang Chiou | fd76300 | 2011-10-06 23:40:48 +0000 | [diff] [blame] | 81 | |
| 82 | retval = run_command_and_time_it(0, argc - 1, argv + 1, &cycles); |
| 83 | report_time(cycles); |
| 84 | |
| 85 | return retval; |
| 86 | } |
| 87 | |
| 88 | U_BOOT_CMD(time, CONFIG_SYS_MAXARGS, 0, do_time, |
| 89 | "run commands and summarize execution time", |
| 90 | "command [args...]\n"); |