• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #9678

pending completion
#9678

push

travis_ci

web-flow
add iotdb-doap.rdf back (#10685)

79139 of 164854 relevant lines covered (48.01%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java
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;
21

22
import org.apache.iotdb.commons.service.metric.enums.Tag;
23
import org.apache.iotdb.metrics.AbstractMetricService;
24
import org.apache.iotdb.metrics.MetricConstant;
25
import org.apache.iotdb.metrics.metricsets.IMetricSet;
26
import org.apache.iotdb.metrics.utils.MetricLevel;
27
import org.apache.iotdb.metrics.utils.MetricType;
28
import org.apache.iotdb.metrics.utils.SystemMetric;
29

30
import com.sun.management.OperatingSystemMXBean;
31

32
import java.lang.management.ManagementFactory;
33

34
public class ProcessMetrics implements IMetricSet {
35
  private final OperatingSystemMXBean sunOsMxBean;
36
  private final Runtime runtime;
37
  private static final String PROCESS = "process";
38
  private long lastUpdateTime = 0L;
×
39
  private volatile long processCpuLoad = 0L;
×
40
  private volatile long processCpuTime = 0L;
×
41

42
  public ProcessMetrics() {
×
43
    sunOsMxBean =
×
44
        (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
×
45
    runtime = Runtime.getRuntime();
×
46
  }
×
47

48
  @Override
49
  public void bindTo(AbstractMetricService metricService) {
50
    collectProcessCpuInfo(metricService);
×
51
    collectProcessMemInfo(metricService);
×
52
    collectProcessStatusInfo(metricService);
×
53
    collectThreadInfo(metricService);
×
54
  }
×
55

56
  @Override
57
  public void unbindFrom(AbstractMetricService metricService) {
58
    removeProcessCpuInfo(metricService);
×
59
    removeProcessMemInfo(metricService);
×
60
    removeProcessStatusInfo(metricService);
×
61
    removeThreadInfo(metricService);
×
62
  }
×
63

64
  private void collectProcessCpuInfo(AbstractMetricService metricService) {
65
    metricService.createAutoGauge(
×
66
        SystemMetric.PROCESS_CPU_LOAD.toString(),
×
67
        MetricLevel.CORE,
68
        sunOsMxBean,
69
        a -> {
70
          if (System.currentTimeMillis() - lastUpdateTime > MetricConstant.UPDATE_INTERVAL) {
×
71
            lastUpdateTime = System.currentTimeMillis();
×
72
            processCpuLoad = (long) (sunOsMxBean.getProcessCpuLoad() * 100);
×
73
            processCpuTime = sunOsMxBean.getProcessCpuTime();
×
74
          }
75
          return processCpuLoad;
×
76
        },
77
        Tag.NAME.toString(),
×
78
        PROCESS);
79

80
    metricService.createAutoGauge(
×
81
        SystemMetric.PROCESS_CPU_TIME.toString(),
×
82
        MetricLevel.CORE,
83
        sunOsMxBean,
84
        bean -> {
85
          if (System.currentTimeMillis() - lastUpdateTime > MetricConstant.UPDATE_INTERVAL) {
×
86
            lastUpdateTime = System.currentTimeMillis();
×
87
            processCpuLoad = (long) (sunOsMxBean.getProcessCpuLoad() * 100);
×
88
            processCpuTime = sunOsMxBean.getProcessCpuTime();
×
89
          }
90
          return processCpuTime;
×
91
        },
92
        Tag.NAME.toString(),
×
93
        PROCESS);
94
  }
×
95

96
  private void removeProcessCpuInfo(AbstractMetricService metricService) {
97
    metricService.remove(
×
98
        MetricType.AUTO_GAUGE,
99
        SystemMetric.PROCESS_CPU_LOAD.toString(),
×
100
        Tag.NAME.toString(),
×
101
        PROCESS);
102

103
    metricService.remove(
×
104
        MetricType.AUTO_GAUGE,
105
        SystemMetric.PROCESS_CPU_TIME.toString(),
×
106
        Tag.NAME.toString(),
×
107
        PROCESS);
108
  }
×
109

110
  private void collectProcessMemInfo(AbstractMetricService metricService) {
111
    Runtime runtime = Runtime.getRuntime();
×
112
    metricService.createAutoGauge(
×
113
        SystemMetric.PROCESS_MAX_MEM.toString(),
×
114
        MetricLevel.CORE,
115
        runtime,
116
        a -> runtime.maxMemory(),
×
117
        Tag.NAME.toString(),
×
118
        PROCESS);
119
    metricService.createAutoGauge(
×
120
        SystemMetric.PROCESS_TOTAL_MEM.toString(),
×
121
        MetricLevel.CORE,
122
        runtime,
123
        a -> runtime.totalMemory(),
×
124
        Tag.NAME.toString(),
×
125
        PROCESS);
126
    metricService.createAutoGauge(
×
127
        SystemMetric.PROCESS_FREE_MEM.toString(),
×
128
        MetricLevel.CORE,
129
        runtime,
130
        a -> runtime.freeMemory(),
×
131
        Tag.NAME.toString(),
×
132
        PROCESS);
133
    // TODO maybe following metrics can be removed
134
    metricService.createAutoGauge(
×
135
        SystemMetric.PROCESS_USED_MEM.toString(),
×
136
        MetricLevel.IMPORTANT,
137
        this,
138
        a -> getProcessUsedMemory(),
×
139
        Tag.NAME.toString(),
×
140
        PROCESS);
141
    metricService.createAutoGauge(
×
142
        SystemMetric.PROCESS_MEM_RATIO.toString(),
×
143
        MetricLevel.IMPORTANT,
144
        this,
145
        a -> Math.round(getProcessMemoryRatio()),
×
146
        Tag.NAME.toString(),
×
147
        PROCESS);
148
  }
×
149

150
  private void removeProcessMemInfo(AbstractMetricService metricService) {
151
    metricService.remove(
×
152
        MetricType.AUTO_GAUGE,
153
        SystemMetric.PROCESS_MAX_MEM.toString(),
×
154
        Tag.NAME.toString(),
×
155
        PROCESS);
156
    metricService.remove(
×
157
        MetricType.AUTO_GAUGE,
158
        SystemMetric.PROCESS_TOTAL_MEM.toString(),
×
159
        Tag.NAME.toString(),
×
160
        PROCESS);
161
    metricService.remove(
×
162
        MetricType.AUTO_GAUGE,
163
        SystemMetric.PROCESS_FREE_MEM.toString(),
×
164
        Tag.NAME.toString(),
×
165
        PROCESS);
166
    metricService.remove(
×
167
        MetricType.AUTO_GAUGE,
168
        SystemMetric.PROCESS_USED_MEM.toString(),
×
169
        Tag.NAME.toString(),
×
170
        PROCESS);
171
    metricService.remove(
×
172
        MetricType.AUTO_GAUGE,
173
        SystemMetric.PROCESS_MEM_RATIO.toString(),
×
174
        Tag.NAME.toString(),
×
175
        PROCESS);
176
  }
×
177

178
  private void collectThreadInfo(AbstractMetricService metricService) {
179
    // TODO maybe duplicated with thread info in jvm related metrics
180
    metricService.createAutoGauge(
×
181
        SystemMetric.PROCESS_THREADS_COUNT.toString(),
×
182
        MetricLevel.IMPORTANT,
183
        this,
184
        a -> getThreadsCount(),
×
185
        Tag.NAME.toString(),
×
186
        PROCESS);
187
  }
×
188

189
  private void removeThreadInfo(AbstractMetricService metricService) {
190
    metricService.remove(
×
191
        MetricType.AUTO_GAUGE,
192
        SystemMetric.PROCESS_THREADS_COUNT.toString(),
×
193
        Tag.NAME.toString(),
×
194
        PROCESS);
195
  }
×
196

197
  private void collectProcessStatusInfo(AbstractMetricService metricService) {
198
    metricService.createAutoGauge(
×
199
        SystemMetric.PROCESS_STATUS.toString(),
×
200
        MetricLevel.IMPORTANT,
201
        this,
202
        a -> (getProcessStatus()),
×
203
        Tag.NAME.toString(),
×
204
        PROCESS);
205
  }
×
206

207
  private void removeProcessStatusInfo(AbstractMetricService metricService) {
208
    metricService.remove(
×
209
        MetricType.AUTO_GAUGE,
210
        SystemMetric.PROCESS_STATUS.toString(),
×
211
        Tag.NAME.toString(),
×
212
        PROCESS);
213
  }
×
214

215
  private long getProcessUsedMemory() {
216
    return runtime.totalMemory() - runtime.freeMemory();
×
217
  }
218

219
  private long getProcessStatus() {
220
    return Thread.currentThread().isAlive() ? 1 : 0;
×
221
  }
222

223
  private int getThreadsCount() {
224
    ThreadGroup parentThread = Thread.currentThread().getThreadGroup();
×
225
    while (parentThread.getParent() != null) {
×
226
      parentThread = parentThread.getParent();
×
227
    }
228
    return parentThread.activeCount();
×
229
  }
230

231
  private double getProcessMemoryRatio() {
232
    long processUsedMemory = getProcessUsedMemory();
×
233
    long totalPhysicalMemorySize = sunOsMxBean.getTotalPhysicalMemorySize();
×
234
    return (double) processUsedMemory / (double) totalPhysicalMemorySize * 100;
×
235
  }
236
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc