I am currently learning about caches in Systems class, and I had a few doubts about what exactly happens when a Computer reads a PDF. This is the sequence that happens in my mind:

  1. The CPU checks if the pages of the PDF I want to read are there in cache.
  2. It is not, so we keep going down the Cache hierarchy until we get to the Hard Drive, from where the PDF is located and read into main memory.
  3. From main memory, it goes all the way back to L1 cache, and the CPU reads it.

Is there anything wrong with the simplification I have given above?

I know that Cache entries use the effective memory address of its data to determine where the data will be placed in the structure. Here, in this example, what exactly will be the memory address of the PDF?

  • 2
    $\begingroup$ Aren't you confusing memory pages and document pages ? $\endgroup$ – Yves Daoust Oct 15 '15 at 7:32
  • 1
    $\begingroup$ @YvesDaoust Not necessarily but +1 for checking. ReiJin, it would be helpful if you'd clarify whether this question is specific to PDFs or applies to files in general. $\endgroup$ – David Richerby Oct 15 '15 at 9:25
  • $\begingroup$ Files in general, I guess. The PDF example was something I came up with when trying to explain it to someone. $\endgroup$ – ReiJin ThunderKeg Oct 15 '15 at 14:55

When a computer reads a PDF... You can take several perspectives.

From Acrobat reader, it asks the operating system to read a file, copies it to memory then interprets drawing commands. From applications POV, caches don't exist.

Of course, there is nowadays a disk cache in RAM, where the last used sectors are kept for shorter access time. Some OS even trick the MMU between the disk cache and application address spaces so that data is not even copied. That cache is not visible to applications (which can also have their own caching, for example pre-rendered pages...)

From the operating system, CPU caches needs sometimes to be manually managed, on some platform, particularly when there are peripherals that are modifying memory without CPU intervention ("non coherent DMA"). It could be the disk controller reading that PDF file. On current high performance CPUs, the kind you find in computers or tablets where Acrobat can be installed, the CPUs practically manage their caches transparently.

So, CPU caches are mostly hidden to software.

Cache are made of two parts : the datas, and the tags. Datas contains a portion of RAM area. Tags indicate which part of the RAM is in the cache. Data is grouped in chunks ("cache line"), usually 32 to 128 consecutive bytes.

When a CPU accesses its caches, it lasts usually less than 5 cycles for L1 caches, and less than 20 cycles for L2 caches. Accesssing DRAM can waste hundreds of cycles. Despite being very small (for example 32kB L1 compared to 4GB DRAM), they are incredibly efficient, because, in reality, accesses are often grouped, or consecutive, or repetititve, so that the same parts of main memory are accesses several times.

| cite | improve this answer | |

Whatever the context, the concept of a cache works as follows:

  • when you want to access an item, you first check if it is stored in the cache;

  • if yes (cache hit), you can access the object in the cache, at a low cost;

  • if no (cache miss), you load the item from another level (at a higher cost) and copy it to the cache (possibly discarding another item to make room); then you are back in the first situation.

When modifying the item, you have the option to write it only to the cache (write-back), or to the other level through the cache (write-through). In the first strategy, the write to the other level is deferred until the item is discarded from the cache.

A cache is beneficial in case of data reuse. That means if you don't just read or write an item once, but do it several times in a relatively short delay (before the item is superseded by others).

In some situations, you have the possibility to fill the cache ahead of time (in parallel with other tasks), so that when the data is needed, access is fast.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.