aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/profiler/CpuUsageTimeSeries.java
blob: faadb7357a366bace06a9e1c1b7636f1363ceb6b (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
// Copyright 2018 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;

import java.util.Arrays;

/**
 * Converts a set of ranges into a graph by counting the number of ranges that are active at any
 * point in time. Time is split into equal-sized buckets, and we compute one value per bucket. If a
 * range partially overlaps a bucket, then the bucket is incremented by the fraction of overlap.
 */
public class CpuUsageTimeSeries {
  private final long startTimeMillis;
  private final long bucketSizeMillis;
  private static final int INITIAL_SIZE = 100;
  private double[] data = new double[INITIAL_SIZE];

  public CpuUsageTimeSeries(long startTimeMillis, long bucketSizeMillis) {
    this.startTimeMillis = startTimeMillis;
    this.bucketSizeMillis = bucketSizeMillis;
  }

  public void addRange(long startTimeMillis, long endTimeMillis) {
    addRange(startTimeMillis, endTimeMillis, /* value= */ 1);
  }

  /** Adds a new range to the time series, by increasing every affected bucket by value. */
  public void addRange(long rangeStartMillis, long rangeEndMillis, double value) {
    // Compute times relative to start and their positions in the data array.
    rangeStartMillis -= startTimeMillis;
    rangeEndMillis -= startTimeMillis;
    int startPosition = (int) (rangeStartMillis / bucketSizeMillis);
    int endPosition = (int) (rangeEndMillis / bucketSizeMillis);

    // Assume we add the following range R:
    // ----------------------------------
    // |     |ssRRR|RRRRR|Reeee|      |
    // ----------------------------------
    // we cannot just add value to each affected bucket but have to correct the values for the first
    // and last bucket by calculating the size of 's' and 'e'.
    double missingStartFraction =
        ((double) (rangeStartMillis - bucketSizeMillis * startPosition)) / bucketSizeMillis;
    double missingEndFraction =
        ((double) (bucketSizeMillis * (endPosition + 1) - rangeEndMillis)) / bucketSizeMillis;

    if (startPosition < 0) {
      startPosition = 0;
      missingStartFraction = 0;
    }
    if (endPosition < startPosition) {
      endPosition = startPosition;
      missingEndFraction = 0;
    }

    // Resize data array if necessary so it can at least fit endPosition.
    if (endPosition >= data.length) {
      data = Arrays.copyOf(data, Math.max(endPosition + 1, 2 * data.length));
    }

    // Do the actual update.
    for (int i = startPosition; i <= endPosition; i++) {
      double fraction = 1;
      if (i == startPosition) {
        fraction -= missingStartFraction;
      }
      if (i == endPosition) {
        fraction -= missingEndFraction;
      }
      data[i] += fraction * value;
    }
  }

  public double[] toDoubleArray(int len) {
    return Arrays.copyOf(data, len);
  }
}