Skip to content

Commit 762c0b2

Browse files
trace cache operation
Can be enabled with LFS_CACHE_TRACE it will output information with LFS_DEBUG
1 parent 78b6168 commit 762c0b2

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

lfs.c

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,62 @@ enum {
2828

2929
/// Caching block device operations ///
3030

31+
enum {
32+
LFS_CACHE_TRACE_EVICT,
33+
LFS_CACHE_TRACE_BYPASS,
34+
LFS_CACHE_TRACE_DROP,
35+
};
36+
37+
#ifdef LFS_CACHE_TRACE
38+
static lfs_size_t read_cache_start_used = 0, read_cache_end_used = 0;
39+
static lfs_size_t read_cache_traffic = 0;
40+
static int read_cache_alloc_line = 0, read_cache_recache = 0;
41+
static inline void lfs_cache_trace_log(lfs_t *lfs, int op, lfs_cache_t *rcache, lfs_block_t block, lfs_off_t off, lfs_size_t size, int line)
42+
{
43+
switch (op) {
44+
case LFS_CACHE_TRACE_EVICT:
45+
case LFS_CACHE_TRACE_DROP:
46+
if (rcache->block != LFS_BLOCK_NULL) {
47+
LFS_DEBUG("cache %d:%d size %d access=[%d-%d] byte_read=%d line=%d\n",
48+
rcache->block, rcache->off, rcache->size,
49+
read_cache_start_used - rcache->off,
50+
read_cache_end_used - rcache->off,
51+
read_cache_traffic, read_cache_alloc_line);
52+
if (rcache->block == block)
53+
read_cache_recache++;
54+
else if (read_cache_recache) {
55+
LFS_DEBUG("cache recache for block %d number %d line=%d\n",
56+
rcache->block, read_cache_recache, read_cache_alloc_line);
57+
read_cache_recache = 0;
58+
}
59+
}
60+
read_cache_start_used = off + lfs->cfg->cache_size;
61+
read_cache_end_used = 0;
62+
read_cache_traffic = 0;
63+
read_cache_alloc_line = line;
64+
65+
break;
66+
case LFS_CACHE_TRACE_BYPASS:
67+
LFS_DEBUG("cache no %d:%d size %d line=%d\n", block, off, size, line);
68+
break;
69+
}
70+
}
71+
72+
static inline void lfs_cache_trace_access(lfs_cache_t *rcache, lfs_off_t off, lfs_size_t size)
73+
{
74+
(void)rcache;
75+
if (read_cache_end_used < off + size)
76+
read_cache_end_used = off + size;
77+
if (read_cache_start_used > off)
78+
read_cache_start_used = off;
79+
80+
read_cache_traffic += size;
81+
}
82+
#else
83+
#define lfs_cache_trace_log(...)
84+
#define lfs_cache_trace_access(...)
85+
#endif
86+
3187
static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) {
3288
// do not zero, cheaper if cache is readonly or only going to be
3389
// written with identical data (during relocates)
@@ -41,11 +97,13 @@ static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
4197
pcache->block = LFS_BLOCK_NULL;
4298
}
4399

44-
static int lfs_bd_read(lfs_t *lfs,
100+
#define lfs_bd_read(...) lfs_bd_read_raw(__VA_ARGS__, __LINE__)
101+
static int lfs_bd_read_raw(lfs_t *lfs,
45102
const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
46103
lfs_block_t block, lfs_off_t off,
47-
void *buffer, lfs_size_t size) {
104+
void *buffer, lfs_size_t size, int line) {
48105
uint8_t *data = buffer;
106+
(void)line;
49107
if (off+size > lfs->cfg->block_size
50108
|| (lfs->block_count && block >= lfs->block_count)) {
51109
return LFS_ERR_CORRUPT;
@@ -77,6 +135,7 @@ static int lfs_bd_read(lfs_t *lfs,
77135
// is already in rcache?
78136
diff = lfs_min(diff, rcache->size - (off-rcache->off));
79137
memcpy(data, &rcache->buffer[off-rcache->off], diff);
138+
lfs_cache_trace_access(rcache, off, diff);
80139

81140
data += diff;
82141
off += diff;
@@ -97,12 +156,15 @@ static int lfs_bd_read(lfs_t *lfs,
97156
return err;
98157
}
99158

159+
lfs_cache_trace_log(lfs, LFS_CACHE_TRACE_BYPASS, rcache, block, off, diff, line);
100160
data += diff;
101161
off += diff;
102162
size -= diff;
163+
103164
continue;
104165
}
105166

167+
lfs_cache_trace_log(lfs, LFS_CACHE_TRACE_EVICT, rcache, block, off, diff, line);
106168
// load to cache, first condition can no longer fail
107169
LFS_ASSERT(!lfs->block_count || block < lfs->block_count);
108170
rcache->block = block;

0 commit comments

Comments
 (0)