cmd_time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <common.h>
  22. #include <command.h>
  23. /*
  24. * TODO(clchiou): This function actually minics the bottom-half of the
  25. * run_command() function. Since this function has ARM-dependent timer
  26. * codes, we cannot merge it with the run_command() for now.
  27. */
  28. static int run_command_and_time_it(int flag, int argc, char * const argv[],
  29. ulong *cycles)
  30. {
  31. cmd_tbl_t *cmdtp = find_cmd(argv[0]);
  32. int retval = 0;
  33. if (!cmdtp) {
  34. printf("%s: command not found\n", argv[0]);
  35. return 1;
  36. }
  37. if (argc > cmdtp->maxargs)
  38. return CMD_RET_USAGE;
  39. /*
  40. * TODO(clchiou): get_timer_masked() is only defined in certain ARM
  41. * boards. We could use the new timer API that Graeme is proposing
  42. * so that this piece of code would be arch-independent.
  43. */
  44. *cycles = get_timer_masked();
  45. retval = cmdtp->cmd(cmdtp, flag, argc, argv);
  46. *cycles = get_timer_masked() - *cycles;
  47. return retval;
  48. }
  49. static void report_time(ulong cycles)
  50. {
  51. ulong minutes, seconds, milliseconds;
  52. ulong total_seconds, remainder;
  53. total_seconds = cycles / CONFIG_SYS_HZ;
  54. remainder = cycles % CONFIG_SYS_HZ;
  55. minutes = total_seconds / 60;
  56. seconds = total_seconds % 60;
  57. /* approximate millisecond value */
  58. milliseconds = (remainder * 1000 + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ;
  59. printf("\ntime:");
  60. if (minutes)
  61. printf(" %lu minutes,", minutes);
  62. printf(" %lu.%03lu seconds, %lu ticks\n",
  63. seconds, milliseconds, cycles);
  64. }
  65. static int do_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  66. {
  67. ulong cycles = 0;
  68. int retval = 0;
  69. if (argc == 1)
  70. return CMD_RET_USAGE;
  71. retval = run_command_and_time_it(0, argc - 1, argv + 1, &cycles);
  72. report_time(cycles);
  73. return retval;
  74. }
  75. U_BOOT_CMD(time, CONFIG_SYS_MAXARGS, 0, do_time,
  76. "run commands and summarize execution time",
  77. "command [args...]\n");