1 /* testspeed.c - Command to test file read speed */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2012 Free Software Foundation, Inc.
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.
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.
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/>.
21 #include <grub/file.h>
22 #include <grub/time.h>
23 #include <grub/misc.h>
25 #include <grub/extcmd.h>
26 #include <grub/i18n.h>
27 #include <grub/normal.h>
29 GRUB_MOD_LICENSE ("GPLv3+");
31 #define DEFAULT_BLOCK_SIZE 65536
33 static const struct grub_arg_option options
[] =
35 {"size", 's', 0, N_("Specify size for each read operation"), 0, ARG_TYPE_INT
},
40 grub_cmd_testspeed (grub_extcmd_context_t ctxt
, int argc
, char **args
)
42 struct grub_arg_list
*state
= ctxt
->state
;
45 grub_ssize_t block_size
;
46 grub_disk_addr_t total_size
;
49 grub_uint64_t whole
, fraction
;
52 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("filename expected"));
54 block_size
= (state
[0].set
) ?
55 grub_strtoul (state
[0].arg
, 0, 0) : DEFAULT_BLOCK_SIZE
;
58 return grub_error (GRUB_ERR_BAD_ARGUMENT
, N_("invalid block size"));
60 buffer
= grub_malloc (block_size
);
64 file
= grub_file_open (args
[0]);
69 start
= grub_get_time_ms ();
72 grub_ssize_t size
= grub_file_read (file
, buffer
, block_size
);
77 end
= grub_get_time_ms ();
78 grub_file_close (file
);
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
);
83 grub_printf_ (N_("Elapsed time: %d.%03d s \n"),
90 grub_divmod64 (total_size
* 100ULL * 1000ULL, end
- start
, 0);
92 grub_printf_ (N_("Speed: %s \n"),
93 grub_get_human_size (speed
,
94 GRUB_HUMAN_SIZE_SPEED
));
103 static grub_extcmd_t cmd
;
105 GRUB_MOD_INIT(testspeed
)
107 cmd
= grub_register_extcmd ("testspeed", grub_cmd_testspeed
, 0, N_("[-s SIZE] FILENAME"),
108 N_("Test file read speed."),
112 GRUB_MOD_FINI(testspeed
)
114 grub_unregister_extcmd (cmd
);