Coverage Report

Created: 2026-03-23 10:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/common/metrics/jvm_metrics.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "common/metrics/jvm_metrics.h"
19
20
#include <functional>
21
22
#include "common/config.h"
23
#include "common/metrics/metrics.h"
24
#include "util/defer_op.h"
25
#include "util/jni-util.h"
26
27
namespace doris {
28
29
#define DEFINE_JVM_SIZE_BYTES_METRIC(name, type)                                     \
30
    DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(name##_##type, MetricUnit::BYTES, "", name, \
31
                                         Labels({{"type", #type}}));
32
33
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_heap_size_bytes, max);
34
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_heap_size_bytes, committed);
35
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_heap_size_bytes, used);
36
37
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_non_heap_size_bytes, used);
38
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_non_heap_size_bytes, committed);
39
40
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_young_size_bytes, used);
41
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_young_size_bytes, peak_used);
42
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_young_size_bytes, max);
43
44
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_old_size_bytes, used);
45
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_old_size_bytes, peak_used);
46
DEFINE_JVM_SIZE_BYTES_METRIC(jvm_old_size_bytes, max);
47
48
#define DEFINE_JVM_THREAD_METRIC(type)                                                          \
49
    DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(jvm_thread_##type, MetricUnit::NOUNIT, "", jvm_thread, \
50
                                         Labels({{"type", #type}}));
51
52
DEFINE_JVM_THREAD_METRIC(count);
53
DEFINE_JVM_THREAD_METRIC(peak_count);
54
DEFINE_JVM_THREAD_METRIC(new_count);
55
DEFINE_JVM_THREAD_METRIC(runnable_count);
56
DEFINE_JVM_THREAD_METRIC(blocked_count);
57
DEFINE_JVM_THREAD_METRIC(waiting_count);
58
DEFINE_JVM_THREAD_METRIC(timed_waiting_count);
59
DEFINE_JVM_THREAD_METRIC(terminated_count);
60
61
DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(jvm_gc_g1_young_generation_count, MetricUnit::NOUNIT, "",
62
                                     jvm_gc,
63
                                     Labels({{"name", "G1 Young generation Count"},
64
                                             {"type", "count"}}));
65
66
DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(jvm_gc_g1_young_generation_time_ms, MetricUnit::MILLISECONDS,
67
                                     "", jvm_gc,
68
                                     Labels({{"name", "G1 Young generation Time"},
69
                                             {"type", "time"}}));
70
71
DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(jvm_gc_g1_old_generation_count, MetricUnit::NOUNIT, "", jvm_gc,
72
                                     Labels({{"name", "G1 Old generation Count"},
73
                                             {"type", "count"}}));
74
75
DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(jvm_gc_g1_old_generation_time_ms, MetricUnit::MILLISECONDS, "",
76
                                     jvm_gc,
77
                                     Labels({{"name", "G1 Old generation Time"},
78
                                             {"type", "time"}}));
79
80
const char* JvmMetrics::_s_hook_name = "jvm_metrics";
81
82
5
JvmMetrics::JvmMetrics(MetricRegistry* registry) {
83
5
    DCHECK(registry != nullptr);
84
5
    _registry = registry;
85
86
5
    _server_entity = _registry->register_entity("server");
87
5
    DCHECK(_server_entity != nullptr);
88
89
5
    do {
90
5
        if (!doris::config::enable_jvm_monitor) {
91
0
            break;
92
0
        }
93
5
        try {
94
5
            Status st = _jvm_stats.init();
95
5
            if (!st) {
96
0
                LOG(WARNING) << "jvm Stats Init Fail. " << st.to_string();
97
0
                break;
98
0
            }
99
5
        } catch (...) {
100
0
            LOG(WARNING) << "jvm Stats Throw Exception Init Fail.";
101
0
            break;
102
0
        }
103
5
        if (!_jvm_stats.init_complete()) {
104
0
            break;
105
0
        }
106
5
        _server_entity->register_hook(_s_hook_name, std::bind(&JvmMetrics::update, this));
107
5
    } while (false);
108
109
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_heap_size_bytes_max);
110
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_heap_size_bytes_committed);
111
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_heap_size_bytes_used);
112
113
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_non_heap_size_bytes_used);
114
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_non_heap_size_bytes_committed);
115
116
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_young_size_bytes_used);
117
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_young_size_bytes_peak_used);
118
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_young_size_bytes_max);
119
120
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_old_size_bytes_used);
121
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_old_size_bytes_peak_used);
122
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_old_size_bytes_max);
123
124
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_count);
125
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_peak_count);
126
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_new_count);
127
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_runnable_count);
128
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_blocked_count);
129
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_waiting_count);
130
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_timed_waiting_count);
131
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_terminated_count);
132
133
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_young_generation_count);
134
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_young_generation_time_ms);
135
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_old_generation_count);
136
5
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_old_generation_time_ms);
137
5
}
138
139
2
JvmMetrics::~JvmMetrics() {
140
2
    if (_jvm_stats.init_complete()) {
141
2
        _server_entity->deregister_hook(_s_hook_name);
142
2
    }
143
2
}
144
145
696
void JvmMetrics::update() {
146
    // If enable_jvm_monitor is false, the jvm stats object is not initialized. call jvm_stats.refresh() may core.
147
696
    if (!doris::config::enable_jvm_monitor) {
148
0
        return;
149
0
    }
150
696
    static long fail_count = 0;
151
696
    if (fail_count >= 30) {
152
0
        return;
153
0
    }
154
155
696
    try {
156
696
        Status st = _jvm_stats.refresh(this);
157
696
        if (!st) {
158
0
            fail_count++;
159
0
            LOG(WARNING) << "Jvm Stats update Fail! " << st.to_string();
160
696
        } else {
161
696
            fail_count = 0;
162
696
        }
163
696
    } catch (...) {
164
0
        LOG(WARNING) << "Jvm Stats update throw Exception!";
165
0
        fail_count++;
166
0
    }
167
168
    //When 30 consecutive exceptions occur, turn off jvm information collection.
169
696
    if (fail_count >= 30) {
170
0
        LOG(WARNING) << "Jvm Stats CLOSE!";
171
0
        jvm_heap_size_bytes_max->set_value(0);
172
0
        jvm_heap_size_bytes_committed->set_value(0);
173
0
        jvm_heap_size_bytes_used->set_value(0);
174
175
0
        jvm_non_heap_size_bytes_used->set_value(0);
176
0
        jvm_non_heap_size_bytes_committed->set_value(0);
177
178
0
        jvm_young_size_bytes_used->set_value(0);
179
0
        jvm_young_size_bytes_peak_used->set_value(0);
180
0
        jvm_young_size_bytes_max->set_value(0);
181
182
0
        jvm_old_size_bytes_used->set_value(0);
183
0
        jvm_old_size_bytes_peak_used->set_value(0);
184
0
        jvm_old_size_bytes_max->set_value(0);
185
186
0
        jvm_thread_count->set_value(0);
187
0
        jvm_thread_peak_count->set_value(0);
188
0
        jvm_thread_new_count->set_value(0);
189
0
        jvm_thread_runnable_count->set_value(0);
190
0
        jvm_thread_blocked_count->set_value(0);
191
0
        jvm_thread_waiting_count->set_value(0);
192
0
        jvm_thread_timed_waiting_count->set_value(0);
193
0
        jvm_thread_terminated_count->set_value(0);
194
195
0
        jvm_gc_g1_young_generation_count->set_value(0);
196
0
        jvm_gc_g1_young_generation_time_ms->set_value(0);
197
0
        jvm_gc_g1_old_generation_count->set_value(0);
198
0
        jvm_gc_g1_old_generation_time_ms->set_value(0);
199
0
    }
200
696
}
201
202
5
Status JvmStats::init() {
203
5
    JNIEnv* env = nullptr;
204
5
    RETURN_IF_ERROR(Jni::Env::Get(&env));
205
206
5
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/management/ManagementFactory",
207
5
                                          &_managementFactoryClass));
