1// Copyright (c) 2012 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
5'use strict';
6
7/**
8 * @fileoverview TimelineModel is a parsed representation of the
9 * TraceEvents obtained from base/trace_event in which the begin-end
10 * tokens are converted into a hierarchy of processes, threads,
11 * subrows, and slices.
12 *
13 * The building block of the model is a slice. A slice is roughly
14 * equivalent to function call executing on a specific thread. As a
15 * result, slices may have one or more subslices.
16 *
17 * A thread contains one or more subrows of slices. Row 0 corresponds to
18 * the "root" slices, e.g. the topmost slices. Row 1 contains slices that
19 * are nested 1 deep in the stack, and so on. We use these subrows to draw
20 * nesting tasks.
21 *
22 */
23base.exportTo('tracing', function() {
24
25  function filterSliceArray(filter, slices) {
26    if (filter === undefined)
27      return slices;
28
29    var matched = [];
30    for (var i = 0; i < slices.length; ++i) {
31      if (filter.matchSlice(slices[i]))
32        matched.push(slices[i]);
33    }
34    return matched;
35  }
36
37  /**
38   * @constructor The generic base class for filtering a TimelineModel based on
39   * various rules. The base class returns true for everything.
40   */
41  function TimelineFilter() {
42  }
43
44  TimelineFilter.prototype = {
45    __proto__: Object.prototype,
46
47    matchCounter: function(counter) {
48      return true;
49    },
50
51    matchCpu: function(cpu) {
52      return true;
53    },
54
55    matchProcess: function(process) {
56      return true;
57    },
58
59    matchSlice: function(slice) {
60      return true;
61    },
62
63    matchThread: function(thread) {
64      return true;
65    }
66  };
67
68  /**
69   * @constructor A filter that matches objects by their name.
70   * Timeline.findAllObjectsMatchingFilter
71   */
72  function TimelineTitleFilter(text) {
73    TimelineFilter.call(this);
74    this.text_ = text;
75  }
76  TimelineTitleFilter.prototype = {
77    __proto__: TimelineFilter.prototype,
78
79    matchSlice: function(slice) {
80      if (this.text_.length == 0)
81        return false;
82      if (slice.title === undefined)
83        return false;
84      return slice.title.indexOf(this.text_) != -1;
85    }
86  };
87
88  /**
89   * @constructor A filter that filters objects by their category.
90   * Objects match if they are NOT in the list of categories
91   * @param {Array<string>} opt_categories Categories to blacklist.
92   */
93  function TimelineCategoryFilter(opt_categories) {
94    TimelineFilter.call(this);
95    this.categories_ = {};
96    var cats = opt_categories || [];
97    for (var i = 0; i < cats.length; i++)
98      this.addCategory(cats[i]);
99  }
100  TimelineCategoryFilter.prototype = {
101    __proto__: TimelineFilter.prototype,
102
103    addCategory: function(cat) {
104      this.categories_[cat] = true;
105    },
106
107    matchSlice: function(slice) {
108      if (!slice.category)
109        return true;
110      return !this.categories_[slice.category];
111    }
112  };
113
114  return {
115    filterSliceArray: filterSliceArray,
116    TimelineFilter: TimelineFilter,
117    TimelineTitleFilter: TimelineTitleFilter,
118    TimelineCategoryFilter: TimelineCategoryFilter
119  };
120});
121