1 | /*
|
---|
2 | * Copyright (c) 2025 Miroslav Cimerman
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup bdwrite
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <block.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <getopt.h>
|
---|
39 | #include <mem.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <abi/ipc/ipc.h>
|
---|
43 | #include <perf.h>
|
---|
44 |
|
---|
45 | static void usage(void);
|
---|
46 |
|
---|
47 | static const char usage_str[] =
|
---|
48 | "Usage: bdwrite <dev> -o <offset in blocks> -c <block count>\n"
|
---|
49 | "\n"
|
---|
50 | " Write cyclic blocks to block device.\n";
|
---|
51 |
|
---|
52 | static struct option const long_options[] = {
|
---|
53 | { 0, 0, 0, 0 }
|
---|
54 | };
|
---|
55 |
|
---|
56 | static void usage(void)
|
---|
57 | {
|
---|
58 | printf("%s", usage_str);
|
---|
59 | }
|
---|
60 |
|
---|
61 | int main(int argc, char **argv)
|
---|
62 | {
|
---|
63 | errno_t rc;
|
---|
64 | size_t bsize;
|
---|
65 | int c;
|
---|
66 | char *name = NULL;
|
---|
67 | size_t blkcnt = 0, off = 0;
|
---|
68 | service_id_t dev;
|
---|
69 |
|
---|
70 | if (argc != 6) {
|
---|
71 | goto bad;
|
---|
72 | }
|
---|
73 |
|
---|
74 | name = argv[1];
|
---|
75 |
|
---|
76 | c = 0;
|
---|
77 | optreset = 1;
|
---|
78 | optind = 0;
|
---|
79 |
|
---|
80 | while (c != -1) {
|
---|
81 | c = getopt_long(argc, argv, "o:c:", long_options, NULL);
|
---|
82 | switch (c) {
|
---|
83 | case 'o':
|
---|
84 | off = strtol(optarg, NULL, 10);
|
---|
85 | break;
|
---|
86 | case 'c':
|
---|
87 | blkcnt = strtol(optarg, NULL, 10);
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | rc = loc_service_get_id(name, &dev, 0);
|
---|
93 | if (rc != EOK) {
|
---|
94 | printf("bdwrite: error resolving device \"%s\"\n", name);
|
---|
95 | return 1;
|
---|
96 | }
|
---|
97 | rc = block_init(dev);
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf("bdwrite: error initializing block device \"%s\"\n", name);
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | rc = block_get_bsize(dev, &bsize);
|
---|
104 | if (rc != EOK) {
|
---|
105 | printf("bdwrite: error getting block size of \"%s\"\n", name);
|
---|
106 | block_fini(dev);
|
---|
107 | return 1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | uint64_t to_alloc = min(DATA_XFER_LIMIT, bsize * blkcnt);
|
---|
111 | uint8_t *buf = malloc(to_alloc);
|
---|
112 | if (buf == NULL) {
|
---|
113 | rc = ENOMEM;
|
---|
114 | goto end;
|
---|
115 | }
|
---|
116 |
|
---|
117 | stopwatch_t stopwatch;
|
---|
118 | stopwatch_init(&stopwatch);
|
---|
119 | stopwatch_start(&stopwatch);
|
---|
120 |
|
---|
121 | uint64_t left = blkcnt;
|
---|
122 | while (left != 0) {
|
---|
123 | uint64_t blks_to_write = min(to_alloc / bsize, left);
|
---|
124 | uint8_t *ptr = buf;
|
---|
125 | for (size_t i = 0; i < blks_to_write; i++) {
|
---|
126 | /* memset(ptr, (i + 1) % 0x100, bsize); */
|
---|
127 | memset(ptr, 'A' + (i % 26), bsize);
|
---|
128 | ptr += bsize;
|
---|
129 | }
|
---|
130 | rc = block_write_direct(dev, off, blks_to_write, buf);
|
---|
131 | if (rc != EOK) {
|
---|
132 | printf("bdwrite: error writing to \"%s\"\n", name);
|
---|
133 | goto end;
|
---|
134 | }
|
---|
135 | left -= blks_to_write;
|
---|
136 | off += blks_to_write;
|
---|
137 | }
|
---|
138 | end:
|
---|
139 | stopwatch_stop(&stopwatch);
|
---|
140 | nsec_t t = stopwatch_get_nanos(&stopwatch);
|
---|
141 | printf("Elapsed time:\n");
|
---|
142 | printf("\t%llu ms\n", NSEC2MSEC(t));
|
---|
143 | printf("\t%lf s\n", NSEC2SEC((double)t));
|
---|
144 | free(buf);
|
---|
145 | block_fini(dev);
|
---|
146 | return rc;
|
---|
147 | bad:
|
---|
148 | usage();
|
---|
149 | return 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /** @}
|
---|
153 | */
|
---|