28.04.2013 Views

What Every Programmer Should Know About Memory

What Every Programmer Should Know About Memory

What Every Programmer Should Know About Memory

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

that the code pages are immutable, the L1i implementation<br />

does not use the MESI protocol but instead a simplified<br />

SI protocol. This means if modifications are detected<br />

a lot of pessimistic assumptions have to be made.<br />

It is highly advised to avoid SMC whenever possible.<br />

<strong>Memory</strong> is not such a scarce resource anymore. It is<br />

better to write separate functions instead of modifying<br />

one function according to specific needs. Maybe one day<br />

SMC support can be made optional and we can detect<br />

exploit code trying to modify code this way. If SMC absolutely<br />

has to be used, the write operations should bypass<br />

the cache as to not create problems with data in L1d<br />

needed in L1i. See section 6.1 for more information on<br />

these instructions.<br />

On Linux it is normally quite easy to recognize programs<br />

which contain SMC. All program code is write-protected<br />

when built with the regular toolchain. The programmer<br />

has to perform significant magic at link time to create<br />

an executable where the code pages are writable. When<br />

this happens, modern Intel x86 and x86-64 processors<br />

have dedicated performance counters which count uses<br />

of self-modifying code. With the help of these counters it<br />

is quite easily possible to recognize programs with SMC<br />

even if the program will succeed due to relaxed permissions.<br />

3.5 Cache Miss Factors<br />

We have already seen that when memory accesses miss<br />

the caches the costs skyrocket. Sometimes this is not<br />

avoidable and it is important to understand the actual<br />

costs and what can be done to mitigate the problem.<br />

3.5.1 Cache and <strong>Memory</strong> Bandwidth<br />

To get a better understanding of the capabilities of the<br />

processors we measure the bandwidth available in optimal<br />

circumstances. This measurement is especially interesting<br />

since different processor versions vary widely.<br />

This is why this section is filled with the data of several<br />

different machines. The program to measure performance<br />

uses the SSE instructions of the x86 and x86-64<br />

processors to load or store 16 bytes at once. The working<br />

set is increased from 1kB to 512MB just as in our other<br />

tests and it is measured how many bytes per cycle can be<br />

loaded or stored.<br />

Figure 3.24 shows the performance on a 64-bit Intel Netburst<br />

processor. For working set sizes which fit into L1d<br />

the processor is able to read the full 16 bytes per cycle,<br />

i.e., one load instruction is performed per cycle (the<br />

movaps instruction moves 16 bytes at once). The test<br />

does not do anything with the read data, we test only the<br />

read instructions themselves. As soon as the L1d is not<br />

sufficient anymore the performance goes down dramatically<br />

to less than 6 bytes per cycle. The step at 2 18 bytes<br />

is due to the exhaustion of the DTLB cache which means<br />

additional work for each new page. Since the reading<br />

Bytes/Cycles<br />

Bytes/Cycles<br />

Bytes/Cycles<br />

16<br />

14<br />

12<br />

10<br />

8<br />

6<br />

4<br />

2<br />

0<br />

16<br />

14<br />

12<br />

10<br />

2 10 2 13 2 16 2 19 2 22 2 25 2 28<br />

Working Set Size (Bytes)<br />

8<br />

6<br />

4<br />

2<br />

0<br />

Read Write Copy<br />

Figure 3.24: Pentium 4 Bandwidth<br />

2 10 2 13 2 16 2 19 2 22 2 25 2 28<br />

Working Set Size (Bytes)<br />

Read Write Copy<br />

Figure 3.25: P4 Bandwidth with 2 Hyper-Threads<br />

16<br />

14<br />

12<br />

10<br />

8<br />

6<br />

4<br />

2<br />

0<br />

2 10 2 13 2 16 2 19 2 22 2 25 2 28<br />

Working Set Size (Bytes)<br />

Read Write Copy<br />

Figure 3.26: Core 2 Bandwidth<br />

32 Version 1.0 <strong>What</strong> <strong>Every</strong> <strong>Programmer</strong> <strong>Should</strong> <strong>Know</strong> <strong>About</strong> <strong>Memory</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!