gprof

gprof 是什么?

gprof 是一个用来分析应用程序把时间花费在哪的性能分析工具.

gprof 怎么用?

  1. 编译和链接程序使用 -pg 选项;
  2. 执行程序, 产生 gmon.out 数据文件;
  3. 使用 gprof 读取 gmon.out 数据文件产生报告;
//使用 **-pg** 参数编译你的程序
pi@raspberrypi:~ $ echo 'int main(){}' | gcc -o hello -xc -pg -O3 -
pi@raspberrypi:~ $ ls 
hello
pi@raspberrypi:~ $ ./hello
pi@raspberrypi:~ $ ls -lah 
gmon.out  hello

pi@raspberrypi:~ $ which gprof
/usr/bin/gprof

pi@raspberrypi:~ $ gprof hello gmon.out

gprof - how it works?

要想使用 gprof 做性能分析, 必须在编译程序的时候注入一些代码, 这些代码在运行时搜集代码的执行情况, 把搜集的数据放到当前路径下的 gmon.out 文件. 然后 gprof 读这个文件, 生成 report.
实现细节: https://sourceware.org/binutils/docs/gprof/Implementation.html
使用 -pg 选项编译的时候, 每个函数的开头都条用 mcount (monitor count). mcount 在不同的平台实现不同, 不过代码都在 profiling 的 lib 里面, 它负责记录并维护这些 call graph 的数据: 包括当前函数以及谁 call 的当前函数. 一般当前函数以及父函数都是通过遍历栈帧的方式获得的.

The mcount routine, included in the profiling library, is responsible for recording in an in-memory call graph table both its parent routine (the child) and its parent's parent. This is typically done by examining the stack frame to find both the address of the child, and the return address in the original parent. Since this is a very machine-dependant operation, mcount itself is typically a short assembly-language stub routine that extracts the required information, and then calls __mcount_internal (a normal C function) with two arguments - frompc and selfpc. __mcount_internal is responsible for maintaining the in-memory call graph, which records frompc, selfpc, and the number of times each of these call arcs was transversed.

库函数的调用记录是通过特殊的库版本实现的, 通常这些库都是使用-pg选项生成的.

为什么是 pg ?

在 gprof 之前, 有个性能分析程序是 prof, 要使用 prof 这个工具, gcc 给的参数是 -p, 那么轮到 gprof 的时候, 就使用参数 -pg

pg 之外的其它编译参数

-g line-by-line profiling
-a if 的每个分支, loop 的每次循环

refer:
https://hpc.llnl.gov/software/development-environment-software/gprof
https://en.wikipedia.org/wiki/Gprof

Java 性能优化视频笔记

https://thestrangeloop.com/2017/the-performance-engineers-guide-to-java-hotspot-virtual-machines-execution-engine.html
这个视频其实和她的那本书基本是一致的, 加了些那年比较新的东西.

  1. JDK 是我们开发用的开发工具集, 里面包含 JRE 和一些工具, 比如 jps, cmd, JVisualVM 等;
  2. JRE 是 Java 运行时引擎, 是 Java 代码跑起来的必要设施. 它处于用户代码和 OS 之间;
  3. API 是我们的代码和 JRE 之间的接口;
  4. JRE 里面主要的功能是 编译 和 内存管理, 所以 JRE 层面的优化主要是编译和内存管理(heap)方面;
  5. Java 的内存管理主要是管理 内存的分配和内存的回收 -> 内存管理算法 -> CMS, G1, etc;
  6. 编译分为 静态编译 和 动态运行时编译;
  7. JIT 优化: 提前编译, 分层编译, 基于已搜集信息的编译(profile-based), 启发式编译;
  8. Java 代码是先翻译(javac 成 class 文件字节码)然后再部分编译(JIT);
  9. JRE(运行时)的主要目的就是把字节码转换成操作系统 native 代码;
  10. 内存分配-> 快速分配 -> TLAB & PLAB;
  11. 锁: 偏向锁, 轻量锁, 锁膨胀, 锁消除;
  12. 代码: inline, 逃逸分析, 代码消除, 栈上分配, 寄存器分配;
  13. String 实现的优化 itern();

MAC 的 PATH 环境变量

在 MAC 上工作, 竟然遇到要更改 PATH 环境变量的情况. 比如有的 Java 工程是基于 JDK8 的, 有的是基于 JDK11 的, 所以经常要改这个变量. 那么我们看到的 PATH 环境变量是从哪里来的, 那些配置文件会改这些 PATH 值呢?

首先, 我们先看下当前的 PATH 变量:

~ xiatian$ echo $PATH
/usr/local//Cellar/curl/7.80.0_1/bin/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/Users/xiatian/work/tools:/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/

这里面有好多路径, 那么我们好奇当系统启动的过程中, 最初的 PATH 是什么呢?

  1. MAC 上最初的 PATH 是从 /etc/paths 文件读的:

    ~ xiatian$ cat /etc/paths
    /usr/local/bin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
  2. 然后, 它会把 /etc/path.d 里面的路径逐个添加到后面, 比如我们可以看到我的 wireshark 路径在上面的 PATH 里:

    ~ xiatian$ ls -l /etc/paths.d/
    total 8
    -rw-r--r--  1 root  wheel  43 Oct 19  2017 Wireshark
    ~ xiatian$ cat /etc/paths.d/Wireshark
    /Applications/Wireshark.app/Contents/MacOS
  3. 在个人的 home 目录, 还有2个文件会影响 PATH 的值: .bash_profile & .bash_rc. 他们的区别是: 当一个 shell 是 login shell 的时候, 它会先执行 .bash_profile, 当是一个非 login 交互式 shell 的时候, 它执行 .bashrc. 但是在 MAC 上, 默认都是 login shell, 所以应该都执行. 来源于这个问答. 我试过了自带的 Terminal 和 安装的 iterm, 发现我的 MAC 都不是这么玩的.
  4. 我本地是执行的 ~/.profile. 查看 bash 的 man, 你会在 INVOCATION section 看到具体的配置执行顺序:

    ~ xiatian$ man bash
    When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file  /etc/profile,  if that  file exists.  After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.  The --noprofile option may be used when the shell is started to inhibit this behavior.
    When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.  This may be inhibited by using the --norc option.  The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

所以, 只要仔细阅读 man bash 的内容, 你就知道 PATH 是怎么变化的啦.