13 #include <sys/times.h>
14 #include <sys/resource.h>
18 static void usage(
const char* argv0,
bool help =
false )
20 std::ostream& str =
help ? std::cout : std::cerr;
21 str <<
"Usage: " << argv0 <<
" [-H|-b|-k|-m] <filename> [<filename> ...]" << std::endl
22 <<
" " << argv0 <<
" [-H|-b|-k|-m] -T" << std::endl;
25 str <<
" " << argv0 <<
" -h" << std::endl;
29 std::cerr <<
" -H : human readable units" << std::endl
30 <<
" -b : bytes" << std::endl
31 <<
" -k : kilobytes (1 kB == 1024 bytes)" << std::endl
32 <<
" -m : megabytes (1 MB == 1024 kB)" << std::endl
33 <<
" -g : gigabytes (1 GB == 1024 MB)" << std::endl
34 <<
" -T : test mode" << std::endl
54 bool sysstats =
true );
60 int main(
int argc,
char* argv[] )
63 bool no_more_flags =
false;
64 bool test_mode =
false;
65 std::vector< int > input_file_list;
68 for(
int i = 1; i < argc; ++i )
70 if( !no_more_flags && argv[i][0] ==
'-' )
72 if( !strcmp( argv[i],
"-H" ) )
74 else if( !strcmp( argv[i],
"-b" ) )
76 else if( !strcmp( argv[i],
"-k" ) )
78 else if( !strcmp( argv[i],
"-m" ) )
80 else if( !strcmp( argv[i],
"-g" ) )
82 else if( !strcmp( argv[i],
"-T" ) )
84 else if( !strcmp( argv[i],
"-h" ) )
85 usage( argv[0],
true );
86 else if( !strcmp( argv[i],
"--" ) )
90 std::cerr << argv[0] <<
": Invalid flag: \"" << argv[i] <<
"\"." << std::endl << std::endl;
96 input_file_list.push_back( i );
103 if( input_file_list.empty() )
return 0;
108 for( std::vector< int >::iterator it = input_file_list.begin(); it != input_file_list.end(); ++it )
110 rval =
mb.load_file( argv[*it] );
116 mb.get_last_error( message );
117 std::cerr <<
mb.get_error_string( rval ) <<
": " << message << std::endl
118 << argv[*it] <<
": Failed to read file." << std::endl;
122 std::cout <<
"Loaded file: " << argv[*it] << std::endl;
151 static std::string
memstr(
unsigned long long val );
160 static std::string
center(
const char* str,
size_t width );
165 const char ANON_TAG_NAME[] =
"(anonymous)";
166 const int TYPE_WIDTH = 10;
167 const int MEM_WIDTH = 7;
168 const int MEM2_WIDTH = 2 * MEM_WIDTH + 1;
169 const int MIN_TAG_NAME_WIDTH = strlen( ANON_TAG_NAME );
170 const int DTYPE_WIDTH = 12;
171 const int STORAGE_WIDTH = 8;
179 std::cout.fill(
' ' );
180 std::cout << std::left << std::setw( TYPE_WIDTH ) <<
"Type" <<
' ' <<
center(
"Total", MEM2_WIDTH ) <<
' '
181 <<
center(
"Entity", MEM2_WIDTH ) <<
' ' <<
center(
"Adjacency", MEM2_WIDTH ) <<
' '
182 <<
center(
"Tag", MEM2_WIDTH ) <<
' ' << std::endl
183 << std::setw( TYPE_WIDTH ) <<
" ";
184 for(
int i = 0; i < 4; ++i )
185 std::cout <<
' ' << std::left << std::setw( MEM_WIDTH ) <<
"Used" <<
' ' << std::left
186 << std::setw( MEM_WIDTH ) <<
"Alloc";
187 std::cout << std::endl;
188 std::cout.fill(
'-' );
189 std::cout << std::setw( TYPE_WIDTH ) <<
'-';
190 for(
int i = 0; i < 8; ++i )
191 std::cout <<
' ' << std::setw( MEM_WIDTH ) <<
'-';
192 std::cout.fill(
' ' );
193 std::cout << std::endl;
199 if(
is_zero( stats ) )
continue;
216 std::vector< moab::Tag > tags;
217 std::vector< moab::Tag >::const_iterator ti;
218 mb.tag_get_tags( tags );
221 unsigned maxlen = MIN_TAG_NAME_WIDTH;
222 for( ti = tags.begin(); ti != tags.end(); ++ti )
225 rval =
mb.tag_get_name( *ti, name );
227 if( name.size() > maxlen ) maxlen = name.size();
233 std::cout.fill(
' ' );
234 std::cout << std::endl
235 << std::left << std::setw( maxlen ) <<
"Tag Name" <<
' ' << std::left << std::setw( DTYPE_WIDTH )
236 <<
"Type" <<
' ' << std::left << std::setw( STORAGE_WIDTH ) <<
"Storage" <<
' ' << std::left
237 << std::setw( MEM_WIDTH ) <<
"Used" <<
' ' << std::left << std::setw( MEM_WIDTH ) <<
"Alloc"
239 std::cout.fill(
'-' );
240 std::cout << std::setw( maxlen ) <<
'-' <<
' ' << std::setw( DTYPE_WIDTH ) <<
'-' <<
' '
241 << std::setw( STORAGE_WIDTH ) <<
'-' <<
' ' << std::setw( MEM_WIDTH ) <<
'-' <<
' '
242 << std::setw( MEM_WIDTH ) <<
'-' << std::endl;
243 std::cout.fill(
' ' );
247 for( ti = tags.begin(); ti != tags.end(); ++ti )
250 rval =
mb.tag_get_name( *ti, name );
253 unsigned long long occupied, allocated;
254 mb.estimated_memory_use( 0, 0, 0, 0, 0, 0, 0, 0, &*ti, 1, &occupied, &allocated );
256 std::cout << std::left << std::setw( maxlen ) << name <<
' ' << std::right << std::setw( DTYPE_WIDTH )
259 <<
memstr( occupied ) <<
' ' << std::right << std::setw( MEM_WIDTH ) <<
memstr( allocated )
268 std::cout << std::endl
269 <<
"TOTAL: (Used/Allocated)" << std::endl
283 std::FILE* filp = std::fopen(
"/proc/self/stat",
"r" );
284 unsigned long long vsize;
286 if( filp && 2 == std::fscanf( filp,
314 long long tmprss = rss * getpagesize();
316 std::cout << std::endl
317 <<
"SYSTEM:" << std::endl
318 <<
"Virtual memory: " <<
memstr( vsize )
321 <<
"Resident set size: " <<
memstr( tmprss )
328 struct rusage sysdata;
329 if( getrusage( RUSAGE_SELF, &sysdata ) )
331 std::cerr <<
"getrusage failed" << std::endl;
335 rss = sysdata.ru_maxrss;
336 long long tmprss = rss * getpagesize();
337 std::cerr << std::endl
338 <<
"SYSTEM:" << std::endl
339 <<
"Resident set size: " <<
memstr( tmprss ) << std::endl;
343 if( filp ) fclose( filp );
357 mb.get_entities_by_type( 0, type, range );
371 static unsigned long long rdiv(
unsigned long long num,
unsigned long long den )
373 return ( num + den / 2 ) / den;
376 std::string
memstr(
unsigned long long val )
378 const unsigned long long kb = 1024;
379 const unsigned long long mb = kb * kb;
380 const unsigned long long gb = kb *
mb;
381 const unsigned long long tb = kb * gb;
383 std::ostringstream s;
387 s <<
rdiv( val, tb ) <<
"TB";
388 else if( val >= 10 * gb )
389 s <<
rdiv( val, gb ) <<
"GB";
390 else if( val >= 10 *
mb )
391 s <<
rdiv( val,
mb ) <<
"MB";
392 else if( val >= 10 * kb )
393 s <<
rdiv( val, kb ) <<
"kB";
401 unsigned long long den = 1;
420 s <<
rdiv( val, den );
428 std::ostringstream s;
431 rval =
mb.tag_get_data_type( tag, type );
440 typesize =
sizeof( int );
444 typesize =
sizeof( double );
465 rval =
mb.tag_get_length( tag,
size );
467 s <<
"VAR " << typestr;
469 s <<
size / typesize <<
" " << typestr;
479 rval =
mb.tag_get_type( tag, type );
495 std::string
center(
const char* str,
size_t width )
497 std::string text( str );
498 if( text.size() >= width )
return text;
500 width -= text.size();
507 std::ostringstream s;
508 s << std::setw( width / 2 ) <<
' ' << text << std::setw( width / 2 + width % 2 ) <<
' ';
514 const char prefix[] =
"****************";
521 const unsigned N = 1000;
524 double coords[3] = { 1, 2, 3 };
525 for(
unsigned i = 0; i < N; ++i )
526 mb.create_vertex( coords, h );
527 std::cout << std::endl << prefix <<
"Created " << N <<
" vertices" << std::endl;
530 for(
unsigned i = 0; i < N; ++i )
531 mb.create_vertex( coords, h );
532 std::cout << std::endl << prefix <<
"Created another " << N <<
" vertices" << std::endl;
535 for(
int i = 0; i < 100; ++i )
537 for(
unsigned j = 0; j < N; ++j )
538 mb.create_vertex( coords, h );
540 std::cout << std::endl << prefix <<
"Created another " << 100 * N <<
" vertices" << std::endl;
546 it = handles.
begin();
547 for(
unsigned i = 0; i < N - 2; ++i, ++it )
559 std::cout << std::endl << prefix <<
"Created " << N - 2 <<
" triangles" << std::endl;
562 it = handles.
begin();
563 for(
unsigned i = 0; i < N - 3; ++i, ++it )
577 std::cout << std::endl << prefix <<
"Created " << N - 3 <<
" quads" << std::endl;
580 for(
int i = 0; i < 100; ++i )
582 it = handles.
begin();
583 for(
unsigned j = 0; j < N - 3; ++j, ++it )
598 std::cout << std::endl << prefix <<
"Created another " << 100 * ( N - 3 ) <<
" quads" << std::endl;
606 std::cerr <<
"Failed to get GLOBAL_ID tag handle" << std::endl;
612 for( it = handles.
begin(); it != handles.
end(); ++it )
614 mb.tag_set_data( tag, &*it, 1, &
id );
617 std::cout << std::endl << prefix <<
"Set global ID tag on " << handles.
size() <<
" vertices" << std::endl;
623 for( it = handles.
begin(); it != handles.
end(); ++it )
625 mb.tag_set_data( tag, &*it, 1, &
id );
628 std::cout << std::endl << prefix <<
"Set global ID tag on " << handles.
size() <<
" quads" << std::endl;
635 for( it = handles.
begin(); it != handles.
end(); ++it )
637 mb.get_coords( &*it, 1, coords );
638 mb.tag_set_data( tag, &*it, 1, coords );
640 std::cout << std::endl
641 << prefix <<
"Copied vertex coords to sparse tag for " << handles.
size() <<
" vertices" << std::endl;
648 for( it = handles.
begin(); it != handles.
end(); ++it )
651 mb.tag_set_data( tag, &*it, 1, &
byte );
653 std::cout << std::endl << prefix <<
"Set 1-bit tag for " << handles.
size() <<
" triangles" << std::endl;
659 std::vector< moab::EntityHandle > adj_vec;
660 mb.get_adjacencies( &*handles.
begin(), 1, 2,
false, adj_vec );
661 std::cout << std::endl << prefix <<
"Created vertex-to-element adjacencies" << std::endl;
663 std::cout << std::endl;