@@ -22,8 +22,10 @@ static bool is_in_infinite_loop_cq(double *elems, int nelems);
2222double
2323get_mean (double * elems ,int nelems )
2424{
25- double sum = 0 ;
26- int i ;
25+ double sum = 0 ;
26+ int i ;
27+
28+ AssertArg (nelems > 0 );
2729
2830for (i = 0 ;i < nelems ;++ i )
2931sum += elems [i ];
@@ -37,7 +39,9 @@ get_mean(double *elems, int nelems)
3739double
3840get_estimation (double * elems ,int nelems )
3941{
40- int start ;
42+ int start ;
43+
44+ AssertArg (nelems > 0 );
4145
4246if (nelems > auto_tuning_window_size )
4347start = nelems - auto_tuning_window_size ;
@@ -53,11 +57,16 @@ get_estimation(double *elems, int nelems)
5357bool
5458is_stable (double * elems ,int nelems )
5559{
56- double est ;
60+ double est ,
61+ last ;
62+
63+ AssertArg (nelems > 1 );
5764
5865est = get_mean (elems ,nelems - 1 );
59- return (est * 1.1 > elems [nelems - 1 ]|| est + 0.1 > elems [nelems - 1 ])&&
60- (est * 0.9 < elems [nelems - 1 ]|| est - 0.1 < elems [nelems - 1 ]);
66+ last = elems [nelems - 1 ];
67+
68+ return (est * 1.1 > last || est + 0.1 > last )&&
69+ (est * 0.9 < last || est - 0.1 < last );
6170}
6271
6372/*
@@ -89,7 +98,7 @@ is_in_infinite_loop_cq(double *elems, int nelems)
8998return false;
9099
91100return !converged_cq (elems ,nelems )&&
92- !converged_cq (elems ,nelems - auto_tuning_window_size );
101+ !converged_cq (elems ,nelems - auto_tuning_window_size );
93102}
94103
95104/*
@@ -142,16 +151,19 @@ automatical_query_tuning(int query_hash, QueryStat * stat)
142151stat -> execution_time_with_aqo_size )+
143152get_estimation (stat -> planning_time_with_aqo ,
144153stat -> planning_time_with_aqo_size );
154+
145155t_not_aqo = get_estimation (stat -> execution_time_without_aqo ,
146156stat -> execution_time_without_aqo_size )+
147157get_estimation (stat -> planning_time_without_aqo ,
148158stat -> planning_time_without_aqo_size );
159+
149160p_use = t_not_aqo / (t_not_aqo + t_aqo );
150161p_use = 1 / (1 + exp ((p_use - 0.5 ) /unstability ));
151162p_use -= 1 / (1 + exp (-0.5 /unstability ));
152163p_use /=1 - 2 / (1 + exp (-0.5 /unstability ));
153164
154- use_aqo = ((double )rand () /RAND_MAX < p_use );
165+ /* borrowed from drandom() in float.c */
166+ use_aqo = (random () / ((double )MAX_RANDOM_VALUE + 1 ))< p_use ;
155167learn_aqo = use_aqo ;
156168}
157169