Changeset 12372 for trunk/FACT++/src/EventBuilder.c
- Timestamp:
- 11/04/11 17:30:35 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/EventBuilder.c
r12227 r12372 125 125 WRK_DATA mBuffer[MAX_EVT * MAX_RUN]; //local working space 126 126 127 #define MXSTR 1000 128 char str[MXSTR]; 129 130 //ETIENNE 131 #define MAX_SLOTS_PER_CHUNK 100 132 133 typedef struct { 134 int32_t eventNumber; 135 int32_t chunk; 136 int32_t slot; 137 } CHUNK_MAPPING; 138 139 CHUNK_MAPPING mBufferMapping[MAX_EVT * MAX_RUN]; 140 141 #define MAX_EVT_MEM (sizeof(EVENT) + NPIX*1024*2 + NTMARK*1024*2) 142 #define MAX_HEAD_MEM (NBOARDS * sizeof(PEVNT_HEADER)) 143 #define MAX_SLOT_SIZE (MAX_EVT_MEM + MAX_HEAD_MEM) 144 #define MAX_CHUNK_SIZE (MAX_SLOT_SIZE*MAX_SLOTS_PER_CHUNK) 145 146 typedef struct { 147 void * pointers[MAX_SLOTS_PER_CHUNK]; 148 int32_t events[MAX_SLOTS_PER_CHUNK]; 149 int32_t nFreeSlots; 150 int32_t nSlots; 151 } ETI_CHUNK; 152 153 int32_t numAllocatedChunks = 0; 154 155 #define MAX_CHUNKS 8096 156 ETI_CHUNK EtiMemoryChunks[MAX_CHUNKS]; 157 158 void* ETI_Malloc(int evtId, int evtIndex) 159 { 160 int i,j; 161 for (i=0;i<numAllocatedChunks;i++) { 162 if (EtiMemoryChunks[i].nFreeSlots > 0) { 163 for (j=0;j<EtiMemoryChunks[i].nSlots;j++) 164 { 165 if (EtiMemoryChunks[i].events[j] == -1) 166 { 167 EtiMemoryChunks[i].events[j] = evtId; 168 EtiMemoryChunks[i].nFreeSlots--; 169 mBufferMapping[evtIndex].eventNumber = evtId; 170 mBufferMapping[evtIndex].chunk = i; 171 mBufferMapping[evtIndex].slot = j; 172 return EtiMemoryChunks[i].pointers[j]; 173 } 174 } 175 //If I reach this point then we have a problem because it should have found 176 //a free spot just above. 177 } 178 } 179 //If we reach this point this means that we should allocate more memory 180 int32_t numNewSlots = 0; 181 if ((numAllocatedChunks + 1)*MAX_CHUNK_SIZE < g_maxMem) 182 numNewSlots = MAX_SLOTS_PER_CHUNK; 183 else 184 numNewSlots = (g_maxMem - numAllocatedChunks*MAX_CHUNK_SIZE)/MAX_SLOT_SIZE; 185 186 if (numNewSlots == 0)//cannot allocate more without exceeding the max mem limit 187 { 188 return NULL; 189 } 190 191 EtiMemoryChunks[numAllocatedChunks].pointers[0] = malloc(MAX_SLOT_SIZE*numNewSlots); 192 if (EtiMemoryChunks[numAllocatedChunks].pointers[0] == NULL) 193 { 194 snprintf (str, MXSTR, "could not allocate %lu bytes. %d chunks are currently allocated (max allowed %lu bytes)", MAX_CHUNK_SIZE, numAllocatedChunks, g_maxMem); 195 factOut (kError, 000, str); 196 return NULL; 197 } 198 199 EtiMemoryChunks[numAllocatedChunks].nSlots = numNewSlots; 200 EtiMemoryChunks[numAllocatedChunks].events[0] = evtId; 201 EtiMemoryChunks[numAllocatedChunks].nFreeSlots = numNewSlots-1; 202 mBufferMapping[evtIndex].eventNumber = evtId; 203 mBufferMapping[evtIndex].chunk = numAllocatedChunks; 204 mBufferMapping[evtIndex].slot = 0; 205 206 for (i=1;i<numNewSlots;i++) 207 { 208 EtiMemoryChunks[numAllocatedChunks].pointers[i] = &(((char*)(EtiMemoryChunks[numAllocatedChunks].pointers[i-1]))[MAX_SLOT_SIZE]); 209 EtiMemoryChunks[numAllocatedChunks].events[i] = -1; 210 } 211 numAllocatedChunks++; 212 213 return EtiMemoryChunks[numAllocatedChunks-1].pointers[0]; 214 } 215 216 void ETI_Free(int evtId, int evtIndex) 217 { 218 ETI_CHUNK* currentChunk = &EtiMemoryChunks[mBufferMapping[evtIndex].chunk]; 219 if (currentChunk->events[mBufferMapping[evtIndex].slot] != evtId) 220 { 221 snprintf (str, MXSTR, "Mismatch in chunk mapping table. expected evtId %d. Got %d. No memory was freed.", evtId, currentChunk->events[mBufferMapping[evtIndex].slot]); 222 factOut (kError, 000, str); 223 return; 224 } 225 currentChunk->events[mBufferMapping[evtIndex].slot] = -1; 226 currentChunk->nFreeSlots++; 227 //check if some chunks should be freed 228 int i; 229 for (i=0;i<numAllocatedChunks;i++) 230 { 231 if (EtiMemoryChunks[i].nFreeSlots == EtiMemoryChunks[i].nSlots) 232 { 233 snprintf(str, MXSTR, "Slot %d could be freed", i); 234 factOut(kError, 000, str); 235 } 236 } 237 int chunkIndex = mBufferMapping[evtIndex].chunk; 238 if (chunkIndex != numAllocatedChunks-1) 239 return; 240 241 while (EtiMemoryChunks[chunkIndex].nFreeSlots == EtiMemoryChunks[chunkIndex].nSlots) 242 {//free this chunk 243 snprintf(str, MXSTR, "Freeing chunk #%d", numAllocatedChunks); 244 factOut(kError, 000, str); 245 free(EtiMemoryChunks[chunkIndex].pointers[0]); 246 numAllocatedChunks--; 247 chunkIndex--; 248 if (numAllocatedChunks == 0) 249 break; 250 } 251 } 252 //END ETIENNE 127 253 128 254 … … 189 315 190 316 191 #define MXSTR 1000192 char str[MXSTR];193 194 317 SHORT_BYTE start, stop; 195 318 … … 211 334 return 0; 212 335 } 213 336 int 337 runFinish (uint32_t runnr) 338 { 339 snprintf (str, MXSTR, "Should finish run %d (but not yet possible)", 340 runnr); 341 factOut (kInfo, 173, str); //but continue anyhow 342 return 0; 343 } 214 344 215 345 int … … 235 365 //this is a not used socket, so do nothing ... 236 366 rd->sockStat = 77; 237 rd->rBuf = =NULL ;367 rd->rBuf = NULL ; 238 368 return 0; 239 369 } … … 339 469 evtCtrl.evtStat[i] = -1; 340 470 evtCtrl.pcTime[i] = actime; //initiate to far future 341 } 342 471 472 //ETIENNE 473 mBufferMapping[i].chunk = -1; 474 mBufferMapping[i].eventNumber = -1; 475 mBufferMapping[i].slot = -1; 476 //END ETIENNE 477 } 343 478 344 479 actRun.FADhead = malloc (NBOARDS * sizeof (PEVNT_HEADER)); … … 515 650 516 651 RUNFOUND: 517 518 needmem = sizeof (EVENT) + NPIX * nRoi[0] * 2 + NTMARK * nRoi[0] * 2; //652 //ETIENNE 653 /* needmem = sizeof (EVENT) + NPIX * nRoi[0] * 2 + NTMARK * nRoi[0] * 2; // 519 654 520 655 headmem = NBOARDS * sizeof (PEVNT_HEADER); … … 534 669 return -11; 535 670 } 536 671 */ 672 mBuffer[i].FADhead = ETI_Malloc(evID, i); 673 mBuffer[i].buffer = NULL; 674 if (mBuffer[i].FADhead != NULL) 675 mBuffer[i].fEvent = (EVENT*)&(((char*)(mBuffer[i].FADhead))[MAX_HEAD_MEM]); 676 else 677 { 678 mBuffer[i].fEvent = NULL; 679 gj.usdMem = 0; 680 for (k=0;k<numAllocatedChunks;k++) 681 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE; 682 if (gj.usdMem > gj.maxMem) 683 gj.maxMem = gj.usdMem; 684 if (gi_memStat > 0) { 685 gi_memStat = -99; 686 snprintf (str, MXSTR, "no memory left to keep event %6d sock %3d", 687 evID, sk); 688 factOut (kError, 882, str); 689 } else { 690 snprintf (str, MXSTR, "no memory left to keep event %6d sock %3d", 691 evID, sk); 692 factOut (kDebug, 882, str); 693 } 694 return -11; 695 } 696 /* 537 697 mBuffer[i].FADhead = malloc (headmem); 538 698 if (mBuffer[i].FADhead == NULL) { … … 560 720 mBuffer[i].fEvent = NULL; 561 721 return -32; 562 } 722 }*/ 723 //END ETIENNE 563 724 //flag all boards as unused 564 725 mBuffer[i].nBoard = 0; … … 588 749 mBuffer[i].Errors[0] = 589 750 mBuffer[i].Errors[1] = mBuffer[i].Errors[2] = mBuffer[i].Errors[3] = 0; 590 591 gj.usdMem += needmem + headmem + gi_maxSize; 751 //ETIENNE 752 //gj.usdMem += needmem + headmem + gi_maxSize; 753 gj.usdMem = 0; 754 for (k=0;k<numAllocatedChunks;k++) 755 { 756 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE; 757 } 758 //END ETIENNE 592 759 if (gj.usdMem > gj.maxMem) 593 760 gj.maxMem = gj.usdMem; … … 636 803 evid = mBuffer[i].evNum; 637 804 freemem = mBuffer[i].evtLen; 638 639 free (mBuffer[i].fEvent); 805 //ETIENNE 806 ETI_Free(evid, i); 807 // free (mBuffer[i].fEvent); 640 808 mBuffer[i].fEvent = NULL; 641 809 642 free (mBuffer[i].FADhead);810 // free (mBuffer[i].FADhead); 643 811 mBuffer[i].FADhead = NULL; 644 812 645 free (mBuffer[i].buffer);813 // free (mBuffer[i].buffer); 646 814 mBuffer[i].buffer = NULL; 647 815 //END ETIENNE 648 816 headmem = NBOARDS * sizeof (PEVNT_HEADER); 649 817 mBuffer[i].evNum = mBuffer[i].nRoi = -1; 650 818 mBuffer[i].runNum = 0; 651 652 gj.usdMem = gj.usdMem - freemem - headmem - gi_maxSize; 819 //ETIENNE 820 // gj.usdMem = gj.usdMem - freemem - headmem - gi_maxSize; 821 gj.usdMem = 0; 822 int k; 823 for (k=0;k<numAllocatedChunks;k++) 824 { 825 gj.usdMem += EtiMemoryChunks[k].nSlots * MAX_SLOT_SIZE; 826 } 827 //END ETIENNE 653 828 gj.bufTot--; 654 829
Note:
See TracChangeset
for help on using the changeset viewer.