31004e6
From 54f2bdbfd75f25d6a109d92b25879dd30e88b227 Mon Sep 17 00:00:00 2001
31004e6
From: Bean <bean123ch@gmail.com>
31004e6
Date: Sun, 5 May 2013 18:16:48 +0200
f74b50e
Subject: [PATCH 427/482] 	* grub-core/commands/testspeed.c: New command
31004e6
 testspeed.
31004e6
31004e6
---
31004e6
 ChangeLog                      |   4 ++
31004e6
 grub-core/Makefile.core.def    |   4 ++
31004e6
 grub-core/commands/ls.c        |   3 +-
31004e6
 grub-core/commands/testspeed.c | 115 +++++++++++++++++++++++++++++++++++++++++
31004e6
 grub-core/normal/misc.c        |  44 +++++++++++-----
31004e6
 include/grub/normal.h          |   9 +++-
31004e6
 6 files changed, 165 insertions(+), 14 deletions(-)
31004e6
 create mode 100644 grub-core/commands/testspeed.c
31004e6
31004e6
diff --git a/ChangeLog b/ChangeLog
31004e6
index c9e6f06..b79c57a 100644
31004e6
--- a/ChangeLog
31004e6
+++ b/ChangeLog
31004e6
@@ -1,3 +1,7 @@
31004e6
+2013-05-05  Bean  <bean123ch@gmail.com>
31004e6
+
31004e6
+	* grub-core/commands/testspeed.c: New command testspeed.
31004e6
+
31004e6
 2013-05-05  Vladimir Serbinenko  <phcoder@gmail.com>
31004e6
 
31004e6
 	Factor-out human-size printing.
31004e6
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
31004e6
index 469524f..56a1ec3 100644
31004e6
--- a/grub-core/Makefile.core.def
31004e6
+++ b/grub-core/Makefile.core.def
31004e6
@@ -2019,3 +2019,7 @@ module = {
31004e6
   enable = i386;
31004e6
 };
31004e6
 
31004e6
+module = {
31004e6
+  name = testspeed;
31004e6
+  common = commands/testspeed.c;
31004e6
+};
31004e6
diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c
31004e6
index 6c608f6..0eaf836 100644
31004e6
--- a/grub-core/commands/ls.c
31004e6
+++ b/grub-core/commands/ls.c
31004e6
@@ -141,7 +141,8 @@ print_files_long (const char *filename, const struct grub_dirhook_info *info,
31004e6
       if (! ctx->human)
31004e6
 	grub_printf ("%-12llu", (unsigned long long) file->size);
31004e6
       else
31004e6
-	grub_printf ("%-12s", grub_get_human_size (file->size, 1));
31004e6
+	grub_printf ("%-12s", grub_get_human_size (file->size,
31004e6
+						   GRUB_HUMAN_SIZE_SHORT));
31004e6
       grub_file_close (file);
31004e6
       grub_free (pathname);
31004e6
     }
