1 | /*
|
---|
2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
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 hbench
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include <block.h>
|
---|
34 | #include <loc.h>
|
---|
35 | #include <str_error.h>
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include "../hbench.h"
|
---|
39 |
|
---|
40 | /** Execute disk sequential read benchmark. */
|
---|
41 | static bool runner(bench_env_t *env, bench_run_t *run, uint64_t size)
|
---|
42 | {
|
---|
43 | const char *disk;
|
---|
44 | const char *nbstr;
|
---|
45 | service_id_t svcid;
|
---|
46 | size_t block_size;
|
---|
47 | aoff64_t dev_nblocks;
|
---|
48 | aoff64_t baddr;
|
---|
49 | bool block_inited = false;
|
---|
50 | char *buf = NULL;
|
---|
51 | uint64_t i;
|
---|
52 | errno_t rc;
|
---|
53 | int nitem;
|
---|
54 | unsigned nb;
|
---|
55 |
|
---|
56 | disk = bench_env_param_get(env, "disk", NULL);
|
---|
57 | if (disk == NULL) {
|
---|
58 | bench_run_fail(run, "Yo must specify 'disk' parameter.");
|
---|
59 | goto error;
|
---|
60 | }
|
---|
61 |
|
---|
62 | nbstr = bench_env_param_get(env, "nb", "1");
|
---|
63 | nitem = sscanf(nbstr, "%u", &nb);
|
---|
64 | if (nitem < 1) {
|
---|
65 | bench_run_fail(run, "'nb' must be an integer number of blocks.");
|
---|
66 | goto error;
|
---|
67 | }
|
---|
68 |
|
---|
69 | rc = loc_service_get_id(disk, &svcid, 0);
|
---|
70 | if (rc != EOK) {
|
---|
71 | bench_run_fail(run, "failed resolving device '%s'", disk);
|
---|
72 | goto error;
|
---|
73 | }
|
---|
74 |
|
---|
75 | rc = block_init(svcid);
|
---|
76 | if (rc != EOK) {
|
---|
77 | bench_run_fail(run, "failed opening block device '%s'",
|
---|
78 | disk);
|
---|
79 | }
|
---|
80 |
|
---|
81 | block_inited = true;
|
---|
82 |
|
---|
83 | rc = block_get_bsize(svcid, &block_size);
|
---|
84 | if (rc != EOK) {
|
---|
85 | bench_run_fail(run, "error determining device block size.");
|
---|
86 | goto error;
|
---|
87 | }
|
---|
88 |
|
---|
89 | rc = block_get_nblocks(svcid, &dev_nblocks);
|
---|
90 | if (rc != EOK) {
|
---|
91 | bench_run_fail(run, "failed to obtain block device size.\n");
|
---|
92 | goto error;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (dev_nblocks < nb) {
|
---|
96 | bench_run_fail(run, "device is smaller than %u blocks.\n",
|
---|
97 | nb);
|
---|
98 | goto error;
|
---|
99 | }
|
---|
100 |
|
---|
101 | buf = malloc(block_size);
|
---|
102 | if (buf == NULL) {
|
---|
103 | bench_run_fail(run, "failed to allocate buffer (%zu bytes)",
|
---|
104 | block_size);
|
---|
105 | goto error;
|
---|
106 | }
|
---|
107 |
|
---|
108 | bench_run_start(run);
|
---|
109 | for (i = 0; i < size; i++) {
|
---|
110 | baddr = i % (dev_nblocks - nb + 1);
|
---|
111 |
|
---|
112 | rc = block_read_direct(svcid, baddr, 1, buf);
|
---|
113 | if (rc != EOK) {
|
---|
114 | bench_run_fail(run, "failed to read blocks %llu-%llu: "
|
---|
115 | "%s", (unsigned long long)baddr,
|
---|
116 | (unsigned long long)(baddr + nb - 1),
|
---|
117 | str_error(errno));
|
---|
118 | goto error;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | bench_run_stop(run);
|
---|
123 | block_fini(svcid);
|
---|
124 | free(buf);
|
---|
125 |
|
---|
126 | return true;
|
---|
127 | error:
|
---|
128 | if (buf != NULL)
|
---|
129 | free(buf);
|
---|
130 | if (block_inited)
|
---|
131 | block_fini(svcid);
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 |
|
---|
135 | benchmark_t benchmark_seq_read = {
|
---|
136 | .name = "seq_read",
|
---|
137 | .desc = "Sequential disk read (must set 'disk' parameter).",
|
---|
138 | .entry = &runner,
|
---|
139 | .setup = NULL,
|
---|
140 | .teardown = NULL
|
---|
141 | };
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * @}
|
---|
145 | */
|
---|