aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/profiler/statistics/SkylarkStatistics.java
blob: 4dff2723d4736994acb17ba4544ad12beefc4c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.profiler.statistics;

import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.devtools.build.lib.profiler.analysis.ProfileInfo;
import com.google.devtools.build.lib.profiler.analysis.ProfileInfo.Task;
import com.google.devtools.build.lib.util.LongArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Extracts the execution times of user-defined and built-in Skylark functions and computes
 * statistics.
 *
 * <p>The statistics are separated for the total duration taken for a function call and the
 * "self duration" taken only within the function itself, but not within any subtask of the
 * corresponding {@link Task}.
 */
public final class SkylarkStatistics {

  private final Map<String, LongArrayList> userFunctionDurations;
  private final Map<String, LongArrayList> userCompiledDurations;
  private final Map<String, LongArrayList> builtinFunctionDurations;

  /**
   * Self duration is the time taken just within a function itself, but not other subtasks of it.
   */
  private final Map<String, LongArrayList> userFunctionSelfDurations;
  private final Map<String, LongArrayList> userCompiledSelfDurations;
  private final Map<String, LongArrayList> builtinFunctionSelfDurations;
  private long userTotalNanos;
  private long userCompiledTotalNanos;
  private long builtinTotalNanos;

  public SkylarkStatistics() {
    userFunctionDurations = new HashMap<>();
    userCompiledDurations = new HashMap<>();
    builtinFunctionDurations = new HashMap<>();
    userFunctionSelfDurations = new HashMap<>();
    userCompiledSelfDurations = new HashMap<>();
    builtinFunctionSelfDurations = new HashMap<>();
  }

  public SkylarkStatistics(ProfileInfo info) {
    this();
    addProfileInfo(info);
  }

  /**
   * Adds Skylark function task durations from a {@link ProfileInfo} file.
   */
  public void addProfileInfo(ProfileInfo info) {
    computeStatistics(
        info.getSkylarkUserFunctionTasks(),
        info.getCompiledSkylarkUserFunctionTasks(),
        info.getSkylarkBuiltinFunctionTasks());
  }

  /**
   * @return the total time taken by all calls to built-in Skylark functions
   */
  public long getBuiltinTotalNanos() {
    return builtinTotalNanos;
  }

  /**
   * @return the total time taken by all calls to user-defined Skylark functions
   */
  public long getCompiledUserTotalNanos() {
    return userCompiledTotalNanos;
  }

  /**
   * @return the total time taken by all calls to user-defined Skylark functions
   */
  public long getUserTotalNanos() {
    return userTotalNanos;
  }

  /**
   * @return The execution durations of all calls to built-in Skylark functions.
   */
  public Map<String, LongArrayList> getBuiltinFunctionDurations() {
    return builtinFunctionDurations;
  }

  /**
   * return The execution durations of all calls to built-in functions excluding the durations of
   * all subtasks.
   */
  public Map<String, LongArrayList> getBuiltinFunctionSelfDurations() {
    return builtinFunctionSelfDurations;
  }

  /**
   * Builds and returns the {@link TasksStatistics} for the durations of each built-in function.
   * The return value is not cached and will be recomputed on another call.
   */
  public Map<String, TasksStatistics> getBuiltinFunctionStatistics() {
    return buildTasksStatistics(builtinFunctionDurations);
  }

  /**
   * Builds and returns the {@link TasksStatistics} for the self-times of each built-in function.
   * The return value is not cached and will be recomputed on another call.
   */
  public Map<String, TasksStatistics> getBuiltinFunctionSelfStatistics() {
    return buildTasksStatistics(builtinFunctionSelfDurations);
  }

  /**
   * @return The execution durations of all calls to user-defined Skylark functions.
   */
  public Map<String, LongArrayList> getCompiledUserFunctionDurations() {
    return userCompiledDurations;
  }

  /**
   * return The execution durations of all calls to user-defined functions excluding the durations
   * of all subtasks.
   */
  public Map<String, LongArrayList> getCompiledUserFunctionSelfDurations() {
    return userCompiledSelfDurations;
  }

