There’s a special performance and counter subsystem is added to linux kernel (since 2.6.31), and can be enabled by CONFIG_PERF_EVENTS in
Symbol: PERF_EVENTS [=y]
│ Prompt: Kernel performance events and counters
│ Defined at init/Kconfig:983
│ Depends on: HAVE_PERF_EVENTS [=y]
│ -> General setup
│ -> Kernel Performance Events And Counters
│ Selects: ANON_INODES [=y]
There’s a user space utility called “perf” which makes the job quite easy. For example, if you want to do profiling for a command, here’s what you need to do.
# perf record -a dd if=/dev/zero oflag=direct of=/tmp/zerofile bs=1024 count=16384
# perf report
1st command will take the performance data, and 2nd will do the reporting (quite nice) from whatever data is collected by 1st command. Really nice.
The entire wiki on how to use perf can be found at : https://perf.wiki.kernel.org/index.php/Tutorial