当前位置: 首页 > news >正文

ceph源码阅读 buffer

ceph::buffer是ceph非常底层的实现,负责管理ceph的内存。ceph::buffer的设计较为复杂,但本身没有任何内容,主要包含buffer::list、buffer::ptr、buffer::hash。这三个类都定义在src/include/buffer.h和src/common/http://buffer.cc中。


buffer::raw:负责维护物理内存的引用计数nref和释放操作。
buffer::ptr:指向buffer::raw的指针。
buffer::list:表示一个ptr的列表(std::list<bufferptr>),相当于将N个ptr构成一个更大的虚拟的连续内存。

buffer::hash:一个或多个bufferlist的有效哈希。

buffer::raw

bufferlist是基于bufferptr和bufferraw实现的,所以先分析buffer::raw。

 class raw {public:std::aligned_storage<sizeof(ptr_node),alignof(ptr_node)>::type bptr_storage;char *data;unsigned len;std::atomic<unsigned> nref { 0 };int mempool;std::pair<size_t, size_t> last_crc_offset {std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max()};std::pair<uint32_t, uint32_t> last_crc_val;mutable ceph::spinlock crc_spinlock;...
}

成员变量:

data:指向原始数据raw的指针。

len:原始数据raw的长度。

nref:引用计数。

mempool:其对应的内存池的index,这个和data空间的分配有关。

crc_spinlock:读写锁。

buffer::ptr

bufferptr是基于bufferraw,bufferptr是bufferraw的指针,指向buffer::raw的一部分数据段。

 class CEPH_BUFFER_API ptr {raw *_raw;public: // dirty hack for testing; if it works, this will be abstractedunsigned _off, _len;...}

成员变量:

_raw:指向raw的指针

_off:数据偏移量

_len:数据长度

buffer::list

在list类前面定义了链表的结构体struct ptr_hook。

 struct ptr_hook {mutable ptr_hook* next;ptr_hook() = default;ptr_hook(ptr_hook* const next): next(next) {}};

buffer::list是由ptr组成的链表

class buffers_t {// _root.next can be thought as _headptr_hook _root;ptr_hook* _tail;std::size_t _size;...
}

_root:链表头

_tail:链表尾

_size:链表节点数

bufferlist将数据以不连续链表的方式存储。

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 bllist_iter_t p;unsigned p_off; // in *p......};

bl:指针,指向bufferlist

ls:指针,指向bufferlist的成员 _buffers

p: 类型是std::list::iterator,用来迭代遍历bufferlist中的bufferptr

p_off:当前位置在对应的bufferptr中的偏移量

off:当前位置在整个bufferlist中的偏移量

buffer常见的函数

void buffer::list::substr_of()

获取子字符串

other:指向原数据的bufferlist指针

off:数据的偏移

len:数据截取长度

 void buffer::list::substr_of(const list& other, unsigned off, unsigned len){if (off + len > other.length())throw end_of_buffer();clear();// skip off//curbuf获得other的链表头auto curbuf = std::cbegin(other._buffers);//找到和off对应的链表节点while (off > 0 && off >= curbuf->length()) {// skip this buffer//cout << "skipping over " << *curbuf << std::endl;off -= (*curbuf).length();++curbuf;}ceph_assert(len == 0 || curbuf != std::cend(other._buffers));//截取所需要的长度,创建新的节点加入到链表尾while (len > 0) {// partial?if (off + len < curbuf->length()) {//cout << "copying partial of " << *curbuf << std::endl;_buffers.push_back(*ptr_node::create( *curbuf, off, len ).release());_len += len;break;}// through end//cout << "copying end (all?) of " << *curbuf << std::endl;unsigned howmuch = curbuf->length() - off;_buffers.push_back(*ptr_node::create( *curbuf, off, howmuch ).release());_len += howmuch;len -= howmuch;off = 0;++curbuf;}}

buffer::create_aligned()

预对齐内存的分配

len:分配内存的大小

align:内存对齐的倍数

ceph::unique_leakable_ptr<buffer::raw> buffer::create_aligned(unsigned len, unsigned align) {return create_aligned_in_mempool(len, align,mempool::mempool_buffer_anon);
}

void buffer::list::claim_append(list& bl, unsigned int flags)

将bl的数据复制_buffers的尾部/头部,然后接bl的数据清空。

void buffer::list::rebuild(
std::unique_ptr<buffer::ptr_node, buffer::ptr_node::disposer> nb)

将bufferlist中_buffers链表中所有的ptr中的数据存到一个ptr中并将_buffers原有数据clear,然后将新的单个ptr push到_buffers中。

http://www.lryc.cn/news/154772.html

相关文章:

  • 基本介绍——数据挖掘
  • Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错
  • 冯诺依曼体系结构/什么是OS?
  • SD卡/TF卡简记
  • Dockerfile COPY的奇怪行为:自动解包一级目录
  • 【每日一题Day311】LC1761一个图中连通三元组的最小度数 | 枚举
  • 前端日期减一天的笑话
  • 高效能,一键批量剪辑,AI智剪让创作更轻松
  • 手写Mybatis:第15章-返回Insert操作自增索引值
  • 【数据结构】动态数组(vector)的基本操作,包括插入、删除、扩容、输出、释放内存等。以下是代码的解释和注释:
  • [unity]三角形顶点顺序
  • 【python爬虫】14.Scrapy框架讲解
  • 功率放大器主要作用是什么呢
  • SpringBoot ApplicationEvent详解
  • WebSocket 报java.io.IOException: 远程主机强迫关闭了一个现有的连接。
  • 关于git约定式提交IDEA
  • 【计算机网络】http协议
  • 仓库太大,clone 后,git pull 老分支成功,最新分支失败
  • javafx Dialog无法关闭
  • vue3中TCplayer应用
  • 算法通关村14关 | 数据流中位数问题
  • 工厂模式 与 抽象工厂模式 的区别
  • 安装虚拟机+安装/删除镜像
  • MySQL的内置函数复合查询内外连接
  • 操作系统(OS)与系统进程
  • 防重复提交:自定义注解 + 拦截器(HandlerInterceptor)
  • Excel中将文本格式的数值转换为数字
  • uni-app开发小程序中遇到的map地图的点聚合以及polygon划分区域问题
  • 【笔记】软件测试的艺术
  • 配置本地maven