1908554 With 1 million records DMLTest insert and delete are slow
[csql.git] / src / server / Chunk.cxx
blob89d07f7a12b94ae7ffb6fe0f52cc900da4a9d361
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<Allocator.h>
17 #include<Database.h>
18 #include<os.h>
19 #include<Debug.h>
20 #include<Config.h>
21 #include<CatalogTables.h>
23 // sets the size of the Chunk allocator for fixed size
24 // allocator
25 // we need one integer to store book keeping information
26 // whether the storage allocation unit is used of not
27 // when it is deleted this flag is only set to unused
28 void Chunk::setSize(size_t size)
31 size_t needSize = size + sizeof(int);
32 size_t multiple = (size_t) os::floor(needSize / sizeof(size_t));
33 size_t rem = needSize % sizeof(size_t);
34 if (0 == rem)
35 allocSize_ = needSize;
36 else
37 allocSize_ = (multiple + 1) * sizeof(size_t);
40 void* Chunk::allocateForLargeDataSize(Database *db)
42 PageInfo* pageInfo = ((PageInfo*)curPage_);
43 DbRetVal ret = db->getAllocDatabaseMutex();
44 if (ret != 0)
46 printError(ErrLockTimeOut,"Unable to acquire alloc database Mutex");
47 return NULL;
50 //check whether we have space in curPage
51 if (pageInfo->hasFreeSpace_ == 1)
53 char *data = ((char*)curPage_) + sizeof(PageInfo);
54 pageInfo->hasFreeSpace_ =0;
55 *((int*)data) = 1;
56 db->releaseAllocDatabaseMutex();
57 return data + sizeof(int);
61 //no space in curpage , get new page from database
62 pageInfo = (PageInfo*)db->getFreePage(allocSize_);
63 if (NULL == pageInfo)
65 db->releaseAllocDatabaseMutex();
66 printError(ErrNoMemory,"No more free pages in the database");
67 return NULL;
69 printDebug(DM_Alloc, "Chunk ID:%d Large Data Item newPage:%x",
70 chunkID_, pageInfo);
71 int multiple = os::floor(allocSize_ / PAGE_SIZE);
72 int offset = ((multiple + 1) * PAGE_SIZE);
74 pageInfo->setPageAsUsed(offset);
76 //create the link
77 ((PageInfo*)curPage_)->nextPage_ = (Page*) pageInfo;
78 //Make this as current page
79 curPage_ = (Page*) pageInfo;
80 char* data = ((char*)curPage_) + sizeof(PageInfo);
81 //TODO::check whether it is locked
82 *((int*)data) = 1;
83 db->releaseAllocDatabaseMutex();
84 return data + sizeof(int);
88 void* Chunk::allocateFromFirstPage(Database *db, int noOfDataNodes)
90 PageInfo *pageIter = ((PageInfo*)firstPage_);
91 printDebug(DM_Alloc, "Chunk ID:%d. No free page in database",
92 chunkID_);
93 printDebug(DM_Alloc, "Scan from firstPage:%x for free nodes",
94 firstPage_);
95 char *data = NULL;
96 int i = 0;
97 //scan from first page to locate a free node available
98 while(NULL != pageIter)
100 data = ((char*)pageIter) + sizeof(PageInfo);
101 if (pageIter->hasFreeSpace_ == 1)
103 for (i = 0; i< noOfDataNodes -1; i++)
105 if (1 == *((int*)data))
106 data = data + allocSize_;
107 else break;
109 if (i != noOfDataNodes -1) break;
111 printDebug(DM_Alloc, "Chunk ID: %d Page :%x does not have free nodes",
112 chunkID_, pageIter);
113 pageIter = (PageInfo*)((PageInfo*) pageIter)->nextPage_;
115 if (NULL == pageIter) return NULL;
116 printDebug(DM_Alloc,"ChunkID:%d Scan for free node End:Page :%x",
117 chunkID_, pageIter);
118 *((int*)data) = 1;
119 return data + sizeof(int);
123 void* Chunk::allocateFromNewPage(Database *db)
125 DbRetVal ret = db->getAllocDatabaseMutex();
126 if (ret != 0)
128 printError(ErrLockTimeOut,"Unable to acquire alloc database Mutex");
129 return NULL;
131 //get a new page from db
132 Page *page = db->getFreePage();
133 if (page == NULL)
135 db->releaseAllocDatabaseMutex();
136 return NULL;
138 printDebug(DM_Alloc, "ChunkID:%d Normal Data Item newPage:%x",
139 chunkID_, page);
140 //Initialize pageInfo for this new page
141 PageInfo *pInfo = (PageInfo*)page;
142 pInfo->setPageAsUsed(0);
144 //create the link between old page and the newly created page
145 PageInfo* pageInfo = ((PageInfo*)curPage_);
146 pageInfo->nextPage_ = page;
148 //make this new page as the current page
149 curPage_ = page;
151 char* data = ((char*)page) + sizeof(PageInfo);
152 *((int*)data) = 1;
153 db->releaseAllocDatabaseMutex();
154 return data + sizeof(int);
157 //Allocates memory to store data
158 //TODO::check whether it is locked before allocating.
159 //delete tuple will set the usedflag to true, but locks will be held
160 //till commit and it shall be rolledback.So make sure that it does not
161 //allocate deleted tuple which is yet to be commited.
163 void* Chunk::allocate(Database *db, DbRetVal *status)
165 PageInfo* pageInfo = ((PageInfo*)curPage_);
167 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/allocSize_);
168 char *data = ((char*)curPage_) + sizeof(PageInfo);
169 printDebug(DM_Alloc, "Chunk::allocate id:%d curPage:%x noOfDataNodes:%d",
170 chunkID_, curPage_, noOfDataNodes);
172 //1.scan through data list and find if any is free to use in current page
173 //2.If there is none then
174 // a) get new free page from db. set the prev->next to point
175 // to this new page
176 //4. b) initialize the free page to zero and get first data ptr.
177 //5.If there is one, return that
179 //For allocation more than PAGE_SIZE
180 if (0 == noOfDataNodes)
182 data = (char*) allocateForLargeDataSize(db);
183 return data;
186 int ret = getChunkMutex(db->procSlot);
187 if (ret != 0)
189 if (status != NULL) *status = ErrLockTimeOut;
190 printError(ErrLockTimeOut,"Unable to acquire chunk Mutex");
191 return NULL;
193 int i = noOfDataNodes;
194 if (pageInfo->hasFreeSpace_ == 1)
196 for (i = 1; i< noOfDataNodes; i++)
198 if (*((int*)data) == 1) data = data + allocSize_;
199 else break;
203 printDebug(DM_Alloc, "ChunkID:%d Node which might be free is %d",
204 chunkID_, i);
205 //It comes here if the pageInfo->hasFreeSpace ==0
206 //or there are no free data space in this page
207 if (i == noOfDataNodes && *((int*)data) == 1)
210 printDebug(DM_Alloc, "ChunkID:%d curPage does not have free nodes.", chunkID_);
211 //there are no free data space in this page
212 pageInfo->hasFreeSpace_ = 0;
213 if (chunkID_ == LockTableId || chunkID_ == TransHasTableId)
215 data = (char*) allocateFromFirstPage(db, noOfDataNodes);
216 if (NULL == data)
218 data = (char*) allocateFromNewPage(db);
219 if (data == NULL)
221 printError(ErrNoMemory, "No memory in any of the pages:Increase db size");
222 if (status != NULL) *status = ErrNoMemory;
226 else
228 data = (char*) allocateFromNewPage(db);
229 if (NULL == data)
231 data = (char*) allocateFromFirstPage(db, noOfDataNodes);
232 if (data == NULL)
234 printError(ErrNoMemory, "No memory in any of the pages:Increase db size");
235 if (status != NULL) *status = ErrNoMemory;
239 releaseChunkMutex(db->procSlot);
240 return data;
242 *((int*)data) = 1;
243 releaseChunkMutex(db->procSlot);
244 return data + sizeof(int);
248 void* Chunk::allocateForLargeDataSize(Database *db, size_t size)
250 //no need to take chunk mutexes for this, as we are taking alloc database mutex
251 int multiple = os::floor(size / PAGE_SIZE);
252 int offset = ((multiple + 1) * PAGE_SIZE);
253 PageInfo* pageInfo = ((PageInfo*)curPage_);
254 DbRetVal ret = db->getAllocDatabaseMutex();
255 if (ret != 0)
257 printError(ErrLockTimeOut,"Unable to acquire alloc database Mutex");
258 return NULL;
260 pageInfo = (PageInfo*)db->getFreePage(allocSize_);
261 if (NULL == pageInfo)
263 db->releaseAllocDatabaseMutex();
264 printError(ErrNoMemory,"No more free pages in the database:Increase db size");
265 return NULL;
267 printDebug(DM_VarAlloc,"Chunk::allocate Large Data Item id:%d Size:%d curPage:%x ",
268 chunkID_, size, curPage_);
269 //TODO:: logic pending
272 //REDESIGN MAY BE REQUIRED:Lets us live with this for now.
273 //what happens to the space lets say 10000 bytes is allocated
274 //it needs 2 pages,= 16000 bytes, 6000 bytes should not be wasted
275 //in this case.So need to take of this.
276 //Will be coded at later stage as this is developed to support
277 //undo logging and currently we shall assume that the logs generated
278 //wont be greater than PAGE_SIZE.
279 db->releaseAllocDatabaseMutex();
280 return NULL;
286 void* Chunk::allocFromNewPageForVarSize(Database *db, size_t size)
288 //Should be called only for data items <PAGE_SIZE
289 DbRetVal ret = db->getAllocDatabaseMutex();
290 if (ret != 0)
292 printError(ErrLockTimeOut,"Unable to acquire alloc database Mutex");
293 return NULL;
296 void *vnode = varSizeFirstFitAllocate(size);
297 if (vnode != NULL)
299 db->releaseAllocDatabaseMutex();
300 return vnode;
303 Page *newPage = db->getFreePage();
304 db->releaseAllocDatabaseMutex();
305 if (NULL == newPage)
307 return NULL;
310 printDebug(DM_VarAlloc, "ChunkID:%d New Page: %x ", chunkID_, newPage);
311 PageInfo *pInfo = (PageInfo*) newPage;
312 pInfo->setPageAsUsed(0);
313 createDataBucket(newPage, PAGE_SIZE, size);
315 ((PageInfo*)curPage_)->nextPage_ = newPage;
316 curPage_ = newPage;
317 char *data= ((char*)newPage) + sizeof(PageInfo) + sizeof(VarSizeInfo);
318 return data;
321 //Allocates from the current page of the chunk.
322 //Scans through the VarSizeInfo objects in the page and gets the free slot
323 void* Chunk::allocateFromCurPageForVarSize(size_t size)
325 //Should be called only for data items <PAGE_SIZE
326 Page *page = ((PageInfo*)curPage_);
327 printDebug(DM_VarAlloc, "Chunk::allocate Normal Data Item id:%d Size:%d curPage:%x ",
328 chunkID_, size, curPage_);
329 VarSizeInfo *varInfo = (VarSizeInfo*)(((char*)page) +
330 sizeof(PageInfo));
331 while ((char*) varInfo < ((char*)page + PAGE_SIZE))
333 if (0 == varInfo->isUsed_)
335 if( size + sizeof(VarSizeInfo) < varInfo->size_)
337 splitDataBucket(varInfo, size);
338 printDebug(DM_VarAlloc, "Chunkid:%d splitDataBucket: Size: %d Item:%x ",
339 chunkID_, size, varInfo);
340 return (char*)varInfo + sizeof(VarSizeInfo);
342 else if (size == varInfo->size_) {
343 varInfo->isUsed_ = 1;
344 return (char *) varInfo + sizeof(VarSizeInfo);
348 varInfo = (VarSizeInfo*)((char*)varInfo + sizeof(VarSizeInfo)
349 +varInfo->size_);
351 return NULL;
354 //Allocates memory to store data of variable size
355 void* Chunk::allocate(Database *db, size_t size, DbRetVal *status)
357 if (0 == size) return NULL;
358 //check if the size is more than PAGE_SIZE
359 //if it is more than the PAGE_SIZE, then allocate new
360 //page using database and then link the curPage to the
361 //newly allocated page
362 //if it is less than PAGE_SIZE, then check the curpage for
363 //free memory of specified size
364 //if not available, then scan from the firstPage for the free
365 //space
367 //TODO::During the scan, merge nearby nodes if both are free
368 //if not available then allocate new page
370 size_t alignedSize = os::align(size);
371 void *data = NULL;
372 int ret = getChunkMutex(db->procSlot);
373 if (ret != 0)
375 printError(ErrLockTimeOut,"Unable to acquire chunk Mutex");
376 return NULL;
378 if (alignedSize > PAGE_SIZE)
380 data = allocateForLargeDataSize(db, alignedSize);
382 else
384 data = allocateFromCurPageForVarSize(alignedSize);
385 if (NULL == data) {
386 //No available spaces in the current page.
387 //allocate new page
388 data= allocFromNewPageForVarSize(db, alignedSize);
389 if (NULL == data) {
390 printError(ErrNoMemory, "No memory in any of the pages:Increase db size");
391 if (status != NULL) *status = ErrNoMemory;
395 releaseChunkMutex(db->procSlot);
396 return data;
399 //Assumes chunk mutex is already taken, before calling this
400 void* Chunk::varSizeFirstFitAllocate(size_t size)
402 printDebug(DM_VarAlloc, "Chunk::varSizeFirstFitAllocate size:%d firstPage:%x",
403 size, firstPage_);
405 Page *page = ((PageInfo*)firstPage_);
406 size_t alignedSize = os::align(size);
407 while(NULL != page)
409 VarSizeInfo *varInfo = (VarSizeInfo*)(((char*)page) + sizeof(PageInfo));
410 while ((char*) varInfo < ((char*)page + PAGE_SIZE))
412 if (0 == varInfo->isUsed_)
414 if( alignedSize < varInfo->size_)
416 splitDataBucket(varInfo, alignedSize);
417 return ((char*)varInfo) + sizeof(VarSizeInfo);
419 else if (alignedSize == varInfo->size_) {
420 varInfo->isUsed_ = 1;
421 printDebug(DM_VarAlloc, "VarSizeFirstFitAllocate returning %x", ((char*)varInfo) +sizeof(VarSizeInfo));
422 return ((char *) varInfo) + sizeof(VarSizeInfo);
425 varInfo = (VarSizeInfo*)((char*)varInfo + sizeof(VarSizeInfo)
426 +varInfo->size_);
428 printDebug(DM_VarAlloc, "Chunk:This page does not have free data nodes page:%x", page);
429 page = ((PageInfo*) page)->nextPage_;
431 return NULL;
434 void Chunk::freeForVarSizeAllocator(void *ptr, int pslot)
436 int ret = getChunkMutex(pslot);
437 if (ret != 0)
439 printError(ErrLockTimeOut,"Unable to acquire chunk Mutex");
440 return;
442 VarSizeInfo *varInfo = (VarSizeInfo*)((char*)ptr- sizeof(VarSizeInfo));
443 varInfo->isUsed_ = 0;
444 printDebug(DM_VarAlloc,"chunkID:%d Unset isUsed for %x", chunkID_, varInfo);
445 releaseChunkMutex(pslot);
446 return;
450 void Chunk::freeForLargeAllocator(void *ptr, int pslot)
452 //There will be max only one data element in a page.
453 //PageInfo is stored just before the data.
454 int ret = getChunkMutex(pslot);
455 if (ret != 0)
457 printError(ErrLockTimeOut,"Unable to acquire chunk Mutex");
458 return;
460 PageInfo *pageInfo = (PageInfo*)(((char*)
461 ptr) - (sizeof(PageInfo) + sizeof(int)));
462 PageInfo *pInfo = (PageInfo*)firstPage_, *prev = (PageInfo*)firstPage_;
463 bool found = false;
464 while(!found)
466 if (pInfo == pageInfo) {found = true; break; }
467 prev = pInfo;
468 pInfo = (PageInfo*)pInfo->nextPage_;
470 if (!found)
472 printError(ErrSysFatal,"Page %x not found in page list:Logical error", pageInfo );
473 releaseChunkMutex(pslot);
474 return ;
476 prev->nextPage_ = pageInfo->nextPage_;
477 pageInfo->nextPageAfterMerge_ = NULL;
478 pageInfo->isUsed_ = 0;
479 os::memset(pageInfo, 0 , allocSize_);
480 pageInfo->hasFreeSpace_ = 1;
481 releaseChunkMutex(pslot);
482 return;
485 //Frees the memory pointed by ptr
486 void Chunk::free(Database *db, void *ptr)
488 if (0 == allocSize_)
490 freeForVarSizeAllocator(ptr, db->procSlot);
491 return;
493 int noOfDataNodes =os::floor((PAGE_SIZE - sizeof(PageInfo)) / allocSize_);
495 if (0 == noOfDataNodes)
497 freeForLargeAllocator(ptr, db->procSlot);
498 return;
500 int ret = getChunkMutex(db->procSlot);
501 if (ret != 0)
503 printError(ErrLockTimeOut,"Unable to acquire chunk Mutex");
504 return;
506 //below is the code for freeing in fixed size allocator
508 //unset the used flag
509 *((int*)ptr -1 ) = 0;
510 PageInfo *pageInfo;
511 pageInfo = getPageInfo(db, ptr);
512 if (NULL == pageInfo)
514 printError(ErrSysFatal,"Probable Data corruption: pageInfo is NULL", pageInfo );
515 releaseChunkMutex(db->procSlot);
516 return;
518 //set the pageinfo where this ptr points
519 pageInfo->hasFreeSpace_ = 1;
520 releaseChunkMutex(db->procSlot);
521 return;
524 //returns the pageInfo of the page where this ptr points
525 //This works only if the data size is less than PAGE_SIZE
526 //If ptr points to data which is more than PAGE_SIZE,then
527 //calling this might lead to memory corruption
528 //Note:IMPORTANT::assumes db lock is taken before calling this
529 PageInfo* Chunk::getPageInfo(Database *db, void *ptr)
531 if (allocSize_ < PAGE_SIZE - sizeof(PageInfo)) {
532 int rem = (long) ptr % 8192;
533 return (PageInfo*)(((char*)ptr) - rem);
534 } else {
535 //large size allocator
536 char *inPtr = (char*)ptr;
537 PageInfo* pageInfo = ((PageInfo*)firstPage_);
539 while( pageInfo != NULL )
541 if (inPtr > (char*) pageInfo && pageInfo->nextPageAfterMerge_ >inPtr)
542 return pageInfo;
543 pageInfo = (PageInfo*)pageInfo->nextPage_ ;
546 return NULL;
549 //If called on chunk used to store tuples, it returns the total number of rows
550 //present in the table
551 long Chunk::getTotalDataNodes()
553 long totalNodes =0;
554 if (0 == allocSize_) //->variable size allocator
556 Page *page = ((PageInfo*)firstPage_);
557 while(NULL != page)
559 VarSizeInfo *varInfo = (VarSizeInfo*)(((char*)page) + sizeof(PageInfo));
560 while ((char*) varInfo < ((char*)page + PAGE_SIZE))
562 if (1 == varInfo->isUsed_) totalNodes++;
563 varInfo = (VarSizeInfo*)((char*)varInfo + sizeof(VarSizeInfo)
564 +varInfo->size_);
566 page = ((PageInfo*) page)->nextPage_;
568 return totalNodes;
571 //TODO::for large size allocator
572 if (allocSize_ >PAGE_SIZE)//->each page has only one data node
573 return 0;
575 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/allocSize_);
576 PageInfo* pageInfo = ((PageInfo*)firstPage_);
577 char *data = ((char*)firstPage_) + sizeof(PageInfo);
578 int i=0;
579 while( pageInfo != NULL )
581 data = ((char*)pageInfo) + sizeof(PageInfo);
582 for (i = 0; i< noOfDataNodes; i++)
584 if (*((int*)data) == 1) { totalNodes++;}
585 data = data + allocSize_;
587 pageInfo = (PageInfo*)(((PageInfo*)pageInfo)->nextPage_) ;
589 return totalNodes;
592 //TODO::for other type of allocators
593 int Chunk::compact()
595 PageInfo* pageInfo = ((PageInfo*)firstPage_);
596 PageInfo* prevPage = pageInfo;
597 if (NULL == pageInfo)
599 return 0;
601 pageInfo = (PageInfo*)pageInfo->nextPage_;
602 if (0 == allocSize_)
604 while( pageInfo != NULL )
606 bool flag = false;
607 VarSizeInfo *varInfo = (VarSizeInfo*)(((char*)pageInfo) +
608 sizeof(PageInfo));
609 while ((char*) varInfo < ((char*)pageInfo + PAGE_SIZE))
611 if (1 == varInfo->isUsed_) {flag=true; break;}
612 varInfo = (VarSizeInfo*)((char*)varInfo + sizeof(VarSizeInfo)
613 +varInfo->size_);
615 if (!flag) {
616 printDebug(DM_VarAlloc,"Freeing unused page in varsize allocator %x\n", pageInfo);
617 prevPage->nextPage_ = pageInfo->nextPage_;
618 pageInfo->isUsed_ = 0;
620 prevPage = pageInfo;
621 pageInfo = (PageInfo*)(((PageInfo*)pageInfo)->nextPage_) ;
622 printDebug(DM_VarAlloc,"compact iter %x\n", pageInfo);
624 }else if (allocSize_ < PAGE_SIZE)
626 while( pageInfo != NULL )
628 bool flag = false;
629 int noOfDataNodes=os::floor((PAGE_SIZE - sizeof(PageInfo))/allocSize_);
630 char *data = ((char*)pageInfo) + sizeof(PageInfo);
631 for (int i = 0; i< noOfDataNodes -1; i++)
633 if (1 == *((int*)data)) { flag = true; break; }
634 data = data +allocSize_;
636 if (!flag) {
637 printDebug(DM_Alloc,"Freeing unused page in fixed allocator %x\n", pageInfo);
638 prevPage->nextPage_ = pageInfo->nextPage_;
639 pageInfo->isUsed_ = 0;
641 prevPage = pageInfo;
642 pageInfo = (PageInfo*)(((PageInfo*)pageInfo)->nextPage_) ;
643 printDebug(DM_Alloc,"compact iter %x\n", pageInfo);
646 return 0;
649 int Chunk::totalPages()
651 //logic is same for variable size and for large data node allocator.
652 PageInfo* pageInfo = ((PageInfo*)firstPage_);
653 int totPages=0;
654 while( pageInfo != NULL )
656 totPages++;
657 pageInfo = (PageInfo*)(((PageInfo*)pageInfo)->nextPage_) ;
659 return totPages;
662 int Chunk::initMutex()
664 return chunkMutex_.init("Chunk");
666 int Chunk::getChunkMutex(int procSlot)
668 return chunkMutex_.getLock(procSlot);
670 int Chunk::releaseChunkMutex(int procSlot)
672 return chunkMutex_.releaseLock(procSlot);
674 int Chunk::destroyMutex()
676 return chunkMutex_.destroy();
678 void Chunk::splitDataBucket(VarSizeInfo *varInfo, size_t needSize)
680 int remSpace = varInfo->size_ - sizeof(VarSizeInfo) - needSize;
681 varInfo->isUsed_ = 1;
682 varInfo->size_ = needSize;
683 varInfo = (VarSizeInfo*)((char*)varInfo +
684 sizeof(VarSizeInfo) + varInfo->size_);
685 varInfo->isUsed_ = 0;
686 varInfo->size_ = remSpace;
687 return;
691 void Chunk::createDataBucket(Page *page, size_t totalSize, size_t needSize)
693 VarSizeInfo *varInfo = (VarSizeInfo*)(((char*)page) + sizeof(PageInfo));
694 varInfo->isUsed_ = 0;
695 varInfo->size_ = PAGE_SIZE - sizeof(PageInfo) - sizeof(VarSizeInfo);
696 splitDataBucket(varInfo, needSize);
697 return;