Commit | Line | Data |
---|---|---|
7af17f1e MA |
1 | // File: OSD_MAllocHook.cxx |
2 | // Created: 04.02.2011 | |
3 | // Author: Mikhail SAZONOV | |
4 | // Copyright: Open CASCADE S.A.S. 2011 | |
5 | ||
6 | #include <OSD_MAllocHook.hxx> | |
7 | ||
8 | #ifndef WNT | |
9 | #if !defined __STDC_LIMIT_MACROS | |
10 | #define __STDC_LIMIT_MACROS | |
11 | #endif | |
12 | #include <stdint.h> | |
13 | #endif | |
14 | ||
15 | #include <set> | |
16 | #include <map> | |
17 | ||
213cb888 M |
18 | #ifndef SIZE_MAX |
19 | #define SIZE_MAX UINT_MAX | |
20 | #endif | |
21 | ||
7af17f1e MA |
22 | #define MAX_STR 80 |
23 | ||
24 | static OSD_MAllocHook::Callback* MypCurrentCallback = NULL; | |
25 | ||
26 | //======================================================================= | |
27 | //function : GetCallback | |
28 | //purpose : | |
29 | //======================================================================= | |
30 | ||
31 | OSD_MAllocHook::Callback* OSD_MAllocHook::GetCallback() | |
32 | { | |
33 | return MypCurrentCallback; | |
34 | } | |
35 | ||
36 | //======================================================================= | |
37 | //function : GetLogFileHandler | |
38 | //purpose : | |
39 | //======================================================================= | |
40 | ||
41 | OSD_MAllocHook::LogFileHandler* OSD_MAllocHook::GetLogFileHandler() | |
42 | { | |
43 | static LogFileHandler MyHandler; | |
44 | return &MyHandler; | |
45 | } | |
46 | ||
47 | //======================================================================= | |
48 | //function : GetCollectBySize | |
49 | //purpose : | |
50 | //======================================================================= | |
51 | ||
52 | OSD_MAllocHook::CollectBySize* OSD_MAllocHook::GetCollectBySize() | |
53 | { | |
54 | static CollectBySize MyHandler; | |
55 | return &MyHandler; | |
56 | } | |
57 | ||
58 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
59 | // Platform-dependent methods | |
60 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
61 | ||
62 | #ifdef WNT | |
63 | #include <crtdbg.h> | |
64 | ||
65 | static long getRequestNum(void* pvData, long lRequest, size_t& theSize) | |
66 | { | |
67 | if (_CrtIsValidHeapPointer(pvData)) | |
68 | { | |
69 | #if _MSC_VER == 1500 // VS 2008 | |
70 | #define nNoMansLandSize 4 | |
71 | // the header struct is taken from crt/src/dbgint.h | |
72 | struct _CrtMemBlockHeader | |
73 | { | |
74 | #ifdef _WIN64 | |
75 | int nBlockUse; | |
76 | size_t nDataSize; | |
77 | #else | |
78 | size_t nDataSize; | |
79 | int nBlockUse; | |
80 | #endif | |
81 | long lRequest; | |
82 | unsigned char gap[nNoMansLandSize]; | |
83 | }; | |
84 | _CrtMemBlockHeader* aHeader = ((_CrtMemBlockHeader*)pvData)-1; | |
85 | theSize = aHeader->nDataSize; | |
86 | return aHeader->lRequest; | |
87 | #endif | |
88 | } | |
89 | return lRequest; | |
90 | } | |
91 | ||
92 | int __cdecl MyAllocHook(int nAllocType, | |
93 | void * pvData, | |
94 | size_t nSize, | |
95 | int nBlockUse, | |
96 | long lRequest, | |
97 | const unsigned char * /*szFileName*/, | |
98 | int /*nLine*/) | |
99 | { | |
100 | if (nBlockUse == _CRT_BLOCK || // Ignore internal C runtime library allocations | |
101 | MypCurrentCallback == NULL) | |
102 | return(1); | |
103 | ||
104 | if (nAllocType == _HOOK_ALLOC) | |
105 | MypCurrentCallback->AllocEvent(nSize, lRequest); | |
106 | else if (nAllocType == _HOOK_FREE) | |
107 | { | |
108 | // for free hook, lRequest is not defined, | |
109 | // but we can take it from the CRT mem block header | |
110 | size_t aSize = 0; | |
111 | lRequest = getRequestNum(pvData, lRequest, aSize); | |
112 | MypCurrentCallback->FreeEvent(pvData, aSize, lRequest); | |
113 | } | |
114 | else // _HOOK_REALLOC | |
115 | { | |
116 | // for realloc hook, lRequest shows the new request, | |
117 | // and we should get request number for old block | |
118 | size_t anOldSize = 0; | |
119 | long anOldRequest = getRequestNum(pvData, 0, anOldSize); | |
120 | MypCurrentCallback->FreeEvent(pvData, anOldSize, anOldRequest); | |
121 | MypCurrentCallback->AllocEvent(nSize, lRequest); | |
122 | } | |
123 | ||
124 | return(1); // Allow the memory operation to proceed | |
125 | } | |
126 | ||
127 | //======================================================================= | |
128 | //function : SetCallback | |
129 | //purpose : | |
130 | //======================================================================= | |
131 | ||
132 | void OSD_MAllocHook::SetCallback(Callback* theCB) | |
133 | { | |
134 | MypCurrentCallback = theCB; | |
135 | if (theCB == NULL) | |
136 | _CrtSetAllocHook(NULL); | |
137 | else | |
138 | _CrtSetAllocHook(MyAllocHook); | |
139 | } | |
140 | ||
141 | #else // ! WNT | |
142 | ||
143 | // Not yet implemented for non-WNT platform | |
144 | ||
145 | void OSD_MAllocHook::SetCallback(Callback* theCB) | |
146 | { | |
147 | MypCurrentCallback = theCB; | |
148 | } | |
149 | ||
150 | #endif // WNT | |
151 | ||
152 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
153 | // LogFileHandler handler methods | |
154 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
155 | ||
156 | //======================================================================= | |
157 | //function : LogFileHandler::LogFileHandler | |
158 | //purpose : | |
159 | //======================================================================= | |
160 | ||
161 | OSD_MAllocHook::LogFileHandler::LogFileHandler() | |
162 | : myLogFile(NULL), | |
163 | myBreakSize(0) | |
164 | { | |
165 | } | |
166 | ||
167 | //======================================================================= | |
168 | //function : LogFileHandler::~LogFileHandler | |
169 | //purpose : | |
170 | //======================================================================= | |
171 | ||
172 | OSD_MAllocHook::LogFileHandler::~LogFileHandler() | |
173 | { | |
174 | Close(); | |
175 | } | |
176 | ||
177 | //======================================================================= | |
178 | //function : LogFileHandler::Open | |
179 | //purpose : | |
180 | //======================================================================= | |
181 | ||
182 | Standard_Boolean OSD_MAllocHook::LogFileHandler::Open(const char* theFileName) | |
183 | { | |
184 | Close(); | |
185 | myLogFile = fopen(theFileName, "w"); | |
186 | if (myLogFile != NULL) | |
187 | { | |
188 | fputs("Operation type; Request Number; Block Size\n", myLogFile); | |
189 | fputs("------------------------------------------\n", myLogFile); | |
190 | } | |
191 | return myLogFile != NULL; | |
192 | } | |
193 | ||
194 | //======================================================================= | |
195 | //function : LogFileHandler::Close | |
196 | //purpose : | |
197 | //======================================================================= | |
198 | ||
199 | void OSD_MAllocHook::LogFileHandler::Close() | |
200 | { | |
201 | if (myLogFile != NULL) | |
202 | { | |
203 | fclose(myLogFile); | |
204 | myLogFile = NULL; | |
205 | } | |
206 | } | |
207 | ||
208 | //======================================================================= | |
209 | //function : LogFileHandler::MakeReport | |
210 | //purpose : | |
211 | //======================================================================= | |
212 | ||
213 | struct StorageInfo | |
214 | { | |
215 | Standard_Size size; | |
216 | int nbAlloc; | |
217 | int nbFree; | |
218 | int nbLeftPeak; | |
219 | std::set<unsigned long>* alive; | |
220 | StorageInfo() | |
221 | : size(0), nbAlloc(0), nbFree(0), nbLeftPeak(0), alive(NULL) {} | |
222 | StorageInfo(Standard_Size theSize) | |
223 | : size(theSize), nbAlloc(0), nbFree(0), nbLeftPeak(0), alive(NULL) {} | |
224 | ~StorageInfo() | |
225 | { | |
226 | if (alive) | |
227 | delete alive; | |
228 | } | |
229 | std::set<unsigned long>& Alive() | |
230 | { | |
231 | if (!alive) | |
232 | alive = new std::set<unsigned long>; | |
233 | return *alive; | |
234 | } | |
235 | const std::set<unsigned long>& Alive() const | |
236 | { | |
237 | return *alive; | |
238 | } | |
239 | }; | |
240 | ||
241 | inline bool operator < (const StorageInfo& one, const StorageInfo& two) | |
242 | { | |
243 | return one.size < two.size; | |
244 | } | |
245 | ||
246 | Standard_Boolean OSD_MAllocHook::LogFileHandler::MakeReport | |
247 | (const char* theLogFile, | |
248 | const char* theOutFile, | |
249 | const Standard_Boolean theIncludeAlive) | |
250 | { | |
251 | // open log file | |
252 | FILE* aLogFile = fopen(theLogFile, "r"); | |
253 | if (aLogFile == NULL) | |
254 | return Standard_False; | |
255 | ||
256 | // skip 2 header lines | |
257 | char aStr[MAX_STR]; | |
258 | if (fgets(aStr, MAX_STR-1, aLogFile) == NULL) | |
259 | { | |
260 | fclose(aLogFile); | |
261 | return Standard_False; | |
262 | } | |
263 | if (fgets(aStr, MAX_STR-1, aLogFile) == NULL) | |
264 | { | |
265 | fclose(aLogFile); | |
266 | return Standard_False; | |
267 | } | |
268 | ||
269 | // scan the log file | |
270 | size_t aTotalLeftSize = 0; | |
271 | size_t aTotalPeakSize = 0; | |
272 | std::set<StorageInfo> aStMap; | |
273 | while (fgets(aStr, MAX_STR-1, aLogFile) != NULL) | |
274 | { | |
275 | // detect operation type, request number and block size | |
276 | unsigned long aReqNum, aSize; | |
277 | char* aType = aStr; | |
278 | char* pStr = aStr; | |
279 | //sscanf(aStr, "%5s %lu %lu", aType, &aReqNum, &aSize); | |
280 | while (*pStr != ' ' && *pStr) pStr++; | |
281 | *pStr++ = '\0'; | |
282 | while (*pStr == ' ' && *pStr) pStr++; | |
283 | aReqNum = atol(pStr); | |
284 | while (*pStr != ' ' && *pStr) pStr++; | |
285 | while (*pStr == ' ' && *pStr) pStr++; | |
286 | aSize = atol(pStr); | |
287 | Standard_Boolean isAlloc = Standard_False; | |
288 | if (strcmp(aType, "alloc") == 0) | |
289 | { | |
290 | isAlloc = Standard_True; | |
291 | } | |
292 | else if (strcmp(aType, "free") != 0) | |
293 | continue; | |
294 | ||
295 | // collect statistics by storage size | |
296 | StorageInfo aSuchInfo(aSize); | |
297 | std::set<StorageInfo>::iterator aFound = aStMap.find(aSuchInfo); | |
298 | if (aFound == aStMap.end()) | |
299 | aFound = aStMap.insert(aSuchInfo).first; | |
300 | StorageInfo& aInfo = const_cast<StorageInfo&>(*aFound); | |
301 | if (isAlloc) | |
302 | { | |
303 | if (aInfo.nbAlloc + 1 > 0) | |
304 | aInfo.nbAlloc++; | |
305 | aTotalLeftSize += aSize; | |
306 | if (aTotalLeftSize > aTotalPeakSize) | |
307 | aTotalPeakSize = aTotalLeftSize; | |
308 | int nbLeft = aInfo.nbAlloc - aInfo.nbFree; | |
309 | if (nbLeft > aInfo.nbLeftPeak) | |
310 | aInfo.nbLeftPeak = nbLeft; | |
311 | aInfo.Alive().insert(aReqNum); | |
312 | } | |
313 | else | |
314 | { | |
315 | std::set<unsigned long>::iterator aFoundReqNum = | |
316 | aInfo.Alive().find(aReqNum); | |
317 | if (aFoundReqNum == aInfo.Alive().end()) | |
318 | // freeing non-registered block, skip it | |
319 | continue; | |
320 | aTotalLeftSize -= aSize; | |
321 | aInfo.Alive().erase(aFoundReqNum); | |
322 | if (aInfo.nbAlloc + 1 > 0) | |
323 | aInfo.nbFree++; | |
324 | } | |
325 | } | |
326 | fclose(aLogFile); | |
327 | ||
328 | // print the report | |
329 | FILE* aRepFile = fopen(theOutFile, "w"); | |
330 | if (aRepFile == NULL) | |
331 | return Standard_False; | |
332 | fprintf(aRepFile, "%10s %10s %10s %10s %10s %10s %10s\n", | |
333 | "BlockSize", "NbAlloc", "NbLeft", "NbLeftPeak", | |
334 | "AllocSize", "LeftSize", "PeakSize"); | |
335 | Standard_Size aTotAlloc = 0; | |
336 | for (std::set<StorageInfo>::const_iterator it = aStMap.begin(); | |
337 | it != aStMap.end(); ++it) | |
338 | { | |
339 | const StorageInfo& aInfo = *it; | |
340 | Standard_Integer nbLeft = aInfo.nbAlloc - aInfo.nbFree; | |
341 | Standard_Size aSizeAlloc = aInfo.nbAlloc * aInfo.size; | |
342 | Standard_Size aSizeLeft = nbLeft * aInfo.size; | |
343 | Standard_Size aSizePeak = aInfo.nbLeftPeak * aInfo.size; | |
344 | fprintf(aRepFile, "%10d %10d %10d %10d %10Iu %10Iu %10Iu\n", aInfo.size, | |
345 | aInfo.nbAlloc, nbLeft, aInfo.nbLeftPeak, | |
346 | aSizeAlloc, aSizeLeft, aSizePeak); | |
347 | if (aTotAlloc + aSizeAlloc < aTotAlloc) // overflow ? | |
348 | aTotAlloc = SIZE_MAX; | |
349 | else | |
350 | aTotAlloc += aSizeAlloc; | |
351 | if (theIncludeAlive && !aInfo.Alive().empty()) | |
352 | { | |
353 | for (std::set<unsigned long>::const_iterator it1 = aInfo.alive->begin(); | |
354 | it1 != aInfo.alive->end(); ++it1) | |
355 | fprintf(aRepFile, "%10lu\n", *it1); | |
356 | } | |
357 | } | |
358 | fprintf(aRepFile, "%10s %10s %10s %10s%c%10Iu %10Iu %10Iu\n", "Total:", | |
359 | "", "", "", (aTotAlloc == SIZE_MAX ? '>' : ' '), aTotAlloc, | |
360 | aTotalLeftSize, aTotalPeakSize); | |
361 | fclose(aRepFile); | |
362 | return Standard_True; | |
363 | } | |
364 | ||
365 | //======================================================================= | |
366 | //function : LogFileHandler::AllocEvent | |
367 | //purpose : | |
368 | //======================================================================= | |
369 | ||
370 | void OSD_MAllocHook::LogFileHandler::AllocEvent | |
371 | (size_t theSize, | |
372 | long theRequestNum) | |
373 | { | |
374 | if (myLogFile != NULL) | |
375 | { | |
376 | myMutex.Lock(); | |
377 | fprintf(myLogFile, "alloc %10lu %10u\n", theRequestNum, theSize); | |
378 | myMutex.Unlock(); | |
379 | if (myBreakSize == theSize) | |
380 | { | |
381 | int a = 1; | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | //======================================================================= | |
387 | //function : LogFileHandler::FreeEvent | |
388 | //purpose : | |
389 | //======================================================================= | |
390 | ||
391 | void OSD_MAllocHook::LogFileHandler::FreeEvent | |
392 | (void* /*theData*/, | |
393 | size_t theSize, | |
394 | long theRequestNum) | |
395 | { | |
396 | if (myLogFile != NULL) | |
397 | { | |
398 | myMutex.Lock(); | |
399 | fprintf(myLogFile, "free %10lu %10u\n", theRequestNum, theSize); | |
400 | myMutex.Unlock(); | |
401 | } | |
402 | } | |
403 | ||
404 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
405 | // CollectBySize handler methods | |
406 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
407 | ||
408 | //======================================================================= | |
409 | //function : CollectBySize::CollectBySize | |
410 | //purpose : | |
411 | //======================================================================= | |
412 | ||
413 | OSD_MAllocHook::CollectBySize::CollectBySize() | |
414 | : myArray(NULL), | |
415 | myTotalLeftSize(0), | |
416 | myTotalPeakSize(0), | |
417 | myBreakSize(0) | |
418 | { | |
419 | Reset(); | |
420 | } | |
421 | ||
422 | //======================================================================= | |
423 | //function : CollectBySize::~CollectBySize | |
424 | //purpose : | |
425 | //======================================================================= | |
426 | ||
427 | OSD_MAllocHook::CollectBySize::~CollectBySize() | |
428 | { | |
429 | if (myArray != NULL) | |
430 | delete [] myArray; | |
431 | } | |
432 | ||
433 | //======================================================================= | |
434 | //function : CollectBySize::Reset | |
435 | //purpose : | |
436 | //======================================================================= | |
437 | ||
438 | #define MAX_ALLOC_SIZE 2000000u | |
439 | ||
440 | void OSD_MAllocHook::CollectBySize::Reset() | |
441 | { | |
442 | myMutex.Lock(); | |
443 | if (myArray == NULL) | |
444 | myArray = new Numbers[MAX_ALLOC_SIZE]; | |
445 | else | |
446 | { | |
447 | for (int i = 0; i < MAX_ALLOC_SIZE; i++) | |
448 | myArray[i] = Numbers(); | |
449 | } | |
450 | myTotalLeftSize = 0; | |
451 | myTotalPeakSize = 0; | |
452 | myMutex.Unlock(); | |
453 | } | |
454 | ||
455 | //======================================================================= | |
456 | //function : CollectBySize::MakeReport | |
457 | //purpose : | |
458 | //======================================================================= | |
459 | ||
460 | Standard_Boolean OSD_MAllocHook::CollectBySize::MakeReport(const char* theOutFile) | |
461 | { | |
462 | // print the report | |
463 | FILE* aRepFile = fopen(theOutFile, "w"); | |
464 | if (aRepFile == NULL) | |
465 | return Standard_False; | |
466 | fprintf(aRepFile, "%10s %10s %10s %10s %10s %10s %10s\n", | |
467 | "BlockSize", "NbAlloc", "NbLeft", "NbLeftPeak", | |
468 | "AllocSize", "LeftSize", "PeakSize"); | |
469 | Standard_Size aTotAlloc = 0; | |
470 | for (int i = 0; i < MAX_ALLOC_SIZE; i++) | |
471 | { | |
472 | if (myArray[i].nbAlloc > 0) | |
473 | { | |
474 | Standard_Integer nbLeft = myArray[i].nbAlloc - myArray[i].nbFree; | |
475 | if (nbLeft < 0) | |
476 | nbLeft = 0; | |
477 | int aSize = i + 1; | |
478 | Standard_Size aSizeAlloc = myArray[i].nbAlloc * aSize; | |
479 | Standard_Size aSizeLeft = nbLeft * aSize; | |
480 | Standard_Size aSizePeak = myArray[i].nbLeftPeak * aSize; | |
481 | fprintf(aRepFile, "%10d %10d %10d %10d %10Iu %10Iu %10Iu\n", aSize, | |
482 | myArray[i].nbAlloc, nbLeft, myArray[i].nbLeftPeak, | |
483 | aSizeAlloc, aSizeLeft, aSizePeak); | |
484 | if (aTotAlloc + aSizeAlloc < aTotAlloc) // overflow ? | |
485 | aTotAlloc = SIZE_MAX; | |
486 | else | |
487 | aTotAlloc += aSizeAlloc; | |
488 | } | |
489 | } | |
490 | fprintf(aRepFile, "%10s %10s %10s %10s%c%10Iu %10Iu %10Iu\n", "Total:", | |
491 | "", "", "", (aTotAlloc == SIZE_MAX ? '>' : ' '), aTotAlloc, | |
492 | myTotalLeftSize, myTotalPeakSize); | |
493 | fclose(aRepFile); | |
494 | return Standard_True; | |
495 | } | |
496 | ||
497 | //======================================================================= | |
498 | //function : CollectBySize::AllocEvent | |
499 | //purpose : | |
500 | //======================================================================= | |
501 | ||
502 | void OSD_MAllocHook::CollectBySize::AllocEvent | |
503 | (size_t theSize, | |
504 | long /*theRequestNum*/) | |
505 | { | |
506 | if (myBreakSize == theSize) | |
507 | { | |
508 | int a = 1; | |
509 | } | |
510 | if (theSize > 0) | |
511 | { | |
512 | myMutex.Lock(); | |
513 | int ind = (theSize > MAX_ALLOC_SIZE ? MAX_ALLOC_SIZE-1 : (int)(theSize-1)); | |
514 | if (myArray[ind].nbAlloc + 1 > 0) | |
515 | myArray[ind].nbAlloc++; | |
516 | myTotalLeftSize += theSize; | |
517 | int nbLeft = myArray[ind].nbAlloc - myArray[ind].nbFree; | |
518 | if (nbLeft > myArray[ind].nbLeftPeak) | |
519 | myArray[ind].nbLeftPeak = nbLeft; | |
520 | if (myTotalLeftSize > myTotalPeakSize) | |
521 | myTotalPeakSize = myTotalLeftSize; | |
522 | myMutex.Unlock(); | |
523 | } | |
524 | } | |
525 | ||
526 | //======================================================================= | |
527 | //function : CollectBySize::FreeEvent | |
528 | //purpose : | |
529 | //======================================================================= | |
530 | ||
531 | void OSD_MAllocHook::CollectBySize::FreeEvent | |
532 | (void* /*theData*/, | |
533 | size_t theSize, | |
534 | long /*theRequestNum*/) | |
535 | { | |
536 | if (theSize > 0 && myTotalLeftSize >= theSize) | |
537 | { | |
538 | myMutex.Lock(); | |
539 | int ind = (theSize > MAX_ALLOC_SIZE ? MAX_ALLOC_SIZE-1 : (int)(theSize-1)); | |
540 | if (myArray[ind].nbFree + 1 > 0) | |
541 | myArray[ind].nbFree++; | |
542 | myTotalLeftSize -= theSize; | |
543 | myMutex.Unlock(); | |
544 | } | |
545 | } |