1 /*
2  * Runner for littlefs benchmarks
3  *
4  * Copyright (c) 2022, The littlefs authors.
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef BENCH_RUNNER_H
8 #define BENCH_RUNNER_H
9 
10 
11 // override LFS_TRACE
12 void bench_trace(const char *fmt, ...);
13 
14 #define LFS_TRACE_(fmt, ...) \
15     bench_trace("%s:%d:trace: " fmt "%s\n", \
16         __FILE__, \
17         __LINE__, \
18         __VA_ARGS__)
19 #define LFS_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
20 #define LFS_EMUBD_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
21 
22 // provide BENCH_START/BENCH_STOP macros
23 void bench_start(void);
24 void bench_stop(void);
25 
26 #define BENCH_START() bench_start()
27 #define BENCH_STOP() bench_stop()
28 
29 
30 // note these are indirectly included in any generated files
31 #include "bd/lfs_emubd.h"
32 #include <stdio.h>
33 
34 // give source a chance to define feature macros
35 #undef _FEATURES_H
36 #undef _STDIO_H
37 
38 
39 // generated bench configurations
40 struct lfs_config;
41 
42 enum bench_flags {
43     BENCH_REENTRANT = 0x1,
44 };
45 typedef uint8_t bench_flags_t;
46 
47 typedef struct bench_define {
48     intmax_t (*cb)(void *data);
49     void *data;
50 } bench_define_t;
51 
52 struct bench_case {
53     const char *name;
54     const char *path;
55     bench_flags_t flags;
56     size_t permutations;
57 
58     const bench_define_t *defines;
59 
60     bool (*filter)(void);
61     void (*run)(struct lfs_config *cfg);
62 };
63 
64 struct bench_suite {
65     const char *name;
66     const char *path;
67     bench_flags_t flags;
68 
69     const char *const *define_names;
70     size_t define_count;
71 
72     const struct bench_case *cases;
73     size_t case_count;
74 };
75 
76 
77 // deterministic prng for pseudo-randomness in benches
78 uint32_t bench_prng(uint32_t *state);
79 
80 #define BENCH_PRNG(state) bench_prng(state)
81 
82 
83 // access generated bench defines
84 intmax_t bench_define(size_t define);
85 
86 #define BENCH_DEFINE(i) bench_define(i)
87 
88 // a few preconfigured defines that control how benches run
89 
90 #define READ_SIZE_i          0
91 #define PROG_SIZE_i          1
92 #define ERASE_SIZE_i         2
93 #define ERASE_COUNT_i        3
94 #define BLOCK_SIZE_i         4
95 #define BLOCK_COUNT_i        5
96 #define CACHE_SIZE_i         6
97 #define LOOKAHEAD_SIZE_i     7
98 #define BLOCK_CYCLES_i       8
99 #define ERASE_VALUE_i        9
100 #define ERASE_CYCLES_i       10
101 #define BADBLOCK_BEHAVIOR_i  11
102 #define POWERLOSS_BEHAVIOR_i 12
103 
104 #define READ_SIZE           bench_define(READ_SIZE_i)
105 #define PROG_SIZE           bench_define(PROG_SIZE_i)
106 #define ERASE_SIZE          bench_define(ERASE_SIZE_i)
107 #define ERASE_COUNT         bench_define(ERASE_COUNT_i)
108 #define BLOCK_SIZE          bench_define(BLOCK_SIZE_i)
109 #define BLOCK_COUNT         bench_define(BLOCK_COUNT_i)
110 #define CACHE_SIZE          bench_define(CACHE_SIZE_i)
111 #define LOOKAHEAD_SIZE      bench_define(LOOKAHEAD_SIZE_i)
112 #define BLOCK_CYCLES        bench_define(BLOCK_CYCLES_i)
113 #define ERASE_VALUE         bench_define(ERASE_VALUE_i)
114 #define ERASE_CYCLES        bench_define(ERASE_CYCLES_i)
115 #define BADBLOCK_BEHAVIOR   bench_define(BADBLOCK_BEHAVIOR_i)
116 #define POWERLOSS_BEHAVIOR  bench_define(POWERLOSS_BEHAVIOR_i)
117 
118 #define BENCH_IMPLICIT_DEFINES \
119     BENCH_DEF(READ_SIZE,          PROG_SIZE) \
120     BENCH_DEF(PROG_SIZE,          ERASE_SIZE) \
121     BENCH_DEF(ERASE_SIZE,         0) \
122     BENCH_DEF(ERASE_COUNT,        (1024*1024)/BLOCK_SIZE) \
123     BENCH_DEF(BLOCK_SIZE,         ERASE_SIZE) \
124     BENCH_DEF(BLOCK_COUNT,        ERASE_COUNT/lfs_max(BLOCK_SIZE/ERASE_SIZE,1))\
125     BENCH_DEF(CACHE_SIZE,         lfs_max(64,lfs_max(READ_SIZE,PROG_SIZE))) \
126     BENCH_DEF(LOOKAHEAD_SIZE,     16) \
127     BENCH_DEF(BLOCK_CYCLES,       -1) \
128     BENCH_DEF(ERASE_VALUE,        0xff) \
129     BENCH_DEF(ERASE_CYCLES,       0) \
130     BENCH_DEF(BADBLOCK_BEHAVIOR,  LFS_EMUBD_BADBLOCK_PROGERROR) \
131     BENCH_DEF(POWERLOSS_BEHAVIOR, LFS_EMUBD_POWERLOSS_NOOP)
132 
133 #define BENCH_GEOMETRY_DEFINE_COUNT 4
134 #define BENCH_IMPLICIT_DEFINE_COUNT 13
135 
136 
137 #endif
138