Skip to content

Commit e8b0687

Browse files
committed
Making memap output consistent across output formats
This commit fixes an issue where the output from memap.py was not consistent across all output formats. This issue stemmed from the fact that a few important calculations were being performed at output generation time. This has been moved to the 'parse' function and saved for future use by the 'generate' functions. Because this commit saves more data to the MemapParser instance, there were some name collisions. The public member 'mem_summary' has been renamed to 'mem_report'. 'mem_report' contains the data structure used by the json generator. This includes both the section data and the memory summary. The 'mem_summary' member now just contains the summary. The summary includes total allocated heap, total static RAM, etc.
1 parent b67e1f7 commit e8b0687

File tree

2 files changed

+65
-71
lines changed

2 files changed

+65
-71
lines changed

tools/memap.py

Lines changed: 64 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,44 +482,39 @@ 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

@@ -584,6 +540,44 @@ def parse(self, mapfile, toolchain):
584540
self.parse_map_file_iar(file_input)
585541
else:
586542
result = False
543+
544+
for k in self.sections:
545+
self.subtotal[k] = 0
546+
547+
for i in sorted(self.modules):
548+
for k in self.sections:
549+
self.subtotal[k] += self.modules[i][k]
550+
551+
# Calculate misc flash sections
552+
self.misc_flash_mem = 0
553+
for i in self.modules:
554+
for k in self.misc_flash_sections:
555+
if self.modules[i][k]:
556+
self.misc_flash_mem += self.modules[i][k]
557+
558+
self.mem_summary = {
559+
'static_ram': (self.subtotal['.data'] + self.subtotal['.bss']),
560+
'heap': (self.subtotal['.heap']),
561+
'stack': (self.subtotal['.stack']),
562+
'total_ram': (self.subtotal['.data'] + self.subtotal['.bss'] +
563+
self.subtotal['.heap']+self.subtotal['.stack']),
564+
'total_flash': (self.subtotal['.text'] + self.subtotal['.data'] +
565+
self.misc_flash_mem),
566+
}
567+
568+
self.mem_report = []
569+
for i in sorted(self.modules):
570+
self.mem_report.append({
571+
"module":i,
572+
"size":{
573+
k:self.modules[i][k] for k in self.print_sections
574+
}
575+
})
576+
577+
self.mem_report.append({
578+
'summary': self.mem_summary
579+
})
580+
587581
except IOError as error:
588582
print "I/O error({0}): {1}".format(error.errno, error.strerror)
589583
result = False

tools/toolchains/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ def mem_stats(self, map):
10471047
# Here we return memory statistics structure (constructed after
10481048
# call to generate_output) which contains raw data in bytes
10491049
# about sections + summary
1050-
return memap.mem_summary
1050+
return memap.mem_report
10511051

10521052
# Set the configuration data
10531053
def set_config_data(self, config_data):

0 commit comments

Comments
 (0)