Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / testspeed.c
blob042645f8d26424695c525a1607fc3c61b28e2b5d
1 /* testspeed.c - Command to test file read speed */
2 /*
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/>.
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>
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},
36 {0, 0, 0, 0, 0, 0}
39 static grub_err_t
40 grub_cmd_testspeed (grub_extcmd_context_t ctxt, int argc, char **args)
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;
51 if (argc == 0)
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;
57 if (block_size <= 0)
58 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid block size"));
60 buffer = grub_malloc (block_size);
61 if (buffer == NULL)
62 return grub_errno;
64 file = grub_file_open (args[0]);
65 if (file == NULL)
66 goto quit;
68 total_size = 0;
69 start = grub_get_time_ms ();
70 while (1)
72 grub_ssize_t size = grub_file_read (file, buffer, block_size);
73 if (size <= 0)
74 break;
75 total_size += 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"),
84 (unsigned) whole,
85 (unsigned) fraction);
87 if (end != start)
89 grub_uint64_t speed =
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));
97 quit:
98 grub_free (buffer);
100 return grub_errno;
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."),
109 options);
112 GRUB_MOD_FINI(testspeed)
114 grub_unregister_extcmd (cmd);