@@ -51,9 +51,14 @@ def __init__(self):
51
51
# list of all object files and mappting to module names
52
52
self .object_to_module = dict ()
53
53
54
- # Memory usage summary structure
54
+ # Memory report (sections + summary)
55
+ self .mem_report = []
56
+
57
+ # Just the memory summary section
55
58
self .mem_summary = dict ()
56
59
60
+ self .subtotal = dict ()
61
+
57
62
def module_add (self , module_name , size , section ):
58
63
""" Adds a module / section to the list
59
64
@@ -399,68 +404,27 @@ def generate_output(self, export_format, file_output=None):
399
404
print "I/O error({0}): {1}" .format (error .errno , error .strerror )
400
405
return False
401
406
402
- subtotal = dict ()
403
- for k in self .sections :
404
- subtotal [k ] = 0
405
-
406
- # Calculate misc flash sections
407
- misc_flash_mem = 0
408
- for i in self .modules :
409
- for k in self .misc_flash_sections :
410
- if self .modules [i ][k ]:
411
- misc_flash_mem += self .modules [i ][k ]
412
-
413
- json_obj = []
414
- for i in sorted (self .modules ):
415
-
416
- row = []
417
-
418
- json_obj .append ({
419
- "module" :i ,
420
- "size" :{
421
- k :self .modules [i ][k ] for k in self .print_sections
422
- }
423
- })
424
-
425
- summary = {
426
- 'summary' :{
427
- 'static_ram' : (subtotal ['.data' ] + subtotal ['.bss' ]),
428
- 'heap' : (subtotal ['.heap' ]),
429
- 'stack' : (subtotal ['.stack' ]),
430
- 'total_ram' : (subtotal ['.data' ] + subtotal ['.bss' ] +
431
- subtotal ['.heap' ]+ subtotal ['.stack' ]),
432
- 'total_flash' : (subtotal ['.text' ] + subtotal ['.data' ] +
433
- misc_flash_mem ),
434
- }
435
- }
436
-
437
- self .mem_summary = json_obj + [summary ]
438
-
439
407
to_call = {'json' : self .generate_json ,
440
408
'csv-ci' : self .generate_csv ,
441
409
'table' : self .generate_table }[export_format ]
442
- to_call (subtotal , misc_flash_mem , file_desc )
410
+ to_call (file_desc )
443
411
444
412
if file_desc is not sys .stdout :
445
413
file_desc .close ()
446
414
447
- def generate_json (self , _ , dummy , file_desc ):
415
+ def generate_json (self , file_desc ):
448
416
"""Generate a json file from a memory map
449
417
450
418
Positional arguments:
451
- subtotal - total sizes for each module
452
- misc_flash_mem - size of misc flash sections
453
419
file_desc - the file to write out the final report to
454
420
"""
455
- file_desc .write (json .dumps (self .mem_summary , indent = 4 ))
421
+ file_desc .write (json .dumps (self .mem_report , indent = 4 ))
456
422
file_desc .write ('\n ' )
457
423
458
- def generate_csv (self , subtotal , misc_flash_mem , file_desc ):
424
+ def generate_csv (self , file_desc ):
459
425
"""Generate a CSV file from a memoy map
460
426
461
427
Positional arguments:
462
- subtotal - total sizes for each module
463
- misc_flash_mem - size of misc flash sections
464
428
file_desc - the file to write out the final report to
465
429
"""
466
430
csv_writer = csv .writer (file_desc , delimiter = ',' ,
@@ -474,36 +438,33 @@ def generate_csv(self, subtotal, misc_flash_mem, file_desc):
474
438
csv_sizes += [self .modules [i ][k ]]
475
439
476
440
csv_module_section += ['static_ram' ]
477
- csv_sizes += [subtotal [ '.data' ] + subtotal [ '.bss ' ]]
441
+ csv_sizes += [self . mem_summary [ 'static_ram ' ]]
478
442
479
443
csv_module_section += ['heap' ]
480
- if subtotal [ '. heap' ] == 0 :
444
+ if self . mem_summary [ ' heap' ] == 0 :
481
445
csv_sizes += ['unknown' ]
482
446
else :
483
- csv_sizes += [subtotal [ '. heap' ]]
447
+ csv_sizes += [self . mem_summary [ ' heap' ]]
484
448
485
449
csv_module_section += ['stack' ]
486
- if subtotal [ '. stack' ] == 0 :
450
+ if self . mem_summary [ ' stack' ] == 0 :
487
451
csv_sizes += ['unknown' ]
488
452
else :
489
- csv_sizes += [subtotal [ '. stack' ]]
453
+ csv_sizes += [self . mem_summary [ ' stack' ]]
490
454
491
455
csv_module_section += ['total_ram' ]
492
- csv_sizes += [subtotal ['.data' ] + subtotal ['.bss' ] +
493
- subtotal ['.heap' ] + subtotal ['.stack' ]]
456
+ csv_sizes += [self .mem_summary ['total_ram' ]]
494
457
495
458
csv_module_section += ['total_flash' ]
496
- csv_sizes += [subtotal [ '.text' ] + subtotal [ '.data' ] + misc_flash_mem ]
459
+ csv_sizes += [self . mem_summary [ 'total_flash' ] ]
497
460
498
461
csv_writer .writerow (csv_module_section )
499
462
csv_writer .writerow (csv_sizes )
500
463
501
- def generate_table (self , subtotal , misc_flash_mem , file_desc ):
464
+ def generate_table (self , file_desc ):
502
465
"""Generate a table from a memoy map
503
466
504
467
Positional arguments:
505
- subtotal - total sizes for each module
506
- misc_flash_mem - size of misc flash sections
507
468
file_desc - the file to write out the final report to
508
469
"""
509
470
# Create table
@@ -521,47 +482,80 @@ def generate_table(self, subtotal, misc_flash_mem, file_desc):
521
482
for i in sorted (self .modules ):
522
483
row = [i ]
523
484
524
- for k in self .sections :
525
- subtotal [k ] += self .modules [i ][k ]
526
-
527
485
for k in self .print_sections :
528
486
row .append (self .modules [i ][k ])
529
487
530
488
table .add_row (row )
531
489
532
490
subtotal_row = ['Subtotals' ]
533
491
for k in self .print_sections :
534
- subtotal_row .append (subtotal [k ])
492
+ subtotal_row .append (self . subtotal [k ])
535
493
536
494
table .add_row (subtotal_row )
537
495
538
496
file_desc .write (table .get_string ())
539
497
file_desc .write ('\n ' )
540
498
541
- if subtotal [ '. heap' ] == 0 :
499
+ if self . mem_summary [ ' heap' ] == 0 :
542
500
file_desc .write ("Allocated Heap: unknown\n " )
543
501
else :
544
502
file_desc .write ("Allocated Heap: %s bytes\n " %
545
- str (subtotal [ '. heap' ]))
503
+ str (self . mem_summary [ ' heap' ]))
546
504
547
- if subtotal [ '. stack' ] == 0 :
505
+ if self . mem_summary [ ' stack' ] == 0 :
548
506
file_desc .write ("Allocated Stack: unknown\n " )
549
507
else :
550
508
file_desc .write ("Allocated Stack: %s bytes\n " %
551
- str (subtotal [ '. stack' ]))
509
+ str (self . mem_summary [ ' stack' ]))
552
510
553
511
file_desc .write ("Total Static RAM memory (data + bss): %s bytes\n " %
554
- (str (subtotal [ '.data' ] + subtotal [ '.bss ' ])))
512
+ (str (self . mem_summary [ 'static_ram ' ])))
555
513
file_desc .write (
556
514
"Total RAM memory (data + bss + heap + stack): %s bytes\n "
557
- % (str (subtotal ['.data' ] + subtotal ['.bss' ] + subtotal ['.heap' ] +
558
- subtotal ['.stack' ])))
515
+ % (str (self .mem_summary ['total_ram' ])))
559
516
file_desc .write ("Total Flash memory (text + data + misc): %s bytes\n " %
560
- (str (subtotal ['.text' ] + subtotal ['.data' ] +
561
- misc_flash_mem )))
517
+ (str (self .mem_summary ['total_flash' ])))
562
518
563
519
toolchains = ["ARM" , "ARM_STD" , "ARM_MICRO" , "GCC_ARM" , "IAR" ]
564
520
521
+ def compute_report (self ):
522
+ for k in self .sections :
523
+ self .subtotal [k ] = 0
524
+
525
+ for i in sorted (self .modules ):
526
+ for k in self .sections :
527
+ self .subtotal [k ] += self .modules [i ][k ]
528
+
529
+ # Calculate misc flash sections
530
+ self .misc_flash_mem = 0
531
+ for i in self .modules :
532
+ for k in self .misc_flash_sections :
533
+ if self .modules [i ][k ]:
534
+ self .misc_flash_mem += self .modules [i ][k ]
535
+
536
+ self .mem_summary = {
537
+ 'static_ram' : (self .subtotal ['.data' ] + self .subtotal ['.bss' ]),
538
+ 'heap' : (self .subtotal ['.heap' ]),
539
+ 'stack' : (self .subtotal ['.stack' ]),
540
+ 'total_ram' : (self .subtotal ['.data' ] + self .subtotal ['.bss' ] +
541
+ self .subtotal ['.heap' ]+ self .subtotal ['.stack' ]),
542
+ 'total_flash' : (self .subtotal ['.text' ] + self .subtotal ['.data' ] +
543
+ self .misc_flash_mem ),
544
+ }
545
+
546
+ self .mem_report = []
547
+ for i in sorted (self .modules ):
548
+ self .mem_report .append ({
549
+ "module" :i ,
550
+ "size" :{
551
+ k :self .modules [i ][k ] for k in self .print_sections
552
+ }
553
+ })
554
+
555
+ self .mem_report .append ({
556
+ 'summary' : self .mem_summary
557
+ })
558
+
565
559
def parse (self , mapfile , toolchain ):
566
560
""" Parse and decode map file depending on the toolchain
567
561
@@ -584,6 +578,9 @@ def parse(self, mapfile, toolchain):
584
578
self .parse_map_file_iar (file_input )
585
579
else :
586
580
result = False
581
+
582
+ self .compute_report ()
583
+
587
584
except IOError as error :
588
585
print "I/O error({0}): {1}" .format (error .errno , error .strerror )
589
586
result = False
0 commit comments