Skip to content

Commit

Permalink
Improvements to scripts/tools/memory/diffsyms.py (#6236)
Browse files Browse the repository at this point in the history
#### Summary of Changes

- Fixed a bug causing symbols to be skipped.
- Sort by size change rather than (mangled) symbol name.
- Report absent symbols as size 0 rather than NaN.
- Remove trailing blanks from text report format.
- Enable selection options.
  • Loading branch information
kpschoedel authored and pull[bot] committed Apr 23, 2021
1 parent 5b9f9dc commit 9108867
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
39 changes: 24 additions & 15 deletions scripts/tools/memory/diffsyms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
CONFIG: ConfigDescription = {
**memdf.util.config.CONFIG,
**memdf.collect.CONFIG,
**memdf.select.CONFIG,
**memdf.report.REPORT_CONFIG,
**memdf.report.OUTPUT_CONFIG,
}
Expand All @@ -47,35 +48,43 @@ def main(argv):
a_dfs = memdf.collect.collect_files(config, files=[inputs[0]])
b_dfs = memdf.collect.collect_files(config, files=[inputs[1]])

a_syms = a_dfs[SymbolDF.name].sort_values(by='symbol')
b_syms = b_dfs[SymbolDF.name].sort_values(by='symbol')
a_syms = a_dfs[SymbolDF.name].sort_values(by='symbol',
ignore_index=True)
b_syms = b_dfs[SymbolDF.name].sort_values(by='symbol',
ignore_index=True)

# TBD: Differences other than size, configurably.
differences = []
ai = a_syms.itertuples()
bi = b_syms.itertuples()
while True:
if (a := next(ai, None)) is None:
break
if (b := next(bi, None)) is None:
differences.append((a.symbol, a.size, None))
break
a = next(ai, None)
b = next(bi, None)
while a and b:
if a.symbol < b.symbol:
differences.append((a.symbol, a.size, None))
differences.append((-a.size, a.size, 0, a.symbol))
a = next(ai, None)
continue
if a.symbol > b.symbol:
differences.append((b.symbol, None, b.size))
differences.append((b.size, 0, b.size, b.symbol))
b = next(bi, None)
continue
if a.size != b.size:
differences.append((a.symbol, a.size, b.size))
differences.append((b.size - a.size, a.size, b.size, a.symbol))
a = next(ai, None)
b = next(bi, None)
for a in ai:
differences.append((a.symbol, a.Index, None))
differences.append((-a.size, a.size, 0, a.symbol))
for b in bi:
differences.append((b.symbol, None, b.Index))

df = pd.DataFrame(differences, columns=['symbol', 'a', 'b'])
differences.append((b.size, 0, b.size, b.symbol))

df = pd.DataFrame(differences,
columns=['change', 'a-size', 'b-size', 'symbol'])
if config['report.demangle']:
# Demangle early to sort by demangled name.
df['symbol'] = df['symbol'].apply(memdf.report.demangle)
config['report.demangle'] = False
df.sort_values(by=['change', 'symbol'], ascending=[False, True],
inplace=True)
memdf.report.write_dfs(config, {'Differences': df})

except Exception as exception:
Expand Down
22 changes: 17 additions & 5 deletions scripts/tools/memory/memdf/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,30 @@ def write_table(config: Config, df: DF, output: IO) -> None:
df = df.copy()
if 'symbol' in df.columns and config['report.demangle']:
df['symbol'] = df['symbol'].apply(demangle)
last_column_is_left_justified = False
formatters = []
for column in df.columns:
if column.endswith('address'):
# Hex format address.
width = (int(df[column].max()).bit_length() + 3) // 4
df[column] = df[column].apply(lambda s: '{0:0{width}X}'.format(
s, width=width))
formatters.append(lambda x:
'{0:0{width}X}'.format(x, width=width))
elif pd.api.types.is_string_dtype(df.dtypes[column]):
# Left justify strings.
df[column] = df[column].astype(str)
# Left justify strings.
width = max(len(column), df[column].str.len().max())
df[column] = df[column].apply(lambda s: s.ljust(width))
print(df.to_string(index=False), file=output)
formatters.append(lambda x: x.ljust(width))
if column == df.columns[-1]:
last_column_is_left_justified = True
else:
formatters.append(str)
s = df.to_string(index=False, formatters=formatters, justify='left')
if last_column_is_left_justified:
# Strip trailing spaces.
for line in s.split('\n'):
print(line.rstrip())
else:
print(s, file=output)
else:
# No rows. `df.to_string()` doesn't look like a text table in this case.
print(' '.join(df.columns))
Expand Down

0 comments on commit 9108867

Please sign in to comment.