mercurial/statprof.py
changeset 43506 9f70512ae2cf
parent 43503 313e3a279828
child 45942 89a2afe31e82
equal deleted inserted replaced
43505:47fac1692ede 43506:9f70512ae2cf
   204 
   204 
   205 
   205 
   206 class CodeSite(object):
   206 class CodeSite(object):
   207     cache = {}
   207     cache = {}
   208 
   208 
   209     __slots__ = (r'path', r'lineno', r'function', r'source')
   209     __slots__ = ('path', 'lineno', 'function', 'source')
   210 
   210 
   211     def __init__(self, path, lineno, function):
   211     def __init__(self, path, lineno, function):
   212         assert isinstance(path, bytes)
   212         assert isinstance(path, bytes)
   213         self.path = path
   213         self.path = path
   214         self.lineno = lineno
   214         self.lineno = lineno
   256 
   256 
   257     def filename(self):
   257     def filename(self):
   258         return os.path.basename(self.path)
   258         return os.path.basename(self.path)
   259 
   259 
   260     def skipname(self):
   260     def skipname(self):
   261         return r'%s:%s' % (self.filename(), self.function)
   261         return '%s:%s' % (self.filename(), self.function)
   262 
   262 
   263 
   263 
   264 class Sample(object):
   264 class Sample(object):
   265     __slots__ = (r'stack', r'time')
   265     __slots__ = ('stack', 'time')
   266 
   266 
   267     def __init__(self, stack, time):
   267     def __init__(self, stack, time):
   268         self.stack = stack
   268         self.stack = stack
   269         self.time = time
   269         self.time = time
   270 
   270 
   736     root = HotNode(None)
   736     root = HotNode(None)
   737     lasttime = data.samples[0].time
   737     lasttime = data.samples[0].time
   738     for sample in data.samples:
   738     for sample in data.samples:
   739         root.add(sample.stack[::-1], sample.time - lasttime)
   739         root.add(sample.stack[::-1], sample.time - lasttime)
   740         lasttime = sample.time
   740         lasttime = sample.time
   741     showtime = kwargs.get(r'showtime', True)
   741     showtime = kwargs.get('showtime', True)
   742 
   742 
   743     def _write(node, depth, multiple_siblings):
   743     def _write(node, depth, multiple_siblings):
   744         site = node.site
   744         site = node.site
   745         visiblechildren = [
   745         visiblechildren = [
   746             c
   746             c
   892         if stack in stack2id:
   892         if stack in stack2id:
   893             return stack2id[stack]
   893             return stack2id[stack]
   894         parent = stackid(stack[1:])
   894         parent = stackid(stack[1:])
   895         myid = len(stack2id)
   895         myid = len(stack2id)
   896         stack2id[stack] = myid
   896         stack2id[stack] = myid
   897         id2stack.append(dict(category=stack[0][0], name=r'%s %s' % stack[0]))
   897         id2stack.append(dict(category=stack[0][0], name='%s %s' % stack[0]))
   898         if parent is not None:
   898         if parent is not None:
   899             id2stack[-1].update(parent=parent)
   899             id2stack[-1].update(parent=parent)
   900         return myid
   900         return myid
   901 
   901 
   902     # The sampling profiler can sample multiple times without
   902     # The sampling profiler can sample multiple times without
   929         if minthreshold <= duration <= maxthreshold:
   929         if minthreshold <= duration <= maxthreshold:
   930             # ensure no zero-duration events
   930             # ensure no zero-duration events
   931             sampletime = max(oldtime + clamp, sample.time)
   931             sampletime = max(oldtime + clamp, sample.time)
   932             samples.append(
   932             samples.append(
   933                 dict(
   933                 dict(
   934                     ph=r'E',
   934                     ph='E',
   935                     name=oldfunc,
   935                     name=oldfunc,
   936                     cat=oldcat,
   936                     cat=oldcat,
   937                     sf=oldsid,
   937                     sf=oldsid,
   938                     ts=sampletime * 1e6,
   938                     ts=sampletime * 1e6,
   939                     pid=0,
   939                     pid=0,
   947 
   947 
   948     for sample in data.samples:
   948     for sample in data.samples:
   949         stack = tuple(
   949         stack = tuple(
   950             (
   950             (
   951                 (
   951                 (
   952                     r'%s:%d'
   952                     '%s:%d'
   953                     % (simplifypath(pycompat.sysstr(frame.path)), frame.lineno),
   953                     % (simplifypath(pycompat.sysstr(frame.path)), frame.lineno),
   954                     pycompat.sysstr(frame.function),
   954                     pycompat.sysstr(frame.function),
   955                 )
   955                 )
   956                 for frame in sample.stack
   956                 for frame in sample.stack
   957             )
   957             )
   969             laststack.appendleft(f)
   969             laststack.appendleft(f)
   970             path, name = f
   970             path, name = f
   971             sid = stackid(tuple(laststack))
   971             sid = stackid(tuple(laststack))
   972             samples.append(
   972             samples.append(
   973                 dict(
   973                 dict(
   974                     ph=r'B',
   974                     ph='B',
   975                     name=name,
   975                     name=name,
   976                     cat=path,
   976                     cat=path,
   977                     ts=sample.time * 1e6,
   977                     ts=sample.time * 1e6,
   978                     sf=sid,
   978                     sf=sid,
   979                     pid=0,
   979                     pid=0,
  1028 
  1028 
  1029     displayargs = {}
  1029     displayargs = {}
  1030 
  1030 
  1031     optstart = 2
  1031     optstart = 2
  1032     displayargs[b'function'] = None
  1032     displayargs[b'function'] = None
  1033     if argv[1] == r'hotpath':
  1033     if argv[1] == 'hotpath':
  1034         displayargs[b'format'] = DisplayFormats.Hotpath
  1034         displayargs[b'format'] = DisplayFormats.Hotpath
  1035     elif argv[1] == r'lines':
  1035     elif argv[1] == 'lines':
  1036         displayargs[b'format'] = DisplayFormats.ByLine
  1036         displayargs[b'format'] = DisplayFormats.ByLine
  1037     elif argv[1] == r'functions':
  1037     elif argv[1] == 'functions':
  1038         displayargs[b'format'] = DisplayFormats.ByMethod
  1038         displayargs[b'format'] = DisplayFormats.ByMethod
  1039     elif argv[1] == r'function':
  1039     elif argv[1] == 'function':
  1040         displayargs[b'format'] = DisplayFormats.AboutMethod
  1040         displayargs[b'format'] = DisplayFormats.AboutMethod
  1041         displayargs[b'function'] = argv[2]
  1041         displayargs[b'function'] = argv[2]
  1042         optstart = 3
  1042         optstart = 3
  1043     elif argv[1] == r'flame':
  1043     elif argv[1] == 'flame':
  1044         displayargs[b'format'] = DisplayFormats.FlameGraph
  1044         displayargs[b'format'] = DisplayFormats.FlameGraph
  1045     else:
  1045     else:
  1046         printusage()
  1046         printusage()
  1047         return 0
  1047         return 0
  1048 
  1048 
  1074             return 0
  1074             return 0
  1075         else:
  1075         else:
  1076             assert False, b"unhandled option %s" % o
  1076             assert False, b"unhandled option %s" % o
  1077 
  1077 
  1078     if not path:
  1078     if not path:
  1079         print(r'must specify --file to load')
  1079         print('must specify --file to load')
  1080         return 1
  1080         return 1
  1081 
  1081 
  1082     load_data(path=path)
  1082     load_data(path=path)
  1083 
  1083 
  1084     display(**pycompat.strkwargs(displayargs))
  1084     display(**pycompat.strkwargs(displayargs))