Changeset d17cf8c in mainline


Ignore:
Timestamp:
2019-01-21T13:39:37Z (6 years ago)
Author:
Vojtech Horky <vojtech.horky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
94d52d5
Parents:
e7f9a09
Message:

hbench: remove global state

Move benchmark parameters into a benchmark environment structure that is
passed to each benchmark.

Location:
uspace/app/hbench
Files:
11 edited
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/app/hbench/Makefile

    re7f9a09 rd17cf8c  
    3636        benchlist.c \
    3737        csv.c \
     38        env.c \
    3839        main.c \
    39         params.c \
    4040        fs/dirread.c \
    4141        fs/fileread.c \
  • uspace/app/hbench/doc/doxygroups.h

    re7f9a09 rd17cf8c  
    1919 * benchmark function to the benchmark_t.
    2020 *
    21  * The benchmarking function has to accept two arguments:
     21 * The benchmarking function has to accept trhee arguments:
     22 *  @li bench_env_t: benchmark environment configuration
    2223 *  @li bench_run_t: call bench_run_start and bench_run_stop around the
    2324 *      actual benchmarking code
     
    2728 * Typically, the structure of the function is following:
    2829 * @code{c}
    29  * static bool runner(bench_run_t *run, uint64_t size)
     30 * static bool runnerconst bench_env_t const *envbench_run_t *run, uint64_t size)
    3031 * {
    3132 *      bench_run_start(run);
  • uspace/app/hbench/env.c

    re7f9a09 rd17cf8c  
    3434 */
    3535
    36 #include <adt/hash_table.h>
    3736#include <stdlib.h>
    3837#include <stdio.h>
     
    9089};
    9190
    92 /** Table of extra parameters (of param_t). */
    93 static hash_table_t param_hash_table;
    94 
    95 extern errno_t bench_param_init(void)
     91extern errno_t bench_env_init(bench_env_t *env)
    9692{
    97         bool ok = hash_table_create(&param_hash_table, 0, 0, &param_hash_table_ops);
     93        bool ok = hash_table_create(&env->parameters, 0, 0, &param_hash_table_ops);
    9894        if (!ok) {
    9995                return ENOMEM;
     
    10399}
    104100
    105 extern void bench_param_cleanup(void)
     101extern void bench_env_cleanup(bench_env_t *env)
    106102{
    107         hash_table_destroy(&param_hash_table);
     103        hash_table_destroy(&env->parameters);
    108104}
    109105
    110 errno_t bench_param_set(const char *key, const char *value)
     106errno_t bench_env_param_set(bench_env_t *env, const char *key, const char *value)
    111107{
    112108        param_t *param = malloc(sizeof(param_t));
     
    126122        }
    127123
    128         hash_table_insert(&param_hash_table, &param->link);
     124        hash_table_insert(&env->parameters, &param->link);
    129125
    130126        return EOK;
    131127}
    132128
    133 const char *bench_param_get(const char *key, const char *default_value)
     129const char *bench_env_param_get(bench_env_t *env, const char *key, const char *default_value)
    134130{
    135         ht_link_t *item = hash_table_find(&param_hash_table, (char *) key);
     131        ht_link_t *item = hash_table_find(&env->parameters, (char *) key);
    136132
    137133        if (item == NULL) {
  • uspace/app/hbench/fs/dirread.c

    re7f9a09 rd17cf8c  
    4444 * that the corresponding blocks would be cached after first run.
    4545 */
    46 static bool runner(bench_run_t *run, uint64_t size)
     46static bool runner(bench_env_t *env, bench_run_t *run, uint64_t size)
    4747{
    48         const char *path = bench_param_get("dirname", "/");
     48        const char *path = bench_env_param_get(env, "dirname", "/");
    4949
    5050        bench_run_start(run);
  • uspace/app/hbench/fs/fileread.c

    re7f9a09 rd17cf8c  
    4545 * corresponding blocks would be cached after first run.
    4646 */
    47 static bool runner(bench_run_t *run, uint64_t size)
     47static bool runner(bench_env_t *env, bench_run_t *run, uint64_t size)
    4848{
    49         const char *path = bench_param_get("filename", "/data/web/helenos.png");
     49        const char *path = bench_env_param_get(env, "filename", "/data/web/helenos.png");
    5050
    5151        char *buf = malloc(BUFFER_SIZE);
  • uspace/app/hbench/hbench.h

    re7f9a09 rd17cf8c  
    3737#define HBENCH_H_
    3838
     39#include <adt/hash_table.h>
    3940#include <errno.h>
    4041#include <stdarg.h>
     
    5859        size_t error_buffer_size;
    5960} bench_run_t;
     61
     62/** Benchmark environment configuration.
     63 *
     64 * Use proper access functions when modifying data inside this structure.
     65 */
     66typedef struct {
     67        hash_table_t parameters;
     68} bench_env_t;
     69
     70typedef bool (*benchmark_entry_t)(bench_env_t *, bench_run_t *, uint64_t);
     71typedef bool (*benchmark_helper_t)(bench_env_t *, bench_run_t *);
     72
     73typedef struct {
     74        const char *name;
     75        const char *desc;
     76        benchmark_entry_t entry;
     77        benchmark_helper_t setup;
     78        benchmark_helper_t teardown;
     79} benchmark_t;
    6080
    6181static inline void bench_run_init(bench_run_t *run, char *error_buffer,
     
    87107}
    88108
    89 typedef bool (*benchmark_entry_t)(bench_run_t *, uint64_t);
    90 typedef bool (*benchmark_helper_t)(bench_run_t *);
    91 
    92 typedef struct {
    93         const char *name;
    94         const char *desc;
    95         benchmark_entry_t entry;
    96         benchmark_helper_t setup;
    97         benchmark_helper_t teardown;
    98 } benchmark_t;
    99 
    100109extern benchmark_t *benchmarks[];
    101110extern size_t benchmark_count;
     
    105114extern void csv_report_close(void);
    106115
    107 extern errno_t bench_param_init(void);
    108 extern errno_t bench_param_set(const char *, const char *);
    109 extern const char *bench_param_get(const char *, const char *);
    110 extern void bench_param_cleanup(void);
     116extern errno_t bench_env_init(bench_env_t *);
     117extern errno_t bench_env_param_set(bench_env_t *, const char *, const char *);
     118extern const char *bench_env_param_get(bench_env_t *, const char *, const char *);
     119extern void bench_env_cleanup(bench_env_t *);
    111120
    112121/* Put your benchmark descriptors here (and also to benchlist.c). */
  • uspace/app/hbench/ipc/ns_ping.c

    re7f9a09 rd17cf8c  
    3838#include "../hbench.h"
    3939
    40 static bool runner(bench_run_t *run, uint64_t niter)
     40static bool runner(bench_env_t *env, bench_run_t *run, uint64_t niter)
    4141{
    4242        bench_run_start(run);
  • uspace/app/hbench/ipc/ping_pong.c

    re7f9a09 rd17cf8c  
    4040static ipc_test_t *test = NULL;
    4141
    42 static bool setup(bench_run_t *run)
     42static bool setup(bench_env_t *env, bench_run_t *run)
    4343{
    4444        errno_t rc = ipc_test_create(&test);
     
    5252}
    5353
    54 static bool teardown(bench_run_t *run)
     54static bool teardown(bench_env_t *env, bench_run_t *run)
    5555{
    5656        ipc_test_destroy(test);
     
    5858}
    5959
    60 static bool runner(bench_run_t *run, uint64_t niter)
     60static bool runner(bench_env_t *env, bench_run_t *run, uint64_t niter)
    6161{
    6262        bench_run_start(run);
  • uspace/app/hbench/main.c

    re7f9a09 rd17cf8c  
    148148}
    149149
    150 static bool run_benchmark(benchmark_t *bench)
     150static bool run_benchmark(bench_env_t *env, benchmark_t *bench)
    151151{
    152152        printf("Warm up and determine workload size...\n");
     
    169169
    170170        if (bench->setup != NULL) {
    171                 ret = bench->setup(&helper_run);
     171                ret = bench->setup(env, &helper_run);
    172172                if (!ret) {
    173173                        goto leave_error;
     
    190190                bench_run_init(&run, error_msg, MAX_ERROR_STR_LENGTH);
    191191
    192                 bool ok = bench->entry(&run, workload_size);
     192                bool ok = bench->entry(env, &run, workload_size);
    193193                if (!ok) {
    194194                        goto leave_error;
     
    212212                bench_run_init(&runs[i], error_msg, MAX_ERROR_STR_LENGTH);
    213213
    214                 bool ok = bench->entry(&runs[i], workload_size);
     214                bool ok = bench->entry(env, &runs[i], workload_size);
    215215                if (!ok) {
    216216                        free(runs);
     
    233233leave:
    234234        if (bench->teardown != NULL) {
    235                 bool ok = bench->teardown(&helper_run);
     235                bool ok = bench->teardown(env, &helper_run);
    236236                if (!ok) {
    237237                        printf("Error: %s\n", error_msg);
     
    245245}
    246246
    247 static int run_benchmarks(void)
     247static int run_benchmarks(bench_env_t *env)
    248248{
    249249        unsigned int count_ok = 0;
     
    256256        for (size_t it = 0; it < benchmark_count; it++) {
    257257                printf("%s (%s)\n", benchmarks[it]->name, benchmarks[it]->desc);
    258                 if (run_benchmark(benchmarks[it])) {
     258                if (run_benchmark(env, benchmarks[it])) {
    259259                        count_ok++;
    260260                        continue;
     
    314314}
    315315
    316 static void handle_param_arg(char *arg)
     316static void handle_param_arg(bench_env_t *env, char *arg)
    317317{
    318318        char *value = NULL;
    319319        char *key = str_tok(arg, "=", &value);
    320         bench_param_set(key, value);
     320        bench_env_param_set(env, key, value);
    321321}
    322322
    323323int main(int argc, char *argv[])
    324324{
    325         errno_t rc = bench_param_init();
     325        bench_env_t bench_env;
     326        errno_t rc = bench_env_init(&bench_env);
    326327        if (rc != EOK) {
    327328                fprintf(stderr, "Failed to initialize internal params structure: %s\n",
     
    350351                        break;
    351352                case 'p':
    352                         handle_param_arg(optarg);
     353                        handle_param_arg(&bench_env, optarg);
    353354                        break;
    354355                case -1:
     
    378379
    379380        if (str_cmp(benchmark, "*") == 0) {
    380                 exit_code = run_benchmarks();
     381                exit_code = run_benchmarks(&bench_env);
    381382        } else {
    382383                bool benchmark_exists = false;
     
    384385                        if (str_cmp(benchmark, benchmarks[i]->name) == 0) {
    385386                                benchmark_exists = true;
    386                                 exit_code = run_benchmark(benchmarks[i]) ? 0 : -1;
     387                                exit_code = run_benchmark(&bench_env, benchmarks[i]) ? 0 : -1;
    387388                                break;
    388389                        }
     
    395396
    396397        csv_report_close();
    397         bench_param_cleanup();
     398        bench_env_cleanup(&bench_env);
    398399
    399400        return exit_code;
  • uspace/app/hbench/malloc/malloc1.c

    re7f9a09 rd17cf8c  
    3636#include "../hbench.h"
    3737
    38 static bool runner(bench_run_t *run, uint64_t size)
     38static bool runner(bench_env_t *env, bench_run_t *run, uint64_t size)
    3939{
    4040        bench_run_start(run);
  • uspace/app/hbench/malloc/malloc2.c

    re7f9a09 rd17cf8c  
    3535#include "../hbench.h"
    3636
    37 static bool runner(bench_run_t *run, uint64_t niter)
     37static bool runner(bench_env_t *env, bench_run_t *run, uint64_t niter)
    3838{
    3939        bench_run_start(run);
  • uspace/app/hbench/synch/fibril_mutex.c

    re7f9a09 rd17cf8c  
    6565}
    6666
    67 static bool runner(bench_run_t *run, uint64_t size)
     67static bool runner(bench_env_t *env, bench_run_t *run, uint64_t size)
    6868{
    6969        shared_t shared;
Note: See TracChangeset for help on using the changeset viewer.