3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
8 <link rel="import" href="/tracing/trace_model/event.html">
9 <link rel="import" href="/tracing/trace_model/object_snapshot.html">
10 <link rel="import" href="/base/range.html">
11 <link rel="import" href="/base/sorted_array_utils.html">
17 * @fileoverview Provides the ObjectSnapshot and ObjectHistory classes.
19 tv.exportTo('tracing.trace_model', function() {
20 var ObjectSnapshot = tracing.trace_model.ObjectSnapshot;
23 * An object with a specific id, whose state has been snapshotted several
28 function ObjectInstance(
29 parent, id, category, name, creationTs, opt_baseTypeName) {
30 tracing.trace_model.Event.call(this);
33 this.category = category;
34 this.baseTypeName = opt_baseTypeName ? opt_baseTypeName : name;
36 this.creationTs = creationTs;
37 this.creationTsWasExplicit = false;
38 this.deletionTs = Number.MAX_VALUE;
39 this.deletionTsWasExplicit = false;
41 this.bounds = new tv.Range();
43 this.hasImplicitSnapshots = false;
46 ObjectInstance.prototype = {
47 __proto__: tracing.trace_model.Event.prototype,
53 addBoundsToRange: function(range) {
54 range.addRange(this.bounds);
57 addSnapshot: function(ts, args, opt_name, opt_baseTypeName) {
58 if (ts < this.creationTs)
59 throw new Error('Snapshots must be >= instance.creationTs');
60 if (ts >= this.deletionTs)
61 throw new Error('Snapshots cannot be added after ' +
62 'an objects deletion timestamp.');
65 if (this.snapshots.length > 0) {
66 lastSnapshot = this.snapshots[this.snapshots.length - 1];
67 if (lastSnapshot.ts == ts)
68 throw new Error('Snapshots already exists at this time!');
69 if (ts < lastSnapshot.ts) {
71 'Snapshots must be added in increasing timestamp order');
75 // Update baseTypeName if needed.
77 (this.name != opt_name)) {
78 if (!opt_baseTypeName)
79 throw new Error('Must provide base type name for name update');
80 if (this.baseTypeName != opt_baseTypeName)
81 throw new Error('Cannot update type name: base types dont match');
85 var snapshotConstructor =
86 tracing.trace_model.ObjectSnapshot.getConstructor(
87 this.category, this.name);
88 var snapshot = new snapshotConstructor(this, ts, args);
89 this.snapshots.push(snapshot);
93 wasDeleted: function(ts) {
95 if (this.snapshots.length > 0) {
96 lastSnapshot = this.snapshots[this.snapshots.length - 1];
97 if (lastSnapshot.ts > ts)
99 'Instance cannot be deleted at ts=' +
100 ts + '. A snapshot exists that is older.');
102 this.deletionTs = ts;
103 this.deletionTsWasExplicit = true;
107 * See ObjectSnapshot constructor notes on object initialization.
109 preInitialize: function() {
110 for (var i = 0; i < this.snapshots.length; i++)
111 this.snapshots[i].preInitialize();
115 * See ObjectSnapshot constructor notes on object initialization.
117 initialize: function() {
118 for (var i = 0; i < this.snapshots.length; i++)
119 this.snapshots[i].initialize();
122 getSnapshotAt: function(ts) {
123 if (ts < this.creationTs) {
124 if (this.creationTsWasExplicit)
125 throw new Error('ts must be within lifetime of this instance');
126 return this.snapshots[0];
128 if (ts > this.deletionTs)
129 throw new Error('ts must be within lifetime of this instance');
131 var snapshots = this.snapshots;
132 var i = tv.findLowIndexInSortedIntervals(
134 function(snapshot) { return snapshot.ts; },
135 function(snapshot, i) {
136 if (i == snapshots.length - 1)
137 return snapshots[i].objectInstance.deletionTs;
138 return snapshots[i + 1].ts - snapshots[i].ts;
142 // Note, this is a little bit sketchy: this lets early ts point at the
143 // first snapshot, even before it is taken. We do this because raster
144 // tasks usually post before their tile snapshots are dumped. This may
145 // be a good line of code to re-visit if we start seeing strange and
146 // confusing object references showing up in the traces.
147 return this.snapshots[0];
149 if (i >= this.snapshots.length)
150 return this.snapshots[this.snapshots.length - 1];
151 return this.snapshots[i];
154 updateBounds: function() {
156 this.bounds.addValue(this.creationTs);
157 if (this.deletionTs != Number.MAX_VALUE)
158 this.bounds.addValue(this.deletionTs);
159 else if (this.snapshots.length > 0)
160 this.bounds.addValue(this.snapshots[this.snapshots.length - 1].ts);
163 shiftTimestampsForward: function(amount) {
164 this.creationTs += amount;
165 if (this.deletionTs != Number.MAX_VALUE)
166 this.deletionTs += amount;
167 this.snapshots.forEach(function(snapshot) {
168 snapshot.ts += amount;
173 tracing.trace_model.Event.decorateSubtype(ObjectInstance);
176 ObjectInstance: ObjectInstance