perf の出力をCommon Trace Format にするやつを試したニャ（失敗からの動いた）

## Env

```sh
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=17.04
DISTRIB_CODENAME=zesty
DISTRIB_DESCRIPTION="Ubuntu 17.04"
```

## How to setup


**Package install**

```sh
# build for babletrace (maybee)
sudo apt install libnewt-dev binutils-arm-none-eabi libcrypto++-dev libunwind-dev systemtap-sdt-dev libssl-dev libperl-dev libiberty-dev

# build for babeltrace
$ sudo apt install dh-autoreconf bison libdw-dev libelf-dev flex uuid-dev libpopt-dev
```

**clone source**

```sh
# make build dir
$ mkdir perf-build
$ cd perf-build

# checkout babeltrace
$ git clone git://git.efficios.com/babeltrace.git

# checkout babeltrace
sudo git clone git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git
```

**Build and Install Babletrace**

```sh
$ cd babeltrace
$ sudo ./bootstrap
$ sudo ./configure --prefix=/opt/libbabeltrace LDFLAGS=-L/usr/local/lib
$ sudo make -j4 prefix=/opt/libbabeltrace
$ sudo make install prefix=/opt/libbabeltrace

# ** optional ** (私はビルドできなかったので以下を追加でInstall)
$ sudo apt install uuid-dev libnuma-dev
```

**Build and Install Perf with Babeltrace**

ユーザーホーム配下へInstallします

```sh
$ cd linux/tools/perf/
$ sudo git checkout perf/core

# build
$ sudo JDIR=/home/$USER/var/lib/java/jdk-11.0.1  LIBBABELTRACE=1 LIBBABELTRACE_DIR=/opt/libbabeltrace/lib make DISTDIR=/home/$USER/linux
# install
$ sudo JDIR=/home/$USER/var/lib/java/jdk-11.0.1  LIBBABELTRACE=1 LIBBABELTRACE_DIR=/opt/libbabeltrace/lib make install DISTDIR=/home/$USER/linux
```

※ Document ディレクトリに `Build.txt` があるので一読がおすすめ

- `bt_field_signed_integer_get_value` ２重定義をコメントアウト

```c
237 extern int bt_field_signed_integer_get_value(struct bt_field *integer,
238                 int64_t *value);

    こっちをコメントアウト
453 extern int bt_field_signed_integer_get_value(
454                 struct bt_field *integer_field, int64_t *value);
```
- JDIR が”未指定 Warn”出力があり変数を設定
- 参考のコマンドは /opt/libbabeltrace となっているが /opt/libbabeltrace/lib を指定
`bt_ctf_validate_identifier` 未定義エラーが発生



### References

* [RoverTracing](https://app4mc-rover.github.io/rover-docs/_downloads/RoverTracing.pdf)
* [building perf with babeltrace](https://stackoverflow.com/questions/43576997/building-perf-with-babeltrace-for-perf-to-ctf-conversion)
* [Common Trace Format](https://diamon.org/ctf/)


## Examples

sleep して試してみる

```sh
$ sudo  /home/$USER/linux/bin/perf sched record -- sleep 15
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 2.613 MB perf.data (14741 samples) ]

$ sudo LD_LIBRARY_PATH=/opt/libbabeltrace/lib /home/$USER/linux/bin/perf data convert -i perf.data  --to-ctf=./ctf 
[ perf data convert: Converted 'perf.data' into CTF data './ctf' ]
[ perf data convert: Converted and wrote 1.654 MB (14741 samples) ]

$ ls ctf/
metadata  perf_stream_0  perf_stream_1  perf_stream_2  perf_stream_3
```

TraceCompass でみてみる


![tracecompass01.png](https://qiita-image-store.s3.amazonaws.com/0/106271/6b481859-4ccb-47b7-834d-1e06f91b54f4.png)

![tracecompass02.png](https://qiita-image-store.s3.amazonaws.com/0/106271/e704f304-b467-3b33-6ea5-99cd9f2affdc.png)

![tracecompass03.png](https://qiita-image-store.s3.amazonaws.com/0/106271/5ad77db8-c0f3-0a34-aa44-7482c3f1838b.png)

![tracecompass04.png](https://qiita-image-store.s3.amazonaws.com/0/106271/33685e08-6024-5050-b320-c573538e8a31.png)



![tracecompass05.png](https://qiita-image-store.s3.amazonaws.com/0/106271/68608272-df23-2d26-13e6-7088f0baf4b3.png)