mercurial/statprof.py
changeset 38259 af402c6b90db
parent 38164 aac4be30e250
child 38260 15a1e37f80bd
equal deleted inserted replaced
38258:ae6e02fcee24 38259:af402c6b90db
   137 ###########################################################################
   137 ###########################################################################
   138 ## Utils
   138 ## Utils
   139 
   139 
   140 def clock():
   140 def clock():
   141     times = os.times()
   141     times = os.times()
   142     return times[0] + times[1]
   142     return (times[0] + times[1], times[4])
   143 
   143 
   144 
   144 
   145 ###########################################################################
   145 ###########################################################################
   146 ## Collection data structures
   146 ## Collection data structures
   147 
   147 
   149     def __init__(self, frequency=None):
   149     def __init__(self, frequency=None):
   150         self.reset(frequency)
   150         self.reset(frequency)
   151 
   151 
   152     def reset(self, frequency=None):
   152     def reset(self, frequency=None):
   153         # total so far
   153         # total so far
   154         self.accumulated_time = 0.0
   154         self.accumulated_time = (0.0, 0.0)
   155         # start_time when timer is active
   155         # start_time when timer is active
   156         self.last_start_time = None
   156         self.last_start_time = None
   157         # a float
   157         # a float
   158         if frequency:
   158         if frequency:
   159             self.sample_interval = 1.0 / frequency
   159             self.sample_interval = 1.0 / frequency
   168         self.profile_level = 0
   168         self.profile_level = 0
   169 
   169 
   170         self.samples = []
   170         self.samples = []
   171 
   171 
   172     def accumulate_time(self, stop_time):
   172     def accumulate_time(self, stop_time):
   173         self.accumulated_time += stop_time - self.last_start_time
   173         increment = (
       
   174             stop_time[0] - self.last_start_time[0],
       
   175             stop_time[1] - self.last_start_time[1],
       
   176         )
       
   177         self.accumulated_time = (
       
   178             self.accumulated_time[0] + increment[0],
       
   179             self.accumulated_time[1] + increment[1],
       
   180         )
   174 
   181 
   175     def seconds_per_sample(self):
   182     def seconds_per_sample(self):
   176         return self.accumulated_time / len(self.samples)
   183         return self.accumulated_time[0] / len(self.samples)
   177 
   184 
   178 state = ProfileState()
   185 state = ProfileState()
   179 
   186 
   180 
   187 
   181 class CodeSite(object):
   188 class CodeSite(object):
   259 def profile_signal_handler(signum, frame):
   266 def profile_signal_handler(signum, frame):
   260     if state.profile_level > 0:
   267     if state.profile_level > 0:
   261         now = clock()
   268         now = clock()
   262         state.accumulate_time(now)
   269         state.accumulate_time(now)
   263 
   270 
   264         state.samples.append(Sample.from_frame(frame, state.accumulated_time))
   271         state.samples.append(Sample.from_frame(frame, state.accumulated_time[0]))
   265 
   272 
   266         signal.setitimer(signal.ITIMER_PROF,
   273         signal.setitimer(signal.ITIMER_PROF,
   267             state.sample_interval, 0.0)
   274             state.sample_interval, 0.0)
   268         state.last_start_time = now
   275         state.last_start_time = now
   269 
   276 
   272     while not stopthread.is_set():
   279     while not stopthread.is_set():
   273         now = clock()
   280         now = clock()
   274         state.accumulate_time(now)
   281         state.accumulate_time(now)
   275 
   282 
   276         frame = sys._current_frames()[tid]
   283         frame = sys._current_frames()[tid]
   277         state.samples.append(Sample.from_frame(frame, state.accumulated_time))
   284         state.samples.append(Sample.from_frame(frame, state.accumulated_time[0]))
   278 
   285 
   279         state.last_start_time = now
   286         state.last_start_time = now
   280         time.sleep(state.sample_interval)
   287         time.sleep(state.sample_interval)
   281 
   288 
   282     stopthread.clear()
   289     stopthread.clear()
   463         raise Exception("Invalid display format")
   470         raise Exception("Invalid display format")
   464 
   471 
   465     if format not in (DisplayFormats.Json, DisplayFormats.Chrome):
   472     if format not in (DisplayFormats.Json, DisplayFormats.Chrome):
   466         print('---', file=fp)
   473         print('---', file=fp)
   467         print('Sample count: %d' % len(data.samples), file=fp)
   474         print('Sample count: %d' % len(data.samples), file=fp)
   468         print('Total time: %f seconds' % data.accumulated_time, file=fp)
   475         print('Total time: %f seconds (%f wall)' % data.accumulated_time,
       
   476               file=fp)
   469 
   477 
   470 def display_by_line(data, fp):
   478 def display_by_line(data, fp):
   471     '''Print the profiler data with each sample line represented
   479     '''Print the profiler data with each sample line represented
   472     as one row in a table.  Sorted by self-time per line.'''
   480     as one row in a table.  Sorted by self-time per line.'''
   473     stats = SiteStats.buildstats(data.samples)
   481     stats = SiteStats.buildstats(data.samples)