Skip to content

Commit

Permalink
osd: compile-time assert that the packing remains valid
Browse files Browse the repository at this point in the history
Signed-off-by: Xinze Chi <xinze@xsky.com>
  • Loading branch information
XinzeChi committed Dec 14, 2015
1 parent b53f087 commit e4de246
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/include/utime.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
/* WARNING: If add member in utime_t, please make sure the encode/decode funtion
* work well. For little-endian machine, we should make sure there is no padding
* in 32-bit machine and 64-bit machine.
* You should also modify the padding_check function.
*/
class utime_t {
public:
Expand Down Expand Up @@ -99,7 +100,14 @@ class utime_t {
tv.tv_sec = v->tv_sec;
tv.tv_nsec = v->tv_usec*1000;
}

void padding_check() {
static_assert(
sizeof(utime_t) ==
sizeof(tv.tv_sec) +
sizeof(tv.tv_nsec)
,
"utime_t have padding");
}
void encode(bufferlist &bl) const {
#if defined(CEPH_LITTLE_ENDIAN)
bl.append((char *)(this), sizeof(__u32) + sizeof(__u32));
Expand Down
87 changes: 85 additions & 2 deletions src/osd/osd_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,11 @@ ostream& operator<<(ostream& out, const pg_pool_t& p);
* a summation of object stats
*
* This is just a container for object stats; we don't know what for.
*
* If you add members in object_stat_sum_t, you should make sure there are
* not padding among these members.
* You should also modify the padding_check function.
*/
struct object_stat_sum_t {
/**************************************************************************
Expand Down Expand Up @@ -1619,6 +1624,46 @@ struct object_stat_sum_t {
void sub(const object_stat_sum_t& o);

void dump(Formatter *f) const;
void padding_check() {
static_assert(
sizeof(object_stat_sum_t) ==
sizeof(num_bytes) +
sizeof(num_objects) +
sizeof(num_object_clones) +
sizeof(num_object_copies) +
sizeof(num_objects_missing_on_primary) +
sizeof(num_objects_degraded) +
sizeof(num_objects_unfound) +
sizeof(num_rd) +
sizeof(num_rd_kb) +
sizeof(num_wr) +
sizeof(num_wr_kb) +
sizeof(num_scrub_errors) +
sizeof(num_objects_recovered) +
sizeof(num_bytes_recovered) +
sizeof(num_keys_recovered) +
sizeof(num_shallow_scrub_errors) +
sizeof(num_deep_scrub_errors) +
sizeof(num_objects_dirty) +
sizeof(num_whiteouts) +
sizeof(num_objects_omap) +
sizeof(num_objects_hit_set_archive) +
sizeof(num_objects_misplaced) +
sizeof(num_bytes_hit_set_archive) +
sizeof(num_flush) +
sizeof(num_flush_kb) +
sizeof(num_evict) +
sizeof(num_evict_kb) +
sizeof(num_promote) +
sizeof(num_flush_mode_high) +
sizeof(num_flush_mode_low) +
sizeof(num_evict_mode_some) +
sizeof(num_evict_mode_full) +
sizeof(num_objects_pinned) +
sizeof(num_objects_missing)
,
"object_stat_sum_t have padding");
}
void encode(bufferlist& bl) const;
void decode(bufferlist::iterator& bl);
static void generate_test_instances(list<object_stat_sum_t*>& o);
Expand Down Expand Up @@ -1724,8 +1769,10 @@ struct pg_stat_t {
bool pin_stats_invalid;

/**
* Above members is fixed size and no-padding types.
* You should add fixed size and no-padding type here.
* WARNING: Above members is raw and no-padding types.
* You should add raw size and no-padding type ahead of encode_flags,
* You should also modify the padding_check function.
* You must make sure that there are no padding among above members.
*/
bool encode_flags;

Expand Down Expand Up @@ -1797,6 +1844,42 @@ struct pg_stat_t {
bool is_acting_osd(int32_t osd, bool primary) const;
void dump(Formatter *f) const;
void dump_brief(Formatter *f) const;
void padding_check() {
static_assert(
offsetof(pg_stat_t, encode_flags) ==
sizeof(last_fresh) +
sizeof(last_change) +
sizeof(last_active) +
sizeof(last_peered) +
sizeof(last_clean) +
sizeof(last_unstale) +
sizeof(last_undegraded) +
sizeof(last_fullsized) +
sizeof(last_scrub_stamp) +
sizeof(last_deep_scrub_stamp) +
sizeof(last_clean_scrub_stamp) +
sizeof(last_became_active) +
sizeof(last_became_peered) +
sizeof(log_size) +
sizeof(ondisk_log_size) +
sizeof(reported_seq) +
sizeof(mapping_epoch) +
sizeof(created) +
sizeof(last_epoch_clean) +
sizeof(parent_split_bits) +
sizeof(reported_epoch) +
sizeof(state) +
sizeof(up_primary) +
sizeof(acting_primary) +
sizeof(stats_invalid) +
sizeof(dirty_stats_invalid) +
sizeof(omap_stats_invalid) +
sizeof(hitset_stats_invalid) +
sizeof(hitset_bytes_stats_invalid) +
sizeof(pin_stats_invalid)
,
"pg_sat_t have padding");
}
void encode(bufferlist &bl, uint64_t features = 0) const;
void decode(bufferlist::iterator &bl);
static void generate_test_instances(list<pg_stat_t*>& o);
Expand Down

0 comments on commit e4de246

Please sign in to comment.