MetallicPriest
MetallicPriest

Reputation: 30765

Is it possible to peg a memory location to a cache

It is possible that a variable in user space program is fixed to a cache location?

Upvotes: 0

Views: 200

Answers (3)

@MetallicPriest, you keep asking mysterious questions without motivating them enough..... You should try, when asking questions here, to ask a question with a short & precise phrase -you do that very well- and then to motivate and give the context of your question in a whole paragraph (you never do that, and you really should). If possible, also give pointers to the current state of your mysterious software (e.g. a public GIT repository).

AFAIK, for an application program on Linux (at least x86 or AMD64), the processor cache (if it is this cache you are speaking of) almost does not exist (i.e. is not really visible from the application point of view).

However, the process might (or not) have some machine instructions to deal with the cache. AFAIK, these are model specific (i.e. an AMD Phenom processor might behave differently than an Intel i7).

Fortunately for you, the latest GCC compiler is able (when optimizing) to generate cache related instructions. It also gives you some cache related builtins like __builtin___clear_cache or __builtin_prefetch. However, be aware that these builtins have imprecise meaning, and are implemented in various ways, and probably depend of your optimization options and your target specific options given to gcc

And probably the volatile attribute of GCC may help you w.r.t. caching (by "removing" it).

At last, a variable (in an application program on Linux) cannot always stay in the cache. For instance, since context switches are initiated by the kernel, a cache line will become used by another process (and its data) from time to time. Actually, the kernel is even sometimes moving an active process from one core to another of your multi-core desktop processor!

Upvotes: 3

Brendan
Brendan

Reputation: 37232

In general, it is possible to do it in some way on most modern CPUs; either by using explicit support in the CPU (which 80x86 doesn't support), by hacks (e.g. loading it into the cache and then disabling cache line fills on modern 80x86), or by preventing other data from causing the cache line/s to be evicted via. other means (e.g. pound the daylights out of the CPU with "CLFLUSH" regularly).

However "in general" doesn't mean "for Linux specifically". I doubt Linux supports these things, especially for user-space processes.

The reason why I doubt Linux supports it is that there is no sane reason for Linux (or any other general purpose OS) to support it. If something is used regularly then it'll be in the CPU's cache anyway; and if something isn't used regularly then you don't want it to waste space in the cache and slow everything else down.

There are only really 3 sane reasons for it. The first is firmware code that runs before RAM chips are initialised (which I doubt applies here). The second reason is extreme "hard real time" where the extra latency of a cache miss can't be tolerated (which I doubt applies here and probably doesn't apply anywhere else either). The third reason is to work around special cases where the normal "least recently used" eviction policy of caches leads to problems (like cache pollution) where there's much better ways of fixing/avoiding these problems (e.g. prevent the unnecessary data from being cached, or flush it soon after it gets into the cache).

Basically, I'm suggesting that you think you might want to force something to stay in the cache, but you actually don't want to force something to stay in the cache.

Upvotes: 2

starrify
starrify

Reputation: 14751

Sorry dude but as far as I know, this can't be done in user space but only in kernel space.

Upvotes: -1

Related Questions