I was going through this link where they are dealing with the statistical data for latencies of main memory, L1 and L2 cache.
I was wondering is it possible to compute the same using a C/c++ code without using the benchmarks tools?
I was going through this link where they are dealing with the statistical data for latencies of main memory, L1 and L2 cache.
I was wondering is it possible to compute the same using a C/c++ code without using the benchmarks tools?
The benchmark tools, like LMBench, are written in C. So when you ask if it can be done in C, the answer is quite simply, "yes".
LMBench tests memory latency (in lat_mem_rd.c
) by doing repeated pointer indirections. This is the same thing as following a linked list, except there is no content in the list, just a pointer to the next cell.
struct cell { struct cell *next };
struct cell *ptr = ...;
for (i = 0; i < count; i++) {
ptr = ptr->next;
ptr = ptr->next;
... 100 of these, unrolled ...
ptr = ptr->next;
ptr = ptr->next;
}
By adjusting the size of the list, you can control whether the memory accesses hit L1 cache, L2 cache, or main memory. If you are testing L2 cache or main memory, however, you will need to ensure that each memory access is to a cache line old enough that it has been evicted from the faster caches by the time you access it again. Some caches also have support for prefetching, so a "strided" approach may also mean that you hit a faster cache, for certain strides.
You will also need to be sure to enable optimizations (-O2
, with GCC/Clang). Otherwise ptr
may get stored on the stack, increasing the latency. Finally, you will need to make sure that the compiler does not consider ptr
to be a "dead" variable. A sophisticated compiler might notice that the above code doesn't actually do anything. Sometimes when writing benchmarks, the compiler is the enemy. The LMBench code has a function use_pointer()
just for this purpose.