208
5
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(
209
5
            env, "getMemoryMXBean", "()Ljava/lang/management/MemoryMXBean;",
210
5
            &_getMemoryMXBeanMethod));
211
212
5
    RETURN_IF_ERROR(
213
5
            Jni::Util::find_class(env, "java/lang/management/MemoryUsage", &_memoryUsageClass));
214
5
    RETURN_IF_ERROR(
215
5
            _memoryUsageClass.get_method(env, "getUsed", "()J", &_getMemoryUsageUsedMethod));
216
5
    RETURN_IF_ERROR(_memoryUsageClass.get_method(env, "getCommitted", "()J",
217
5
                                                 &_getMemoryUsageCommittedMethod));
218
5
    RETURN_IF_ERROR(_memoryUsageClass.get_method(env, "getMax", "()J", &_getMemoryUsageMaxMethod));
219
220
5
    RETURN_IF_ERROR(
221
5
            Jni::Util::find_class(env, "java/lang/management/MemoryMXBean", &_memoryMXBeanClass));
222
5
    RETURN_IF_ERROR(_memoryMXBeanClass.get_method(env, "getHeapMemoryUsage",
223
5
                                                  "()Ljava/lang/management/MemoryUsage;",
224
5
                                                  &_getHeapMemoryUsageMethod));
