Skip to content

Commit

Permalink
buffer: allow mempool to be passed into raw* ctors and create methods
Browse files Browse the repository at this point in the history
This is more convenient, and also faster than initializing it in
buffer_anon and the immediately moving it elsewhere.

Drop the optionality of the alignment argument.

No users yet.

Signed-off-by: Sage Weil <sage@redhat.com>
  • Loading branch information
liewegas committed Oct 19, 2017
1 parent cc09e30 commit 481277b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/common/buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,17 @@ using namespace ceph;
char *data;
unsigned len;
std::atomic<unsigned> nref { 0 };
int mempool = mempool::mempool_buffer_anon;
int mempool;

mutable ceph::spinlock crc_spinlock;
map<pair<size_t, size_t>, pair<uint32_t, uint32_t> > crc_map;

explicit raw(unsigned l)
: data(NULL), len(l), nref(0) {
explicit raw(unsigned l, int mempool=mempool::mempool_buffer_anon)
: data(NULL), len(l), nref(0), mempool(mempool) {
mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
}
raw(char *c, unsigned l)
: data(c), len(l), nref(0) {
raw(char *c, unsigned l, int mempool=mempool::mempool_buffer_anon)
: data(c), len(l), nref(0), mempool(mempool) {
mempool::get_pool(mempool::pool_index_t(mempool)).adjust_count(1, len);
}
virtual ~raw() {
Expand Down Expand Up @@ -277,8 +277,9 @@ using namespace ceph;
class buffer::raw_combined : public buffer::raw {
size_t alignment;
public:
raw_combined(char *dataptr, unsigned l, unsigned align=0)
: raw(dataptr, l),
raw_combined(char *dataptr, unsigned l, unsigned align,
int mempool)
: raw(dataptr, l, mempool),
alignment(align) {
inc_total_alloc(len);
inc_history_alloc(len);
Expand All @@ -290,7 +291,9 @@ using namespace ceph;
return create(len, alignment);
}

static raw_combined *create(unsigned len, unsigned align=0) {
static raw_combined *create(unsigned len,
unsigned align,
int mempool = mempool::mempool_buffer_anon) {
if (!align)
align = sizeof(size_t);
size_t rawlen = ROUND_UP_TO(sizeof(buffer::raw_combined),
Expand All @@ -310,7 +313,7 @@ using namespace ceph;

// actual data first, since it has presumably larger alignment restriction
// then put the raw_combined at the end
return new (ptr + datalen) raw_combined(ptr, len, align);
return new (ptr + datalen) raw_combined(ptr, len, align, mempool);
}

static void operator delete(void *ptr) {
Expand Down Expand Up @@ -767,6 +770,9 @@ using namespace ceph;
buffer::raw* buffer::create(unsigned len) {
return buffer::create_aligned(len, sizeof(size_t));
}
buffer::raw* buffer::create_in_mempool(unsigned len, int mempool) {
return buffer::create_aligned_in_mempool(len, sizeof(size_t), mempool);
}
buffer::raw* buffer::claim_char(unsigned len, char *buf) {
return new raw_claimed_char(len, buf);
}
Expand All @@ -783,7 +789,8 @@ using namespace ceph;
return new raw_claim_buffer(buf, len, std::move(del));
}

buffer::raw* buffer::create_aligned(unsigned len, unsigned align) {
buffer::raw* buffer::create_aligned_in_mempool(
unsigned len, unsigned align, int mempool) {
// If alignment is a page multiple, use a separate buffer::raw to
// avoid fragmenting the heap.
//
Expand All @@ -801,7 +808,12 @@ using namespace ceph;
return new raw_hack_aligned(len, align);
#endif
}
return raw_combined::create(len, align);
return raw_combined::create(len, align, mempool);
}
buffer::raw* buffer::create_aligned(
unsigned len, unsigned align) {
return create_aligned_in_mempool(len, align,
mempool::mempool_buffer_anon);
}

buffer::raw* buffer::create_page_aligned(unsigned len) {
Expand Down Expand Up @@ -1918,7 +1930,7 @@ using namespace ceph;
size_t need = ROUND_UP_TO(len, sizeof(size_t)) + sizeof(raw_combined);
size_t alen = ROUND_UP_TO(need, CEPH_BUFFER_ALLOC_UNIT) -
sizeof(raw_combined);
append_buffer = raw_combined::create(alen);
append_buffer = raw_combined::create(alen, 0);
append_buffer.set_length(0); // unused, so far.
if (_mempool >= 0) {
append_buffer.get_raw()->reassign_to_mempool(_mempool);
Expand Down
2 changes: 2 additions & 0 deletions src/include/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ namespace buffer CEPH_BUFFER_API {
*/
raw* copy(const char *c, unsigned len);
raw* create(unsigned len);
raw* create_in_mempool(unsigned len, int mempool);
raw* claim_char(unsigned len, char *buf);
raw* create_malloc(unsigned len);
raw* claim_malloc(unsigned len, char *buf);
raw* create_static(unsigned len, char *buf);
raw* create_aligned(unsigned len, unsigned align);
raw* create_aligned_in_mempool(unsigned len, unsigned align, int mempool);
raw* create_page_aligned(unsigned len);
raw* create_zero_copy(unsigned len, int fd, int64_t *offset);
raw* create_unshareable(unsigned len);
Expand Down

0 comments on commit 481277b

Please sign in to comment.