Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplest tool to measure C program cache hit/miss and cpu time in linux?

I'm writing a small program in C, and I want to measure it's performance.

I want to see how much time do it run in the processor and how many cache hit+misses has it made. Information about context switches and memory usage would be nice to have too.

The program takes less than a second to execute.

I like the information of /proc/[pid]/stat, but I don't know how to see it after the program has died/been killed.

Any ideas?

EDIT: I think Valgrind adds a lot of overhead. That's why I wanted a simple tool, like /proc/[pid]/stat, that is always there.

like image 939
jperelli Avatar asked Apr 10 '12 02:04

jperelli


People also ask

How do you determine hit or miss in cache?

To calculate a hit ratio, divide the number of cache hits with the sum of the number of cache hits, and the number of cache misses. For example, if you have 51 cache hits and three misses over a period of time, then that would mean you would divide 51 by 54. The result would be a hit ratio of 0.944.

What is cache miss and cache hit explain with example?

What Is Cache Miss and Hit? A cache miss occurs when a cache doesn't have the requested data in its memory. Meanwhile, a hit is when a cache successfully finds the requested data, satisfying the search query. For a more effective caching system, the hit ratio should be higher than the miss rate.

What is miss rate in cache memory?

Similarly, the miss rate is the number of total cache misses divided by the total number of memory requests made to the cache. One might also calculate the number of hits or misses on reads or writes only. Clearly, a higher hit rate will generally result in higher performance.


2 Answers

Use perf:

perf stat ./yourapp 

See the kernel wiki perf tutorial for details. This uses the hardware performance counters of your CPU, so the overhead is very small.

Example from the wiki:

perf stat -B dd if=/dev/zero of=/dev/null count=1000000  Performance counter stats for 'dd if=/dev/zero of=/dev/null count=1000000':          5,099 cache-misses             #      0.005 M/sec (scaled from 66.58%)       235,384 cache-references         #      0.246 M/sec (scaled from 66.56%)     9,281,660 branch-misses            #      3.858 %     (scaled from 33.50%)   240,609,766 branches                 #    251.559 M/sec (scaled from 33.66%) 1,403,561,257 instructions             #      0.679 IPC   (scaled from 50.23%) 2,066,201,729 cycles                   #   2160.227 M/sec (scaled from 66.67%)           217 page-faults              #      0.000 M/sec             3 CPU-migrations           #      0.000 M/sec            83 context-switches         #      0.000 M/sec    956.474238 task-clock-msecs         #      0.999 CPUs     0.957617512  seconds time elapsed 

No need to load a kernel module manually, on a modern debian system (with the linux-base package) it should just work. With the perf record -a / perf report combo you can also do full-system profiling. Any application or library that has debugging symbols will show up with details in the report.

For visualization flame graphs seem to work well. (Update 2020: the hotspot UI has flame graphs integrated.)

like image 82
maxy Avatar answered Sep 19 '22 21:09

maxy


The best tool for you is called valgrind. It is capable of memory profiling, call-graph building and much more.

sudo apt get install valgrind valgrind ./yourapp 

However, to obtain the time your program executed, you can use time(8) linux utility.

time ./yourapp 
like image 28
iehrlich Avatar answered Sep 17 '22 21:09

iehrlich