225
5
    RETURN_IF_ERROR(_memoryMXBeanClass.get_method(env, "getNonHeapMemoryUsage",
226
5
                                                  "()Ljava/lang/management/MemoryUsage;",
227
5
                                                  &_getNonHeapMemoryUsageMethod));
228
229
5
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(
230
5
            env, "getMemoryPoolMXBeans", "()Ljava/util/List;", &_getMemoryPoolMXBeansMethod));
231
232
5
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/util/List", &_listClass));
233
5
    RETURN_IF_ERROR(_listClass.get_method(env, "size", "()I", &_getListSizeMethod));
234
5
    RETURN_IF_ERROR(
235
5
            _listClass.get_method(env, "get", "(I)Ljava/lang/Object;", &_getListUseIndexMethod));
236
237
5
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/management/MemoryPoolMXBean",
238
5
                                          &_memoryPoolMXBeanClass));
239
5
    RETURN_IF_ERROR(_memoryPoolMXBeanClass.get_method(env, "getUsage",
240
5
                                                      "()Ljava/lang/management/MemoryUsage;",
241
5
                                                      &_getMemoryPoolMXBeanUsageMethod));
242
5
    RETURN_IF_ERROR(_memoryPoolMXBeanClass.get_method(env, "getPeakUsage",
243
5
                                                      "()Ljava/lang/management/MemoryUsage;",
244
5
                                                      &_getMemoryPoolMXBeanPeakMethod));
245
5
    RETURN_IF_ERROR(_memoryPoolMXBeanClass.get_method(env, "getName", "()Ljava/lang/String;",
246
5
                                                      &_getMemoryPoolMXBeanNameMethod));
247
248
5
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(
249
5
            env, "getThreadMXBean", "()Ljava/lang/management/ThreadMXBean;",
250
5
            &_getThreadMXBeanMethod));
251
5
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(env, "getGarbageCollectorMXBeans",
252
5
                                                              "()Ljava/util/List;",
253
5
                                                              &_getGarbageCollectorMXBeansMethod));
254
255
5
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/management/GarbageCollectorMXBean",
256
5
                                          &_garbageCollectorMXBeanClass));
257
5
    RETURN_IF_ERROR(_garbageCollectorMXBeanClass.get_method(env, "getName", "()Ljava/lang/String;",
258
5
                                                            &_getGCNameMethod));
259
5
    RETURN_IF_ERROR(_garbageCollectorMXBeanClass.get_method(env, "getCollectionCount", "()J",
260
5
                                                            &_getGCCollectionCountMethod));
