1<!DOCTYPE html>
2<!--
3Copyright (c) 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/iteration_helpers.html">
9<link rel="import" href="/tracing/model/event_set.html">
10
11<script>
12'use strict';
13
14tr.exportTo('tr.model', function() {
15  function getAssociatedEvents(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 self time if they have any.
51      if (event.cpuSelfTime)
52        cpuMs += event.cpuSelfTime;
53    });
54    return cpuMs;
55  }
56
57  function getIRCoverageFromModel(model) {
58    var associatedEvents = getAssociatedEvents(model.userModel.expectations);
59
60    if (!associatedEvents.length)
61      return undefined;
62
63    var unassociatedEvents = getUnassociatedEvents(
64        model, associatedEvents);
65
66    var associatedCpuMs = getTotalCpuDuration(associatedEvents);
67    var unassociatedCpuMs = getTotalCpuDuration(unassociatedEvents);
68
69    var totalEventCount = associatedEvents.length + unassociatedEvents.length;
70    var totalCpuMs = associatedCpuMs + unassociatedCpuMs;
71    var coveredEventsCpuTimeRatio = undefined;
72    if (totalCpuMs !== 0)
73      coveredEventsCpuTimeRatio = associatedCpuMs / totalCpuMs;
74
75    return {
76      associatedEventsCount: associatedEvents.length,
77      unassociatedEventsCount: unassociatedEvents.length,
78      associatedEventsCpuTimeMs: associatedCpuMs,
79      unassociatedEventsCpuTimeMs: unassociatedCpuMs,
80      coveredEventsCountRatio: associatedEvents.length / totalEventCount,
81      coveredEventsCpuTimeRatio: coveredEventsCpuTimeRatio
82    };
83  }
84
85  return {
86    getIRCoverageFromModel: getIRCoverageFromModel,
87    getAssociatedEvents: getAssociatedEvents,
88    getUnassociatedEvents: getUnassociatedEvents
89  };
90});
91</script>
92