Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hardware Performance Counters in Linux

I want to use the Hardware Performance Counters that come with the Intel and AMD x86_64 multicore processors to calculate the number of retired stores by a program. I want each thread to calculate its retired stores separately. Can it be done? And if so, how in C/C++?

like image 537
MetallicPriest Avatar asked Aug 18 '11 13:08

MetallicPriest


People also ask

Which Linux tool provides data from hardware performance counters?

perf (sometimes called perf_events or perf tools, originally Performance Counters for Linux, PCL) is a performance analyzing tool in Linux, available from Linux kernel version 2.6.

What is meant by hardware performance counters?

In computers, hardware performance counters (HPC), or hardware counters are a set of special-purpose registers built into modern microprocessors to store the counts of hardware-related activities within computer systems. Advanced users often rely on those counters to conduct low-level performance analysis or tuning.

What are counters in Linux?

Performance Counters for Linux (PCL) is a new kernel-based subsystem that provides a framework for collecting and analyzing performance data. These events will vary based on the performance monitoring hardware and the software configuration of the system.

What is perf command in Linux?

The perf command is used as a primary interface to the Linux kernel performance monitoring capabilities and can record CPU performance counters and trace points.


2 Answers

You can use Perfctr or PAPI if you want to count hardware events on some part of the program internally (without starting any 3rd party tool).

Perfctr quickstart: http://www.ale.csce.kyushu-u.ac.jp/~satoshi/how_to_use_perfctr.htm

PAPI homepage: http://icl.cs.utk.edu/papi/

PerfSuite good doc: http://perfsuite.ncsa.illinois.edu/publications/LJ135/x27.html

If you can do this externally, there is a perf command of modern Linux.

perf wiki: https://perf.wiki.kernel.org/index.php/Main_Page

like image 141
osgx Avatar answered Oct 06 '22 09:10

osgx


The best approach will be using perf in linux as osgx mentioned, as it is part of linux kernel. But it CAN be called in the C/C++ code as well, and there is no need for it to be external perf stat calls.

Just download the kernel source code and take a look at it. Or alternatively take a look at this library I think by google:

http://perfmon2.sourceforge.net/docs_v4.html

it is part of perfmon2 project but is designed to work with perf. Take a look at perf_examples directory and you will get the idea. That is how I handle perf calls from within my C codes.

like image 25
Saman Barghi Avatar answered Oct 06 '22 09:10

Saman Barghi