  /**
   * Builds and returns the {@link TasksStatistics} for the durations of each user-defined
   * function. The return value is not cached and will be recomputed on another call.
   */
  public Map<String, TasksStatistics> getCompiledUserFunctionStatistics() {
    return buildTasksStatistics(userCompiledDurations);
  }

  /**
   * Builds and returns the {@link TasksStatistics} for the self-times of each user-defined
   * function. The return value is not cached and will be recomputed on another call.
   */
  public Map<String, TasksStatistics> getCompiledUserFunctionSelfStatistics() {
    return buildTasksStatistics(userCompiledSelfDurations);
  }

  /**
   * @return The execution durations of all calls to user-defined Skylark functions.
   */
  public Map<String, LongArrayList> getUserFunctionDurations() {
    return userFunctionDurations;
  }

  /**
   * return The execution durations of all calls to user-defined functions excluding the durations
   * of all subtasks.
   */
  public Map<String, LongArrayList> getUserFunctionSelfDurations() {
    return userFunctionSelfDurations;
  }

  /**
   * Builds and returns the {@link TasksStatistics} for the durations of each user-defined
   * function. The return value is not cached and will be recomputed on another call.
   */
  public Map<String, TasksStatistics> getUserFunctionStatistics() {
    return buildTasksStatistics(userFunctionDurations);
  }

  /**
   * Builds and returns the {@link TasksStatistics} for the self-times of each user-defined
   * function. The return value is not cached and will be recomputed on another call.
   */
  public Map<String, TasksStatistics> getUserFunctionSelfStatistics() {
    return buildTasksStatistics(userFunctionSelfDurations);
  }

  /**
   * For each Skylark function get the list of durations and self durations from the task maps.
   */
  private void computeStatistics(
      Multimap<String, Task> userTasks,
      Multimap<String, Task> userCompiledTasks,
      Multimap<String, Task> builtinTasks) {
    userTotalNanos += addDurations(userTasks, userFunctionDurations, userFunctionSelfDurations);
    userCompiledTotalNanos +=
        addDurations(userCompiledTasks, userCompiledDurations, userCompiledSelfDurations);
    builtinTotalNanos +=
        addDurations(builtinTasks, builtinFunctionDurations, builtinFunctionSelfDurations);
  }

  /**
   * Add all new durations to previously collected durations for all functions mapped to tasks.
   * @return The sum of the execution times of all {@link Task} values in the map.
   */
  private static long addDurations(
      Multimap<String, Task> functionTasks,
      Map<String, LongArrayList> durationsMap,
      Map<String, LongArrayList> selfDurationsMap) {
    long totalTime = 0;
    for (Entry<String, Collection<Task>> entry : functionTasks.asMap().entrySet()) {
      String function = entry.getKey();
      Collection<Task> tasks = entry.getValue();
      LongArrayList durations;
      LongArrayList selfDurations;
      if (durationsMap.containsKey(function)) {
        durations = durationsMap.get(function);
        selfDurations = selfDurationsMap.get(function);
      } else {
        durations = new LongArrayList(tasks.size());
        selfDurations = new LongArrayList(tasks.size());
        durationsMap.put(function, durations);
        selfDurationsMap.put(function, selfDurations);
      }
      totalTime += addDurations(tasks, durations, selfDurations);
    }
    return totalTime;
  }

  /**
   * Add all durations and self-times of the given function to the maps.
   * @return The sum of the execution times of all {@link Task} values in the collection.
   */
  private static long addDurations(
      Collection<Task> tasks, LongArrayList durations, LongArrayList selfDurations) {
    long totalTime = 0;
    durations.ensureCapacity(durations.size() + tasks.size());
    selfDurations.ensureCapacity(selfDurations.size() + tasks.size());
    for (Task task : tasks) {
      durations.add(task.durationNanos);
      selfDurations.add(task.durationNanos - task.getInheritedDuration());
      totalTime += task.durationNanos;
    }
    return totalTime;
  }

  /**
   * Build a Map of {@link TasksStatistics} from the given duration maps.
   */
  private static Map<String, TasksStatistics> buildTasksStatistics(
      final Map<String, LongArrayList> durationsMap) {
    return Maps.transformEntries(durationsMap, TasksStatistics::create);
  }
}