Measure FLOPS on Linux using perf
Sometimes, we want to obtain the floating point operations a C++ application executed to measure the performance on a single node.
One option is the perf proivied by the linux kernel tools.
Compile perf
If perf is not installed on the Linux operating system, we need to compile perf as the following:
git clone --depth 1 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux/tools/perf
makeCompile libpfm
To obtain the events, we want to measure using perf another tool libpfm is needed. We compile libpfm as the following
git clone git://perfmon2.git.sourceforge.net/gitroot/perfmon2/libpfm4
cd libpfm4
makeObtain the event id
To obtain all available events on the Intel CPU, we run
./examples/showevtinfoThis produces a long list of CPU information and available events, which we will have a look at. However, we are interested in floating point operations. Therefore we search in the long output for floating point operations resulting in the following events:
IDX : 668991557
PMU name : skx (Intel Skylake X)
Name : FP_ARITH
Equiv : FP_ARITH_INST_RETIRED
Flags : None
Desc : Floating-point instructions retired
Code : 0xc7
Umask-00 : 0x01 : PMU : [SCALAR_DOUBLE] : None : Number of scalar double precision floating-point arithmetic instructions (multiply by 1 to get flops)
Umask-01 : 0x02 : PMU : [SCALAR_SINGLE] : None : Number of scalar single precision floating-point arithmetic instructions (multiply by 1 to get flops)
Umask-02 : 0x04 : PMU : [128B_PACKED_DOUBLE] : None : Number of scalar 128-bit packed double precision floating-point arithmetic instructions (multiply by 2 to get flops)
Umask-03 : 0x08 : PMU : [128B_PACKED_SINGLE] : None : Number of scalar 128-bit packed single precision floating-point arithmetic instructions (multiply by 4 to get flops)
Umask-04 : 0x10 : PMU : [256B_PACKED_DOUBLE] : None : Number of scalar 256-bit packed double precision floating-point arithmetic instructions (multiply by 4 to get flops)
Umask-05 : 0x20 : PMU : [256B_PACKED_SINGLE] : None : Number of scalar 256-bit packed single precision floating-point arithmetic instructions (multiply by 8 to get flops)
Umask-06 : 0x40 : PMU : [512B_PACKED_DOUBLE] : None : Number of scalar 512-bit packed double precision floating-point arithmetic instructions (multiply by 8 to get flops)
Umask-07 : 0x80 : PMU : [512B_PACKED_SINGLE] : None : Number of scalar 512-bit packed single precision floating-point arithmetic instructions (multiply by 16 to get flops)Now, we now the name FP_ARITH of the event and the mask 01 of the scalar double operations, we want to track using perf. To obtain the id for perf, we need to use the tool check_events provided by libpfm and execute the following command:
./examples/check_events FP_ARITH:0x01which will output plenty of information, however, we are only interested in the last few lines:
Requested Event: FP_ARITH:0x01
Actual Event: skx::FP_ARITH_INST_RETIRED:0x1:k=1:u=1:e=0:i=0:c=0:t=0:intx=0:intxcp=0
PMU : Intel Skylake X
IDX : 668991558
Codes : 0x5301c7The code 5301c71 is now used within perf to measure the event by using -e r5301c7.
Collecting performance with perf
Now, since we collected the id of the each monitored events, we can run perf to collect the events, which in our case are the floating point operations, as the following
./perf stat -e r5301c7 -e r5302c7 -e r5304c7 ./taylor 1000000000 0.5 and receive the following output
Performance counter stats for './taylor 1000000000 0.5 1':
100,000,028,581 r5301c7:u
0 r5302c7:u
0 r5304c7:u
91.822704139 seconds time elapsed
90.294908000 seconds user
1.401873000 seconds syswhich indicates that 100,000,028,581 floating point operations using the double type were executed. Now we can use the obtained time and compute the FLOP/s as 100,000,028,581/91.822704139=1.09e9.