Rate This Document
Findability
Accuracy
Completeness
Readability

-fauto-profile,-fprofile-correction

Description

Different from instrumentation-based FDO, AutoFDO uses perf to collect program running information, uses the create_gcov tool to parse the sampling information from perf to the profile required by the compiler, and uses the -fauto-profile option to read the profile to complete optimization.

The -fprofile-correction option is used to enable the MCF algorithm to smooth the basic block count imbalance caused by sampling.

Usage

The test case test.c is as follows:

 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. Install a software package.

    For AutoFDO, you need to use the perf tool to sample program hotspot information and use the create_gcov tool to generate the profile required by the compiler. In openEuler 22.03 LTS or later, you can run the following command to install it:

    1
    yum install perf autofdo
    
  2. Add debugging information during compilation.

    AutoFDO requires that the program contain debugging information. Therefore, you need to use the -g option to add debugging information. The following uses the test program as an example:

    1
    gcc -g -O2 -o test test.c
    
  3. Generate a profile.

    AutoFDO uses perf to collect profiles.

    • For an Arm architecture, run the following command to collect the perf file and generate the profile:
      1
      2
      perf record -e inst_retired:u -- ./test
      create_gcov --binary=./test --profile=perf.data --gcov=test.gcov --gcov_version=1 --use_lbr=0
      
    • For an x86_64 architecture, run the following command to collect the perf file and generate the profile:
      1
      2
      perf record -b -e br_inst_retired.near_taken:pp -- ./test
      create_gcov --binary=./test --profile=perf.data --gcov=test.gcov --gcov_version=1
      
  4. Enable optimization.

    The compiler option -fauto-profile is added to enable AutoFDO.

    1
    gcc -g -O2 -o test test.c -fauto-profile=test.gcov
    

    (Optional) Add the compiler option -fprofile-correction to enable the mcf algorithm.

    1
    gcc -g -O2 -o test test.c -fauto-profile=test.gcov -fprofile-correction
    

    Note: The effect of the mcf algorithm depends on the actual application scenario. You are advised to use the mcf algorithm after thorough tests.