Helllo Guys ,
I am bringing in one of the most unknown issue (few among us may know this) called False Cache Sharing.
Have u ever noticed when u create object in any object oriented programming language, how it is allocated in the memory? Even if it is allocated how processor reacts to it? How its is fed into pipelines and what will happen if u have ur cpu which is SMP with hyper threading enabled?
CLR(Common Language Runtime ) : To make it simpler i must say , its task is to just keep all the object allocated by User into as near as possible into assigned HEAP(So that further locality of refrence can be used)
lets take an example
public class Demo
{
public int status1;
public int status2;
}
When Demo object is instansiated , and loaded into Memory..SMP processor which has HyperThreading Enabled,reads the data from RAM through Cache-Line via System Bus into its fast cache(L2 cache) and now able to be fed into superscalar architacture.
consider there are two execution streams (two thread) for one CPU(concept of Hyper-Technology).One Thread(T1) is accessing status1 and another(T2) is using status2. Lets Suppose T1 has to read status1 and T2 has to write status=10. As both addresses belong to same cache-line, One among them has to wait for other to get its task completed , and during that time other thread's execution gets stalled.Situation would be evn worse when dual processor is used as 4 Logical CPU may demand the data from same cache line .
Trivial Solution To this problem :
As a programmer i can add some padding objects/variable in my above code snippet as follows:
{
public int status1;
public int pad1;
...
..
..
public int status2;
}
Now thread concering data may not fall into same cache line (depends up on again cache-line size again, need to know your proceesor uses how much size for cache-line).
But this intoduces bad programming habbit as code will be unruly if u effective lines are few thousands n padding lines are multiple of them.
Limitation of this solution:
Suppose you are adding few padding objects in betwn , again it depends up on your compiler whether it reorders the code or not..even if it is not reordered , bt sometimes if there are
some unused objects , and unfortunatly if it is garbage collected even before two threads can use it..then situation will be same as now there is no padded data (Becouse of the grace of our garbage collector and compilers)
2.Most unfeasible solution will be to not pick sequential addressess out of RAM , so that at least we will not be able to find cache-line-sharing.but it causes change in the modern processors design.
3.A solution may be to add some logic so that predict the locality of data (which is being used by multiple threads ) without wasting much cpu clock ticks , and if two instructions/data belong to same cache-line ,copy it to some other location in cache from where it could be fetched by needed thread .But surely this is much costlier approach than waiting for thread of line to be vacted.
In my view first solution is feasible if you have knowledge about processor cache line and also compiler characterstics too.
Read it out above article and if you guys find any solution , then let me know also so that we can check its feasiblity too....