]> git.proxmox.com Git - grub2.git/blame - grub-core/commands/testspeed.c
tsc: Use alternative delay sources whenever appropriate.
[grub2.git] / grub-core / commands / testspeed.c
CommitLineData
fa292343
B
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
20#include <grub/mm.h>
21#include <grub/file.h>
22#include <grub/time.h>
23#include <grub/misc.h>
24#include <grub/dl.h>
25#include <grub/extcmd.h>
26#include <grub/i18n.h>
27#include <grub/normal.h>
28
29GRUB_MOD_LICENSE ("GPLv3+");
30
31#define DEFAULT_BLOCK_SIZE 65536
32
33static const struct grub_arg_option options[] =
34 {
35 {"size", 's', 0, N_("Specify size for each read operation"), 0, ARG_TYPE_INT},
36 {0, 0, 0, 0, 0, 0}
37 };
38
39static grub_err_t
40grub_cmd_testspeed (grub_extcmd_context_t ctxt, int argc, char **args)
41{
42 struct grub_arg_list *state = ctxt->state;
43 grub_uint64_t start;
44 grub_uint64_t end;
45 grub_ssize_t block_size;
46 grub_disk_addr_t total_size;
47 char *buffer;
48 grub_file_t file;
49 grub_uint64_t whole, fraction;
50
51 if (argc == 0)
52 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
53
54 block_size = (state[0].set) ?
55 grub_strtoul (state[0].arg, 0, 0) : DEFAULT_BLOCK_SIZE;
56
57 if (block_size <= 0)
58 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid block size"));
59
60 buffer = grub_malloc (block_size);
61 if (buffer == NULL)
62 return grub_errno;
63
64 file = grub_file_open (args[0]);
65 if (file == NULL)
66 goto quit;
67
68 total_size = 0;
69 start = grub_get_time_ms ();
70 while (1)
71 {
72 grub_ssize_t size = grub_file_read (file, buffer, block_size);
73 if (size <= 0)
74 break;
75 total_size += size;
76 }
77 end = grub_get_time_ms ();
78 grub_file_close (file);
79
80 grub_printf_ (N_("File size: %s\n"),
81 grub_get_human_size (total_size, GRUB_HUMAN_SIZE_NORMAL));
82 whole = grub_divmod64 (end - start, 1000, &fraction);
0dc11c08
VS
83 grub_printf_ (N_("Elapsed time: %d.%03d s \n"),
84 (unsigned) whole,
85 (unsigned) fraction);
fa292343
B
86
87 if (end != start)
88 {
89 grub_uint64_t speed =
90 grub_divmod64 (total_size * 100ULL * 1000ULL, end - start, 0);
91
92 grub_printf_ (N_("Speed: %s \n"),
93 grub_get_human_size (speed,
94 GRUB_HUMAN_SIZE_SPEED));
95 }
96
97 quit:
98 grub_free (buffer);
99
100 return grub_errno;
101}
102
103static grub_extcmd_t cmd;
104
105GRUB_MOD_INIT(testspeed)
106{
107 cmd = grub_register_extcmd ("testspeed", grub_cmd_testspeed, 0, N_("[-s SIZE] FILENAME"),
108 N_("Test file read speed."),
109 options);
110}
111
112GRUB_MOD_FINI(testspeed)
113{
114 grub_unregister_extcmd (cmd);
115}