bufferlist是ceph的底層組件,用于存儲(chǔ)二進(jìn)制數(shù)據(jù),其存儲(chǔ)的數(shù)據(jù)可以直接寫入磁盤,在代碼中有很廣泛的使用。
bufflist對(duì)應(yīng)的類為buffer::list(using bufferlist = buffer::list;),而buffer::list又基于buffer::ptr和buffer::raw實(shí)現(xiàn),探討buffer::list的實(shí)現(xiàn),不能跳過(guò)它們。
buffer::raw
raw的數(shù)據(jù)成員部分代碼如下:
class buffer::raw
{
public:
char *data;
unsigned len;
std::atomic<unsigned> nref{0};
int mempool;
mutable ceph::spinlock crc_spinlock;
map<pair<size_t, size_t>, pair<uint32_t, uint32_t>> crc_map;
......
};
最基本的成員:data是指向具體數(shù)據(jù)的指針,len是數(shù)據(jù)的長(zhǎng)度,nref是引用計(jì)數(shù)。而mempool是其對(duì)應(yīng)的內(nèi)存池的index,這個(gè)和data空間的分配有關(guān),暫時(shí)不去管它。
data指向的數(shù)據(jù)有很多來(lái)源,直接通過(guò)malloc從內(nèi)存分配只是最基礎(chǔ)的一種,可能還來(lái)自mmap內(nèi)存映射的空間等等。對(duì)于每一種數(shù)據(jù)來(lái)源,需要不同邏輯的數(shù)據(jù)分配和釋放函數(shù),所以raw對(duì)應(yīng)了很多子類,分別表示不同的數(shù)據(jù)。
舉個(gè)例子,對(duì)應(yīng)于malloc的raw子類為buffer::raw_malloc,構(gòu)造和析構(gòu)函數(shù)中實(shí)現(xiàn)了使用malloc進(jìn)行數(shù)據(jù)分配和釋放的邏輯:
class buffer::raw_malloc : public buffer::raw
{
public:
MEMPOOL_CLASS_HELPERS();
explicit raw_malloc(unsigned l) : raw(l)
{
if (len)
{
data = (char *)malloc(len);
if (!data)
throw bad_alloc();
}
else
{
data = 0;
}
inc_total_alloc(len);
inc_history_alloc(len);
bdout << "raw_malloc " << this << " alloc " << (void *)data << " " << l << " " << buffer::get_total_alloc() << bendl;
}
raw_malloc(unsigned l, char *b) : raw(b, l)
{
inc_total_alloc(len);
bdout << "raw_malloc " << this << " alloc " << (void *)data << " " << l << " " << buffer::get_total_alloc() << bendl;
}
~raw_malloc() override
{
free(data);
dec_total_alloc(len);
bdout << "raw_malloc " << this << " free " << (void *)data << " " << buffer::get_total_alloc() << bendl;
}
raw *clone_empty() override
{
return new raw_malloc(len);
}
};
buffer::ptr
ptr是基于raw的,成員部分如下:
class CEPH_BUFFER_API ptr
{
raw *_raw;
unsigned _off, _len;
......
};
ptr和raw的關(guān)系和golang中splice和array的關(guān)系有點(diǎn)像。raw是真正存儲(chǔ)數(shù)據(jù)的地方,而ptr只是指向某個(gè)raw中的一段的指針。其數(shù)據(jù)成員 _raw為指向raw的指針,_off表示數(shù)據(jù)起始偏移,_len表示數(shù)據(jù)長(zhǎng)度。
這邊還有提一下ptr的append函數(shù),直觀上ptr不應(yīng)該提供append函數(shù),事實(shí)上ptr的append確實(shí)很局限,只有當(dāng)ptr對(duì)應(yīng)的raw區(qū)域后方有空閑空間的時(shí)候,才能append成功,至于空間不夠的情況,應(yīng)該是交給list等高層類來(lái)處理。代碼如下:
unsigned buffer::ptr::append(const char *p, unsigned l)
{
assert(_raw);
assert(l <= unused_tail_length());
char *c = _raw->data + _off + _len;
maybe_inline_memcpy(c, p, l, 32);
_len += l;
return _len + _off;
}
buffer::list
簡(jiǎn)單來(lái)說(shuō),list就是一個(gè)ptr組成的鏈表:
class CEPH_BUFFER_API list
{
// my private bits
std::list<ptr> _buffers;
unsigned _len;
unsigned _memcopy_count; //the total of memcopy using rebuild().
ptr append_buffer; // where i put small appends.
......
};
_buffers是一個(gè)ptr的鏈表,_len是整個(gè)_buffers中所有的ptr的數(shù)據(jù)的總長(zhǎng)度,_memcopy_count用于統(tǒng)計(jì)memcopy的字節(jié)數(shù),append_buffer是用于優(yōu)化append操作的緩沖區(qū),可以看出bufferlist將數(shù)據(jù)以不連續(xù)鏈表的方式存儲(chǔ)。
在說(shuō)具體函數(shù)之前,先說(shuō)一下bufferlist的迭代器:
template <bool is_const>
class CEPH_BUFFER_API iterator_impl
: public std::iterator<std::forward_iterator_tag, char>
{
protected:
bl_t *bl;
list_t *ls; // meh.. just here to avoid an extra pointer dereference..
unsigned off; // in bl
list_iter_t p;
unsigned p_off; // in *p
......
};
其數(shù)據(jù)成員的含義如下:
- bl:指針,指向bufferlist
- ls:指針,指向bufferlist的成員 _buffers
- p: 類型是std::list::iterator,用來(lái)迭代遍歷bufferlist中的bufferptr
- p_off:當(dāng)前位置在對(duì)應(yīng)的bufferptr中的偏移量
- off:當(dāng)前位置在整個(gè)bufferlist中的偏移量
迭代器中提供的seek(unsigned o)和advance(int o)等函數(shù)中的o都是指bufferlist的偏移,而不是單個(gè)ptr內(nèi)的偏移。
下面對(duì)bufferlist中我遇到的一些函數(shù)進(jìn)行分析,其他函數(shù)遇到再說(shuō)。
clear()
清空bufferlist中的內(nèi)容
push_front(raw* / ptr &)
push_back(raw* / ptr &)
在_buffers的前面或后面增加新的ptr
rebuild()
rebuild(ptr &nb)
將bufferlist中buffers鏈表中所有的ptr中的數(shù)據(jù)存到一個(gè)ptr中,并將_buffers原有數(shù)據(jù)clear,然后將新的單個(gè)ptr push到_buffers中。
帶參數(shù)時(shí)使用參數(shù)傳入的ptr作為目標(biāo)ptr,不帶參數(shù)時(shí)自己創(chuàng)建一個(gè)ptr。
claim(list &bl, unsigned int flags = CLAIM_DEFAULT);
將bl的數(shù)據(jù)拿過(guò)來(lái),替換原有的數(shù)據(jù)。調(diào)用后bl數(shù)據(jù)被清空。
claim_append(list &bl, unsigned int flags = CLAIM_DEFAULT);
claim_prepend(list &bl, unsigned int flags = CLAIM_DEFAULT);
將bl的數(shù)據(jù)拿過(guò)來(lái),splice到_buffers的尾部/頭部。
append(...)
將數(shù)據(jù)追加到_buffers尾部,已有ptr空間不夠時(shí),會(huì)自動(dòng)分配新的ptr。
splice(unsigned off, unsigned len, list *claim_by = 0)
將_buffers中總偏移off處長(zhǎng)度為len的數(shù)據(jù),move到claim_by對(duì)應(yīng)的bufferlist的尾部。注意是move不是copy。
write(int off, int len, std::ostream &out)
將_buffers中總偏移量off處長(zhǎng)度為len的數(shù)據(jù),寫入到ostream。注意是copy,不是move。