Chromium Code Reviews| Index: chrome/browser/resources/tracing/timeline_model.js |
| diff --git a/chrome/browser/resources/tracing/timeline_model.js b/chrome/browser/resources/tracing/timeline_model.js |
| index 35858186d5c81e5734687349cabb53f0bab059ca..e9bf54088e1029a19c343116aa2a2b4c6c293912 100644 |
| --- a/chrome/browser/resources/tracing/timeline_model.js |
| +++ b/chrome/browser/resources/tracing/timeline_model.js |
| @@ -120,8 +120,8 @@ cr.define('tracing', function() { |
| * Comparison between threads that orders first by pid, |
| * then by names, then by tid. |
| */ |
| - TimelineThread.compare = function(x,y) { |
| - if(x.parent.pid != y.parent.pid) { |
| + TimelineThread.compare = function(x, y) { |
| + if (x.parent.pid != y.parent.pid) { |
| return x.parent.pid - y.parent.pid; |
| } |
| @@ -132,7 +132,7 @@ cr.define('tracing', function() { |
| return tmp; |
| } else if (x.name) { |
| return -1; |
| - } else if (y.name){ |
| + } else if (y.name) { |
| return 1; |
| } else { |
| return x.tid - y.tid; |
| @@ -152,7 +152,14 @@ cr.define('tracing', function() { |
| }; |
| TimelineProcess.prototype = { |
| - getThread: function(tid) { |
| + get numThreads() { |
| + var n = 0; |
| + for (var p in this.threads) |
|
arv (Not doing code reviews)
2011/10/21 17:09:18
Missing {}
nduca
2011/10/21 19:50:40
Done.
|
| + n++; |
| + return n; |
| + }, |
| + |
| + getOrCreateThread: function(tid) { |
| if (!this.threads[tid]) |
| this.threads[tid] = new TimelineThread(this, tid); |
| return this.threads[tid]; |
| @@ -160,6 +167,18 @@ cr.define('tracing', function() { |
| }; |
| /** |
| + * Computes a simplistic hashcode of the provide name. Used to chose colors |
| + * for slices. |
| + * @param {string} name The string to hash. |
| + */ |
| + function getStringHash(name) { |
|
arv (Not doing code reviews)
2011/10/21 17:09:18
This looks like it might be overkill for choosing
nduca
2011/10/21 19:50:40
Do you have any suggestions? The goal is to get a
arv (Not doing code reviews)
2011/10/21 23:43:53
I don't have any better idea. Are the strings shor
|
| + var hash = 0; |
| + for (var i = 0; i < name.length; ++i) |
| + hash = (hash + 37 * hash + 11 * name.charCodeAt(i)) % 0xFFFFFFFF; |
| + return hash; |
| + } |
| + |
| + /** |
| * Builds a model from an array of TraceEvent objects. |
| * @param {Array} events An array of TraceEvents created by |
| * TraceEvent.ToJSON(). |
| @@ -176,7 +195,14 @@ cr.define('tracing', function() { |
| TimelineModel.prototype = { |
| __proto__: cr.EventTarget.prototype, |
| - getProcess: function(pid) { |
| + get numProcesses() { |
| + var n = 0; |
| + for (var p in this.processes) |
| + n++; |
| + return n; |
| + }, |
| + |
| + getOrCreateProcess: function(pid) { |
| if (!this.processes[pid]) |
| this.processes[pid] = new TimelineProcess(pid); |
| return this.processes[pid]; |
| @@ -202,11 +228,7 @@ cr.define('tracing', function() { |
| var nameToColorMap = {}; |
| function getColor(name) { |
| if (!(name in nameToColorMap)) { |
| - // Compute a simplistic hashcode of the string so we get consistent |
| - // coloring across traces. |
| - var hash = 0; |
| - for (var i = 0; i < name.length; ++i) |
| - hash = (hash + 37 * hash + 11 * name.charCodeAt(i)) % 0xFFFFFFFF; |
| + var hash = getStringHash(name); |
| nameToColorMap[name] = hash % numColorIds; |
| } |
| return nameToColorMap[name]; |
| @@ -253,7 +275,8 @@ cr.define('tracing', function() { |
| slice.slice.duration = event.ts - slice.slice.start; |
| // Store the slice in a non-nested subrow. |
| - var thread = self.getProcess(event.pid).getThread(event.tid); |
| + var thread = self.getOrCreateProcess(event.pid) |
| + .getOrCreateThread(event.tid); |
|
arv (Not doing code reviews)
2011/10/21 17:09:18
binop before newline
nduca
2011/10/21 19:50:40
Done.
|
| thread.addNonNestedSlice(slice.slice); |
| delete state.openNonNestedSlices[name]; |
| } else { |
| @@ -265,7 +288,8 @@ cr.define('tracing', function() { |
| slice.duration = event.ts - slice.start; |
| // Store the slice on the correct subrow. |
| - var thread = self.getProcess(event.pid).getThread(event.tid); |
| + var thread = self.getOrCreateProcess(event.pid) |
| + .getOrCreateThread(event.tid); |
| var subRowIndex = state.openSlices.length; |
| thread.getSubrow(subRowIndex).push(slice); |
| @@ -297,14 +321,15 @@ cr.define('tracing', function() { |
| processEnd(state, event); |
| } else if (event.ph == 'M') { |
| if (event.name == 'thread_name') { |
| - var thread = this.getProcess(event.pid).getThread(event.tid); |
| + var thread = this.getOrCreateProcess(event.pid) |
| + .getOrCreateThread(event.tid); |
| thread.name = event.args.name; |
| } else { |
| this.importErrors.push('Unrecognized metadata name: ' + event.name); |
| } |
| } else { |
| this.importErrors.push('Unrecognized event phase: ' + event.ph + |
| - '(' + event.name + ')'); |
| + '(' + event.name + ')'); |
| } |
| } |
| this.pruneEmptyThreads(); |
| @@ -322,7 +347,8 @@ cr.define('tracing', function() { |
| var event = events[slice.index]; |
| // Store the slice on the correct subrow. |
| - var thread = this.getProcess(event.pid).getThread(event.tid); |
| + var thread = this.getOrCreateProcess(event.pid) |
| + .getOrCreateThread(event.tid); |
| var subRowIndex = state.openSlices.length; |
| thread.getSubrow(subRowIndex).push(slice.slice); |
| @@ -347,11 +373,11 @@ cr.define('tracing', function() { |
| pruneEmptyThreads: function() { |
| for (var pid in this.processes) { |
| var process = this.processes[pid]; |
| - var prunedThreads = []; |
| + var prunedThreads = {}; |
| for (var tid in process.threads) { |
| var thread = process.threads[tid]; |
| if (thread.subRows[0].length || thread.nonNestedSubRows.legnth) |
| - prunedThreads.push(thread); |
| + prunedThreads[tid] = thread; |
| } |
| process.threads = prunedThreads; |
| } |
| @@ -411,6 +437,7 @@ cr.define('tracing', function() { |
| }; |
| return { |
| + getStringHash: getStringHash, |
| TimelineSlice: TimelineSlice, |
| TimelineThread: TimelineThread, |
| TimelineProcess: TimelineProcess, |