[c2db02a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Vojtech Horky
|
---|
| 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 <fibril_synch.h>
|
---|
| 34 | #include <stdatomic.h>
|
---|
| 35 | #include "../hbench.h"
|
---|
| 36 |
|
---|
| 37 | /*
|
---|
| 38 | * Simple benchmark for fibril mutexes. There are two fibrils that compete
|
---|
| 39 | * over the same mutex as that is the simplest scenario.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | typedef struct {
|
---|
| 43 | fibril_mutex_t mutex;
|
---|
| 44 | uint64_t counter;
|
---|
| 45 | atomic_bool done;
|
---|
| 46 | } shared_t;
|
---|
| 47 |
|
---|
| 48 | static errno_t competitor(void *arg)
|
---|
| 49 | {
|
---|
| 50 | shared_t *shared = arg;
|
---|
| 51 | fibril_detach(fibril_get_id());
|
---|
| 52 |
|
---|
| 53 | while (true) {
|
---|
| 54 | fibril_mutex_lock(&shared->mutex);
|
---|
| 55 | uint64_t local = shared->counter;
|
---|
| 56 | fibril_mutex_unlock(&shared->mutex);
|
---|
| 57 | if (local == 0) {
|
---|
| 58 | break;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | atomic_store(&shared->done, true);
|
---|
| 63 |
|
---|
| 64 | return EOK;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[d17cf8c] | 67 | static bool runner(bench_env_t *env, bench_run_t *run, uint64_t size)
|
---|
[c2db02a] | 68 | {
|
---|
| 69 | shared_t shared;
|
---|
| 70 | fibril_mutex_initialize(&shared.mutex);
|
---|
| 71 | shared.counter = size;
|
---|
| 72 | atomic_store(&shared.done, false);
|
---|
| 73 |
|
---|
| 74 | fid_t other = fibril_create(competitor, &shared);
|
---|
| 75 | fibril_add_ready(other);
|
---|
| 76 |
|
---|
[e7f9a09] | 77 | bench_run_start(run);
|
---|
[c2db02a] | 78 | for (uint64_t i = 0; i < size; i++) {
|
---|
| 79 | fibril_mutex_lock(&shared.mutex);
|
---|
| 80 | shared.counter--;
|
---|
| 81 | fibril_mutex_unlock(&shared.mutex);
|
---|
| 82 | }
|
---|
[e7f9a09] | 83 | bench_run_stop(run);
|
---|
[c2db02a] | 84 |
|
---|
| 85 | while (!atomic_load(&shared.done)) {
|
---|
| 86 | fibril_yield();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | return true;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | benchmark_t benchmark_fibril_mutex = {
|
---|
| 93 | .name = "fibril_mutex",
|
---|
| 94 | .desc = "Speed of mutex lock/unlock operations",
|
---|
| 95 | .entry = &runner,
|
---|
| 96 | .setup = NULL,
|
---|
| 97 | .teardown = NULL
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | /** @}
|
---|
| 101 | */
|
---|