[英]How to interpret the report of perf
我正在學習如何使用工具perf
來分析我的 C++ 項目。 這是我的代碼:
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
std::mutex mtx;
long long_val = 0;
void do_something(long &val)
{
std::unique_lock<std::mutex> lck(mtx);
for(int j=0; j<1000; ++j)
val++;
}
void thread_func()
{
for(int i=0; i<1000000L; ++i)
{
do_something(long_val);
}
}
int main(int argc, char* argv[])
{
std::vector<std::unique_ptr<std::thread>> threads;
for(int i=0; i<100; ++i)
{
threads.push_back(std::move(std::unique_ptr<std::thread>(new std::thread(thread_func))));
}
for(int i=0; i<100; ++i)
{
threads[i]->join();
}
threads.clear();
std::cout << long_val << std::endl;
return 0;
}
為了編譯它,我運行g++ -std=c++11 main.cpp -lpthread -g
然后我得到名為a.out
的可執行文件。
然后我運行perf record --call-graph dwarf -- ./a.out
並等待 10 秒,然后我按Ctrl+c
中斷./a.out
因為它需要太多時間來執行。
最后,我運行perf report -g graph --no-children
,這是輸出:
我的目標是找出代碼的哪一部分最重。 所以看起來這個輸出可以告訴我do_something
是最重的部分(46.25%)。 但是當我進入do_something
,我無法理解它是什么: std::_Bind_simple
、 std::thread::_Impl
等。
那么如何從perf report
的輸出中獲取更多有用的信息呢? 或者除了do_something
是最重的這一事實之外,我們無法獲得更多?
這里的問題是您的互斥體正在相互等待,迫使您的程序經常命中調度程序。
如果您使用更少的線程,您將獲得更好的性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.