Home | History | Annotate | Download | only in trace_model
      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 Provides the Process class.
      9  */
     10 base.require('tracing.trace_model.process_base');
     11 base.exportTo('tracing.trace_model', function() {
     12   /**
     13    * The Process represents a single userland process in the
     14    * trace.
     15    * @constructor
     16    */
     17   function Process(model, pid) {
     18     if (model === undefined)
     19       throw new Error('model must be provided');
     20     if (pid === undefined)
     21       throw new Error('pid must be provided');
     22     tracing.trace_model.ProcessBase.call(this, model);
     23     this.pid = pid;
     24     this.name = undefined;
     25     this.labels = [];
     26     this.instantEvents = [];
     27   };
     28 
     29   /**
     30    * Comparison between processes that orders by pid.
     31    */
     32   Process.compare = function(x, y) {
     33     var tmp = tracing.trace_model.ProcessBase.compare(x, y);
     34     if (tmp)
     35       return tmp;
     36 
     37     tmp = base.comparePossiblyUndefinedValues(
     38         x.name, y.name,
     39         function(x, y) { return x.localeCompare(y); });
     40     if (tmp)
     41       return tmp;
     42 
     43     tmp = base.compareArrays(x.labels, y.labels,
     44         function(x, y) { return x.localeCompare(y); });
     45     if (tmp)
     46       return tmp;
     47 
     48     return x.pid - y.pid;
     49   };
     50 
     51   Process.prototype = {
     52     __proto__: tracing.trace_model.ProcessBase.prototype,
     53 
     54     compareTo: function(that) {
     55       return Process.compare(this, that);
     56     },
     57 
     58     pushInstantEvent: function(instantEvent) {
     59       this.instantEvents.push(instantEvent);
     60     },
     61 
     62     get userFriendlyName() {
     63       var res;
     64       if (this.name)
     65         res = this.name;
     66       else
     67         res = 'Process ' + this.pid;
     68       if (this.labels.length)
     69         res += ': ' + this.labels.join(', ');
     70       return res;
     71     },
     72 
     73     get userFriendlyDetails() {
     74       if (this.name)
     75         return this.name + ' (pid ' + this.pid + ')';
     76       return 'pid: ' + this.pid;
     77     },
     78 
     79     getSettingsKey: function() {
     80       if (!this.name)
     81         return undefined;
     82       if (!this.labels.length)
     83         return 'processes.' + this.name;
     84       return 'processes.' + this.name + '.' + this.labels.join('.');
     85     },
     86 
     87     shiftTimestampsForward: function(amount) {
     88       for (var id in this.instantEvents)
     89         this.instantEvents[id].start += amount;
     90 
     91       tracing.trace_model.ProcessBase.prototype
     92           .shiftTimestampsForward.apply(this, arguments);
     93     }
     94   };
     95 
     96   return {
     97     Process: Process
     98   };
     99 });
    100