Home | History | Annotate | Download | only in model
      1 <!DOCTYPE html>
      2 <!--
      3 Copyright (c) 2015 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="/base/iteration_helpers.html">
      9 <link rel="import" href="/model/event_set.html">
     10 
     11 <script>
     12 'use strict';
     13 
     14 tr.exportTo('tr.model', function() {
     15   function getAllAssociatedEvents(irs) {
     16     var allAssociatedEvents = new tr.model.EventSet();
     17     irs.forEach(function(ir) {
     18       ir.associatedEvents.forEach(function(event) {
     19         // FlowEvents don't have parentContainers or cpuDurations, and it's
     20         // annoying to highlight them.
     21         if (event instanceof tr.model.FlowEvent)
     22           return;
     23         allAssociatedEvents.push(event);
     24       });
     25     });
     26     return allAssociatedEvents;
     27   }
     28 
     29   function getUnassociatedEvents(model, associatedEvents) {
     30     var unassociatedEvents = new tr.model.EventSet();
     31     model.getAllProcesses().forEach(function(process) {
     32       for (var tid in process.threads) {
     33         var thread = process.threads[tid];
     34         thread.sliceGroup.iterateAllEvents(function(event) {
     35           // The set of unassociated events contains only events that are not in
     36           // the set of associated events.
     37           // Only add event to the set of unassociated events if it is not in
     38           // the set of associated events.
     39           if (!associatedEvents.contains(event))
     40             unassociatedEvents.push(event);
     41         });
     42       }
     43     });
     44     return unassociatedEvents;
     45   }
     46 
     47   function getTotalCpuDuration(events) {
     48     var cpuMs = 0;
     49     events.forEach(function(event) {
     50       // Add up events' cpu duration if they have any.
     51       // Avoid double-counting cpu duration by only counting top-level events.
     52       if (event.cpuDuration && event.isTopLevel)
     53         cpuMs += event.cpuDuration;
     54     });
     55     return cpuMs;
     56   }
     57 
     58   function getIRCoverageFromModel(model) {
     59     var associatedEvents = getAllAssociatedEvents(model.interaction_records);
     60 
     61     if (!associatedEvents.length)
     62       return undefined;
     63 
     64     var unassociatedEvents = getUnassociatedEvents(
     65         model, associatedEvents);
     66 
     67     var associatedCpuMs = getTotalCpuDuration(associatedEvents);
     68     var unassociatedCpuMs = getTotalCpuDuration(unassociatedEvents);
     69 
     70     var totalEventCount = associatedEvents.length + unassociatedEvents.length;
     71     var totalCpuMs = associatedCpuMs + unassociatedCpuMs;
     72     var coveredCpuRatio = undefined;
     73     var uncoveredCpuRatio = undefined;
     74     if (totalCpuMs) {
     75       coveredCpuRatio = associatedCpuMs / totalCpuMs;
     76       uncoveredCpuRatio = 1 - coveredCpuRatio;
     77     }
     78 
     79     return {
     80       associatedEvents: associatedEvents,
     81       unassociatedEvents: unassociatedEvents,
     82       associatedCpuMs: associatedCpuMs,
     83       unassociatedCpuMs: unassociatedCpuMs,
     84       coveredEventsRatio: associatedEvents.length / totalEventCount,
     85       uncoveredEventsRatio: unassociatedEvents.length / totalEventCount,
     86       coveredCpuRatio: coveredCpuRatio,
     87       uncoveredCpuRatio: uncoveredCpuRatio
     88     };
     89   }
     90 
     91   return {
     92     getIRCoverageFromModel: getIRCoverageFromModel
     93   };
     94 });
     95 </script>
     96