261
5
    RETURN_IF_ERROR(_garbageCollectorMXBeanClass.get_method(env, "getCollectionTime", "()J",
262
5
                                                            &_getGCCollectionTimeMethod));
263
264
5
    RETURN_IF_ERROR(
265
5
            Jni::Util::find_class(env, "java/lang/management/ThreadMXBean", &_threadMXBeanClass));
266
5
    RETURN_IF_ERROR(
267
5
            _threadMXBeanClass.get_method(env, "getAllThreadIds", "()[J", &_getAllThreadIdsMethod));
268
5
    RETURN_IF_ERROR(_threadMXBeanClass.get_method(env, "getThreadInfo",
269
5
                                                  "([JI)[Ljava/lang/management/ThreadInfo;",
270
5
                                                  &_getThreadInfoMethod));
271
5
    RETURN_IF_ERROR(_threadMXBeanClass.get_method(env, "getPeakThreadCount", "()I",
272
5
                                                  &_getPeakThreadCountMethod));
273
274
5
    RETURN_IF_ERROR(
275
5
            Jni::Util::find_class(env, "java/lang/management/ThreadInfo", &_threadInfoClass));
276
5
    RETURN_IF_ERROR(_threadInfoClass.get_method(env, "getThreadState", "()Ljava/lang/Thread$State;",
277
5
                                                &_getThreadStateMethod));
278
279
5
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/Thread$State", &_threadStateClass));
280
5
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
281
5
            env, "NEW", "Ljava/lang/Thread$State;", &_newThreadStateObj));
282
5
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
283
5
            env, "RUNNABLE", "Ljava/lang/Thread$State;", &_runnableThreadStateObj));
284
5
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
285
5
            env, "BLOCKED", "Ljava/lang/Thread$State;", &_blockedThreadStateObj));
286
5
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
287
5
            env, "WAITING", "Ljava/lang/Thread$State;", &_waitingThreadStateObj));
288
5
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
289
5
            env, "TIMED_WAITING", "Ljava/lang/Thread$State;", &_timedWaitingThreadStateObj));
290
5
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
291
5
            env, "TERMINATED", "Ljava/lang/Thread$State;", &_terminatedThreadStateObj));
292
293
5
    _init_complete = true;
294
5
    LOG(INFO) << "Start JVM monitoring.";
295
5
    return Status::OK();
