nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0427-grub-core-commands-testspeed.c-New-command-testspeed.patch

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