[bdd9e92] | 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. */
|
---|
| 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, "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 |
|
---|
[7ae01d5] | 75 | rc = block_init(svcid);
|
---|
[bdd9e92] | 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 | bench_run_start(run);
|
---|
| 109 | for (i = 0; i < size; i++) {
|
---|
| 110 | /* Generate pseudo-random block address */
|
---|
| 111 | baddr = (rand() + rand() * RAND_MAX) % (dev_nblocks - nb + 1);
|
---|
| 112 |
|
---|
| 113 | rc = block_read_direct(svcid, baddr, nb, buf);
|
---|
| 114 | if (rc != EOK) {
|
---|
| 115 | bench_run_fail(run, "failed to read blockd %llu-%llu: "
|
---|
| 116 | "%s", (unsigned long long)baddr,
|
---|
| 117 | (unsigned long long)(baddr + nb - 1),
|
---|
| 118 | str_error(errno));
|
---|
| 119 | goto error;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | bench_run_stop(run);
|
---|
| 124 | block_fini(svcid);
|
---|
| 125 | free(buf);
|
---|
| 126 |
|
---|
| 127 | return true;
|
---|
| 128 | error:
|
---|
| 129 | if (buf != NULL)
|
---|
| 130 | free(buf);
|
---|
| 131 | if (block_inited)
|
---|
| 132 | block_fini(svcid);
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | benchmark_t benchmark_rand_read = {
|
---|
| 137 | .name = "rand_read",
|
---|
| 138 | .desc = "Random disk read (must set 'disk' parameter).",
|
---|
| 139 | .entry = &runner,
|
---|
| 140 | .setup = NULL,
|
---|
| 141 | .teardown = NULL
|
---|
| 142 | };
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * @}
|
---|
| 146 | */
|
---|