def libcore_profiler_profiler_reuse_test(): name = 'dos.library' fd = read_lib_fd(name) prof = LibProfiler() prof.parse_config(ConfigDict({ "names": [name], "calls": True })) prof.setup() p = prof.create_profile(name, fd) assert p f = p.get_func_by_name("Input") assert f idx = f.get_func_id() assert p.get_func_by_index(idx) == f f.count(1.0) # store/restore data = prof.get_data() prof2 = LibProfiler() assert prof2.set_data(data) prof2.setup() # reuse profile p2 = prof2.create_profile(name, fd) assert p == p2 f2 = p2.get_func_by_name("Input") assert f2.get_num_calls() == 1 f2.count(1.0) assert f2.get_num_calls() == 2 idx = f2.get_func_id() assert p2.get_func_by_index(idx) == f2
def libcore_profiler_profiler_set_get_data_test(): name = 'dos.library' fd = read_lib_fd(name) prof = LibProfiler(names=[name]) prof.setup() p = prof.create_profile(name, fd) data = prof.get_data() prof2 = LibProfiler() assert prof2.set_data(data) p2 = prof2.get_profile(name) assert p == p2
def libcore_profiler_profiler_config_test(): name = 'dos.library' fd = read_lib_fd(name) prof = LibProfiler() prof.parse_config(ConfigDict({"names": [name], "calls": True})) prof.setup() p = prof.create_profile(name, fd) data = prof.get_data() prof2 = LibProfiler() assert prof2.set_data(data) p2 = prof2.get_profile(name) assert p == p2
def libcore_profiler_profiler_config_test(): name = 'dos.library' fd = read_lib_fd(name) prof = LibProfiler() prof.parse_config(ConfigDict({ "names": [name], "calls": True })) prof.setup() p = prof.create_profile(name, fd) data = prof.get_data() prof2 = LibProfiler() assert prof2.set_data(data) p2 = prof2.get_profile(name) assert p == p2
class LibProfilerTool(Tool): def __init__(self): Tool.__init__(self, "libprof", "library profile utilities") self.profiler = None def add_args(self, arg_parser): sub = arg_parser.add_subparsers(dest="libprof_cmd") # dump parser = sub.add_parser("dump", help="display library function profile info") parser.add_argument("input", help="profile json file") # missing parser = sub.add_parser("missing", help="display missing library functions") parser.add_argument("input", help="profile json file") # coverage parser = sub.add_parser("coverage", help="display library function coverage info") parser.add_argument("input", help="profile json file") parser.add_argument( "-f", "--functions", action="store_true", default=False, help="show uncovered functions", ) def setup(self, args): return True def shutdown(self): pass def run(self, args): if not self._load_profile(args): return 1 cmd = args.libprof_cmd if cmd == "dump": return self._do_dump() elif cmd == "missing": return self._do_missing() elif cmd == "coverage": return self._do_coverage(args) else: return 1 def _load_profile(self, args): # read file try: with open(args.input) as fh: data = json.load(fh) if "libs" in data: data = ConfigDict(data["libs"]) self.profiler = LibProfiler() self.profiler.set_data(data) return True else: print("no 'libs' found in '%s'" % args.input) return False except IOError as e: print("loading '%s' failed: %s" % (args.input, e)) return False def _do_dump(self): self.profiler.dump() return 0 def _do_missing(self): p = self.profiler for lib_name, lib_prof in p.get_all_libs(): print(lib_name) for func_name, func_prof in lib_prof.get_all_funcs(): if func_prof.tag != LibImplScan.TAG_VALID: print(" ", func_name) def _do_coverage(self, args): print("%-40s total valid called" % "library") p = self.profiler for lib_name, lib_prof in p.get_all_libs(): self._print_lib_cov(lib_name, lib_prof) if args.functions: self._print_funcs(lib_prof) return 0 def _print_lib_cov(self, lib_name, lib_prof): num_valid = 0 num_covered = 0 num_total = 0 for _, func_prof in lib_prof.get_all_funcs(): if func_prof.tag == LibImplScan.TAG_VALID: num_valid += 1 if func_prof.num > 0: num_covered += 1 num_total += 1 impl_ratio = 100.0 * num_valid / num_total if num_valid > 0: coverage = 100.0 * num_covered / num_valid else: coverage = 0.0 print("%-40s %4d %6.2f " "%4d %6.2f %4d" % (lib_name, num_total, impl_ratio, num_valid, coverage, num_covered)) def _print_funcs(self, lib_prof): for name, func_prof in lib_prof.get_all_funcs(): if func_prof.tag == LibImplScan.TAG_VALID: if func_prof.num == 0: print(" ", name)
class LibProfilerTool(Tool): def __init__(self): Tool.__init__(self, "libprof", "library profile utilities") self.profiler = None def add_args(self, arg_parser): sub = arg_parser.add_subparsers(dest='libprof_cmd') # dump parser = sub.add_parser('dump', help='display library function profile info') parser.add_argument('input', help='profile json file') # missing parser = sub.add_parser('missing', help='display missing library functions') parser.add_argument('input', help='profile json file') # coverage parser = sub.add_parser('coverage', help='display library function coverage info') parser.add_argument('input', help='profile json file') parser.add_argument('-f', '--functions', action='store_true', default=False, help='show uncovered functions') def setup(self, args): return True def shutdown(self): pass def run(self, args): if not self._load_profile(args): return 1 cmd = args.libprof_cmd if cmd == 'dump': return self._do_dump() elif cmd == 'missing': return self._do_missing() elif cmd == 'coverage': return self._do_coverage(args) else: return 1 def _load_profile(self, args): # read file try: with open(args.input) as fh: data = json.load(fh) if 'libs' in data: data = ConfigDict(data['libs']) self.profiler = LibProfiler() self.profiler.set_data(data) return True else: print("no 'libs' found in '%s'" % args.input) return False except IOError as e: print("loading '%s' failed: %s" % (args.input, e)) return False def _do_dump(self): self.profiler.dump() return 0 def _do_missing(self): p = self.profiler for lib_name, lib_prof in p.get_all_libs(): print(lib_name) for func_name, func_prof in lib_prof.get_all_funcs(): if func_prof.tag != LibImplScan.TAG_VALID: print(" ", func_name) def _do_coverage(self, args): print("%-40s total valid called" % 'library') p = self.profiler for lib_name, lib_prof in p.get_all_libs(): self._print_lib_cov(lib_name, lib_prof) if args.functions: self._print_funcs(lib_prof) return 0 def _print_lib_cov(self, lib_name, lib_prof): num_valid = 0 num_covered = 0 num_total = 0 for _, func_prof in lib_prof.get_all_funcs(): if func_prof.tag == LibImplScan.TAG_VALID: num_valid += 1 if func_prof.num > 0: num_covered += 1 num_total += 1 impl_ratio = 100.0 * num_valid / num_total if num_valid > 0: coverage = 100.0 * num_covered / num_valid else: coverage = 0.0 print("%-40s %4d %6.2f " "%4d %6.2f %4d" % (lib_name, num_total, impl_ratio, num_valid, coverage, num_covered)) def _print_funcs(self, lib_prof): for name, func_prof in lib_prof.get_all_funcs(): if func_prof.tag == LibImplScan.TAG_VALID: if func_prof.num == 0: print(" ", name)