Skip to content

[Tools] Add HTML report output to memap script #2165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 115 additions & 4 deletions tools/memap.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ def __init__(self):
# Memory usage summary structure
self.mem_summary = dict()

# Colors used for sections in HTML report
self.html_section_color_map = {
# This is in module part
'.data' : 'LightSalmon',
'.bss' : 'MediumSlateBlue',
'.text' : 'LightGreen',

# This is included in 'summary' section
'total_flash' : 'Crimson',
'static_ram' : 'LightPink',
'stack' : 'DarkSeaGreen',
'heap' : 'DodgerBlue',
'total_ram' : 'GoldenRod',
}

def module_add(self, module_name, size, section):
"""
Adds a module / section to the list
Expand Down Expand Up @@ -340,7 +355,97 @@ def search_objects(self, path, toolchain):
else:
self.object_to_module.update({object_name:module_name})

export_formats = ["json", "csv-ci", "table"]
export_formats = ["json", "csv-ci", "table", "html"]

def memap_generate_html_module_report(self, module_data):
"""! Function generates memory map usage HTML with bar chart and summary on the right
@param module_data Dict with module memory map usage statistics

module_data = {
"module": "Misc",
"size": {
".data": 2228,
".bss": 2152,
".text": 57635
}
}
@return HTML formatted output with module's memory map stats
"""

result = str()
module_size = module_data.get('size', module_data.get('summary', dict()))
table_str = """
<table cellspacing=5 cellpadding=0>
<tr>
<td>
<table cellspacing=0 cellpadding=2>
<tr>
%s
</tr>
</table>
</td>
<td>
<table cellspacing=2 cellpadding=0>
%s
</table>
</td>
</tr>
</table>
"""

if module_size:
total_section_size = 0
for section in sorted(module_size):
# section: .data, .bss, .text
total_section_size += module_size[section]

td_str = str()
td_summary_str = str()
for section in sorted(module_size):
section_size = 100.0 * module_size[section] / total_section_size
td_size_px = 4 * int(section_size)
#print td_size_px, module_size[section], total_section_size
color = self.html_section_color_map.get(section, "Silver")
td_str += """
<td valign='top' bgcolor='%s' width='%dpx' height='55px' title='Section %s %d Bytes'>
</td>
"""% (color,
td_size_px,
section,
module_size[section])

td_summary_str += """
<tr>
<td width=150px>Section <font color='%s'><b>%s</b></font>:</td>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The <font> tag has been deprecated from the HTML specification for a long time: http://www.w3schools.com/tags/tag_font.asp

Can we switch to using CSS? The same thing is valid for the following HTML attributes: width, height, align, bgcolor

<td width=100px align='right'>%d Bytes</td>
<td width=100px align='right'>%.2f %%</td>
</tr>
"""% (color,
section,
module_size[section],
section_size)

result += table_str% (td_str, td_summary_str)

return result


def memap_generate_html_report(self, module_data):
"""! Function generates report from memory usage data
@param module_data Memory usage data, modules + summary
@return string with HTML report output (should be redirected to .html file)
"""
result = "<h2>mbed-os memory usage statistics report</h2>\n"

for data in module_data:
if 'module' in data:
module_name = data['module']
result += "<b>Module '%s' memory statistics:</b> </br>"% (module_name)
else:
result += "<hr>"
result += "<b>Summary of memory statistics:</b> </br>"
result += self.memap_generate_html_module_report(data)
return result

def generate_output(self, export_format, file_output=None):
"""
Expand Down Expand Up @@ -425,6 +530,12 @@ def generate_output(self, export_format, file_output=None):
file_desc.write(json.dumps(json_to_file, indent=4))
file_desc.write('\n')

elif export_format == 'html': # HTML human friendly format

json_to_file = json_obj + [summary]
html_output = self.memap_generate_html_report(json_to_file)
file_desc.write(html_output)

elif export_format == 'csv-ci': # CSV format for the CI system

csv_writer = csv.writer(file_desc, delimiter=',', quoting=csv.QUOTE_NONE)
Expand All @@ -436,16 +547,16 @@ def generate_output(self, export_format, file_output=None):
csv_module_section += [i+k]
csv_sizes += [self.modules[i][k]]

csv_module_section += ['total_static_ram']
csv_module_section += ['static_ram']
csv_sizes += [subtotal['.data']+subtotal['.bss']]

csv_module_section += ['allocated_heap']
csv_module_section += ['heap']
if subtotal['.heap'] == 0:
csv_sizes += ['unknown']
else:
csv_sizes += [subtotal['.heap']]

csv_module_section += ['allocated_stack']
csv_module_section += ['stack']
if subtotal['.stack'] == 0:
csv_sizes += ['unknown']
else:
Expand Down
6 changes: 5 additions & 1 deletion tools/toolchains/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class mbedToolchain:
"Cortex-M7" : ["__CORTEX_M7", "ARM_MATH_CM7", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
"Cortex-A9" : ["__CORTEX_A9", "ARM_MATH_CA9", "__FPU_PRESENT", "__CMSIS_RTOS", "__EVAL", "__MBED_CMSIS_RTOS_CA9"],
}

CORTEX_FPU_SYMBOLS = {
"single" : ["__FPU_PRESENT=1"],
"double" : ["__FPU_PRESENT=1"],
Expand Down Expand Up @@ -927,6 +927,10 @@ def mem_stats(self, map):
map_csv = splitext(map)[0] + "_map.csv"
memap.generate_output('csv-ci', map_csv)

# Write output to file in HTML format for humans
map_htm = splitext(map)[0] + "_map.html"
memap.generate_output('html', map_htm)

# Here we return memory statistics structure (constructed after
# call to generate_output) which contains raw data in bytes
# about sections + summary
Expand Down