testspeed.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* testspeed.c - Command to test file read speed */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2012 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/mm.h>
  20. #include <grub/file.h>
  21. #include <grub/time.h>
  22. #include <grub/misc.h>
  23. #include <grub/dl.h>
  24. #include <grub/extcmd.h>
  25. #include <grub/i18n.h>
  26. #include <grub/normal.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. #define DEFAULT_BLOCK_SIZE 65536
  29. static const struct grub_arg_option options[] =
  30. {
  31. {"size", 's', 0, N_("Specify size for each read operation"), 0, ARG_TYPE_INT},
  32. {0, 0, 0, 0, 0, 0}
  33. };
  34. static grub_err_t
  35. grub_cmd_testspeed (grub_extcmd_context_t ctxt, int argc, char **args)
  36. {
  37. struct grub_arg_list *state = ctxt->state;
  38. grub_uint64_t start;
  39. grub_uint64_t end;
  40. grub_ssize_t block_size;
  41. grub_disk_addr_t total_size;
  42. char *buffer;
  43. grub_file_t file;
  44. grub_uint64_t whole, fraction;
  45. if (argc == 0)
  46. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  47. block_size = (state[0].set) ?
  48. grub_strtoul (state[0].arg, 0, 0) : DEFAULT_BLOCK_SIZE;
  49. if (block_size <= 0)
  50. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid block size"));
  51. buffer = grub_malloc (block_size);
  52. if (buffer == NULL)
  53. return grub_errno;
  54. file = grub_file_open (args[0]);
  55. if (file == NULL)
  56. goto quit;
  57. total_size = 0;
  58. start = grub_get_time_ms ();
  59. while (1)
  60. {
  61. grub_ssize_t size = grub_file_read (file, buffer, block_size);
  62. if (size <= 0)
  63. break;
  64. total_size += size;
  65. }
  66. end = grub_get_time_ms ();
  67. grub_file_close (file);
  68. grub_printf_ (N_("File size: %s\n"),
  69. grub_get_human_size (total_size, GRUB_HUMAN_SIZE_NORMAL));
  70. whole = grub_divmod64 (end - start, 1000, &fraction);
  71. grub_printf_ (N_("Elapsed time: %d.%03d s \n"),
  72. (unsigned) whole,
  73. (unsigned) fraction);
  74. if (end != start)
  75. {
  76. grub_uint64_t speed =
  77. grub_divmod64 (total_size * 100ULL * 1000ULL, end - start, 0);
  78. grub_printf_ (N_("Speed: %s \n"),
  79. grub_get_human_size (speed,
  80. GRUB_HUMAN_SIZE_SPEED));
  81. }
  82. quit:
  83. grub_free (buffer);
  84. return grub_errno;
  85. }
  86. static grub_extcmd_t cmd;
  87. GRUB_MOD_INIT(testspeed)
  88. {
  89. cmd = grub_register_extcmd ("testspeed", grub_cmd_testspeed, 0, N_("[-s SIZE] FILENAME"),
  90. N_("Test file read speed."),
  91. options);
  92. }
  93. GRUB_MOD_FINI(testspeed)
  94. {
  95. grub_unregister_extcmd (cmd);
  96. }