ceph perf counter 源码分析及使用

来源:互联网 发布:诺基亚s60软件下载 编辑:程序博客网 时间:2024/06/05 02:08

ceph perf counter 源码分析及使用

示例

1     enum {2       test_perfcounter_first = 200,3       test_perfcounter_count,4       test_perfcounter_time,5       test_perfcounter_sum,6       test_perfcounter_last,7     };89     PerfCountersCollection *coll = g_ceph_context->get_perfcounters_collection();1011    PerfCountersBuilder test_plb(g_ceph_context, "test_perfcounter", test_perfcounter_first, test_perfcounter_last);1213    test_plb.add_u64_counter(test_perfcounter_count, "test_count");14    test_plb.add_time(test_perfcounter_time, "test_time", "Time of system service");15    test_plb.add_u64(test_perfcounter_sum, "test_sum", "Sum of variable", "wr_bytes_sum");1617    test_logger = test_plb.create_perf_counters();1819    coll->add(test_plb);2021    test_logger->set(test_perfcounter_count, 1);22    test_logger->tset(test_perfcounter_time, utime_t(0, 1));23    test_logger->set(test_perfcounter_sum, 1);2425    test_logger->inc(test_perfcounter_count);26    test_logger->tinc(test_perfcounter_time, utime_t());27    test_logger->inc(test_percounter_sum, 10);2829    test_logger->dec(test_perfcounter_count);30    test_logger->dec(test_perfcounter_sum, 5);3132    test_logger->reset();3334    coll->reset(string("test_perfcounter"));35    coll->reset(string("all"));36    coll->remove(test_logger);37    coll->clear();

分析

1-7: 定义一个枚举型,在使用perf counter的时候,每一个couter会有一个data size,也就是有多少个perf counter对象,所以都会定义一个enum,enum的第一个和最后一个元素是必须要有的(也就是first和last),然后我们要计算perf的指标都定义在这两个enum元素之间就OK。

9: 定义了一个PerfCounterCollection对象,该类中有perf_counters_set_t m_loggers成员变量,该类是多个PerfCounter子模块的集合,所有的Perf counter模块都在这个集合中。

11: 定义了一个PerfCountersBuilder对象,该类中有PerfCounters *m_perf_counters成员变量。

m_perf_counters变量在实例化类的时候,通过类的构造函数初始化了,初始化:new PerfCounters(cct, name, first, last),实例化一个PerfCounters对象。

PerfCounters类中有perf_counter_data_vec_t m_data及int m_lower_bound, int m_upper_bound, string m_name;

PerfCounters类的构造函数:PerfCounters::PerfCounters(CephContext *cct, const std::string &name, int lower_bound, int upper_bound),构造函数的参数中的name是计算perf子模块的名称, 在这里就是”test_perfcounter”, lower_bound和high_bound是我们要跟踪计算perf的子模块的上下界限,主要是为了调整m_data(是一个vector)的长度。

13-15:将具体的跟踪指标(test_perfcounter_count等)加入到跟踪子模块(test_perfcounter)中。

根据不同的指标的类型不同,调用的函数也不同,也是根据不同的类型来区分的。这些函数最后还是调用同样的函数:add_impl(idx, name, description, nick, PERFCOUNTER_U64);只是最后这个type参数不同。目前根据time、普通数值、计量值来区分,分别有add_time()、add_u64()、add_u64_counter().

add_impl(…)函数如下:

 0   PerfCounters::perf_counter_data_vec_t &vec(m_perf_counters->m_data); 1   PerfCounters::perf_counter_data_any_d 2     &data(vec[idx - m_perf_counters->m_lower_bound - 1]); 3   assert(data.type == PERFCOUNTER_NONE); 4   data.name = name; 5   data.description = description; 6   data.nick = nick; 7   data.type = (enum perfcounter_type_d)ty;

0: 取m_data的值
1-2: 取m_data中的和具体的跟踪指标对应index的值
4-7: 给m_data对应的index位置赋值,从上层传下来的关于这个指标的信息,主要是index、指标名称、指标描述信息、指标的别名、指标类型

17: 返回一个完整perf counter的PerfCounter类型的m_perf_counters; 上面关于perf指标的操作都是为了创建一个完整的PerfCounter对象。

 1 PerfCounters *PerfCountersBuilder::create_perf_counters() 2 { 3   PerfCounters::perf_counter_data_vec_t::const_iterator d = m_perf_counters->m_data.begin(); 4   PerfCounters::perf_counter_data_vec_t::const_iterator d_end = m_perf_counters->m_data.end(); 5   for (; d != d_end; ++d) { 6     if (d->type == PERFCOUNTER_NONE) { 7       assert(d->type != PERFCOUNTER_NONE); 8     } 9   }10   PerfCounters *ret = m_perf_counters;11   m_perf_counters = NULL;12   return ret;13 }

19: 将新的Perf Counter字模块加入Perf Counters集合中。

21-30: Perf具体的计算函数,包括set()、tset()、inc()、tinc()、dec()

这些函数都有一个参数: int idx, 就是前面我们定义的enum类型的具体指标。函数首字母是’t’的是关于time的处理。
set()/tset():设置一个值。
inc()/tinc(): 增加,如果给定了增加多少,就增加具体的值;如果没有指定增加多少,+1.
dec(): 减少,如果给定了减少的值,就减少多少;如果没有指定,-1

32: 将test_perfcounter子模块中的所有指标都清0

34-35: 在perf counter集合的层面上清空,其实内部最后还是调用的PerfCounter::reset(), 也就是32行的调用函数

PerfCounterCollection::reset(string name)函数中的参数如果是”all”的话,perf集合会遍历所有的perf counter,依次调用reset();
参数可以是具体的perf counter子模块名,就只清空该模块, 比如reset(“test_perfcounter”)

36: 从perf counter集合中删除某个子perf counter模块

37: 从perf counter集合中删除所有的perf counter模块

参考

ceph源代码: test/perf_counters.cc

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二级重伤对方法庭拒绝赔偿怎么办? 在麦当劳工作收到假钞怎么办 东西湖小学分配太远怎么办 农行k令过期了怎么办 穿军训的鞋捂坏了怎么办 联通手机号销户话费怎么办 建行员工所持有的原始股怎么办 孕妇喝了午时茶怎么办 苹果6id被锁了怎么办 苹果手机app密码忘了怎么办 好哥们借钱手上没钱怎么办 武汉ca证书u盾怎么办 判了刑发现还有漏案没判怎么办 高铁旅客漏乘怎么办 水库里面要养殖小龙虾最好怎么办 点读机的笔丢了怎么办 皮肤晒的很黑怎么办 电脑wifi连接受限制怎么办 高中孩子班管理松怎么办 脱式计算有余数怎么办 手机被别人绑定微信怎么办 饿了么入职查不到学历怎么办 高中没考上家人让打工怎么办 一建证书丢了怎么办 自考大专证书丢了怎么办 护士学分卡丢了怎么办 公司电脑只能用内网怎么办 遇见素质低的人怎么办 被素质低的人骂怎么办 对素质差的人怎么办 碰到素质低的人怎么办 和韩国人结婚后工作怎么办 大连明珠卡坏了怎么办 护肤品酒精含量太高怎么办 父母出国三年想孩子怎么办 孩子高考不理想想出国怎么办 交大附中创新班剩余孩子怎么办 棋牌开发公司倒闭了怎么办 7月1日本地流量怎么办 装电池的弹簧生锈了怎么办 电器被电池碱了怎么办