/usr/share/pyshared/timechart/model.py is in pytimechart 1.0.0~rc1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | # timechart project
# the timechart model with all loading facilities
from numpy import amin, amax, arange, searchsorted, sin, pi, linspace
import numpy as np
import traceback
import re
from traits.api import HasTraits, Instance, Str, Float,Delegate,\
DelegatesTo, Int, Long, Enum, Color, List, Bool, CArray, Property, cached_property, String, Button, Dict
from traitsui.api import Group, HGroup, Item, View, spring, Handler,VGroup,TableEditor
from enable.colors import ColorTrait
from pyface.image_resource import ImageResource
from pyface.api import ProgressDialog
from process_table import process_table_editor
import colors
import numpy
import sys
def _pretty_time(time):
if time > 1000000:
time = time/1000000.
return "%.1f s"%(time)
if time > 1000:
time = time/1000.
return "%.1f ms"%(time)
return "%.1f us"%(time)
class tcGeneric(HasTraits):
name = String
start_ts = CArray
end_ts = CArray
types = CArray
has_comments = Bool(True)
total_time = Property(Int)
max_types = Property(Int)
max_latency = Property(Int)
max_latency_ts = Property(CArray)
overview_ts_cache = Dict({})
@cached_property
def _get_total_time(self):
return sum(self.end_ts-self.start_ts)
@cached_property
def _get_max_types(self):
return amax(self.types)
@cached_property
def _get_max_latency(self):
return -1
def get_partial_tables(self,start,end):
low_i = searchsorted(self.end_ts,start)
high_i = searchsorted(self.start_ts,end)
ends = self.end_ts[low_i:high_i].copy()
starts = self.start_ts[low_i:high_i].copy()
if len(starts)==0:
return np.array([]),np.array([]),[]
# take care of activities crossing the selection
if starts[0]<start:
starts[0] = start
if ends[-1]>end:
ends[-1] = end
types = self.types[low_i:high_i]
return starts,ends,types
def get_overview_ts(self, threshold):
"""merge events so that there never are two events in the same "threshold" microsecond
"""
if threshold in self.overview_ts_cache:
return self.overview_ts_cache[threshold]
# we recursively use the lower threshold caches
# this allows to pre-compute the whole cache more efficiently
if threshold > 4:
origin_start_ts, origin_end_ts = self.get_overview_ts(threshold/2)
else:
origin_start_ts, origin_end_ts = self.start_ts, self.end_ts
# only calculate overview if it worth.
if len(origin_start_ts) < 500:
overview = (origin_start_ts, origin_end_ts)
self.overview_ts_cache[threshold] = overview
return overview
# assume at least one event
start_ts = []
end_ts = []
# start is the first start of the merge list
start = origin_start_ts[0]
i = 1
while i < len(origin_start_ts):
if origin_start_ts[i] > origin_start_ts[i-1] + threshold:
start_ts.append(start)
end_ts.append(origin_end_ts[i-1])
start = origin_start_ts[i]
i += 1
start_ts.append(start)
end_ts.append(origin_end_ts[i-1])
overview = (numpy.array(start_ts), numpy.array(end_ts))
self.overview_ts_cache[threshold] = overview
return overview
# UI traits
default_bg_color = Property(ColorTrait)
bg_color = Property(ColorTrait)
@cached_property
def _get_bg_color(self):
return colors.get_traits_color_by_name("idle_bg")
class tcIdleState(tcGeneric):
def get_comment(self,i):
return colors.get_colorname_by_id(self.types[i])
class tcFrequencyState(tcGeneric):
def get_comment(self,i):
return "%d"%(self.types[i])
class tcProcess(tcGeneric):
name = Property(String) # overide TimeChart
# start_ts=CArray # inherited from TimeChart
# end_ts=CArray # inherited from TimeChart
# values = CArray # inherited from TimeChart
pid = Long
ppid = Long
selection_time = Long(0)
selection_pc = Float(0)
comm = String
cpus = CArray
comments = []
has_comments = Bool(True)
show = Bool(True)
process_type = String
project = None
@cached_property
def _get_name(self):
return "%s:%d (%s)"%(self.comm,self.pid, _pretty_time(self.total_time))
def get_comment(self,i):
if len(self.comments)>i:
return "%s"%(self.comments[int(i)])
elif len(self.cpus)>i:
return "%d"%(self.cpus[i])
else:
return ""
@cached_property
def _get_max_latency(self):
if self.pid==0 and self.comm.startswith("irq"):
return 1000
@cached_property
def _get_max_latency_ts(self):
if self.max_latency > 0:
indices = np.nonzero((self.end_ts - self.start_ts) > self.max_latency)[0]
return np.array(sorted(map(lambda i:self.start_ts[i], indices)))
return []
@cached_property
def _get_default_bg_color(self):
if self.max_latency >0 and max(self.end_ts - self.start_ts)>self.max_latency:
return (1,.1,.1,1)
return colors.get_traits_color_by_name(self.process_type+"_bg")
def _get_bg_color(self):
if self.project != None and self in self.project.selected:
return colors.get_traits_color_by_name("selected_bg")
return self.default_bg_color
class tcProject(HasTraits):
c_states = List(tcGeneric)
p_states = List(tcGeneric)
processes = List(tcProcess)
selected = List(tcProcess)
filtered_processes = List(tcProcess)
remove_filter = Button(image=ImageResource("clear.png"),width_padding=0,height_padding=0,style='toolbar')
minimum_time_filter = Enum((0,1000,10000,50000,100000,500000,1000000,5000000,1000000,5000000,10000000,50000000))
minimum_events_filter = Enum((0,2,4,8,10,20,40,100,1000,10000,100000,1000000))
plot_redraw = Long()
filter = Str("")
filter_invalid = Property(depends_on="filter")
filename = Str("")
power_event = CArray
num_cpu = Property(Int,depends_on='c_states')
num_process = Property(Int,depends_on='process')
traits_view = View(
VGroup(
HGroup(
Item('filter',invalid="filter_invalid",width=1,
tooltip='filter the process list using a regular expression,\nallowing you to quickly find a process'),
Item('remove_filter', show_label=False, style='custom',
tooltip='clear the filter')
),
HGroup(
Item('minimum_time_filter',width=1,label='dur',
tooltip='filter the process list with minimum duration process is scheduled'),
Item('minimum_events_filter',width=1,label='num',
tooltip='filter the process list with minimum number of events process is generating'),
)
),
Item( 'filtered_processes',
show_label = False,
height=40,
editor = process_table_editor
)
)
first_ts = 0
def _get_filter_invalid(self):
try:
r = re.compile(self.filter)
except:
return True
return False
def _remove_filter_changed(self):
self.filter=""
def _filter_changed(self):
try:
r = re.compile(self.filter)
except:
r = None
filtered_processes =self.processes
if self.minimum_events_filter:
filtered_processes = filter(lambda p:self.minimum_events_filter < len(p.start_ts), filtered_processes)
if self.minimum_time_filter:
filtered_processes = filter(lambda p:self.minimum_time_filter < p.total_time, filtered_processes)
if r:
filtered_processes = filter(lambda p:r.search(p.comm), filtered_processes)
self.filtered_processes = filtered_processes
_minimum_time_filter_changed = _filter_changed
_minimum_events_filter_changed = _filter_changed
def _processes_changed(self):
self._filter_changed()
def _on_show(self):
for i in self.selected:
i.show = True
self.plot_redraw +=1
def _on_hide(self):
for i in self.selected:
i.show = False
self.plot_redraw +=1
def _on_select_all(self):
if self.selected == self.filtered_processes:
self.selected = []
else:
self.selected = self.filtered_processes
self.plot_redraw +=1
def _on_invert(self):
for i in self.filtered_processes:
i.show = not i.show
self.plot_redraw +=1
@cached_property
def _get_num_cpu(self):
return len(self.c_states)
def _get_num_process(self):
return len(self.processes)
def process_list_selected(self, selection):
print selection
######### stats part ##########
def process_stats(self,start,end):
fact = 100./(end-start)
for tc in self.processes:
starts,ends,types = tc.get_partial_tables(start,end)
inds = np.where(types==colors.get_color_id("running"))
tot = sum(ends[inds]-starts[inds])
tc.selection_time = int(tot)
tc.selection_pc = tot*fact
def get_selection_text(self,start,end):
low_line = -1
high_line = -1
low_i = searchsorted(self.timestamps,start)
high_i = searchsorted(self.timestamps,end)
low_line = self.linenumbers[low_i]
high_line = self.linenumbers[high_i]
return self.get_partial_text(self.filename, low_line, high_line)
######### generic parsing part ##########
def generic_find_process(self,pid,comm,ptype,same_pid_match_timestamp=0):
if self.tmp_process.has_key((pid,comm)):
return self.tmp_process[(pid,comm)]
# else try to find if there has been a process with same pid recently, and different name
if same_pid_match_timestamp != 0 and comm != "swapper":
for k, p in self.tmp_process.items():
if k[0] == pid:
if len(p['start_ts'])>0 and p['start_ts'][-1] > same_pid_match_timestamp:
p['comm'] = comm
self.tmp_process[(pid,comm)] = p
del self.tmp_process[k]
return p
tmp = {'type':ptype,'comm':comm,'pid':pid,'start_ts':[],'end_ts':[],'types':[],'cpus':[],'comments':[]}
if not (pid==0 and comm =="swapper"):
self.tmp_process[(pid,comm)] = tmp
return tmp
def generic_process_start(self,process,event, build_p_stack=True):
if process['comm']=='swapper' and process['pid']==0:
return # ignore swapper event
if len(process['start_ts'])>len(process['end_ts']):
process['end_ts'].append(event.timestamp)
if self.first_ts == 0:
self.first_ts = event.timestamp
self.cur_process_by_pid[process['pid']] = process
if build_p_stack :
p_stack = self.cur_process[event.common_cpu]
if p_stack:
p = p_stack[-1]
if len(p['start_ts'])>len(p['end_ts']):
p['end_ts'].append(event.timestamp)
# mark old process to wait for cpu
p['start_ts'].append(int(event.timestamp))
p['types'].append(colors.get_color_id("waiting_for_cpu"))
p['cpus'].append(event.common_cpu)
p_stack.append(process)
else:
self.cur_process[event.common_cpu] = [process]
# mark process to use cpu
process['start_ts'].append(event.timestamp)
process['types'].append(colors.get_color_id("running"))
process['cpus'].append(event.common_cpu)
def generic_process_end(self,process,event, build_p_stack=True):
if process['comm']=='swapper' and process['pid']==0:
return # ignore swapper event
if len(process['start_ts'])>len(process['end_ts']):
process['end_ts'].append(event.timestamp)
if build_p_stack :
p_stack = self.cur_process[event.common_cpu]
if p_stack:
p = p_stack.pop()
if p['pid'] != process['pid']:
print "warning: process premption stack following failure on CPU",event.common_cpu, p['comm'],p['pid'],process['comm'],process['pid'],map(lambda a:"%s:%d"%(a['comm'],a['pid']),p_stack),event.linenumber
p_stack = []
if p_stack:
p = p_stack[-1]
if len(p['start_ts'])>len(p['end_ts']):
p['end_ts'].append(event.timestamp)
# mark old process to run on cpu
p['start_ts'].append(event.timestamp)
p['types'].append(colors.get_color_id("running"))
p['cpus'].append(event.common_cpu)
def generic_process_single_event(self,process,event):
if len(process['start_ts'])>len(process['end_ts']):
process['end_ts'].append(event.timestamp)
# mark process to use cpu
process['start_ts'].append(event.timestamp)
process['types'].append(colors.get_color_id("running"))
process['cpus'].append(event.common_cpu)
process['end_ts'].append(event.timestamp)
def do_function_default(self,event):
process = self.generic_find_process(0,"kernel function:%s"%(event.callee),"function")
self.generic_process_single_event(process,event)
def do_event_default(self,event):
event.name = event.event.split(":")[0]
process = self.generic_find_process(0,"event:%s"%(event.name),"event")
self.generic_process_single_event(process,event)
process['comments'].append(event.event)
def start_parsing(self, get_partial_text):
# we build our data into python data formats, who are resizeable
# once everything is parsed, we will transform it into numpy array, for fast access
self.tmp_c_states = []
self.tmp_p_states = []
self.tmp_process = {}
self.timestamps = []
self.linenumbers = []
self.cur_process_by_pid = {}
self.wake_events = []
self.cur_process = [None]*20
self.last_irq={}
self.last_spi=[]
self.missed_power_end = 0
self.get_partial_text = get_partial_text
self.methods = {}
import plugin
colors.parse_colors(plugin.get_plugins_additional_colors())
plugin.get_plugins_methods(self.methods)
self.process_types = {
"function":(tcProcess, plugin.MISC_TRACES_CLASS),
"event":(tcProcess, plugin.MISC_TRACES_CLASS)}
self.process_types.update(plugin.get_plugins_additional_process_types())
def finish_parsing(self):
#put generated data in unresizable numpy format
c_states = []
i=0
for tc in self.tmp_c_states:
t = tcIdleState(name='cpu%d'%(i))
while len(tc['start_ts'])>len(tc['end_ts']):
tc['end_ts'].append(tc['start_ts'][-1])
t.start_ts = numpy.array(tc['start_ts'])
t.end_ts = numpy.array(tc['end_ts'])
t.types = numpy.array(tc['types'])
c_states.append(t)
i+=1
self.c_states=c_states
i=0
p_states = []
for tc in self.tmp_p_states:
t = tcFrequencyState(name='cpu%d'%(i))
t.start_ts = numpy.array(tc['start_ts'])
t.end_ts = numpy.array(tc['end_ts'])
t.types = numpy.array(tc['types'])
i+=1
p_states.append(t)
self.wake_events = numpy.array(self.wake_events,dtype=[('waker',tuple),('wakee',tuple),('time','uint64')])
self.p_states=p_states
processes = []
last_ts = 0
for pid,comm in self.tmp_process:
tc = self.tmp_process[pid,comm]
if len(tc['end_ts'])>0 and last_ts < tc['end_ts'][-1]:
last_ts = tc['end_ts'][-1]
if len(self.tmp_process) >0:
progress = ProgressDialog(title="precomputing data", message="precomputing overview data...", max=len(self.tmp_process), show_time=False, can_cancel=False)
progress.open()
i = 0
for pid,comm in self.tmp_process:
tc = self.tmp_process[pid,comm]
if self.process_types.has_key(tc['type']):
klass, order = self.process_types[tc['type']]
t = klass(pid=pid,comm=tc['comm'],project=self)
else:
t = tcProcess(pid=pid,comm=comm,project=self)
while len(tc['start_ts'])>len(tc['end_ts']):
tc['end_ts'].append(last_ts)
t.start_ts = numpy.array(tc['start_ts'])
t.end_ts = numpy.array(tc['end_ts'])
t.types = numpy.array(tc['types'])
t.cpus = numpy.array(tc['cpus'])
t.comments = tc['comments'] #numpy.array(tc['comments'])
t.process_type = tc["type"]
# precompute 16 levels of overview cache
t.get_overview_ts(1<<16)
processes.append(t)
progress.update(i)
i += 1
if len(self.tmp_process) > 0:
progress.close()
self.tmp_process = []
def cmp_process(x,y):
# sort process by type, pid, comm
def type_index(t):
try:
return self.process_types[t][1]
except ValueError:
return len(order)+1
c = cmp(type_index(x.process_type),type_index(y.process_type))
if c != 0:
return c
c = cmp(x.pid,y.pid)
if c != 0:
return c
c = cmp(x.comm,y.comm)
return c
processes.sort(cmp_process)
self.processes = processes
self.p_states=p_states
self.tmp_c_states = []
self.tmp_p_states = []
self.tmp_process = {}
def ensure_cpu_allocated(self,cpu):
# ensure we have enough per_cpu p/c_states timecharts
while len(self.tmp_c_states)<=cpu:
self.tmp_c_states.append({'start_ts':[],'end_ts':[],'types':[]})
while len(self.tmp_p_states)<=cpu:
self.tmp_p_states.append({'start_ts':[],'end_ts':[],'types':[]})
def run_callbacks(self, callback, event):
if callback in self.methods:
for m in self.methods[callback]:
try:
m(self,event)
except AttributeError:
if not hasattr(m,"num_exc"):
m.num_exc = 0
m.num_exc += 1
if m.num_exc <10:
print "bug in ", m, "still continue.."
traceback.print_exc()
print event
if m.num_exc == 10:
print m, "is too buggy, disabling, please report bug!"
self.methods[callback].remove(m)
if len(self.methods[callback])==0:
del self.methods[callback]
return True
return False
def handle_trace_event(self,event):
self.linenumbers.append(event.linenumber)
self.timestamps.append(event.timestamp)
if event.event=='function':
callback = "do_function_"+event.callee
self.run_callbacks("do_all_functions", event)
else:
callback = "do_event_"+event.event
self.run_callbacks("do_all_events", event)
if not self.run_callbacks(callback, event):
if event.event=='function':
self.do_function_default(event)
else:
self.do_event_default(event)
|