4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Macでgperftoolsを使ってCPUプロファイルを取得すると関数名が表示されない

Posted at

Google製の速いmallocを提供するgperftoolsというツールがある。
https://github.com/gperftools/gperftools

このツールにはheap-checker, heap-profiler, cpu-profilerも含まれており、メモリ使用量、メモリリークのチェック、CPUプロファイルの取得なども行うことができる。

Mac上で通常の手順でCPUプロファイルを取得すると、関数のアドレスだけが表示され関数名が表示されないという問題がある。その問題の回避手順を示す。

参考情報

gperftoolsについて

環境

テストした環境

  • MacOSX 10.10
  • clang 7.0.0
  • gperftools 2.4
    • homebrewでインストールした。

問題

gperftoolsでCPUプロファイルを取る際の通常の手順で-lprofilerをつけて、プロファイルを取りたいコードをビルドし、環境変数CPUPROFILEを設定してコードを実行する。
実行した結果はpprofコマンドで確認できる。

g++ test.cpp -lprofiler
env CPUPROFILE=test.prof ./a.out
pprof --text ./a.out test.prof

すると出力が

Using local file ./a.out.
Using local file test.prof.
Total: 195 samples
      80  41.0%  41.0%       80  41.0% 0x000000010a625f54
      49  25.1%  66.2%       49  25.1% 0x000000010a625f62
      28  14.4%  80.5%       28  14.4% 0x000000010a625f5a
      20  10.3%  90.8%       20  10.3% 0x000000010a625f66
      15   7.7%  98.5%       15   7.7% 0x000000010a625f4c
       3   1.5% 100.0%        3   1.5% 0x000000010a625f74

のようになり、関数のアドレスは表示されるが関数名が表示されない。どこがボトルネックになっているのかわからない。

原因と回避方法

関連する情報が以下のページにあった。

まとめると、Mac上ではASLR(address space layout randomization)というセキュリティ上の機能が使用されているため。
この機能はmoduleをロードするとき、ランダムなアドレスに読み込まれる。

回避するにはリンク時に -Wl,-no_pie というオプションをつける。

g++ test.cpp -lprofiler -Wl,-no_pie

すると表示が、

Using local file ./a.out.
Using local file test.prof.
Total: 196 samples
     196 100.0% 100.0%      196 100.0% myfunc

となり、関数名が表示されるようになった。

関連情報

MacでCPUプロファイルを取りたい場合、Xcode付属のInstrumentsを使う方法もある。
http://qiita.com/yohm13/items/450fbae1ee0aebe1261e

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?