summaryrefslogtreecommitdiff
path: root/storage/ndb/src/kernel/blocks/dbtup/test_varpage.cpp
blob: 2e52fe049491d16a9972aa3ec9b6ad2c59c3bde6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Copyright (C) 2006 MySQL AB
   Use is subject to license terms

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

#include <ndb_global.h>
#include "tuppage.hpp"
#include <Vector.hpp>

struct Record
{
  Uint32 idx;
  Uint32 size;
  Uint32* data;
};

NdbOut&
operator <<(NdbOut& out, const Record& rec)
{
  out << "[ idx: " << rec.idx << " sz: " << rec.size << " ]";
  return out;
}

#define TRACE(x) x

static
bool
cmp(const Uint32 *p1, const Uint32 *p2, Uint32 words)
{
  if(memcmp(p1, p2, 4*words) == 0)
    return true;
  
  for(Uint32 i = 0; i<words; i++)
    printf(" %.8x", p1[i]);
  printf("\n");

  for(Uint32 i = 0; i<words; i++)
    printf(" %.8x", p2[i]);
  printf("\n");

  return false;
}

static
void
do_test(int loops, int dist[5])
{
  fprintf(stderr, "do_test(%d, [ %d %d %d %d %d ])\n",
	  loops, 
	  dist[0],
	  dist[1],
	  dist[2],
	  dist[3],
	  dist[4]);
  int allocated= 0;
  Record records[8192];

  Tup_varsize_page page, tmp;
  page.init();

  for(int i = 0; i<loops; i++)
  {
    assert(page.high_index + page.insert_pos <= page.DATA_WORDS);

    for(int j = 0; j<allocated; j++)
    {
      Record rec= records[j];
      Uint32* ptr= page.get_ptr(rec.idx);
      Uint32 pos = page.get_ptr(rec.idx) - page.m_data;
      if (page.get_entry_len(rec.idx) != rec.size)
      {
	ndbout << "INVALID LEN " << j << " " << rec << " pos: " << pos << endl;
	ndbout << page << endl;
	abort();
      }
      
      if(!cmp(ptr, rec.data, rec.size))
      {
	ndbout << "FAILED " << j << " " << rec << " pos: " << pos << endl;
	ndbout << page << endl;
	abort();
      }
    }
    
loop:
    int op;
    int rnd= rand() % 100;
    for(op= 0; op<5; op++)
      if(rnd < dist[op])
	break;
    
    if(allocated == 0)
      op= 0;
    if(page.free_space <= 2 && op == 0) goto loop;

    switch(op){
    case 0: // Alloc
    {
      Record rec;
      rec.size= 1 + (rand() % (page.free_space-1));
      rec.data = new Uint32[rec.size];
      for(Uint32 i= 0; i<rec.size; i++)
      {
	rec.data[i] = rand();
      }
      ndbout << "Alloc hi: " << page.high_index << " (" << 
	((rnd < 30) ? "any" : 
	 (rnd < 60) ? "dir" : 
	 (rnd < 80) ? "exp" : "fail") << ") ";
      ndbout << rec.size << flush;
      if (rnd < 30)
      {
	rec.idx= page.alloc_record(rec.size, &tmp, 0);
      }
      else if (rnd < 60)
      {
        // Alloc with id, from directory
	Vector<Uint32> free;
	for(Uint32 i = page.high_index - 1; i > 0; i--)
	{
	  if (page.get_index_word(i) & page.FREE)
	  {
	    free.push_back(i);
	    if (free.size() > 100)
	      break;
	  }
	}
	if (free.size())
	{
	  rec.idx = free[rand() % free.size()];
	  if (page.alloc_record(rec.idx, rec.size, &tmp) != rec.idx)
	  {
	    abort();
	  }
	}
	else
	{
	  rec.idx = page.high_index;
	  if (page.alloc_record(rec.idx, rec.size, &tmp) != rec.idx)
	  {
	    if (rec.size + 1 != page.free_space)
	      abort();
	    delete [] rec.data;
	    ndbout_c(" FAIL");
	    break;
	  }
	}
      }
      else if(rnd < 80)
      {
        // Alloc with id, outside of directory
	rec.idx = page.high_index + (rand() % (page.free_space - rec.size));
	if (page.alloc_record(rec.idx, rec.size, &tmp) != rec.idx)
	{
	  abort();
	}
      }
      else
      {
	rec.idx = page.high_index + (page.free_space - rec.size) + 1;
	if (page.alloc_record(rec.idx, rec.size, &tmp) == rec.idx)
	{
	  abort();
	}
	delete [] rec.data;
	ndbout_c(" FAIL");
	break;
      }
      
      Uint32 pos = page.get_ptr(rec.idx) - page.m_data;
      ndbout << " -> " << rec.idx 
	     << " pos: " << pos << endl;
      Uint32* ptr= page.get_ptr(rec.idx);
      memcpy(ptr, rec.data, 4*rec.size);
      records[allocated++] = rec;
      break;
    }
    case 1: // Free
    {
      int no= rand() % allocated;
      Record rec= records[no];
      Uint32 pos = page.get_ptr(rec.idx) - page.m_data;
      ndbout << "Free hi: " << page.high_index << " no: " << no << " idx: " << rec.idx << " pos: " << pos << endl;
      Uint32* ptr= page.get_ptr(rec.idx);
      assert(page.get_entry_len(rec.idx) == rec.size);
      cmp(ptr, rec.data, rec.size);
      delete[] rec.data;
      page.free_record(rec.idx, 0);
      
      for (unsigned k = no; k + 1 < allocated; k++)
	records[k] = records[k+1];
      allocated--;
      
      break;
    }
    case 2: // Reorg
      ndbout << "Reorg" << endl;      
      page.reorg(&tmp);
      break;
    case 3: 
    {
      Uint32 free = page.free_space;
      if (free <= 2)
      {
	goto shrink;
      }
      free /= 2;
      int  no = rand() % allocated;
      Record rec= records[no];
      ndbout << "Expand no: " << no << " idx: " << rec.idx
	     << " add: " << free << " reorg: " 
	     << !page.is_space_behind_entry(rec.idx, free)
	     << endl;
      if (!page.is_space_behind_entry(rec.idx, free))
      {
	Uint32 buffer[8192];
	Uint32 len = page.get_entry_len(rec.idx);
	memcpy(buffer, page.get_ptr(rec.idx), 4*len);
	page.set_entry_len(rec.idx, 0);
	page.free_space += len;
	page.reorg(&tmp);
	memcpy(page.get_free_space_ptr(), buffer, 4*len);
	page.set_entry_offset(rec.idx, page.insert_pos);
	free += len;
	records[no].size = 0;
      }

      page.grow_entry(rec.idx, free);
      records[no].size += free;
      Uint32 *ptr = page.get_ptr(rec.idx);
      Uint32 *new_data = new Uint32[records[no].size];
      for(Uint32 i= 0; i<records[no].size; i++)
      {
	ptr[i] = new_data[i] = rand();
      }
      delete []rec.data;
      records[no].data = new_data;
      break;
    }
    case 4:
    {
  shrink:
      int no = rand() % allocated;
      Record rec = records[no];
      Uint32 sz = rec.size / 2 + 1;
      ndbout << "Shrink no: " << no << " idx: " << rec.idx << " remove: " 
	     <<  (rec.size - sz) << endl;
      page.shrink_entry(rec.idx, sz);
      records[no].size = sz;
      break;
    }
    }

  }
  ndbout << page << endl;
}

int
main(int argc, char **argv)
{
  ndb_init();

  if (argc > 1)
  {
    time_t seed = time(0);
    srand(seed);
    fprintf(stderr, "srand(%d)\n", seed);
  }
  // alloc, free, reorg, grow, shrink
  
  int t1[] = { 10, 60, 70, 85, 100 };
  int t2[] = { 30, 60, 70, 85, 100 };
  int t3[] = { 50, 60, 70, 85, 100 };
  
  do_test(10000, t1);
  do_test(10000, t2);
  do_test(10000, t3);

  return 0;
}

template class Vector<Record>;

// hp3750
struct Signal { Signal(); int foo; };
Signal::Signal(){}