Question: I wish to write software which could essentially profile the CPU cache (L2,L3, possibly L1) and the memory, to analyze performance.

Am I right in thinking this is un-doable because there is no access for the software to the cache content? 

Another way of wording my Q: is there any way to know, from the OS/Application level, what data has been loaded into cache/memory?

EDIT: Operating System Windows or Linux and CPU Intel Desktop/Xeon

Answer: 
You might want to look at Intel's PMU i.e. Performance Monitoring Unit. Some processors have one. It is a bunch of special purpose registers (Intel calls them Model Specific Registers, or MSRs) which you can program to count events, like cache misses, using the `RDMSR` and `WRMSR` instructions.

Here is a document about [Performance Analysis on i7 and Xeon 5500](http://software.intel.com/sites/products/collateral/hpc/vtune/performance_analysis_guide.pdf).

You might want to check out Intel's **[Performance Counter Monitor](http://software.intel.com/en-us/articles/intel-performance-counter-monitor/)**, which is basically some routines that abstract the PMU, which you can use in a C++ application to measure several performance metrics live, including cache misses. It also has some GUI/Commandline tools for standalone use.

Apparently, [the Linux kernel has a facility for manipulating MSRs](http://www.kernel.org/doc/man-pages/online/pages/man4/msr.4.html).

There are other utilities/APIs that also use the PMU: [perf](https://perf.wiki.kernel.org/), [PAPI](http://icl.cs.utk.edu/papi/index.html).
Cache performance is generally measured in terms of hit rate and miss rate.

There are many tools to do this for you. Check how [Valgrind does cache profiling](http://tldp.org/HOWTO/Valgrind-HOWTO/deeper.html). 

Also ***cache performance is generally measured on a per program basis***. Well written programs will result in a fewer cache misses and better cache performance and vice versa for poorly written code.

Measuring the **actual cache speed** is the headache of the hardware manufacturers and you can refer their manuals to know this value.

[Callgrind](http://valgrind.org/docs/manual/cl-manual.html)/[Cachegrind](http://wwwpub.zih.tu-dresden.de/~mlieber/practical_performance/04_callgrind.pdf) combination can help you track cache hits/misses