MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
arrayPoolTest.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 
21 #include <ndb_global.h>
22 
23 #include <ArrayList.hpp>
24 #include <NdbOut.hpp>
25 #include <NdbTick.h>
26 
28  Uint32 next;
29  char somedata[12];
30 
31  void print (NdbOut & out) {
32  out << "A_Poolable_Object: next = " << next << endl;
33  }
34 
35 };
36 
37 
38 NdbOut &
39 operator<<(NdbOut & o, A_Poolable_Object & a){
40  a.print(o);
41  return o;
42 }
43 
45 #if 1
46 #define BPool ArrayPool<A_Poolable_Object>
47 #else
48 #define BPool ArrayPool(A_Poolable_Object, next)
49 #endif
50 
52 public:
53  static void tryPool1(int poolSize, int iterations){
54  BPool aPool;
55 
56  if(!aPool.setSize(poolSize)){
57  ndbout << "Failed to do aPool.setSize(" << poolSize << ")" << endl;
58  return;
59  }
60 
61  ndbout << "Seizing/Releaseing " << iterations
62  << " times over pool with " << poolSize << " elements" << endl;
63 
64  int * anArray = new int[poolSize];
65  int arrayElements = 0;
66 
67  int noOfSeize = 0;
68  int noFailSeize = 0;
69  int noOfRelease = 0;
70 
71  for(int i = 0; i<iterations; i++){
72  if(!((arrayElements <= poolSize) &&
73  (aPool.noOfFree() == aPool.noOfFree2()) &&
74  (aPool.noOfFree() == (poolSize - arrayElements)))){
75  ndbout << "Assertion!!"
76  << " iteration=" << i << endl;
77  const int f1 = aPool.noOfFree();
78  const int f2 = aPool.noOfFree2();
79  ndbout << "noOfFree() = " << f1 << endl;
80  ndbout << "noOfFree2() = " << f2 << endl;
81  ndbout << "poolSize = " << poolSize << endl;
82  ndbout << "arrayElemts = " << arrayElements << endl;
83  aPool.print(ndbout);
84  assert(0);
85  }
86 
87  const int r = rand() % (10 * poolSize);
88  if(r < (arrayElements - 1)){
92  noOfRelease++;
93  aPool.release(anArray[r]);
94  arrayElements--;
95  for(int j = r; j<arrayElements; j++)
96  anArray[j] = anArray[j+1];
97 
98  } else {
102  A_Poolable_ObjectPtr p;
103  const int ret = aPool.seize(p);
104  if(ret == RNIL && arrayElements != poolSize){
105  ndbout << "Failed to seize!!"
106  << " iteration=" << i << endl;
107  ndbout << "Have seized " << arrayElements
108  << " out of " << poolSize << endl;
109  ndbout << "Terminating..." << endl;
110  abort();
111  }
112  if(arrayElements >= poolSize && ret != RNIL){
113  ndbout << "Seize did not fail when it should have"
114  << " iteration=" << i << endl;
115  ndbout << "Have seized " << arrayElements
116  << " out of " << poolSize << endl;
117  ndbout << "Terminating..." << endl;
118  abort();
119  }
120  if(ret != RNIL){
121  noOfSeize++;
122  anArray[arrayElements] = ret;
123  arrayElements++;
124  memset(p.p, i, sizeof(p.p->somedata));
125  } else {
126  noFailSeize++;
127  }
128  }
129  }
130  delete []anArray;
131 
132  ndbout << "Seized: " << noOfSeize
133  << " Seized with buffer full: " << noFailSeize
134  << " Release: " << noOfRelease << " --- ";
135  ndbout << "(" << noOfSeize << " + " << noFailSeize << " + " << noOfRelease
136  << " = " << (noOfSeize + noFailSeize + noOfRelease) << ")" << endl;
137  }
138 
139  static void tryPool2(int size, int iter, int fail){
140  BPool aPool;
141 
142  if(!aPool.setSize(size)){
143  ndbout << "Failed to do aPool.setSize(" << size << ")" << endl;
144  return;
145  }
146 
147  ndbout << "doing getPtr(i) where i > size(" << size << ") "
148  << fail << " times mixed with " << iter
149  << " ordinary seize/release" << endl;
150 
151  int * anArray = new int[size];
152  int arrayElements = 0;
153 
154  int noOfSeize = 0;
155  int noFailSeize = 0;
156  int noOfRelease = 0;
157 
158  for(int i = 0; i<iter; i++){
159  if(!((arrayElements <= size) &&
160  (aPool.noOfFree() == aPool.noOfFree2()) &&
161  (aPool.noOfFree() == (size - arrayElements)))){
162  ndbout << "Assertion!!"
163  << " iteration=" << i << endl;
164  const int f1 = aPool.noOfFree();
165  const int f2 = aPool.noOfFree2();
166  ndbout << "noOfFree() = " << f1 << endl;
167  ndbout << "noOfFree2() = " << f2 << endl;
168  ndbout << "poolSize = " << size << endl;
169  ndbout << "arrayElemts = " << arrayElements << endl;
170  aPool.print(ndbout);
171  assert(0);
172  }
173  const int r = rand() % (10 * size);
174 
175  if((i + 1)%(iter/fail) == 0){
176  aPool.getPtr(size + r);
177  continue;
178  }
179 
180  if(r < (arrayElements - 1)){
184  noOfRelease++;
185  aPool.release(anArray[r]);
186  arrayElements--;
187  for(int j = r; j<arrayElements; j++)
188  anArray[j] = anArray[j+1];
189 
190  } else {
194  A_Poolable_ObjectPtr p;
195  const int ret = aPool.seize(p);
196  if(ret == RNIL && arrayElements != size){
197  ndbout << "Failed to seize!!"
198  << " iteration=" << i << endl;
199  ndbout << "Have seized " << arrayElements
200  << " out of " << size << endl;
201  ndbout << "Terminating..." << endl;
202  abort();
203  }
204  if(arrayElements >= size && ret != RNIL){
205  ndbout << "Seize did not fail when it should have"
206  << " iteration=" << i << endl;
207  ndbout << "Have seized " << arrayElements
208  << " out of " << size << endl;
209  ndbout << "Terminating..." << endl;
210  abort();
211  }
212  if(ret != RNIL){
213  noOfSeize++;
214  anArray[arrayElements] = ret;
215  arrayElements++;
216  memset(p.p, p.i, sizeof(p.p->somedata));
217  } else {
218  noFailSeize++;
219  }
220  }
221  }
222  delete []anArray;
223  }
224 
225  static void
226  tryPool3(int size, int fail){
227  ndbout << "Failing " << fail << " times " << endl;
228 
229  for(int i = 0; i<fail; i++){
230  BPool aPool;
231  if(!aPool.setSize(size)){
232  ndbout << "Failed to do aPool.setSize(" << size << ")" << endl;
233  return;
234  }
235 
236  const int noOfElementsInBufferWhenFail = (i + 1) * (size /(fail + 1));
237 
238  int * anArray = new int[size];
239  for(int i = 0; i<size; i++)
240  anArray[i] = i;
241  int arrayElements = 0;
242 
243  int noOfSeize = 0;
244  int noFailSeize = 0;
245  int noOfRelease = 0;
246 
247  while(true){
248  assert(arrayElements <= size);
249  if(arrayElements == noOfElementsInBufferWhenFail){
250  ndbout << "++ You should get a ErrorReporter::handle... " << endl;
251  aPool.release(anArray[arrayElements]);
252  ndbout << "++ Inbetween these lines" << endl << endl;
253  break;
254  }
255  const int r = rand() % (10 * size);
256  if(r < (arrayElements - 1)){
260  noOfRelease++;
261  aPool.release(anArray[r]);
262  arrayElements--;
263  for(int j = r; j<arrayElements; j++)
264  anArray[j] = anArray[j+1];
265 
266  } else {
270  A_Poolable_ObjectPtr p;
271  const int ret = aPool.seize(p);
272  if(ret == RNIL && arrayElements != size){
273  ndbout << "Failed to seize!!" << endl;
274  ndbout << "Have seized " << arrayElements
275  << " out of " << size << endl;
276  ndbout << "Terminating..." << endl;
277  abort();
278  }
279  if(arrayElements >= size && ret != RNIL){
280  ndbout << "Seize did not fail when it should have" << endl;
281  ndbout << "Have seized " << arrayElements
282  << " out of " << size << endl;
283  ndbout << "Terminating..." << endl;
284  abort();
285  }
286  if(ret != RNIL){
287  noOfSeize++;
288  anArray[arrayElements] = ret;
289  arrayElements++;
290  } else {
291  noFailSeize++;
292  }
293  }
294  }
295  delete []anArray;
296  }
297 
298  }
299 };
300