source: mainline/uspace/app/hbench/disk/randread.c@ bdd9e92

Last change on this file since bdd9e92 was bdd9e92, checked in by Jiri Svoboda <jiri@…>, 18 months ago

Add random and sequential disk read benchmark

  • Property mode set to 100644
File size: 3.9 KB
Line 
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 random read benchmark. */
41static 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, "You 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, 2048);
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 * nb);
102 if (buf == NULL) {
103 bench_run_fail(run, "failed to allocate buffer (%zu bytes)",
104 block_size * nb);
105 goto error;
106 }
107
108 printf("NOTE Block size=%zu nb=%u\n", block_size, nb);
109
110 bench_run_start(run);
111 for (i = 0; i < size; i++) {
112 /* Generate pseudo-random block address */
113 baddr = (rand() + rand() * RAND_MAX) % (dev_nblocks - nb + 1);
114
115 rc = block_read_direct(svcid, baddr, nb, buf);
116 if (rc != EOK) {
117 bench_run_fail(run, "failed to read blockd %llu-%llu: "
118 "%s", (unsigned long long)baddr,
119 (unsigned long long)(baddr + nb - 1),
120 str_error(errno));
121 goto error;
122 }
123 }
124
125 bench_run_stop(run);
126 block_fini(svcid);
127 free(buf);
128
129 return true;
130error:
131 if (buf != NULL)
132 free(buf);
133 if (block_inited)
134 block_fini(svcid);
135 return false;
136}
137
138benchmark_t benchmark_rand_read = {
139 .name = "rand_read",
140 .desc = "Random disk read (must set 'disk' parameter).",
141 .entry = &runner,
142 .setup = NULL,
143 .teardown = NULL
144};
145
146/**
147 * @}
148 */
Note: See TracBrowser for help on using the repository browser.