|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.service.metrics.memory; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.service.metric.enums.Metric; |
| 23 | +import org.apache.iotdb.commons.service.metric.enums.Tag; |
| 24 | +import org.apache.iotdb.db.conf.IoTDBConfig; |
| 25 | +import org.apache.iotdb.db.conf.IoTDBDescriptor; |
| 26 | +import org.apache.iotdb.db.storageengine.rescon.memory.SystemInfo; |
| 27 | +import org.apache.iotdb.metrics.AbstractMetricService; |
| 28 | +import org.apache.iotdb.metrics.metricsets.IMetricSet; |
| 29 | +import org.apache.iotdb.metrics.utils.MetricLevel; |
| 30 | +import org.apache.iotdb.metrics.utils.MetricType; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collections; |
| 34 | + |
| 35 | +public class GlobalMemoryMetrics implements IMetricSet { |
| 36 | + private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig(); |
| 37 | + private static final SystemInfo systemInfo = SystemInfo.getInstance(); |
| 38 | + |
| 39 | + private static final String TOTAL = "Total"; |
| 40 | + public static final String ON_HEAP = "OnHeap"; |
| 41 | + public static final String OFF_HEAP = "OffHeap"; |
| 42 | + public static final String[] LEVELS = {"0", "1", "2", "3", "4"}; |
| 43 | + |
| 44 | + private static final String DIRECT_BUFFER = "DirectBuffer"; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void bindTo(AbstractMetricService metricService) { |
| 48 | + metricService |
| 49 | + .getOrCreateGauge( |
| 50 | + Metric.MEMORY_THRESHOLD_SIZE.toString(), |
| 51 | + MetricLevel.NORMAL, |
| 52 | + Tag.NAME.toString(), |
| 53 | + TOTAL, |
| 54 | + Tag.TYPE.toString(), |
| 55 | + ON_HEAP, |
| 56 | + Tag.LEVEL.toString(), |
| 57 | + LEVELS[0]) |
| 58 | + .set(Runtime.getRuntime().maxMemory()); |
| 59 | + metricService |
| 60 | + .getOrCreateGauge( |
| 61 | + Metric.MEMORY_THRESHOLD_SIZE.toString(), |
| 62 | + MetricLevel.NORMAL, |
| 63 | + Tag.NAME.toString(), |
| 64 | + TOTAL, |
| 65 | + Tag.TYPE.toString(), |
| 66 | + OFF_HEAP, |
| 67 | + Tag.LEVEL.toString(), |
| 68 | + LEVELS[0]) |
| 69 | + .set(config.getMaxOffHeapMemoryBytes()); |
| 70 | + StorageEngineMemoryMetrics.getInstance().bindTo(metricService); |
| 71 | + QueryEngineMemoryMetrics.getInstance().bindTo(metricService); |
| 72 | + SchemaEngineMemoryMetrics.getInstance().bindTo(metricService); |
| 73 | + ConsensusMemoryMetrics.getInstance().bindTo(metricService); |
| 74 | + StreamEngineMemoryMetrics.getInstance().bindTo(metricService); |
| 75 | + metricService |
| 76 | + .getOrCreateGauge( |
| 77 | + Metric.MEMORY_THRESHOLD_SIZE.toString(), |
| 78 | + MetricLevel.NORMAL, |
| 79 | + Tag.NAME.toString(), |
| 80 | + DIRECT_BUFFER, |
| 81 | + Tag.TYPE.toString(), |
| 82 | + OFF_HEAP, |
| 83 | + Tag.LEVEL.toString(), |
| 84 | + LEVELS[1]) |
| 85 | + .set(systemInfo.getTotalDirectBufferMemorySizeLimit()); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void unbindFrom(AbstractMetricService metricService) { |
| 90 | + Arrays.asList(ON_HEAP, OFF_HEAP) |
| 91 | + .forEach( |
| 92 | + type -> { |
| 93 | + metricService.remove( |
| 94 | + MetricType.GAUGE, |
| 95 | + Metric.MEMORY_THRESHOLD_SIZE.toString(), |
| 96 | + Tag.NAME.toString(), |
| 97 | + TOTAL, |
| 98 | + Tag.TYPE.toString(), |
| 99 | + type, |
| 100 | + Tag.LEVEL.toString(), |
| 101 | + LEVELS[0]); |
| 102 | + }); |
| 103 | + StorageEngineMemoryMetrics.getInstance().unbindFrom(metricService); |
| 104 | + QueryEngineMemoryMetrics.getInstance().unbindFrom(metricService); |
| 105 | + SchemaEngineMemoryMetrics.getInstance().unbindFrom(metricService); |
| 106 | + ConsensusMemoryMetrics.getInstance().unbindFrom(metricService); |
| 107 | + StreamEngineMemoryMetrics.getInstance().unbindFrom(metricService); |
| 108 | + Collections.singletonList(DIRECT_BUFFER) |
| 109 | + .forEach( |
| 110 | + name -> |
| 111 | + metricService.remove( |
| 112 | + MetricType.GAUGE, |
| 113 | + Metric.MEMORY_THRESHOLD_SIZE.toString(), |
| 114 | + Tag.NAME.toString(), |
| 115 | + name, |
| 116 | + Tag.TYPE.toString(), |
| 117 | + OFF_HEAP, |
| 118 | + Tag.LEVEL.toString(), |
| 119 | + LEVELS[1])); |
| 120 | + } |
| 121 | + |
| 122 | + public static GlobalMemoryMetrics getInstance() { |
| 123 | + return GlobalMemoryMetricsHolder.INSTANCE; |
| 124 | + } |
| 125 | + |
| 126 | + private static class GlobalMemoryMetricsHolder { |
| 127 | + |
| 128 | + private static final GlobalMemoryMetrics INSTANCE = new GlobalMemoryMetrics(); |
| 129 | + |
| 130 | + private GlobalMemoryMetricsHolder() {} |
| 131 | + } |
| 132 | +} |
0 commit comments