31004e6
diff --git a/grub-core/commands/testspeed.c b/grub-core/commands/testspeed.c
31004e6
new file mode 100644
31004e6
index 0000000..d45fa7c
31004e6
--- /dev/null
31004e6
+++ b/grub-core/commands/testspeed.c
31004e6
@@ -0,0 +1,115 @@
31004e6
+/* testspeed.c - Command to test file read speed  */
31004e6
+/*
31004e6
+ *  GRUB  --  GRand Unified Bootloader
31004e6
+ *  Copyright (C) 2012  Free Software Foundation, Inc.
31004e6
+ *
31004e6
+ *  GRUB is free software: you can redistribute it and/or modify
31004e6
+ *  it under the terms of the GNU General Public License as published by
31004e6
+ *  the Free Software Foundation, either version 3 of the License, or
31004e6
+ *  (at your option) any later version.
31004e6
+ *
31004e6
+ *  GRUB is distributed in the hope that it will be useful,
31004e6
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
31004e6
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31004e6
+ *  GNU General Public License for more details.
31004e6
+ *
31004e6
+ *  You should have received a copy of the GNU General Public License
31004e6
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
31004e6
+ */
31004e6
+
31004e6
+#include <grub/mm.h>
31004e6
+#include <grub/file.h>
31004e6
+#include <grub/time.h>
31004e6
+#include <grub/misc.h>
31004e6
+#include <grub/dl.h>
31004e6
+#include <grub/extcmd.h>
31004e6
+#include <grub/i18n.h>
31004e6
+#include <grub/normal.h>
31004e6
+
31004e6
+GRUB_MOD_LICENSE ("GPLv3+");
31004e6
+
31004e6
+#define DEFAULT_BLOCK_SIZE	65536
31004e6
+
31004e6
+static const struct grub_arg_option options[] =
31004e6
+  {
31004e6
+    {"size", 's', 0, N_("Specify size for each read operation"), 0, ARG_TYPE_INT},
31004e6
+    {0, 0, 0, 0, 0, 0}
31004e6
+  };
31004e6
+
31004e6
+static grub_err_t
31004e6
+grub_cmd_testspeed (grub_extcmd_context_t ctxt, int argc, char **args)
31004e6
+{
31004e6
+  struct grub_arg_list *state = ctxt->state;
31004e6
+  grub_uint64_t start;
31004e6
+  grub_uint64_t end;
31004e6
+  grub_ssize_t block_size;
31004e6
+  grub_disk_addr_t total_size;
31004e6
+  char *buffer;
31004e6
+  grub_file_t file;
31004e6
+  grub_uint64_t whole, fraction;
31004e6
+
31004e6
+  if (argc == 0)
31004e6
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
31004e6
+
31004e6
+  block_size = (state[0].set) ?
31004e6
+    grub_strtoul (state[0].arg, 0, 0) : DEFAULT_BLOCK_SIZE;
31004e6
+
31004e6
+  if (block_size <= 0)
31004e6
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid block size"));
31004e6
+
31004e6
+  buffer = grub_malloc (block_size);
31004e6
+  if (buffer == NULL)
31004e6
+    return grub_errno;
31004e6
+
31004e6
+  file = grub_file_open (args[0]);
31004e6
+  if (file == NULL)
31004e6
+    goto quit;
31004e6
+
31004e6
+  total_size = 0;
31004e6
+  start = grub_get_time_ms ();
31004e6
+  while (1)
31004e6
+    {
31004e6
+      grub_ssize_t size = grub_file_read (file, buffer, block_size);
31004e6
+      if (size <= 0)
31004e6
+	break;
31004e6
+      total_size += size;
31004e6
+    }
31004e6
+  end = grub_get_time_ms ();
31004e6
+  grub_file_close (file);
31004e6
+
31004e6
+  grub_printf_ (N_("File size: %s\n"),
31004e6
+		grub_get_human_size (total_size, GRUB_HUMAN_SIZE_NORMAL));
31004e6
+  whole = grub_divmod64 (end - start, 1000, &fraction);
31004e6
+  grub_printf_ (N_("Elapsed time: %llu.%03llu s \n"),
31004e6
+		(unsigned long long) whole,
31004e6
+		(unsigned long long) fraction);
31004e6
+
31004e6
+  if (end != start)
31004e6
+    {
31004e6
+      grub_uint64_t speed =
31004e6
+	grub_divmod64 (total_size * 100ULL * 1000ULL, end - start, 0);
31004e6
+
31004e6
+      grub_printf_ (N_("Speed: %s \n"),
31004e6
+		    grub_get_human_size (speed,
31004e6
+					 GRUB_HUMAN_SIZE_SPEED));
31004e6
+    }
31004e6
+
31004e6
+ quit:
31004e6
+  grub_free (buffer);
31004e6
+
31004e6
+  return grub_errno;
31004e6
+}
31004e6
+
31004e6
+static grub_extcmd_t cmd;
31004e6
+
31004e6
+GRUB_MOD_INIT(testspeed)
31004e6
+{
31004e6
+  cmd = grub_register_extcmd ("testspeed", grub_cmd_testspeed, 0, N_("[-s SIZE] FILENAME"),
31004e6
+			      N_("Test file read speed."),
31004e6
+			      options);
31004e6
+}
31004e6
+
31004e6
+GRUB_MOD_FINI(testspeed)
31004e6
+{
31004e6
+  grub_unregister_extcmd (cmd);
31004e6
+}
31004e6
diff --git a/grub-core/normal/misc.c b/grub-core/normal/misc.c
31004e6
index 1a86e0f..bc6ce17 100644
31004e6
--- a/grub-core/normal/misc.c
31004e6
+++ b/grub-core/normal/misc.c
31004e6
@@ -28,25 +28,45 @@
31004e6
 #include <grub/i18n.h>
31004e6
 #include <grub/partition.h>
31004e6
 
