0

I have created a shared object and access it from two different program and measuring the time.

DATA array is the shared object between two processes.

Case 1: Use of while inside program1

program1 :

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program1, Time1=%d\n",Time_needed);    


start=timer;
access shared DATA array   
end=timer;

Time_needed= end-start
printf("Inside Program1, Time2=%d\n",Time_needed);    

while(1){}; // I replace this by sleep(1000) in CASE-2

Program2 :

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    
start=timer;
access shared DATA array      
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    

OUTPUT : First I run program1, then Program2

Inside Program1, Time1 = 17620
Inside Program1, Time1 = 17680

Inside Program2, Time1 = 7620
Inside Program2, Time1 = 7600

Case 2: Use of sleep() inside program1

program1 :

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program1, Time1=%d\n",Time_needed);    


start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program1, Time2=%d\n",Time_needed);    

sleep(1000);

Program2 :

access shared DATA array ;// to load into memory and avoid page fault during access time calculation

start=timer;
access shared DATA array
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    
start=timer;
access shared DATA array   
end=timer;

Time_needed= end-start
printf("Inside Program2, Time1=%d\n",Time_needed);    

OUTPUT : First I run program1, then Program2

Inside Program1, Time1 = 17620
Inside Program1, Time1 = 17680

Inside Program2, Time1 = 17620
Inside Program2, Time1 = 17600

From the output in case -1, I can say shared data DATA array is loaded into memory/cache by 1st program and second program access it from cache. Whereas this also true for CASE-2, but the result looks like it is flushed out from cache while Program1 goes into sleep.

I am using GCC under linux.

Any clue ? Thanks in advance .

bholanath
  • 1,699
  • 1
  • 22
  • 40
  • As per comment by Mysticial in another post, http://stackoverflow.com/questions/21720012/unexpected-lower-access-time-in-multiple-process-scenario-as-compared-to-single?noredirect=1#comment32914182_21720012 the reason is CPU goes into power saver state while program2 is not there ( program2 is with sleep can be assumed as there is no program2,only Program1 is running). As CPU goes into power saver state, it needs to wake up from that state and thats why higher access time . When while loop is used in program2, CPU is forced to not to go into power saver state and that's why lower access time. – bholanath Feb 13 '14 at 18:14

2 Answers2

0

I my opinion we can't predict the cache behavior as per your case.

It also depends upon your H/W which, you have not mentioned (like how many CPU cores(physical or logical) are present).

We can't say that program_2 will be scheduled on the same core and just after program_1 because it totally depends upon OS scheduler. So program_2 may use cache filled by program_1 or not.

It's possible that the cache is flushed out due to some other program which was scheduled just after program_1.

akp
  • 1,753
  • 3
  • 18
  • 26
  • Thanks AKP. There is Only one core. I have disabled all other core as well as disabled Hyper threading . – bholanath Feb 10 '14 at 12:27
  • @bholanath to differenciate sleep() and while(1){}, we can say that in while(1) made the process will not be scheduled till end of it's time-quanta or any interrupt and in case of sleep(), the process is put back in the waiting queue of the cpu core till it's time expires, and it leave the core by calling schedule functionality of OS to give CPU resource to another process. – akp Feb 10 '14 at 12:36
0

You didn't describe exactly how you run the different versions (different processes?), but assuming they're sequential - It is possible that you're seeing the affect of sleep()

It depends of course on the exact implementation and HW, but it's very likely to send your CPU into some power-saving/sleep state (that's what it's designed for). If that's the case, then the core caches will have to be flushed as part of the process, and you'll wake-up with cold caches. The whie loop on the other hand is intended to do a busy wait loop while grinding your CPU and keeping it alive (along with the caches), unless you happen to get a context switch along the way.

The exact details would again depend on implementation, on x86 you can use inline assembly to invoke monitor+mwait instructions that allow you to specify the exact C-state depth you want to achieve. The deeper it is, the more caches will get closed (mostly relevant for the L3).

Leeor
  • 19,260
  • 5
  • 56
  • 87
  • As per comment by Mysticial in another post, http://stackoverflow.com/questions/21720012/unexpected-lower-access-time-in-multiple-process-scenario-as-compared-to-single?noredirect=1#comment32914182_21720012 the reason is CPU goes into power saver state while program2 is not there ( program2 is with sleep can be assumed as there is no program2,only Program1 is running). As CPU goes into power saver state, it needs to wake up from that state and thats why higher access time . When while loop is used in program2, CPU is forced to not to go into power saver state and that's why lower access time. – bholanath Feb 13 '14 at 18:19
  • you also mentioned here CPU goes into power saving state. To nullify the effect of power saving state, Mysticial proposed in other post http://stackoverflow.com/questions/21720012/unexpected-lower-access-time-in-multiple-process-scenario-as-compared-to-single?noredirect=1#comment32914182_21720012 to use multiple iterations . I am getting some weird result after sleep in both case - without multiple iteration Vs with multiple iteration. after sleep I am getting higher access time (as expected ) with single access to array Vs after sleep lower access time with multiple iteration. – bholanath Feb 13 '14 at 18:27
  • @bholanath: You can use what I wrote about monitor/mwait to determine the exact power state you're entering (instead of relying on the underlying sleep implementation) – Leeor Feb 13 '14 at 18:50