aboutsummaryrefslogtreecommitdiffstats
path: root/container-disc/src/main/java/com/yahoo/container/jdisc/metric/GarbageCollectionMetrics.java
blob: 33795e575e309d37d4ec7f8c980b34b028d7b268 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.metric;

import ai.vespa.metrics.ContainerMetrics;
import com.yahoo.jdisc.Metric;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author ollivir
 */
public class GarbageCollectionMetrics {

    private static final String GC_COUNT = ContainerMetrics.JDISC_GC_COUNT.baseName();
    private static final String GC_TIME = ContainerMetrics.JDISC_GC_MS.baseName();
    private static final String DIMENSION_KEY = "gcName";

    public static final Duration REPORTING_INTERVAL = Duration.ofSeconds(62);

    static class GcStats {
        private final Instant when;
        private final long count;
        private final Duration totalRuntime;

        private GcStats(Instant when, long count, Duration totalRuntime) {
            this.when = when;
            this.count = count;
            this.totalRuntime = totalRuntime;
        }
    }

    private final Map<String, LinkedList<GcStats>> gcStatistics;

    private final Clock clock;

    public GarbageCollectionMetrics(Clock clock) {
        this.clock = clock;
        this.gcStatistics = new HashMap<>();
        collectGcStatistics(clock.instant());
    }

    private void collectGcStatistics(Instant now) {
        for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
            String gcName = gcBean.getName().replace(" ", "");
            GcStats stats = new GcStats(now, gcBean.getCollectionCount(), Duration.ofMillis(gcBean.getCollectionTime()));

            LinkedList<GcStats> window = gcStatistics.computeIfAbsent(gcName, anyName -> new LinkedList<>());
            window.addLast(stats);
        }
    }

    private void cleanStatistics(Instant now) {
        Instant oldestToKeep = now.minus(REPORTING_INTERVAL);

        for(Iterator<Map.Entry<String, LinkedList<GcStats>>> it = gcStatistics.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, LinkedList<GcStats>> entry = it.next();
            LinkedList<GcStats> history = entry.getValue();
            while( ! history.isEmpty() && oldestToKeep.isAfter(history.getFirst().when)) {
                history.removeFirst();
            }
            if (history.isEmpty()) {
                it.remove();
            }
        }
    }

    public void emitMetrics(Metric metric) {
        Instant now = clock.instant();

        collectGcStatistics(now);
        cleanStatistics(now);

        for (Map.Entry<String, LinkedList<GcStats>> item : gcStatistics.entrySet()) {
            GcStats reference = item.getValue().getFirst();
            GcStats latest = item.getValue().getLast();
            Map<String, String> contextData = new HashMap<>();
            contextData.put(DIMENSION_KEY, item.getKey());
            Metric.Context gcContext = metric.createContext(contextData);

            metric.set(GC_COUNT, latest.count - reference.count, gcContext);
            metric.set(GC_TIME, latest.totalRuntime.minus(reference.totalRuntime).toMillis(), gcContext);
        }
    }

    // partial exposure for testing
    Map<String, LinkedList<GcStats>> getGcStatistics() {
        return gcStatistics;
    }

}