1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5class TimelineEvent(object):
6  """Represents a timeline event.
7
8     thread_start, thread_duration and thread_end are the start time, duration
9     and end time of this event as measured by the thread-specific CPU clock
10     (ticking when the thread is actually scheduled). Thread time is optional
11     on trace events and the corresponding attributes in TimelineEvent will be
12     set to None (not 0) if not present. Users of this class need to properly
13     handle this case.
14  """
15  def __init__(self, category, name, start, duration, thread_start=None,
16               thread_duration=None, args=None):
17    self.category = category
18    self.name = name
19    self.start = start
20    self.duration = duration
21    self.thread_start = thread_start
22    self.thread_duration = thread_duration
23    self.args = args
24
25  @property
26  def end(self):
27    return self.start + self.duration
28
29  @property
30  def has_thread_timestamps(self):
31    return self.thread_start is not None and self.thread_duration is not None
32
33  @property
34  def thread_end(self):
35    """Thread-specific CPU time when this event ended.
36
37       May be None if the trace event didn't have thread time data.
38    """
39    if self.thread_start == None or self.thread_duration == None:
40      return None
41    return self.thread_start + self.thread_duration
42
43  def __repr__(self):
44    if self.args:
45      args_str = ', ' + repr(self.args)
46    else:
47      args_str = ''
48
49    return ("TimelineEvent(name='%s', start=%f, duration=%s, " +
50            "thread_start=%s, thread_duration=%s%s)") % (
51                self.name,
52                self.start,
53                self.duration,
54                self.thread_start,
55                self.thread_duration,
56                args_str)
57