如何优雅的写C++代码 Obotcha介绍(Perf调优)

  这几天在调试ScheduledThreadPoolThread的大数据并发。测试的case很简单,代码如下:

    ScheduledExecutorService pool = st(Executors)::newScheduledThreadPool();
    long time = st(System)::currentTimeMillis();
    for(int i = 0; i < 32*1024;i++) {
        pool->schedule(createMyLoopSubmit(),0);
    }

同一时间发送了32K个action,这个时候发现这个for循环耗时在3S左右~。额,这个性能实在是太差,32K就要花费3S,如果大并发的话,基本就凉凉了。好吧,我们开始用perf来调试一下,看看具体是什么情况.

1.安装perf

sudo apt-get install perf-tools-unstable

2.抓取perf数据

$ sudo perf record -e cpu-clock -g ./mytest

3.分析perf数据

$ sudo perf report -g -i perf.data

我们可以看到下面cpu占比较高的处理:

Samples: 25K of event 'cpu-clock', Event count (approx.): 6382250000
  Children      Self  Command  Shared Object        Symbol
+   93.50%     0.00%  mytest   libpthread-2.27.so   [.] start_thread
+   93.50%     0.00%  mytest   libobotcha.so        [.] obotcha::_Thread::localRun
+   86.65%     0.07%  mytest   libobotcha.so        [.] obotcha::_ScheduledThreadPoolThread::run
+   82.76%     0.02%  mytest   libobotcha.so        [.] obotcha::_ArrayList<obotcha::sp<obotcha::_WaitingTask> >::removeAt
+   82.69%     0.01%  mytest   libobotcha.so        [.] std::vector<obotcha::sp<obotcha::_WaitingTask>, std::allocator<obotcha::sp<obotcha::_WaitingTask> > >::erase
+   82.64%     0.02%  mytest   libobotcha.so        [.] std::vector<obotcha::sp<obotcha::_WaitingTask>, std::allocator<obotcha::sp<obotcha::_WaitingTask> > >::_M_erase
+   82.56%     0.02%  mytest   libobotcha.so        [.] std::move<__gnu_cxx::__normal_iterator<obotcha::sp<obotcha::_WaitingTask>*, std::vector<obotcha::sp<obotcha::_WaitingTask>, std::allocator<obotcha::
+   82.53%     0.03%  mytest   libobotcha.so        [.] std::__copy_move_a2<true, __gnu_cxx::__normal_iterator<obotcha::sp<obotcha::_WaitingTask>*, std::vector<obotcha::sp<obotcha::_WaitingTask>, std::all
+   82.48%     0.02%  mytest   libobotcha.so        [.] std::__copy_move_a<true, obotcha::sp<obotcha::_WaitingTask>*, obotcha::sp<obotcha::_WaitingTask>*>
+   79.04%     3.91%  mytest   libobotcha.so        [.] std::__copy_move<true, false, std::random_access_iterator_tag>::__copy_m<obotcha::sp<obotcha::_WaitingTask>*, obotcha::sp<obotcha::_WaitingTask>*>
+   71.24%    12.47%  mytest   libobotcha.so        [.] obotcha::sp<obotcha::_WaitingTask>::operator=
+   37.14%     3.63%  mytest   mytest               [.] obotcha::Object::incStrong
+   34.33%    34.32%  mytest   mytest               [.] std::__atomic_base<int>::operator--
+   25.72%     4.51%  mytest   mytest               [.] obotcha::Object::decStrong
+   22.99%    22.99%  mytest   mytest               [.] std::__atomic_base<int>::operator--
+    6.51%     0.02%  mytest   libobotcha.so        [.] obotcha::_ThreadCachedPoolExecutorHandler::run

好吧,我们发现ArrayList的removeAt竟然占比在80%以上,这个应该就是for循环耗时的主要原因。

记下来,我们可以用上下按键将光标移动到removeAt上,回车查看removeAt的具体耗时:

-   82.76%     0.02%  mytest   libobotcha.so        [.] obotcha::_ArrayList<obotcha::sp<obotcha::_WaitingTask> >::removeAt                                                                                 ▒
   - 82.75% obotcha::_ArrayList<obotcha::sp<obotcha::_WaitingTask> >::removeAt                                                                                                                             ▒
      - 82.68% std::vector<obotcha::sp<obotcha::_WaitingTask>, std::allocator<obotcha::sp<obotcha::_WaitingTask> > >::erase                                                                                ▒
         - 82.64% std::vector<obotcha::sp<obotcha::_WaitingTask>, std::allocator<obotcha::sp<obotcha::_WaitingTask> > >::_M_erase                                                                          ▒
            - 82.55% std::move<__gnu_cxx::__normal_iterator<obotcha::sp<obotcha::_WaitingTask>*, std::vector<obotcha::sp<obotcha::_WaitingTask>, std::allocator<obotcha::sp<obotcha::_WaitingTask> > > >, _▒
               - 82.52% std::__copy_move_a2<true, __gnu_cxx::__normal_iterator<obotcha::sp<obotcha::_WaitingTask>*, std::vector<obotcha::sp<obotcha::_WaitingTask>, std::allocator<obotcha::sp<obotcha::_Wa▒
                  - 82.47% std::__copy_move_a<true, obotcha::sp<obotcha::_WaitingTask>*, obotcha::sp<obotcha::_WaitingTask>*>                                                                              ▒
                     - 79.03% std::__copy_move<true, false, std::random_access_iterator_tag>::__copy_m<obotcha::sp<obotcha::_WaitingTask>*, obotcha::sp<obotcha::_WaitingTask>*>                           ▒
                        - 70.22% obotcha::sp<obotcha::_WaitingTask>::operator=                                                                                                                             ▒
                           + 33.31% obotcha::Object::incStrong                                                                                                                                             ▒
                           + 22.80% obotcha::Object::decStrong                                                                                                                                             ▒
                             1.72% std::__atomic_base<int>::operator--                                                                                                                                     ▒
                             0.79% std::__atomic_base<int>::operator--                                                                                                                                     ▒
                          1.79% obotcha::Object::incStrong                                                                                                                                                 ▒
                          1.70% obotcha::Object::decStrong                                                                                                                                                 ▒
                          1.34% std::move<obotcha::sp<obotcha::_WaitingTask>&> 