31004e6
-static const char *grub_human_sizes[] = {N_("B"), N_("KiB"), N_("MiB"), N_("GiB"), N_("TiB")};
31004e6
-static const char *grub_human_short_sizes[] = {"", "K", "M", "G", "T"};
31004e6
+static const char *grub_human_sizes[3][6] =
31004e6
+  {
31004e6
+    /* This algorithm in reality would work only up to (2^64) / 100 B = 81 PiB.
31004e6
+       Put here all possible suffixes it can produce so no array bounds check
31004e6
+       is needed.
31004e6
+     */
31004e6
+    /* TRANSLATORS: that's the list of binary unit prefixes.  */
31004e6
+    { N_("B"),   N_("KiB"),   N_("MiB"),   N_("GiB"),   N_("TiB"),   N_("PiB")},
31004e6
+    /* TRANSLATORS: that's the list of binary unit prefixes.  */
31004e6
+    {    "",     N_("K"),     N_("M"),     N_("G"),     N_("T"),     N_("P") },
31004e6
+    /* TRANSLATORS: that's the list of binary unit prefixes.  */
31004e6
+    { N_("B/s"), N_("KiB/s"), N_("MiB/s"), N_("GiB/s"), N_("TiB/s"), N_("PiB/s"),  },    
31004e6
+  };
31004e6
 
31004e6
 const char *
31004e6
-grub_get_human_size (grub_uint64_t size, int sh)
31004e6
+grub_get_human_size (grub_uint64_t size, enum grub_human_size_type type)
31004e6
 {
31004e6
-  grub_uint64_t fsize = size * 100ULL;
31004e6
-  grub_uint64_t fsz = size;
31004e6
-  int units = 0;
31004e6
-  static char buf[20];
31004e6
+  grub_uint64_t fsize;
31004e6
+  unsigned units = 0;
31004e6
+  static char buf[30];
31004e6
+  const char *umsg;
31004e6
 
31004e6
-  while (fsz / 1024)
31004e6
+  if (type != GRUB_HUMAN_SIZE_SPEED)
31004e6
+    fsize = size * 100ULL;
31004e6
+  else
31004e6
+    fsize = size;
31004e6
+
31004e6
+  /* Since 2^64 / 1024^5  < 102400, this can give at most 5 iterations.
31004e6
+     So units <=5, so impossible to go past the end of array.
31004e6
+   */
31004e6
+  while (fsize >= 102400)
31004e6
     {
31004e6
       fsize = (fsize + 512) / 1024;
31004e6
-      fsz /= 1024;
31004e6
       units++;
31004e6
     }
31004e6
 
31004e6
-  if (units)
31004e6
+  umsg = _(grub_human_sizes[type][units]);
31004e6
+
31004e6
+  if (units || type == GRUB_HUMAN_SIZE_SPEED)
31004e6
     {
31004e6
       grub_uint64_t whole, fraction;
31004e6
 
31004e6
@@ -54,11 +74,11 @@ grub_get_human_size (grub_uint64_t size, int sh)
31004e6
       grub_snprintf (buf, sizeof (buf),
31004e6
 		     "%" PRIuGRUB_UINT64_T
31004e6
 		     ".%02" PRIuGRUB_UINT64_T "%s", whole, fraction,
31004e6
-		     sh ? grub_human_short_sizes[units] : _(grub_human_sizes[units]));
31004e6
+		     umsg);
31004e6
     }
31004e6
   else
31004e6
     grub_snprintf (buf, sizeof (buf), "%llu%s", (unsigned long long) size,
31004e6
-		   sh ? grub_human_short_sizes[units] : _(grub_human_sizes[units]));
31004e6
+		   umsg);
31004e6
   return buf;
31004e6
 }
31004e6
 
31004e6
diff --git a/include/grub/normal.h b/include/grub/normal.h
31004e6
index 930b3b9..c32bc96 100644
31004e6
--- a/include/grub/normal.h
31004e6
+++ b/include/grub/normal.h
31004e6
@@ -150,7 +150,14 @@ grub_dyncmd_get_cmd (grub_command_t cmd);
31004e6
 void
31004e6
 grub_gettext_reread_prefix (const char *val);
31004e6
 
31004e6
+enum grub_human_size_type
31004e6
+  {
31004e6
+    GRUB_HUMAN_SIZE_NORMAL,
31004e6
+    GRUB_HUMAN_SIZE_SHORT,
31004e6
+    GRUB_HUMAN_SIZE_SPEED,
31004e6
+  };
31004e6
+
31004e6
 const char *
31004e6
-grub_get_human_size (grub_uint64_t size, int sh);
31004e6
+grub_get_human_size (grub_uint64_t size, enum grub_human_size_type type);
31004e6
 
31004e6
 #endif /* ! GRUB_NORMAL_HEADER */
31004e6
-- 
31004e6
1.8.2.1
31004e6