
Chapter 3: General-Purpose Programming
121
24593—Rev. 3.09—September 2003
AMD 64-Bit Technology
Self-Modifying Code.
Software that writes into a code segment is
classified as self-modifying code. To avoid cache-coherency
problems due to self-modifying code, implementations of the
AMD64 architecture invalidate a cache line during a memory
write if the cache line corresponds to a code-segment memory
location. By invalidating the cache line, the processor is forced
to write the modified instruction into main memory. A
subsequent fetch of the modified instruction goes to main
memory to get the coherent version of the instruction.
3.9.5
Cache Pollution
Because cache sizes are limited, caches should be filled only
with data that is frequently used by an application. Data that is
used infrequently, or not at all, is said to
pollute
the cache
because it occupies otherwise useful cache lines. Ideally, the
best data to cache is data that adheres to the
principle of locality
.
This principle has two components:
temporal locality
and
spatial
locality
.
Temporal locality
refers to data that is likely to be used more
than once in a short period of time. It is useful to cache
temporal data because subsequent accesses can retrieve the
data quickly. Non-temporal data is assumed to be used once,
and then not used again for a long period of time, or ever.
Caching of non-temporal data pollutes the cache and should
be avoided.
Cache-control instructions (“Cache-Control Instructions” on
page 122) are available to applications to minimize cache
pollution caused by non-temporal data.
Spatial locality
refers to data that resides at addresses
adjacent to or very close to the data being referenced.
Typically, when data is accessed, it is likely the data at
nearby addresses will be accessed in a short period of time.
Caches perform cache-line fills in order to take advantage of
spatial locality. During a cache-line fill, the referenced data
and nearest neighbors are loaded into the cache. If the
characteristics of spacial locality do not fit the data used by
an application, then the cache becomes polluted with a large
amount of unreferenced data.
Applications can avoid problems with this type of cache
pollution by using data structures with good spatial-locality
characteristics.
Another form of cache pollution is
stale data
. Data that adheres
to the principle of locality can become stale when it is no longer