问题
I am accessing a shared library (shared array data structure)from program1 and find the access time to read all elements of that array. I got around 17000 ticks while only Program1 executed alone.
Now when I execute program2 (having empty while loop to hold it from termination) in another tab first , then run program1 and measure the access time to read all elements of that array. To my surprise now I am getting 8000ticks as compared to previous scenario where only Program1 executing.
It looks like while ONLY program1 is executing it takes more time to read the array as compared to while there are 2 Programs ,program1 is doing same task as previous and program2 keeps CPU busy by while loop. Expected is higher access time with presence of program1, whereas real outcome is opposite .
Why this happening?
Here is shared library
#include <stdio.h>
static const int DATA[1024]={1 ,2 ,3,.....1024];
inline void foo(void)
{
int j, k=0,count=0;
for(j=0;j<1024;j++)
{
k=DATA[j];
}
k+=0;
}
Program1
int main(void)
{
foo();
start=timer();
foo();
end=timer();
printf("Time1=%llu\n",end-start);
start=timer();
foo();
end=timer();
printf("Time2=%llu\n",end-start);
start=timer();
foo();
end=timer();
printf("Time3=%llu\n",end-start);
sleep(1);
start=timer();
foo();
end=timer();
printf("after sleep(1)\n");
printf("Time4=%llu\n",end-start);
start=timer();
foo();
end=timer();
printf("Time5=%llu\n",end-start);
sleep(2);
start=timer();
foo();
end=timer();
printf("after sleep(2)\n");
printf("Time6=%llu\n",end-start);
return 0;
}
program2
int main(void)
{
while(1)
{}
return 0;
}
CASE1 (ONLY Program1 running)
output
Time1=17918
Time2=17672
Time3=17816
after sleep(1)
**Time4= 20716 ** // Is it due to wake up from sleep mode ?
Time5=17722
after sleep(2)
**Time6=20910** // Is it due to wake up from sleep mode ?
CASE1 (Program2 runs first, then Program1 starts running)
output
Time1 =7483
Time2=7205
Time3=7399
after sleep(1)
**Time4= 8734 ** // Is it due to wake up from sleep mode ?
Time5=7326
after sleep(2)
**Time6=9070** // Is it due to wake up from sleep mode ?
As per my understanding while CPU is used by program1 alone time required to read array must be less as compared to while CPU is used by both Program1 and program2.
Where I am making mistakes ? I have i7 machine , only one core , hyperthreading is disabled, ASLR disabled.
EDIT 1:
As per suggestion of Mysticial, my CPU goes into power saving mode while ONLY program1 is there , so CPU goes into power saving mode and then to wake it up from Power saving mode it takes larger access time. So his suggestion was to access the DATA array multiple times.
Here is my modified Shared library . Program1 and Program2 are unaltered.
#include <stdio.h>
static const int DATA[1024]={1 ,2 ,3,.....1024];
inline void foo(void)
{
int j, k=0,count=0;
while(count++<10000)
{
for(j=0;j<1024;j++)
{
k=DATA[j];
}
}
k+=0;
}
Now the output is as below
CASE1 (ONLY Program1 running)
output
Time1=75186246
Time2=77570299
Time3=80548529
after sleep(1)
**Time4= 92608363 ** // Is it due to wake up from sleep mode ?
Time5=75616487
after sleep(2)
**Time6=97021338** // Is it due to wake up from sleep mode ?
CASE1 (Program2 runs first, then Program1 starts running)
output
Time1 =139337099
Time2=155801957
Time3=146586856
after sleep(1)
**Time4= 130558062 ** // Why lower access time after sleep mode ?
Time5=145250551 // Time5 is expected lower than Time4 as other run . Why lower here ?
after sleep(2)
**Time6=130940183** // Again Why lower access time after sleep mode ?
Here are my new questions with respect to modified shared library
When there is no program2, then after sleep access time (t4/t6) is higher as compared to previous access time (t3/t5, before going to sleep). Can I say, it is due to waking up CPU from sleep as explained by Mysticial ?
Now, with program2 running in another tab, after sleep access time (t4/t6) is lower as compared to previous access time (t3/t5, before going to sleep). Reason for my q(1) and q(2) are contradictory . What is the reason behind getting lower access time after waking up from sleep ( t4 < t3)? Although I am getting higher access time after sleep without multiple access to DATA array ( original shared library).
Why
t2<t1 and t3<t2
not holding true always as shared library is loaded already into memory as well as cache . Is it due to PAGE SWAPPING ?
I am using gcc under linux. Any help to understand this will be highly appreciated. Thanks in advance.
回答1:
Here's my speculative answer which seems to have been confirmed in the comments:
In the original benchmark, you run only one iteration of each. So the benchmark doesn't run long enough to "average out" all the randomness.
When you run program1 by itself, it needs to wake up the CPU from its power-saving state. This takes time and is likely causing the run-time to be longer.
When you run both programs together (starting with program2 first), program2 kicks the CPU out of power-saving state ahead of time. So this warm up penalty is not realized when you run program1.
Once you loop the benchmark to take longer, this warm up penalty becomes insignificant and you finally see the expected steady-state performance of the code. (program1 by itself is faster)
来源:https://stackoverflow.com/questions/21720012/unexpected-lower-access-time-in-multiple-process-scenario-as-compared-to-single