两个耗时点竟然是incStrong/decStrong,原来removeAt移除一个item之后,其他的所有的item都需要移动(析构/创建),这个时候就会触发计数。32K的数据,计算量基本在1+2+3+4+45......32K,运算量的确是非常大的。

4.如何优化

最先想到的是使用RBTree,这样每次移除item之后,不需要做item的移动。但是我在STL里面没有找到相关的接口。所以只能退而求其次,将数据放到HashMap中,这样每次erase也不会做item的移动。

相关的修改参看:

https://github.com/wangsun1983/Obotcha/commit/44d067cdda087c1049d5aed8ae7d2fb6ab6f3ce3

google-perftools 简介 google-perftools 是一款针对 C/C++ 程序的性能分析工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,本文将重点介绍如何进行 CPU 时间片的剖析。 google-perftools 对一个程序的 CPU 性能剖析包括以下几个步骤。 1. 编译目标程序,加入对 google-perftools 库的依赖。 2. 运行目标程序,并用某种方式启动 / 终止剖析函数并产生剖析结果。 3. 运行剖结果转换工具,将不可读的结果数据转化成某种格式的文档(例如 pdf,txt,gv 等)。 安装 您可以在 google-perftools 的网站 (http://code.google.com/p/google-perftools/downloads/list) 上下载最新版的安装包。为完成步骤 3 的工作,您还需要一个将剖析结果转化为程序员可读文档的工具,例如 gv(http://www.gnu.org/software/gv/)。 编译与运行 您需要在原有的编译选项中加入对 libprofiler.so 的引用,这样在目标程序运行时会加载工具的动态库。例如本例中作者的系统中,libprofiler.so 安装在"/usr/lib"目录下,所以需要在 makefile 文件中的编译选项加入“-L/usr/lib -lprofiler”。 google-perftools 需要在目标代码的开始和结尾点分别调用剖析模块的启动和终止函数,这样在目标程序运行时就可以对这段时间内程序实际占用的 CPU 时间片进行统计和分析。工具的启动和终止可以采用以下两种方式。 a. 使用调试工具 gdb 在程序中手动运行性能工具的启动 / 终止函数。 gdb 是 Linux 上广泛使用的调试工具,它提供了强大的命令行功能,使我们可以在程序运行时插入断点并在断点处执行其他函数。具体的文档请参照 http://www.gnu.org/software/gdb/,本文中将只对用到的几个基本功能进行简单介绍。使用以下几个功能就可以满足我们性能调试的基本需求,具体使用请参见下文示例。 命令 功能 ctrl+c 暂停程序的运行 c 继续程序的运行 b 添加函数断点(参数可以是源代码中的行号或者一个函数名) p 打印某个量的值或者执行一个函数调用 b. 在目标代码中直接加入性能工具函数的调用,该方法就是在程序代码中直接加入调试函数的调用。 两种方式都需要对目标程序重新编译,加入对性能工具的库依赖。对于前者,他的好处是使用比较灵活,但工具的启动和终止依赖于程序员的手动操作,常常需要一些暂停函数(比如休眠 sleep)的支持才能达到控制程序的目的,因此精度可能受到影响。对于后者,它需要对目标代码的进行修改,需要处理函数声明等问题,但得到的结果精度较高,缺点是每次重新设置启动点都需要重新编译,灵活度不高,读者可以根据自己的实际需求采用有效的方式。 示例详解 该程序是一个简单的例子,文中有两处耗时的无用操作,并且二者间有一定的调用关系。 清单 1. 示例程序 void consumeSomeCPUTime1(int input){ int i = 0; input++; while(i++ < 10000){ i--; i++; i--; i++; } }; void consumeSomeCPUTime2(int input){ input++; consumeSomeCPUTime1(input); int i = 0; while(i++ < 10000){ i--; i++; i--; i++; } }; int stupidComputing(int a, int b){ int i = 0; while( i++ < 10000){ consumeSomeCPUTime1(i); } int j = 0; while(j++ < 5000){ consumeSomeCPUTime2(j); } return a+b; }; int smartComputing(int a, int b){ return a+b; }; void main(){ int i = 0; printf("reached the start point of performance bottle neck\n"); sleep(5); //ProfilerStart("CPUProfile"); while( i++ MyProfile.pdf 转换后产生的结果文档如下图。图中的数字和框体的大小代表了的某个函数的运行时间占整个剖析时间的比例。由代码的逻辑可知,stupidComputing,stupidComputing2 都是费时操作并且它们和 consumeSomeCPUTime 存在着一定的调用关系。 图 1. 剖析结果 结束语 本文介绍了一个 Linux 平台上的性能剖析工具 google-perftools,并结合实例向读者展示了如何使用该工具配置、使用及分析性能瓶颈。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值