296
5
}
297
298
696
Status JvmStats::refresh(JvmMetrics* jvm_metrics) const {
299
696
    if (!_init_complete) {
300
0
        return Status::InternalError("Jvm Stats not init complete.");
301
0
    }
302
303
696
    JNIEnv* env = nullptr;
304
696
    RETURN_IF_ERROR(Jni::Env::Get(&env));
305
306
696
    Jni::LocalObject memoryMXBeanObj;
307
696
    RETURN_IF_ERROR(_managementFactoryClass.call_static_object_method(env, _getMemoryMXBeanMethod)
308
696
                            .call(&memoryMXBeanObj));
309
310
696
    Jni::LocalObject heapMemoryUsageObj;
311
696
    RETURN_IF_ERROR(memoryMXBeanObj.call_object_method(env, _getHeapMemoryUsageMethod)
312
696
                            .call(&heapMemoryUsageObj));
313
314
696
    jlong heapMemoryUsed = 0;
315
696
    RETURN_IF_ERROR(heapMemoryUsageObj.call_long_method(env, _getMemoryUsageUsedMethod)
316
696
                            .call(&heapMemoryUsed));
317
318
696
    jlong heapMemoryCommitted = 0;
319
696
    RETURN_IF_ERROR(heapMemoryUsageObj.call_long_method(env, _getMemoryUsageCommittedMethod)
320
696
                            .call(&heapMemoryCommitted));
321
322
696
    jlong heapMemoryMax = 0;
323
696
    RETURN_IF_ERROR(heapMemoryUsageObj.call_long_method(env, _getMemoryUsageMaxMethod)
324
696
                            .call(&heapMemoryMax));
325
326
696
    jvm_metrics->jvm_heap_size_bytes_used->set_value(heapMemoryUsed < 0 ? 0 : heapMemoryUsed);
327
696
    jvm_metrics->jvm_heap_size_bytes_committed->set_value(
328
696
            heapMemoryCommitted < 0 ? 0 : heapMemoryCommitted);
329
696
    jvm_metrics->jvm_heap_size_bytes_max->set_value(heapMemoryMax < 0 ? 0 : heapMemoryMax);
330
331
696
    Jni::LocalObject nonHeapMemoryUsageObj;
332
696
    RETURN_IF_ERROR(memoryMXBeanObj.call_object_method(env, _getNonHeapMemoryUsageMethod)
333
696
                            .call(&nonHeapMemoryUsageObj));
334
335
696
    jlong nonHeapMemoryCommitted = 0;
336
696
    RETURN_IF_ERROR(nonHeapMemoryUsageObj.call_long_method(env, _getMemoryUsageCommittedMethod)
337
696
                            .call(&nonHeapMemoryCommitted));
338
339
696
    jlong nonHeapMemoryUsed = 0;
340
696
    RETURN_IF_ERROR(nonHeapMemoryUsageObj.call_long_method(env, _getMemoryUsageUsedMethod)
341
696
                            .call(&nonHeapMemoryUsed));
342
343
696
    jvm_metrics->jvm_non_heap_size_bytes_committed->set_value(
344
696
            nonHeapMemoryCommitted < 0 ? 0 : nonHeapMemoryCommitted);
345
696
    jvm_metrics->jvm_non_heap_size_bytes_used->set_value(nonHeapMemoryUsed < 0 ? 0
346
696
                                                                               : nonHeapMemoryUsed);
347
348
696
    Jni::LocalObject memoryPoolMXBeansList;
349
696
    RETURN_IF_ERROR(
350
696
            _managementFactoryClass.call_static_object_method(env, _getMemoryPoolMXBeansMethod)
351
696
                    .call(&memoryPoolMXBeansList));
352
353
696
    jint beanSize = 0;
354
696
    RETURN_IF_ERROR(memoryPoolMXBeansList.call_int_method(env, _getListSizeMethod).call(&beanSize));
355
356
6.26k
    for (int i = 0; i < beanSize; ++i) {
357
5.56k
        Jni::LocalObject memoryPoolMXBean;
358
5.56k
        RETURN_IF_ERROR(memoryPoolMXBeansList.call_object_method(env, _getListUseIndexMethod)
359
5.56k
                                .with_arg(i)
360
5.56k
                                .call(&memoryPoolMXBean));
361
362
5.56k
        Jni::LocalObject usageObject;
363
5.56k
        RETURN_IF_ERROR(memoryPoolMXBean.call_object_method(env, _getMemoryPoolMXBeanUsageMethod)
364
5.56k
                                .call(&usageObject));
365
366
5.56k
        jlong used = 0;
367
5.56k
        RETURN_IF_ERROR(usageObject.call_long_method(env, _getMemoryUsageUsedMethod).call(&used));
368
369
5.56k
        jlong max = 0;
370
5.56k
        RETURN_IF_ERROR(usageObject.call_long_method(env, _getMemoryUsageMaxMethod).call(&max));
371
372
5.56k
        Jni::LocalObject peakUsageObject;
373
5.56k
        RETURN_IF_ERROR(memoryPoolMXBean.call_object_method(env, _getMemoryPoolMXBeanPeakMethod)
374
5.56k
                                .call(&peakUsageObject));
375
376
5.56k
        jlong peakUsed = 0;
377
5.56k
        RETURN_IF_ERROR(
378
5.56k
                peakUsageObject.call_long_method(env, _getMemoryUsageUsedMethod).call(&peakUsed));
379
380
5.56k
        Jni::LocalString name;
381
5.56k
        RETURN_IF_ERROR(memoryPoolMXBean.call_object_method(env, _getMemoryPoolMXBeanNameMethod)
382
5.56k
                                .call(&name));
383
384
5.56k
        Jni::LocalStringBufferGuard nameStr;
385
5.56k
        RETURN_IF_ERROR(name.get_string_chars(env, &nameStr));
386
5.56k
        if (nameStr.get() != nullptr) {
387
5.56k
            auto it = _memoryPoolName.find(nameStr.get());
388
5.56k
            if (it == _memoryPoolName.end()) {
389
3.48k
                continue;
390
3.48k
            }
391
2.08k
            if (it->second == memoryPoolNameEnum::YOUNG) {
392
696
                jvm_metrics->jvm_young_size_bytes_used->set_value(used < 0 ? 0 : used);
393
696
                jvm_metrics->jvm_young_size_bytes_peak_used->set_value(peakUsed < 0 ? 0 : peakUsed);
394
696
                jvm_metrics->jvm_young_size_bytes_max->set_value(max < 0 ? 0 : max);
395
396
1.39k
            } else if (it->second == memoryPoolNameEnum::OLD) {
397
696
                jvm_metrics->jvm_old_size_bytes_used->set_value(used < 0 ? 0 : used);
398
696
                jvm_metrics->jvm_old_size_bytes_peak_used->set_value(peakUsed < 0 ? 0 : peakUsed);
399
696
                jvm_metrics->jvm_old_size_bytes_max->set_value(max < 0 ? 0 : max);
400
696
            }
401
2.08k
        }
402
5.56k
    }
403
404
696
    Jni::LocalObject threadMXBean;
405
696
    RETURN_IF_ERROR(_managementFactoryClass.call_static_object_method(env, _getThreadMXBeanMethod)
406
696
                            .call(&threadMXBean));
407
408
696
    Jni::LocalArray threadIds;
409
696
    RETURN_IF_ERROR(threadMXBean.call_object_method(env, _getAllThreadIdsMethod).call(&threadIds));
410
411
696
    jsize threadCount = 0;
412
696
    RETURN_IF_ERROR(threadIds.get_length(env, &threadCount));
413
414
696
    Jni::LocalArray threadInfos;
415
696
    RETURN_IF_ERROR(threadMXBean.call_object_method(env, _getThreadInfoMethod)
416
696
                            .with_arg(threadIds)
417
696
                            .with_arg(0)
418
696
                            .call(&threadInfos));
419
420
696
    int threadsNew = 0, threadsRunnable = 0, threadsBlocked = 0, threadsWaiting = 0,
421
696
        threadsTimedWaiting = 0, threadsTerminated = 0;
422
423
696
    jint peakThreadCount = 0;
424
696
    RETURN_IF_ERROR(
425
696
            threadMXBean.call_int_method(env, _getPeakThreadCountMethod).call(&peakThreadCount));
426
427
696
    jvm_metrics->jvm_thread_peak_count->set_value(peakThreadCount < 0 ? 0 : peakThreadCount);
428
696
    jvm_metrics->jvm_thread_count->set_value(threadCount < 0 ? 0 : threadCount);
429
430
67.1k
    for (int i = 0; i < threadCount; i++) {
431
66.4k
        Jni::LocalObject threadInfo;
432
66.4k
        RETURN_IF_ERROR(threadInfos.get_object_array_element(env, i, &threadInfo));
433
66.4k
        if (threadInfo.uninitialized()) {
434
0
            continue;
435
0
        }
436
66.4k
        Jni::LocalObject threadState;
437
66.4k
        RETURN_IF_ERROR(
438
66.4k
                threadInfo.call_object_method(env, _getThreadStateMethod).call(&threadState));
439
440
66.4k
        if (threadState.equal(env, _newThreadStateObj)) {
441
0
            threadsNew++;
442
66.4k
        } else if (threadState.equal(env, _runnableThreadStateObj)) {
443
63.0k
            threadsRunnable++;
444
63.0k
        } else if (threadState.equal(env, _blockedThreadStateObj)) {
445
0
            threadsBlocked++;
446
3.44k
        } else if (threadState.equal(env, _waitingThreadStateObj)) {
447
731
            threadsWaiting++;
448
2.71k
        } else if (threadState.equal(env, _timedWaitingThreadStateObj)) {
449
2.71k
            threadsTimedWaiting++;
450
2.71k
        } else if (threadState.equal(env, _terminatedThreadStateObj)) {
451
0
            threadsTerminated++;
452
0
        }
453
66.4k
    }
454
455
696
    jvm_metrics->jvm_thread_new_count->set_value(threadsNew < 0 ? 0 : threadsNew);
456
696
    jvm_metrics->jvm_thread_runnable_count->set_value(threadsRunnable < 0 ? 0 : threadsRunnable);
457
696
    jvm_metrics->jvm_thread_blocked_count->set_value(threadsBlocked < 0 ? 0 : threadsBlocked);
458
696
    jvm_metrics->jvm_thread_waiting_count->set_value(threadsWaiting < 0 ? 0 : threadsWaiting);
459
696
    jvm_metrics->jvm_thread_timed_waiting_count->set_value(
460
696
            threadsTimedWaiting < 0 ? 0 : threadsTimedWaiting);
461
696
    jvm_metrics->jvm_thread_terminated_count->set_value(threadsTerminated < 0 ? 0
462
696
                                                                              : threadsTerminated);
463
464
696
    Jni::LocalObject gcMXBeansList;
465
696
    RETURN_IF_ERROR(_managementFactoryClass
466
696
                            .call_static_object_method(env, _getGarbageCollectorMXBeansMethod)
467
696
                            .call(&gcMXBeansList));
468
696
    jint numCollectors = 0;
469
696
    RETURN_IF_ERROR(gcMXBeansList.call_int_method(env, _getListSizeMethod).call(&numCollectors));
470
471
2.08k
    for (int i = 0; i < numCollectors; i++) {
472
1.39k
        Jni::LocalObject gcMXBean;
473
1.39k
        RETURN_IF_ERROR(gcMXBeansList.call_object_method(env, _getListUseIndexMethod)
474
1.39k
                                .with_arg(i)
475
1.39k
                                .call(&gcMXBean));
476
477
1.39k
        Jni::LocalString gcName;
478
1.39k
        RETURN_IF_ERROR(gcMXBean.call_object_method(env, _getGCNameMethod).call(&gcName));
479
480
1.39k
        jlong gcCollectionCount = 0;
481
1.39k
        RETURN_IF_ERROR(gcMXBean.call_long_method(env, _getGCCollectionCountMethod)
482
1.39k
                                .call(&gcCollectionCount));
483
484
1.39k
        jlong gcCollectionTime = 0;
485
1.39k
        RETURN_IF_ERROR(
486
1.39k
                gcMXBean.call_long_method(env, _getGCCollectionTimeMethod).call(&gcCollectionTime));
487
488
1.39k
        Jni::LocalStringBufferGuard gcNameStr;
489
1.39k
        RETURN_IF_ERROR(gcName.get_string_chars(env, &gcNameStr));
490
491
1.39k
        if (gcNameStr.get() != nullptr) {
492
1.39k
            if (strcmp(gcNameStr.get(), "G1 Young Generation") == 0) {
493
696
                jvm_metrics->jvm_gc_g1_young_generation_count->set_value(gcCollectionCount);
494
696
                jvm_metrics->jvm_gc_g1_young_generation_time_ms->set_value(gcCollectionTime);
495
496
696
            } else {
497
696
                jvm_metrics->jvm_gc_g1_old_generation_count->set_value(gcCollectionCount);
498
696
                jvm_metrics->jvm_gc_g1_old_generation_time_ms->set_value(gcCollectionTime);
499
696
            }
500
1.39k
        }
501
1.39k
    }
502
503
696
    return Status::OK();
504
696
}
505
2
JvmStats::~JvmStats() {}
506
507
} // namespace doris