summaryrefslogtreecommitdiffstats
path: root/container-disc/src/main/java/com/yahoo/container/jdisc/metric/MetricUpdater.java
blob: 7f5ac58054e023676c9f554570d11c0018753c20 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.metric;

import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.jdisc.Metric;
import com.yahoo.jdisc.statistics.ActiveContainerMetrics;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Uses a timer to emit metrics
 * 
 * @author vegardh
 * @since 5.17
 *
 */
public class MetricUpdater extends AbstractComponent {

    @Deprecated private static final String DEPRECATED_FREE_MEMORY_BYTES = "freeMemoryBytes";
    @Deprecated private static final String DEPRECATED_USED_MEMORY_BYTES = "usedMemoryBytes";
    @Deprecated private static final String DEPRECATED_TOTAL_MEMORY_BYTES = "totalMemoryBytes";
    private static final String FREE_MEMORY_BYTES = "mem.heap.free";
    private static final String USED_MEMORY_BYTES = "mem.heap.used";
    private static final String TOTAL_MEMORY_BYTES = "mem.heap.total";
    private static final String MEMORY_MAPPINGS_COUNT = "jdisc.memory_mappings";
    private static final String OPEN_FILE_DESCRIPTORS = "jdisc.open_file_descriptors";

    private final Metric metric;
    private final ActiveContainerMetrics activeContainerMetrics;
    private final Timer timer = new Timer();
    long freeMemory = -1;
    long totalMemory = -1;

    @Inject
    public MetricUpdater(Metric metric, ActiveContainerMetrics activeContainerMetrics) {
        this(metric, activeContainerMetrics, 10*1000);
    }
    
    public MetricUpdater(Metric metric, ActiveContainerMetrics activeContainerMetrics, long delayMillis) {
        this.metric = metric;
        this.activeContainerMetrics = activeContainerMetrics;
        timer.schedule(new UpdaterTask(), delayMillis, delayMillis);
    }
    
    @Override
    public void deconstruct() {
        if (timer!=null) timer.cancel();
    }

    // For testing
    long getFreeMemory() { return freeMemory; }
    long getTotalMemory() { return totalMemory; }

    private class UpdaterTask extends TimerTask {
        @SuppressWarnings("deprecation")
        @Override
        public void run() {
            freeMemory = Runtime.getRuntime().freeMemory();
            totalMemory = Runtime.getRuntime().totalMemory();
            long usedMemory = totalMemory - freeMemory;
            metric.set(DEPRECATED_FREE_MEMORY_BYTES, freeMemory, null);
            metric.set(DEPRECATED_USED_MEMORY_BYTES, usedMemory, null);
            metric.set(DEPRECATED_TOTAL_MEMORY_BYTES, totalMemory, null);
            metric.set(FREE_MEMORY_BYTES, freeMemory, null);
            metric.set(USED_MEMORY_BYTES, usedMemory, null);
            metric.set(TOTAL_MEMORY_BYTES, totalMemory, null);
            metric.set(MEMORY_MAPPINGS_COUNT, count_mappings(), null);
            metric.set(OPEN_FILE_DESCRIPTORS, count_open_files(), null);
            activeContainerMetrics.emitMetrics(metric);
        }

        // Note: Linux-specific
        private long count_mappings() {
            long count = 0;
            try {
                Path p = Paths.get("/proc/self/maps");
                byte[] data = Files.readAllBytes(p);
                for (byte b : data) {
                    if (b == '\n') {
                        ++count;
                    }
                }
            } catch (Exception e) {
                System.err.println("Could not read /proc/self/maps: " + e);
            }
            return count;
        }

        // Note: Linux-specific
        private long count_open_files() {
            long count = 0;
            try {
                Path p = Paths.get("/proc/self/fd");
                try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
                    for (Path entry : stream) {
                        ++count;
                    }
                }
            } catch (Exception e) {
                System.err.println("Could not read /proc/self/fd: " + e);
            }
            return count;
        }
    }
}