Skip to content

Commit 1047ab4

Browse files
authored
Merge pull request #2834 from bridadan/memap-consolidate
[tools] Making memap output consistent across output formats
2 parents 64d9665 + abb6d26 commit 1047ab4

File tree

4 files changed

+237
-71
lines changed

4 files changed

+237
-71
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ script:
55
- PYTHONPATH=. python tools/test/config_test/config_test.py
66
- python tools/test/pylint.py
77
- py.test tools/test/toolchains/api.py
8+
- python tools/test/memap/memap_test.py
89
- python tools/build_travis.py
910
before_install:
1011
- sudo add-apt-repository -y ppa:terry.guo/gcc-arm-embedded

tools/memap.py

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ def __init__(self):
5151
# list of all object files and mappting to module names
5252
self.object_to_module = dict()
5353

54-
# Memory usage summary structure
54+
# Memory report (sections + summary)
55+
self.mem_report = []
56+
57+
# Just the memory summary section
5558
self.mem_summary = dict()
5659

60+
self.subtotal = dict()
61+
5762
def module_add(self, module_name, size, section):
5863
""" Adds a module / section to the list
5964
@@ -399,68 +404,27 @@ def generate_output(self, export_format, file_output=None):
399404
print "I/O error({0}): {1}".format(error.errno, error.strerror)
400405
return False
401406

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-
439407
to_call = {'json': self.generate_json,
440408
'csv-ci': self.generate_csv,
441409
'table': self.generate_table}[export_format]
442-
to_call(subtotal, misc_flash_mem, file_desc)
410+
to_call(file_desc)
443411

444412
if file_desc is not sys.stdout:
445413
file_desc.close()
446414

447-
def generate_json(self, _, dummy, file_desc):
415+
def generate_json(self, file_desc):
448416
"""Generate a json file from a memory map
449417
450418
Positional arguments:
451-
subtotal - total sizes for each module
452-
misc_flash_mem - size of misc flash sections
453419
file_desc - the file to write out the final report to
454420
"""
455-
file_desc.write(json.dumps(self.mem_summary, indent=4))
421+
file_desc.write(json.dumps(self.mem_report, indent=4))
456422
file_desc.write('\n')
457423

458-
def generate_csv(self, subtotal, misc_flash_mem, file_desc):
424+
def generate_csv(self, file_desc):
459425
"""Generate a CSV file from a memoy map
460426
461427
Positional arguments:
462-
subtotal - total sizes for each module
463-
misc_flash_mem - size of misc flash sections
464428
file_desc - the file to write out the final report to
465429
"""
466430
csv_writer = csv.writer(file_desc, delimiter=',',
@@ -474,36 +438,33 @@ def generate_csv(self, subtotal, misc_flash_mem, file_desc):
474438
csv_sizes += [self.modules[i][k]]
475439

476440
csv_module_section += ['static_ram']
477-
csv_sizes += [subtotal['.data']+subtotal['.bss']]
441+
csv_sizes += [self.mem_summary['static_ram']]
478442

479443
csv_module_section += ['heap']
480-
if subtotal['.heap'] == 0:
444+
if self.mem_summary['heap'] == 0:
481445
csv_sizes += ['unknown']
482446
else:
483-
csv_sizes += [subtotal['.heap']]
447+
csv_sizes += [self.mem_summary['heap']]
484448

485449
csv_module_section += ['stack']
486-
if subtotal['.stack'] == 0:
450+
if self.mem_summary['stack'] == 0:
487451
csv_sizes += ['unknown']
488452
else:
489-
csv_sizes += [subtotal['.stack']]
453+
csv_sizes += [self.mem_summary['stack']]
490454

491455
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']]
494457

495458
csv_module_section += ['total_flash']
496-
csv_sizes += [subtotal['.text']+subtotal['.data']+misc_flash_mem]
459+
csv_sizes += [self.mem_summary['total_flash']]
497460

498461
csv_writer.writerow(csv_module_section)
499462
csv_writer.writerow(csv_sizes)
500463

501-
def generate_table(self, subtotal, misc_flash_mem, file_desc):
464+
def generate_table(self, file_desc):
502465
"""Generate a table from a memoy map
503466
504467
Positional arguments:
505-
subtotal - total sizes for each module
506-
misc_flash_mem - size of misc flash sections
507468
file_desc - the file to write out the final report to
508469
"""
509470
# Create table
@@ -521,47 +482,80 @@ def generate_table(self, subtotal, misc_flash_mem, file_desc):
521482
for i in sorted(self.modules):
522483
row = [i]
523484

524-
for k in self.sections:
525-
subtotal[k] += self.modules[i][k]
526-
527485
for k in self.print_sections:
528486
row.append(self.modules[i][k])
529487

530488
table.add_row(row)
531489

532490
subtotal_row = ['Subtotals']
533491
for k in self.print_sections:
534-
subtotal_row.append(subtotal[k])
492+
subtotal_row.append(self.subtotal[k])
535493

536494
table.add_row(subtotal_row)
537495

538496
file_desc.write(table.get_string())
539497
file_desc.write('\n')
540498

541-
if subtotal['.heap'] == 0:
499+
if self.mem_summary['heap'] == 0:
542500
file_desc.write("Allocated Heap: unknown\n")
543501
else:
544502
file_desc.write("Allocated Heap: %s bytes\n" %
545-
str(subtotal['.heap']))
503+
str(self.mem_summary['heap']))
546504

547-
if subtotal['.stack'] == 0:
505+
if self.mem_summary['stack'] == 0:
548506
file_desc.write("Allocated Stack: unknown\n")
549507
else:
550508
file_desc.write("Allocated Stack: %s bytes\n" %
551-
str(subtotal['.stack']))
509+
str(self.mem_summary['stack']))
552510

553511
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'])))
555513
file_desc.write(
556514
"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'])))
559516
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'])))
562518

563519
toolchains = ["ARM", "ARM_STD", "ARM_MICRO", "GCC_ARM", "IAR"]
564520

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+
565559
def parse(self, mapfile, toolchain):
566560
""" Parse and decode map file depending on the toolchain
567561
@@ -584,6 +578,9 @@ def parse(self, mapfile, toolchain):
584578
self.parse_map_file_iar(file_input)
585579
else:
586580
result = False
581+
582+
self.compute_report()
583+
587584
except IOError as error:
588585
print "I/O error({0}): {1}".format(error.errno, error.strerror)
589586
result = False

0 commit comments

Comments
 (0)