Skip to content

Commit

Permalink
Fix bug in zero cache "current_cache_size" stats value (issue #204).
Browse files Browse the repository at this point in the history
  • Loading branch information
archiecobbs committed Jan 27, 2023
1 parent aef39af commit 571b09d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGES
Expand Up @@ -2,7 +2,8 @@ Version Next

- Use fallocate(FALLOC_FL_PUNCH_HOLE) for empty disk cache blocks (issue #200)
- Made sync(1) work on s3backer file to flush cached data (issue #197)
- Fix bug where s3b_compress=deflate NDB flag would fail (issue #195)
- Fixed bug where s3b_compress=deflate NDB flag would fail (issue #195)
- Fixed bug in zero cache "current_cache_size" stats value (issue #204)
- Reuse cURL handles after normal HTTP error codes (issue #199)
- Run modprobe(8) if needed when starting with the --nbd flag (issue #203)

Expand Down
2 changes: 1 addition & 1 deletion s3backer.h
Expand Up @@ -120,7 +120,7 @@ extern char **environ;
typedef uint32_t s3b_block_t;

// Integral type used for bitmaps
typedef uintptr_t bitmap_t;
typedef uint32_t bitmap_t;

/*
* How many hex digits we will use to print a block number.
Expand Down
23 changes: 23 additions & 0 deletions util.c
Expand Up @@ -267,6 +267,19 @@ bitmap_or(bitmap_t *dst, const bitmap_t *src, s3b_block_t num_blocks)
dst[i] |= src[i];
}

size_t
bitmap_or2(bitmap_t *dst, const bitmap_t *src, s3b_block_t num_blocks)
{
const size_t nwords = bitmap_size(num_blocks);
size_t count = 0;
size_t i;

assert(sizeof(bitmap_t) == 4);
for (i = 0; i < nwords; i++)
count += popcount32(dst[i] |= src[i]);
return count;
}

void
bitmap_not(bitmap_t *bitmap, s3b_block_t num_blocks)
{
Expand All @@ -277,6 +290,16 @@ bitmap_not(bitmap_t *bitmap, s3b_block_t num_blocks)
bitmap[i] = ~bitmap[i];
}

// https://stackoverflow.com/q/109023/263801
int
popcount32(uint32_t value)
{
value = value - ((value >> 1) & 0x55555555);
value = (value & 0x33333333) + ((value >> 2) & 0x33333333);
value = (value + (value >> 4)) & 0x0F0F0F0F;
return (int)((value * 0x01010101) >> 24);
}

int
init_zero_block(u_int block_size)
{
Expand Down
5 changes: 2 additions & 3 deletions util.h
Expand Up @@ -37,9 +37,6 @@
// Forward decl's
struct s3b_config;

// Bitmap type
typedef uintptr_t bitmap_t;

// A list of block numbers
struct block_list {
s3b_block_t *blocks;
Expand Down Expand Up @@ -103,6 +100,7 @@ extern int add_string(struct string_array *array, const char *fmt, ...) __attrib
extern void free_strings(struct string_array *array);
extern int init_zero_block(u_int block_size);
extern void set_config_log(struct s3b_config *config, log_func_t *log);
extern int popcount32(uint32_t value);

// Versions of <err.h> that work properly even when daemonized
extern void daemon_debug(const struct s3b_config *config, const char *fmt, ...)
Expand Down Expand Up @@ -130,6 +128,7 @@ extern int bitmap_test(const bitmap_t *bitmap, s3b_block_t block_num);
extern void bitmap_set(bitmap_t *bitmap, s3b_block_t block_num, int value);
extern void bitmap_and(bitmap_t *dst, const bitmap_t *src, s3b_block_t num_blocks);
extern void bitmap_or(bitmap_t *dst, const bitmap_t *src, s3b_block_t num_blocks);
extern size_t bitmap_or2(bitmap_t *dst, const bitmap_t *src, s3b_block_t num_blocks);
extern void bitmap_not(bitmap_t *bitmap, s3b_block_t num_blocks);

// Block lists
Expand Down
2 changes: 1 addition & 1 deletion zero_cache.c
Expand Up @@ -242,7 +242,7 @@ zero_cache_survey_main(void *arg)
// Apply results (only if we completed the survey with no error)
pthread_mutex_lock(&priv->survey_mutex);
if (r == 0)
bitmap_or(priv->zeros, priv->survey_zeros, config->num_blocks);
priv->stats.current_cache_size = bitmap_or2(priv->zeros, priv->survey_zeros, config->num_blocks);
survey_count = priv->survey_count;
CHECK_RETURN(pthread_mutex_unlock(&priv->survey_mutex));

Expand Down

0 comments on commit 571b09d

Please sign in to comment.