119ea8026Sopenharmony_ci/*
219ea8026Sopenharmony_ci * Runner for littlefs benchmarks
319ea8026Sopenharmony_ci *
419ea8026Sopenharmony_ci * Copyright (c) 2022, The littlefs authors.
519ea8026Sopenharmony_ci * SPDX-License-Identifier: BSD-3-Clause
619ea8026Sopenharmony_ci */
719ea8026Sopenharmony_ci#ifndef BENCH_RUNNER_H
819ea8026Sopenharmony_ci#define BENCH_RUNNER_H
919ea8026Sopenharmony_ci
1019ea8026Sopenharmony_ci
1119ea8026Sopenharmony_ci// override LFS_TRACE
1219ea8026Sopenharmony_civoid bench_trace(const char *fmt, ...);
1319ea8026Sopenharmony_ci
1419ea8026Sopenharmony_ci#define LFS_TRACE_(fmt, ...) \
1519ea8026Sopenharmony_ci    bench_trace("%s:%d:trace: " fmt "%s\n", \
1619ea8026Sopenharmony_ci        __FILE__, \
1719ea8026Sopenharmony_ci        __LINE__, \
1819ea8026Sopenharmony_ci        __VA_ARGS__)
1919ea8026Sopenharmony_ci#define LFS_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
2019ea8026Sopenharmony_ci#define LFS_EMUBD_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
2119ea8026Sopenharmony_ci
2219ea8026Sopenharmony_ci// provide BENCH_START/BENCH_STOP macros
2319ea8026Sopenharmony_civoid bench_start(void);
2419ea8026Sopenharmony_civoid bench_stop(void);
2519ea8026Sopenharmony_ci
2619ea8026Sopenharmony_ci#define BENCH_START() bench_start()
2719ea8026Sopenharmony_ci#define BENCH_STOP() bench_stop()
2819ea8026Sopenharmony_ci
2919ea8026Sopenharmony_ci
3019ea8026Sopenharmony_ci// note these are indirectly included in any generated files
3119ea8026Sopenharmony_ci#include "bd/lfs_emubd.h"
3219ea8026Sopenharmony_ci#include <stdio.h>
3319ea8026Sopenharmony_ci
3419ea8026Sopenharmony_ci// give source a chance to define feature macros
3519ea8026Sopenharmony_ci#undef _FEATURES_H
3619ea8026Sopenharmony_ci#undef _STDIO_H
3719ea8026Sopenharmony_ci
3819ea8026Sopenharmony_ci
3919ea8026Sopenharmony_ci// generated bench configurations
4019ea8026Sopenharmony_cistruct lfs_config;
4119ea8026Sopenharmony_ci
4219ea8026Sopenharmony_cienum bench_flags {
4319ea8026Sopenharmony_ci    BENCH_REENTRANT = 0x1,
4419ea8026Sopenharmony_ci};
4519ea8026Sopenharmony_citypedef uint8_t bench_flags_t;
4619ea8026Sopenharmony_ci
4719ea8026Sopenharmony_citypedef struct bench_define {
4819ea8026Sopenharmony_ci    intmax_t (*cb)(void *data);
4919ea8026Sopenharmony_ci    void *data;
5019ea8026Sopenharmony_ci} bench_define_t;
5119ea8026Sopenharmony_ci
5219ea8026Sopenharmony_cistruct bench_case {
5319ea8026Sopenharmony_ci    const char *name;
5419ea8026Sopenharmony_ci    const char *path;
5519ea8026Sopenharmony_ci    bench_flags_t flags;
5619ea8026Sopenharmony_ci    size_t permutations;
5719ea8026Sopenharmony_ci
5819ea8026Sopenharmony_ci    const bench_define_t *defines;
5919ea8026Sopenharmony_ci
6019ea8026Sopenharmony_ci    bool (*filter)(void);
6119ea8026Sopenharmony_ci    void (*run)(struct lfs_config *cfg);
6219ea8026Sopenharmony_ci};
6319ea8026Sopenharmony_ci
6419ea8026Sopenharmony_cistruct bench_suite {
6519ea8026Sopenharmony_ci    const char *name;
6619ea8026Sopenharmony_ci    const char *path;
6719ea8026Sopenharmony_ci    bench_flags_t flags;
6819ea8026Sopenharmony_ci
6919ea8026Sopenharmony_ci    const char *const *define_names;
7019ea8026Sopenharmony_ci    size_t define_count;
7119ea8026Sopenharmony_ci
7219ea8026Sopenharmony_ci    const struct bench_case *cases;
7319ea8026Sopenharmony_ci    size_t case_count;
7419ea8026Sopenharmony_ci};
7519ea8026Sopenharmony_ci
7619ea8026Sopenharmony_ci
7719ea8026Sopenharmony_ci// deterministic prng for pseudo-randomness in benches
7819ea8026Sopenharmony_ciuint32_t bench_prng(uint32_t *state);
7919ea8026Sopenharmony_ci
8019ea8026Sopenharmony_ci#define BENCH_PRNG(state) bench_prng(state)
8119ea8026Sopenharmony_ci
8219ea8026Sopenharmony_ci
8319ea8026Sopenharmony_ci// access generated bench defines
8419ea8026Sopenharmony_ciintmax_t bench_define(size_t define);
8519ea8026Sopenharmony_ci
8619ea8026Sopenharmony_ci#define BENCH_DEFINE(i) bench_define(i)
8719ea8026Sopenharmony_ci
8819ea8026Sopenharmony_ci// a few preconfigured defines that control how benches run
8919ea8026Sopenharmony_ci
9019ea8026Sopenharmony_ci#define READ_SIZE_i          0
9119ea8026Sopenharmony_ci#define PROG_SIZE_i          1
9219ea8026Sopenharmony_ci#define ERASE_SIZE_i         2
9319ea8026Sopenharmony_ci#define ERASE_COUNT_i        3
9419ea8026Sopenharmony_ci#define BLOCK_SIZE_i         4
9519ea8026Sopenharmony_ci#define BLOCK_COUNT_i        5
9619ea8026Sopenharmony_ci#define CACHE_SIZE_i         6
9719ea8026Sopenharmony_ci#define LOOKAHEAD_SIZE_i     7
9819ea8026Sopenharmony_ci#define BLOCK_CYCLES_i       8
9919ea8026Sopenharmony_ci#define ERASE_VALUE_i        9
10019ea8026Sopenharmony_ci#define ERASE_CYCLES_i       10
10119ea8026Sopenharmony_ci#define BADBLOCK_BEHAVIOR_i  11
10219ea8026Sopenharmony_ci#define POWERLOSS_BEHAVIOR_i 12
10319ea8026Sopenharmony_ci
10419ea8026Sopenharmony_ci#define READ_SIZE           bench_define(READ_SIZE_i)
10519ea8026Sopenharmony_ci#define PROG_SIZE           bench_define(PROG_SIZE_i)
10619ea8026Sopenharmony_ci#define ERASE_SIZE          bench_define(ERASE_SIZE_i)
10719ea8026Sopenharmony_ci#define ERASE_COUNT         bench_define(ERASE_COUNT_i)
10819ea8026Sopenharmony_ci#define BLOCK_SIZE          bench_define(BLOCK_SIZE_i)
10919ea8026Sopenharmony_ci#define BLOCK_COUNT         bench_define(BLOCK_COUNT_i)
11019ea8026Sopenharmony_ci#define CACHE_SIZE          bench_define(CACHE_SIZE_i)
11119ea8026Sopenharmony_ci#define LOOKAHEAD_SIZE      bench_define(LOOKAHEAD_SIZE_i)
11219ea8026Sopenharmony_ci#define BLOCK_CYCLES        bench_define(BLOCK_CYCLES_i)
11319ea8026Sopenharmony_ci#define ERASE_VALUE         bench_define(ERASE_VALUE_i)
11419ea8026Sopenharmony_ci#define ERASE_CYCLES        bench_define(ERASE_CYCLES_i)
11519ea8026Sopenharmony_ci#define BADBLOCK_BEHAVIOR   bench_define(BADBLOCK_BEHAVIOR_i)
11619ea8026Sopenharmony_ci#define POWERLOSS_BEHAVIOR  bench_define(POWERLOSS_BEHAVIOR_i)
11719ea8026Sopenharmony_ci
11819ea8026Sopenharmony_ci#define BENCH_IMPLICIT_DEFINES \
11919ea8026Sopenharmony_ci    BENCH_DEF(READ_SIZE,          PROG_SIZE) \
12019ea8026Sopenharmony_ci    BENCH_DEF(PROG_SIZE,          ERASE_SIZE) \
12119ea8026Sopenharmony_ci    BENCH_DEF(ERASE_SIZE,         0) \
12219ea8026Sopenharmony_ci    BENCH_DEF(ERASE_COUNT,        (1024*1024)/BLOCK_SIZE) \
12319ea8026Sopenharmony_ci    BENCH_DEF(BLOCK_SIZE,         ERASE_SIZE) \
12419ea8026Sopenharmony_ci    BENCH_DEF(BLOCK_COUNT,        ERASE_COUNT/lfs_max(BLOCK_SIZE/ERASE_SIZE,1))\
12519ea8026Sopenharmony_ci    BENCH_DEF(CACHE_SIZE,         lfs_max(64,lfs_max(READ_SIZE,PROG_SIZE))) \
12619ea8026Sopenharmony_ci    BENCH_DEF(LOOKAHEAD_SIZE,     16) \
12719ea8026Sopenharmony_ci    BENCH_DEF(BLOCK_CYCLES,       -1) \
12819ea8026Sopenharmony_ci    BENCH_DEF(ERASE_VALUE,        0xff) \
12919ea8026Sopenharmony_ci    BENCH_DEF(ERASE_CYCLES,       0) \
13019ea8026Sopenharmony_ci    BENCH_DEF(BADBLOCK_BEHAVIOR,  LFS_EMUBD_BADBLOCK_PROGERROR) \
13119ea8026Sopenharmony_ci    BENCH_DEF(POWERLOSS_BEHAVIOR, LFS_EMUBD_POWERLOSS_NOOP)
13219ea8026Sopenharmony_ci
13319ea8026Sopenharmony_ci#define BENCH_GEOMETRY_DEFINE_COUNT 4
13419ea8026Sopenharmony_ci#define BENCH_IMPLICIT_DEFINE_COUNT 13
13519ea8026Sopenharmony_ci
13619ea8026Sopenharmony_ci
13719ea8026Sopenharmony_ci#endif
138