简体   繁体   中英

Find the size of L1 and L2 cache with C++

I need to find the size of L1 and L2 cache for an assignment using a c++ simple program in a Windows operating system. I was able to find the size of the L3 cache in 2 different computers by calculating the time it takes to access the elements in an array in increasing sizes. When the jump in time is big, we go from the cache level to the ram level.

How do I figure out the L1 and L2 sizes from here?

The restriction is that I cannot read config or use built in functions to determine the values. I must time read/write operations instead.

I need to find the size of L1 and L2 cache for an assignment using ac/c++ simple program.

In general, you cannot (in theory).

Since a standard conforming C11 implementation (read n1570 ) don't even need to run on a real computer with caches. And likewise for C++11 or C++14 (read n3337 ).

It could run:

  • with people (using a bunch of slaves to run a C program would be unethical, inefficient, slow, but is possible; using for half an hour students in a classroom is an entertaining way of teaching C or C++ - the entire class becomes a C or C++ implementation).

  • on a computer without any cache. Today, microcontrollers (like Arduino ) can be programmed in C or C++ (and routinely are) and don't have any cache.

  • on your favorite x86-64 laptop. You'll better read more about the way Intel and AMD processors deal with cache.

  • on something else (eg some Power9 motherboard , some Intel Edison , some Raspberry Pi , some emulator in your browser , etc...). You could have surprises!

How do I figure out the L1 and L2 sizes from here?

You could look at the generated assembler code (with GCC compile using g++ -O1 -fverbose-asm -S then look into the generated .s file), imagine what kind of processor and ISA you have, and make some educated guess (from the measured timings). Avoid asking too strong optimizations (since your program probably has undefined behavior ).

On many OSes, you might use operating system specific API to query about the processor. On Linux, you could use proc(5) and use /proc/cpuinfo

If you did run your program, benchmark it several times, and gave the timing, you could make an educated guess about cache sizes (but you need to assume that your process has not been scheduled out too often; on a very loaded system, that is not the case; you should avoid thrashing ).

BTW I guess that under the as-if rule your program might be optimized to something you did not think about (see this ). Notice that using cells inside a new allocated array without initializing them is (in principle) undefined behavior (or at least unspecified behavior ). So I believe a wise enough compiler could optimize your program to the equivalent of some abort() (or maybe just keep the printf ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM