forked from InsightSoftwareConsortium/ITK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkSmapsFileParser.cxx
525 lines (464 loc) · 14.4 KB
/
itkSmapsFileParser.cxx
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkSmapsFileParser.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "itkSmapsFileParser.h"
#include <stdlib.h>
namespace itk
{
bool ITKCommon_EXPORT ci_equal(char a, char b)
{
return tolower(static_cast<int>(a)) == tolower(static_cast<int>(b));
}
MapRecord::~MapRecord()
{
}
void MapRecord::Reset(void)
{
m_Tokens.clear();
m_RecordName = "";
}
/* SmapsRecord implementation */
ITKCommon_EXPORT std::istream& operator>>(std::istream &in, SmapsRecord &record)
{
record.Reset();
try
{
// Get Header line
std::string headerline;
std::getline( in, headerline);
if (headerline.empty())
{
return in;
}
// Get name
std::istringstream stream(headerline);
std::string address, perms, offset, device;
int inode=-1;
// the header is defined with the following expression: "address permissions offset device inode [name]"
stream>>address;
if (!stream.good()) itkGenericExceptionMacro( << "bad address: " << address );
stream>>perms;
if (!stream.good()) itkGenericExceptionMacro( << "bad perms: " << perms );
stream>>offset;
if (!stream.good()) itkGenericExceptionMacro( << "bad offset: " << offset );
stream>>device;
if (!stream.good()) itkGenericExceptionMacro( << "bad device: " << device );
stream>>inode;
// name can be empty
if (!stream.eof())
{
std::getline(stream, record.m_RecordName);
}
std::string token;
std::streampos lastPos = in.tellg();
// a token is defined with the following expression: "token: N kB"
while ( std::getline(in,token,':').good() )
{
//make sure it is a token and not a new record. A token doesn't contains space character
if ( token.find(' ') != std::string::npos )
{
in.seekg (lastPos, std::ios::beg);
break;
}
//fill the token with the memory usage N in kB
in>>record.m_Tokens[token];
std::getline(in,token);
if ( token != " kB" || !in.good()) itkGenericExceptionMacro( << "bad size: " << record.m_Tokens[token] );
lastPos = in.tellg();
}
}
catch (ExceptionObject excp)
{
record.Reset();
// propagate the exception
itkGenericExceptionMacro( << "The smaps header is corrupted" );
}
return in;
}
ITKCommon_EXPORT std::istream& operator>>(std::istream &in, VMMapSummaryRecord &record)
{
record.Reset();
try
{
// the record name can have spaces.
in >> record.m_RecordName;
if (in.eof() && record.m_RecordName.empty())
{
return in;
}
if (!in.good())
{
itkGenericExceptionMacro( << "Bad record name: " << record.m_RecordName );
}
std::string bracket;
while ( (in>>bracket).good() && bracket.find("[",0) == std::string::npos )
{
record.m_RecordName += std::string(" ") + bracket;
}
if (!in.good() || bracket.find("[",0) == std::string::npos )
{
itkGenericExceptionMacro( << "For record: " << record.m_RecordName
<< ", bad left bracket: " << bracket );
}
in >> record.m_Tokens["Size"];
if( !in.good() )
{
itkGenericExceptionMacro( << "For record: " << record.m_RecordName
<< ", bad size: " << record.m_Tokens["Size"] );
}
in >> bracket;
if( !in.good() )
{
itkGenericExceptionMacro( << "For record: " << record.m_RecordName
<< ", bad right bracket: " << bracket );
}
}
catch (ExceptionObject excp)
{
record.Reset();
// propagate the exception
itkGenericExceptionMacro( << "The smaps header is corrupted" );
}
return in;
}
/* typical output:
* __DATA c000 [ 4K] rw-/rwx SM=ZER ...l/bin/cronwake
* VALLOC_USED(DefaultMallocZone_ 25000 [ 4K] rw-/rwx SM=COW
* MALLOC_USED(DefaultMallocZone_ 26000 [ 44K] rw-/rwx SM=ZER
* 31000 [ 4K] rw-/rwx SM=COW 34300000 00000...
* 32000 [ 76K] rw-/rwx SM=COW 00001eaa 01001...
* 45000 [ 4K] rw-/rwx SM=COW 34300000 00000...
* 46000 [ 344K] rw-/rwx SM=COW 00000000 00000...
* Memory tag=7 100000 [1044K] rw-/rwx SM=COW
* MALLOC_USED(DefaultMallocZone_ 300000 [ 4K] rw-/rwx SM=PRV
* Submap 90000000-9fffffff r--/r-- machine-wide submap
* __DATA a0000000 [ 4K] rw-/rw- SM=COW ...System.B.dylib
* __DATA a0001000 [ 4K] rw-/rw- SM=ZER ...System.B.dylib
* __DATA a0002000 [ 4K] rw-/rw- SM=COW ...System.B.dylib
* __DATA a0003000 [ 20K] rw-/rw- SM=COW ...System.B.dylib
*/
ITKCommon_EXPORT std::istream& operator>>(std::istream &in, VMMapRecord &record)
{
record.Reset();
bool submapFound = false;
bool recordHasNoName = false;
char line[256];
try
{
std::string address;
do
{
// the record name can have spaces.
in >> record.m_RecordName;
if( in.eof() || record.m_RecordName.empty() )
{
return in;
}
if (!in.good())
{
itkGenericExceptionMacro( << "Bad record name: " << record.m_RecordName );
}
//skip Submap entries
if ( record.m_RecordName == "Submap" )
{
in.getline(line,256);
submapFound = true;
}
// all the records have been parsed, this is a new section
else if ( record.m_RecordName == "====" )
{
record.Reset();
return in;
}
else
{
// the name is folowed by an address
in >> address;
if( !in.good() )
{
itkGenericExceptionMacro( << "For record: " << record.m_RecordName
<< ", bad address: " << address );
}
// If address is "[" then recordName was the address and there is name for
// the record, skip it.
if ( address.find("[",0) != std::string::npos )
{
in.getline(line,256);
recordHasNoName = true;
}
else
{
recordHasNoName = false;
}
submapFound = false;
}
} while(submapFound || recordHasNoName);
std::string bracket;
while ( (in>>bracket).good() && bracket.find("[",0) == std::string::npos )
{
// the string is not a bracket yet, but probably the address. So the previous
// address was just the name
record.m_RecordName += std::string(" ") + address;
address = bracket;
}
if (!in.good() || bracket.find("[",0) == std::string::npos )
{
itkGenericExceptionMacro( << "For record: " << record.m_RecordName
<< ", bad left bracket: " << bracket );
}
if ( bracket.length() > 1 )
{//bracket contains the size, ie "[1024K]"
record.m_Tokens["Size"] = atoi(bracket.substr(1,bracket.length()-3).c_str());
}
else
{
in>>record.m_Tokens["Size"];
}
if (!in.good())
{
itkGenericExceptionMacro( << "For record: " << record.m_RecordName
<< ", bad size: " << record.m_Tokens["Size"] );
}
in.getline(line,256);
if (!in.good())
{
itkGenericExceptionMacro( << "For record: " << record.m_RecordName
<< ", bad end of line: " << line );
}
}
catch (ExceptionObject excp)
{
record.Reset();
// propagate the exception
itkGenericExceptionMacro( << "The smaps header is corrupted" );
}
return in;
}
//bool ITKCommon_EXPORT ci_equal(char a, char b); // located in itkSmapsFileParser.cxx
/** Binary functor to accumulate memory usage in kB
*/
template<class TFirstType>
struct MapRecordPlusor{
MapRecordPlusor<TFirstType>(const char * token = "Size")
:m_Token(token)
{
}
TFirstType operator()(TFirstType first, const MapRecord* const & second)
{
std::map<std::string,unsigned int>::const_iterator it = second->m_Tokens.find(m_Token);
return first + ((it!=second->m_Tokens.end())?it->second:0);
}
const char * m_Token;
};
/** Binary functor to accumulate memory usage in kB
* The record must match (insensitively) the filter in order to be taken into account
*/
template<class TFirstType>
struct MapRecordConditionalPlusor{
MapRecordConditionalPlusor<TFirstType>(const char * filter, const char * token = "Size")
:m_Filter(filter), m_Token(token)
{
}
TFirstType operator()(TFirstType first, const MapRecord* const &second)
{
if ( std::search(second->m_RecordName.begin(),second->m_RecordName.end(),
m_Filter.begin(),m_Filter.end(),ci_equal) != second->m_RecordName.end())
{
return MapRecordPlusor<TFirstType>(m_Token)(first,second);
}
return first;
}
std::string m_Filter;
const char * m_Token;
};
/** --- MapData --- **/
/** MadData destructor */
MapData::~MapData()
{
this->Reset();
}
MapData::MemoryLoadType
MapData::GetTotalMemoryUsage( )
{
return std::accumulate(this->m_Records.begin(), this->m_Records.end(), 0, MapRecordPlusor<MemoryLoadType>());
}
MapData::MemoryLoadType
MapData::GetMemoryUsage( const char * filter , const char * token)
{
return std::accumulate(this->m_Records.begin(), this->m_Records.end(), 0, MapRecordConditionalPlusor<MemoryLoadType>(filter,token));
}
/** is the data empty ? */
bool MapData::Empty()
{
return m_Records.empty();
}
void DeleteMapRecord(MapRecord* const &record)
{
delete record;
}
void MapData::Reset()
{
std::for_each(m_Records.begin(),m_Records.end(),DeleteMapRecord);
m_Records.clear();
}
/** --- SmapsData --- **/
SmapsData_2_6::~SmapsData_2_6()
{
}
ITK_EXPORT std::istream& operator>>( std::istream & smapsStream, SmapsData_2_6 & data)
{
SmapsRecord* record = NULL;
// reset the records from a previous parsing
data.Reset();
try
{
record = new SmapsRecord;
// parse each line of the Smaps file and fill the record vector.
while( smapsStream >> *record )
{
data.m_Records.push_back( record );
record = new SmapsRecord;
}
}
catch( ExceptionObject excp )
{
// in case of error, erase the records.
data.Reset();
// propagate the exception
itkGenericExceptionMacro( << "The Smaps stream contains errors, can't read the memory records." );
}
delete record;
return smapsStream;
}
SmapsData_2_6::MemoryLoadType
SmapsData_2_6::GetHeapUsage()
{
MemoryLoadType heapUsage = this->GetMemoryUsage("heap","Size");
// in some machines, there is no [heap] record;
if ( heapUsage == 0 )
{
//use the unnamed segments instead
heapUsage = this->GetMemoryUsage("","Size");
}
return heapUsage;
}
SmapsData_2_6::MemoryLoadType
SmapsData_2_6::GetStackUsage()
{
return this->GetMemoryUsage("stack","Size");
}
/** --- VMMapData --- **/
VMMapData_10_2
::VMMapData_10_2()
:m_UsingSummary(false)
{
}
VMMapData_10_2
::~VMMapData_10_2()
{
}
ITK_EXPORT std::istream& operator>>( std::istream & stream, VMMapData_10_2 & data)
{
MapRecord* record = NULL;
// reset the records from a previous parsing
data.Reset();
try
{
std::string line;
std::streampos startPos = stream.tellg();
// get to the Summary subsection
while( std::getline(stream,line).good() )
{
if ( line.find("==== Summary for process",0) != std::string::npos )
break;
}
// get to the first record
while( std::getline(stream,line).good() )
{
if ( line.find("REGION TYPE",0) != std::string::npos )
break;
}
// burn the line "=========== [ =======]"
if ( !std::getline(stream,line).good() )
{
//sometimes, vmmap doesn't have any Region Type summary sections,
//parse "Writable regions" instead
//go back to the beginning of the file
stream.clear();
stream.seekg(0,std::ios_base::beg);
// get to the Summary subsection
while( std::getline(stream,line).good() )
{
if ( line.find("==== Writable regions for process",0) != std::string::npos )
break;
}
if ( stream.fail() )
{
itkGenericExceptionMacro( << "Can't find the \"Writable regions\" section, can't read the memory records." );
}
data.m_UsingSummary = false;
}
else
{
data.m_UsingSummary = true;
}
if( data.m_UsingSummary )
{
record = new VMMapSummaryRecord;
// parse each line of the Smaps file and fill the record vector.
while( stream >> *dynamic_cast<VMMapSummaryRecord *>(record) )
{
if( record->m_RecordName.empty() )
{
break;
}
data.m_Records.push_back( record );
record = new VMMapSummaryRecord;
}
}
else
{
record = new VMMapRecord;
// parse each line of the Smaps file and fill the record vector.
while( stream >> *dynamic_cast<VMMapRecord* >(record) )
{
if( record->m_RecordName.empty() )
{
break;
}
data.m_Records.push_back( record );
record = new VMMapRecord;
}
}
}
catch( ExceptionObject excp )
{
// in case of error, erase the records.
data.Reset();
// propagate the exception
itkGenericExceptionMacro( << "The VMMap stream contains errors, can't read the memory records." );
}
//last record failed, it hasn't be added into data, delete it.
delete record;
return stream;
}
VMMapData_10_2::MemoryLoadType
VMMapData_10_2::GetHeapUsage()
{
return this->GetMemoryUsage("malloc","Size");
}
VMMapData_10_2::MemoryLoadType
VMMapData_10_2::GetStackUsage()
{
return this->GetMemoryUsage("stack","Size");
}
} // end namespace itk