1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #define ARRAY_LEN 30000 static struct timeval tm1; static inline void start() { gettimeofday(&tm1, NULL); } static inline void stop() { struct timeval tm2; gettimeofday(&tm2, NULL); unsigned long long t = 1000 * (tm2.tv_sec - tm1.tv_sec) +\ (tm2.tv_usec - tm1.tv_usec) / 1000; printf("%llu ms\n", t); } void bubble_sort (int *a, int n) { int i, t, s = 1; while (s) { s = 0; for (i = 1; i < n; i++) { if (a[i] < a[i - 1]) { t = a[i]; a[i] = a[i - 1]; a[i - 1] = t; s = 1; } } } } void sort_array() { printf("Bubble sorting array of %d elements\n", ARRAY_LEN); int data[ARRAY_LEN], i; for(i=0; i<ARRAY_LEN; ++i) { data[i] = rand(); } bubble_sort(data, ARRAY_LEN); } int main() { start(); sort_array(); stop(); return 0; } |
1 2 | gcc -g -O2 -o test test.c -Wl,-q perf record -e cycles:up -o pmu.data ./test |
AutoBOLT模式获取profile
1 | create_gcov --binary=test --profile=pmu.data --gcov=pmu.gcov --gcov_version=1 --use_lbr=0 |
perf2bolt获取profile
1 | perf2bolt -p=pmu.data test -o pmu.fdata -nl |
该模式必须和选项-fauto-profile或-fprofile-use共同使用,必须增加-Wl,-q保留重定位信息。以test程序为例:
1 | gcc -g -O2 -o test test.c -fauto-profile=pmu.gcov -fauto-bolt -Wl,-q |
或
1 | gcc -g -O2 -o test test.c -fprofile-use -fauto-bolt -Wl,-q |
本次更新中,-fprofile-use和-fauto-profile支持-flto
1 2 3 | gcc -g -O2 -o test test.c -fprofile-generate=./profile -Wl,-q ./test gcc -g -O2 -o test test.c -fprofile-use=./profile -fauto-bolt -flto -Wl,-q |
该模式需要提前准备好BOLT优化所需要的profile。该profile可以使用AutoBOLT模式获取,也可以使用perf2bolt工具获取。
1 | gcc -g -O2 -o test test.c -fbolt-use=pmu.fdata -Wl,-q |