Home | History | Annotate | Download | only in system_health
      1 <!DOCTYPE html>
      2 <!--
      3 Copyright 2016 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.
      6 -->
      7 
      8 <link rel="import" href="/tracing/metrics/metric_registry.html">
      9 <link rel="import" href="/tracing/value/histogram.html">
     10 
     11 <script>
     12 'use strict';
     13 
     14 tr.exportTo('tr.metrics.sh', function() {
     15   /**
     16    * This metric measures total CPU time for Chrome processes, per second of
     17    *   clock time.
     18    * This metric requires only the 'toplevel' tracing category.
     19    *
     20    * @param {!tr.v.ValueSet} values
     21    * @param {!tr.model.Model} model
     22    * @param {!Object=} optOptions
     23    */
     24   function cpuTimeMetric(values, model, optOptions) {
     25     var rangeOfInterest = optOptions ? optOptions.rangeOfInterest : undefined;
     26     var allProcessCpuTime = 0;
     27 
     28     for (var pid in model.processes) {
     29       var process = model.processes[pid];
     30       var processCpuTime = 0;
     31       for (var tid in process.threads) {
     32         var thread = process.threads[tid];
     33         var threadCpuTime = 0;
     34         thread.sliceGroup.topLevelSlices.forEach(function(slice) {
     35           if (rangeOfInterest &&
     36               !rangeOfInterest.intersectsExplicitRangeInclusive(
     37                   slice.start, slice.end))
     38             return;
     39           threadCpuTime += slice.cpuDuration;
     40         });
     41         processCpuTime += threadCpuTime;
     42       }
     43       allProcessCpuTime += processCpuTime;
     44     }
     45 
     46     // Normalize cpu time by clock time.
     47     var normalizationRange = rangeOfInterest ?
     48         rangeOfInterest : model.bounds.range;
     49     var MILLISECONDS_PER_SECOND = 1000;
     50     var clockTimeInSeconds = normalizationRange / MILLISECONDS_PER_SECOND;
     51 
     52     // Use a minimum clock time of 0.0001 to allow 0-sized ranges.
     53     clockTimeInSeconds = Math.max(clockTimeInSeconds, 0.0001);
     54     var normalizedAllProcessCpuTime = allProcessCpuTime /
     55         clockTimeInSeconds;
     56 
     57     var unit = tr.b.Unit.byName.timeDurationInMs_smallerIsBetter;
     58     var boundaries = tr.v.HistogramBinBoundaries
     59       .createExponential(1, 100000, 200)
     60     var cpuTimeNumeric = new tr.v.Histogram('cpu_time', unit, boundaries);
     61     cpuTimeNumeric.description =
     62       'Total CPU time on all Chrome processes, per second of clock time.';
     63     cpuTimeNumeric.addSample(normalizedAllProcessCpuTime);
     64     values.addHistogram(cpuTimeNumeric);
     65   }
     66 
     67   tr.metrics.MetricRegistry.register(cpuTimeMetric, {
     68     supportsRangeOfInterest: true
     69   });
     70 
     71   return {
     72     cpuTimeMetric: cpuTimeMetric,
     73   };
     74 });
     75 </script>
     76