Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | #!/usr/bin/env python3
"""
This script help you to compare footprint results with previous commits in git.
If you don't have a git repository, it will compare your current tree
against the last release results.
To run it you need to set up the same environment as sanity check.
The scripts take 2 optional args COMMIT and BASE_COMMIT, which tell the scripts
which commit to use as current commit and as base for comparing, respectively.
The script can take any SHA commit recognized for git.
COMMIT is the commit to compare against BASE_COMMIT.
Default
current working directory if we have changes in git tree or we don't have git.
HEAD in any other case.
BASE_COMMIT is the commit used as base to compare results.
Default:
sanity_last_release.csv if we don't have git tree.
HEAD is we have changes in the working tree.
HEAD~1 if we don't have changes and we have default COMMIT.
COMMIT~1 if we have a valid COMMIT.
"""
import argparse
import os
import sys
import csv
import subprocess
import logging
import tempfile
import shutil
if "ZEPHYR_BASE" not in os.environ:
logging.error("$ZEPHYR_BASE environment variable undefined.\n")
exit(1)
logger = None
GIT_ENABLED = False
RELEASE_DATA = 'sanity_last_release.csv'
def is_git_enabled():
global GIT_ENABLED
proc = subprocess.Popen('git rev-parse --is-inside-work-tree',
stdout=subprocess.PIPE,
cwd=os.environ.get('ZEPHYR_BASE'), shell=True)
if proc.wait() != 0:
GIT_ENABLED = False
GIT_ENABLED = True
def init_logs():
global logger
log_lev = os.environ.get('LOG_LEVEL', None)
level = logging.INFO
if log_lev == "DEBUG":
level = logging.DEBUG
elif log_lev == "ERROR":
level = logging.ERROR
console = logging.StreamHandler()
format = logging.Formatter('%(levelname)-8s: %(message)s')
console.setFormatter(format)
logger = logging.getLogger('')
logger.addHandler(console)
logger.setLevel(level)
logging.debug("Log init completed")
def parse_args():
parser = argparse.ArgumentParser(
description="Compare footprint apps RAM and ROM sizes. Note: "
"To run it you need to set up the same environment as sanitycheck.")
parser.add_argument('-b', '--base-commit', default=None,
help="Commit ID to use as base for footprint "
"compare. Default is parent current commit."
" or sanity_last_release.csv if we don't have git.")
parser.add_argument('-c', '--commit', default=None,
help="Commit ID to use compare footprint against base. "
"Default is HEAD or working tree.")
return parser.parse_args()
def get_git_commit(commit):
commit_id = None
proc = subprocess.Popen('git rev-parse %s' % commit, stdout=subprocess.PIPE,
cwd=os.environ.get('ZEPHYR_BASE'), shell=True)
if proc.wait() == 0:
commit_id = proc.stdout.read().decode("utf-8").strip()
return commit_id
def sanity_results_filename(commit=None, cwd=os.environ.get('ZEPHYR_BASE')):
if not commit:
file_name = "tmp.csv"
else:
if commit == RELEASE_DATA:
file_name = RELEASE_DATA
else:
file_name = "%s.csv" % commit
return os.path.join(cwd,'scripts', 'sanity_chk', file_name)
def git_checkout(commit, cwd=os.environ.get('ZEPHYR_BASE')):
proc = subprocess.Popen('git diff --quiet', stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=cwd, shell=True)
if proc.wait() != 0:
raise Exception("Cannot continue, you have unstaged changes in your working tree")
proc = subprocess.Popen('git reset %s --hard' % commit,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=cwd, shell=True)
if proc.wait() == 0:
return True
else:
logger.error(proc.stdout.read())
return False
def run_sanity_footprint(commit=None, cwd=os.environ.get('ZEPHYR_BASE'),
output_file=None):
if not output_file:
output_file = sanity_results_filename(commit)
cmd = '/bin/bash -c "source ./zephyr-env.sh && sanitycheck'
cmd += ' +scripts/sanity_chk/sanity_compare.args -o %s"' % output_file
logger.debug('Sanity (%s) %s' %(commit, cmd))
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
cwd=cwd, shell=True)
output,error=proc.communicate()
if proc.wait() == 0:
logger.debug(output)
return True
logger.error("Couldn't build footprint apps in commit %s" % commit)
logger.error(output)
raise Exception("Couldn't build footprint apps in commit %s" % commit)
def run_footprint_build(commit=None):
logging.debug("footprint build for %s" % commit)
if not commit:
run_sanity_footprint()
else:
cmd = "git clone --no-hardlinks %s" % os.environ.get('ZEPHYR_BASE')
tmp_location = os.path.join(tempfile.gettempdir(),
os.path.basename(os.environ.get('ZEPHYR_BASE')))
if os.path.exists(tmp_location):
shutil.rmtree(tmp_location)
logging.debug("clonning into %s" % tmp_location)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=tempfile.gettempdir(), shell=True)
if proc.wait() == 0:
if git_checkout(commit, tmp_location):
run_sanity_footprint(commit, tmp_location)
else:
logger.error(proc.stdout.read())
shutil.rmtree(tmp_location, ignore_errors=True)
return True
def read_sanity_report(filename):
data = []
with open(filename) as fp:
tmp = csv.DictReader(fp)
for row in tmp:
data.append(row)
return data
def get_footprint_results(commit=None):
results = {}
sanity_file = sanity_results_filename(commit)
if (not os.path.exists(sanity_file) or not commit) and commit != RELEASE_DATA:
run_footprint_build(commit)
return read_sanity_report(sanity_file)
def tree_changes():
proc = subprocess.Popen('git diff --quiet', stdout=subprocess.PIPE,
cwd=os.environ.get('ZEPHYR_BASE'), shell=True)
if proc.wait() != 0:
return True
return False
def get_default_current_commit():
if tree_changes():
return None
else:
return get_git_commit('HEAD')
def get_default_base_commit(current_commit):
if not current_commit:
if tree_changes():
return get_git_commit('HEAD')
else:
return get_git_commit('HEAD~1')
else:
return get_git_commit('%s~1'%current_commit)
def build_history(b_commit=None, c_commit=None):
if not GIT_ENABLED:
logger.info('Working on current tree, not git enabled.')
current_commit = None
base_commit = RELEASE_DATA
else:
if not c_commit:
current_commit = get_default_current_commit()
else:
current_commit = get_git_commit(c_commit)
if not b_commit:
base_commit = get_default_base_commit(current_commit)
else:
base_commit = get_git_commit(b_commit)
if not base_commit:
logger.error("Cannot resolve base commit")
return
logger.info("Base: %s" % base_commit)
logger.info("Current: %s" % (current_commit if current_commit else
'working space'))
current_results = get_footprint_results(current_commit)
base_results = get_footprint_results(base_commit)
deltas = compare_results(base_results, current_results)
print_deltas(deltas)
def compare_results(base_results, current_results):
interesting_metrics = [("ram_size", int),
("rom_size", int)]
results = {}
metrics = {}
for type, data in {'base': base_results, 'current': current_results}.items():
metrics[type] = {}
for row in data:
d = {}
for m, mtype in interesting_metrics:
if row[m]:
d[m] = mtype(row[m])
if not row["test"] in metrics[type]:
metrics[type][row["test"]] = {}
metrics[type][row["test"]][row["platform"]] = d
for test, platforms in metrics['current'].items():
if not test in metrics['base']:
continue
tests = {}
for platform, test_data in platforms.items():
if not platform in metrics['base'][test]:
continue
golden_metric = metrics['base'][test][platform]
tmp = {}
for metric, _ in interesting_metrics:
if metric not in golden_metric or metric not in test_data:
continue
if test_data[metric] == "":
continue
delta = test_data[metric] - golden_metric[metric]
if delta == 0:
continue
tmp[metric] = {
'delta': delta,
'current': test_data[metric],
}
if len(tmp) != 0:
tests[platform] = tmp
if len(tests) != 0:
results[test] = tests
return results
def print_deltas(deltas):
error_count = 0
for test in sorted(deltas):
print("\n{:<25}".format(test))
for platform, data in deltas[test].items():
print(" {:<25}".format(platform))
for metric, value in data.items():
percentage = (float(value['delta']) / float(value['current'] -
value['delta']))
print(" {} ({:+.2%}) {:+6} current size {:>7} bytes".format(
"RAM" if metric == "ram_size" else "ROM", percentage,
value['delta'], value['current']))
error_count = error_count + 1
if error_count == 0:
print("There are no changes in RAM neither in ROM of footprint apps.")
return error_count
def main():
args = parse_args()
build_history(args.base_commit, args.commit)
if __name__ == "__main__":
init_logs()
is_git_enabled()
main()
|