Archived

This topic is now archived and is closed to further replies.

soehrimnir

what is a cache miss?

Recommended Posts

soehrimnir    122
Yesterday I received my copy of ''Game Programming Gems 2''. And the first two articles (''code optimization'' and ''inline functions vs. macro''s'') both use the word ''cache miss''. Can anyone tell me what a cache miss is. I don''t know what it means, but I guess it''s bad because an instruction has to be resent. Thanks

Share this post


Link to post
Share on other sites
BeerNutts    4400
Your cache is basically like a very small part of memory, only much, much faster to access data from. However, since it is so small, it can only hold so much data and so many instructions in it (it stores both data and instructions, called D-cache, and I-cache, respectively).
So, what happens is, if the process is supposed to execute an instruction stored in memory address X, it first checks to see if that location in memory is already stored in the cache, and if it is, it pulls it from there (very fast), other wise, it ''misses'' the instruciton in cache (that memory lcation is not stored in cache), so, it pulls the instruction form memory (much slower), and executes it (it also stored that piece of memory in cache).
There''s actually a little more to it than that, but that''s the basics.

Share this post


Link to post
Share on other sites
Brother Bob    10344
Variables are stored in the system memory, and as they are accessed, they are put in the cache (this is controlled by the processor, not the OS/program) for fast access next time. You can''t really say variables are stored in the cache. They are stored in system memory, and the cache is a small copy of that memory. As you access different parts of the memory, a variable that''s stored in the cache will/might be thrown out of the cache, just to be replaced by the new data your program accesses.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Just adding an important point: cache is not an addressable memory region. There is no way to directly access / change the contents of the cache. The stack is just a normal piece of system RAM that happens to be attributed to a stack. You can change it at will, even if your OS might generate a GPF, it''s physically still addressable, cache is not. But you can modify the behaviour of the cache using instructions like ''prefetch'', to avoid cache misses.

BTW: there is actually a method to *directly* access cache lines, AFAIK, through test registers tr0 to tr6. I''m not sure, if they still exist on newer CPUs, but they used to on Pentium1. They are almost not documented, and very dangerous to use, since they were developed to debug the CPU hardware. In my beginning days as a programmer, I tried to use them as general purpose registers, and effects were *very* weird...
- AH

Share this post


Link to post
Share on other sites
ashayh    122

Is there any way I can write a program to generate a cache miss at a particular line of code? That is, its garunteed that the variable I am reading/writing is not in cache ?
Will a cache miss occur if I directly try and read a random memory location for example ?

OTOH Can I generate a cache hit ?

I am interested in measuring the time taken to access cache Vs Main memory.(if that''s even possible)

Thanks

Share this post


Link to post
Share on other sites
dede    132
quote:
Original post by ashayh

Is there any way I can write a program to generate a cache miss at a particular line of code? That is, its garunteed that the variable I am reading/writing is not in cache ?
Yes, the easiest way to generate a cache miss, is to transverse the wrong way in a 2d array. C++ is row major, so memory is stores like this

1 2 3 4 5 6 ...
7 8 9 10 ...
. .
. .
. .

So if your rows are larger than your cache, every access will end up being a cache miss.

This was true of PI, I''m not sure if its still avalible. There is a great program that I can''t remember the name, that is purposely written to never use the cache. Its goal was to generate a cache miss on every single instruction for the PI, I think it was pretty close to reaching its goal, I''d have to dig around for that web site.
quote:

I am interested in measuring the time taken to access cache Vs Main memory.(if that''s even possible)
That data should be in an optimization guide. It is possible, however, since you have to use a timer function that won''t be cached, you might not be able to get exact figures.

~~~~~
"I don''t want people whining...when the Reply To Topic button turns into a Please Ban Me button" - 23yearold3yearold.
Download and play Slime King I.

Share this post


Link to post
Share on other sites