4

I want to know if there are implemented stuff in C# which allows to access CPU cache. It is just interesting for me but I do not have something to do with the cpu cache at the moment. So I was wondering if it is a system limited access or it is avilable to users also. I am talking about L1/L2 or whatever they are called!

Would be nice to hear your comments, external linkes and maybe some code snippets! Thanks.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38
Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • It is impossible to write real C# code that does *not* access the cpu cache. All memory reads and writes go through the cache. This question makes little sense. – Hans Passant May 27 '11 at 13:15
  • @Hans Well I am aware that want it or not the cache is going to be filled up with some 0 and 1s but the question was if we can have any kind of control over what is going on there in the cache. – Dumbo May 27 '11 at 13:24
  • Make this an answerable question by explaining why you think you need to control the cache. – Hans Passant May 27 '11 at 14:54
  • @Hans Well I was just wondering if such thing is even exist, Then you are right I should have ask the question another way. – Dumbo May 27 '11 at 16:16
  • @HansPassant maybe for http://dl.acm.org/citation.cfm?id=2046670 – Offler Mar 14 '14 at 06:53
  • @HansPassant Maybe someone adventurous want to build a OS and BIOS that start from CPU cache instead of RAM, genius. –  Aug 02 '14 at 20:59

3 Answers3

11

No programming language has direct access to CPU cache. Reading and writing the cache is something done automatically by the hardware; there's no way to write instructions which treat the cache as any kind of separate entity. Reads and writes to the cache happen as side-effect to all instructions that touch memory.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • A good general answer, perhaps, but [ARM9 can in fact dump cache for review in software](http://stackoverflow.com/questions/1006981/read-cpu-cache-contents?rq=1). I assume it maintains copies of the cache such that some proxy registers are read by software, without being affected by the same operations that are affecting the _actual_ cache. – Engineer Nov 27 '13 at 20:29
  • That does not seem to be true. I have seen OS developments where for shared files the L2 is explicitly used. THerefore the OS must have a bit of control, and the OS is programmed in a programming language. – Offler Mar 14 '14 at 06:51
4

C# is a bit too high level for this type of activity. Unless you are calling some other library that was written in C/ASM =) Of course, working with the cache in general is a pretty obscure topic, and for the most part it is controlled entirely by hardware. On the other hand, there are some pretty interesting questions / discussions on SO that you may have already run across.

C++ cache aware programming
How can I do a CPU cache flush in x86 Windows?

At any rate, despite the limitations, you can still predict (somewhat) what the cache is going to do and take advantage of that for special applications.

Community
  • 1
  • 1
A.R.
  • 15,405
  • 19
  • 77
  • 123
  • Good point AR, but it becomes harder to do this when you have multiple cores and shared L2 caches and so on. – Robinson May 27 '11 at 12:23
  • Don't get me wrong, this isn't something I would try with a ten foot pole. I can think of more exciting ways to get a headache =D – A.R. May 27 '11 at 12:33
-1
public static string GetComponent(string hwclass, string syntax)
    {
        var mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwclass);
        foreach (var o in mos.Get())
        {
            var mj = (ManagementObject)o;
            CpuModel = Convert.ToString(mj[syntax]);
        }
        return CpuModel;
    }

private string L2CacheSize = HardwareManagement.GetComponent("Win32_Processor", "L2CacheSize");
private string L3CacheSize = HardwareManagement.GetComponent("Win32_Processor", "L3CacheSize");