Coverage Report

Created: 2026-04-09 13:35

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
6
JvmMetrics::JvmMetrics(MetricRegistry* registry) {
83
6
    DCHECK(registry != nullptr);
84
6
    _registry = registry;
85
86
6
    _server_entity = _registry->register_entity("server");
87
6
    DCHECK(_server_entity != nullptr);
88
89
6
    do {
90
6
        if (!doris::config::enable_jvm_monitor) {
91
0
            break;
92
0
        }
93
6
        try {
94
6
            Status st = _jvm_stats.init();
95
6
            if (!st) {
96
0
                LOG(WARNING) << "jvm Stats Init Fail. " << st.to_string();
97
0
                break;
98
0
            }
99
6
        } catch (...) {
100
0
            LOG(WARNING) << "jvm Stats Throw Exception Init Fail.";
101
0
            break;
102
0
        }
103
6
        if (!_jvm_stats.init_complete()) {
104
0
            break;
105
0
        }
106
6
        _server_entity->register_hook(_s_hook_name, std::bind(&JvmMetrics::update, this));
107
6
    } while (false);
108
109
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_heap_size_bytes_max);
110
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_heap_size_bytes_committed);
111
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_heap_size_bytes_used);
112
113
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_non_heap_size_bytes_used);
114
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_non_heap_size_bytes_committed);
115
116
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_young_size_bytes_used);
117
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_young_size_bytes_peak_used);
118
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_young_size_bytes_max);
119
120
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_old_size_bytes_used);
121
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_old_size_bytes_peak_used);
122
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_old_size_bytes_max);
123
124
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_count);
125
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_peak_count);
126
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_new_count);
127
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_runnable_count);
128
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_blocked_count);
129
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_waiting_count);
130
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_timed_waiting_count);
131
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_thread_terminated_count);
132
133
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_young_generation_count);
134
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_young_generation_time_ms);
135
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_old_generation_count);
136
6
    INT_GAUGE_METRIC_REGISTER(_server_entity, jvm_gc_g1_old_generation_time_ms);
137
6
}
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
634
void JvmMetrics::update() {
146
    // If enable_jvm_monitor is false, the jvm stats object is not initialized. call jvm_stats.refresh() may core.
147
634
    if (!doris::config::enable_jvm_monitor) {
148
0
        return;
149
0
    }
150
634
    static long fail_count = 0;
151
634
    if (fail_count >= 30) {
152
0
        return;
153
0
    }
154
155
634
    try {
156
634
        Status st = _jvm_stats.refresh(this);
157
634
        if (!st) {
158
0
            fail_count++;
159
0
            LOG(WARNING) << "Jvm Stats update Fail! " << st.to_string();
160
634
        } else {
161
634
            fail_count = 0;
162
634
        }
163
634
    } 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
634
    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
634
}
201
202
6
Status JvmStats::init() {
203
6
    JNIEnv* env = nullptr;
204
6
    RETURN_IF_ERROR(Jni::Env::Get(&env));
205
206
6
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/management/ManagementFactory",
207
6
                                          &_managementFactoryClass));
208
6
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(
209
6
            env, "getMemoryMXBean", "()Ljava/lang/management/MemoryMXBean;",
210
6
            &_getMemoryMXBeanMethod));
211
212
6
    RETURN_IF_ERROR(
213
6
            Jni::Util::find_class(env, "java/lang/management/MemoryUsage", &_memoryUsageClass));
214
6
    RETURN_IF_ERROR(
215
6
            _memoryUsageClass.get_method(env, "getUsed", "()J", &_getMemoryUsageUsedMethod));
216
6
    RETURN_IF_ERROR(_memoryUsageClass.get_method(env, "getCommitted", "()J",
217
6
                                                 &_getMemoryUsageCommittedMethod));
218
6
    RETURN_IF_ERROR(_memoryUsageClass.get_method(env, "getMax", "()J", &_getMemoryUsageMaxMethod));
219
220
6
    RETURN_IF_ERROR(
221
6
            Jni::Util::find_class(env, "java/lang/management/MemoryMXBean", &_memoryMXBeanClass));
222
6
    RETURN_IF_ERROR(_memoryMXBeanClass.get_method(env, "getHeapMemoryUsage",
223
6
                                                  "()Ljava/lang/management/MemoryUsage;",
224
6
                                                  &_getHeapMemoryUsageMethod));
225
6
    RETURN_IF_ERROR(_memoryMXBeanClass.get_method(env, "getNonHeapMemoryUsage",
226
6
                                                  "()Ljava/lang/management/MemoryUsage;",
227
6
                                                  &_getNonHeapMemoryUsageMethod));
228
229
6
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(
230
6
            env, "getMemoryPoolMXBeans", "()Ljava/util/List;", &_getMemoryPoolMXBeansMethod));
231
232
6
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/util/List", &_listClass));
233
6
    RETURN_IF_ERROR(_listClass.get_method(env, "size", "()I", &_getListSizeMethod));
234
6
    RETURN_IF_ERROR(
235
6
            _listClass.get_method(env, "get", "(I)Ljava/lang/Object;", &_getListUseIndexMethod));
236
237
6
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/management/MemoryPoolMXBean",
238
6
                                          &_memoryPoolMXBeanClass));
239
6
    RETURN_IF_ERROR(_memoryPoolMXBeanClass.get_method(env, "getUsage",
240
6
                                                      "()Ljava/lang/management/MemoryUsage;",
241
6
                                                      &_getMemoryPoolMXBeanUsageMethod));
242
6
    RETURN_IF_ERROR(_memoryPoolMXBeanClass.get_method(env, "getPeakUsage",
243
6
                                                      "()Ljava/lang/management/MemoryUsage;",
244
6
                                                      &_getMemoryPoolMXBeanPeakMethod));
245
6
    RETURN_IF_ERROR(_memoryPoolMXBeanClass.get_method(env, "getName", "()Ljava/lang/String;",
246
6
                                                      &_getMemoryPoolMXBeanNameMethod));
247
248
6
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(
249
6
            env, "getThreadMXBean", "()Ljava/lang/management/ThreadMXBean;",
250
6
            &_getThreadMXBeanMethod));
251
6
    RETURN_IF_ERROR(_managementFactoryClass.get_static_method(env, "getGarbageCollectorMXBeans",
252
6
                                                              "()Ljava/util/List;",
253
6
                                                              &_getGarbageCollectorMXBeansMethod));
254
255
6
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/management/GarbageCollectorMXBean",
256
6
                                          &_garbageCollectorMXBeanClass));
257
6
    RETURN_IF_ERROR(_garbageCollectorMXBeanClass.get_method(env, "getName", "()Ljava/lang/String;",
258
6
                                                            &_getGCNameMethod));
259
6
    RETURN_IF_ERROR(_garbageCollectorMXBeanClass.get_method(env, "getCollectionCount", "()J",
260
6
                                                            &_getGCCollectionCountMethod));
261
6
    RETURN_IF_ERROR(_garbageCollectorMXBeanClass.get_method(env, "getCollectionTime", "()J",
262
6
                                                            &_getGCCollectionTimeMethod));
263
264
6
    RETURN_IF_ERROR(
265
6
            Jni::Util::find_class(env, "java/lang/management/ThreadMXBean", &_threadMXBeanClass));
266
6
    RETURN_IF_ERROR(
267
6
            _threadMXBeanClass.get_method(env, "getAllThreadIds", "()[J", &_getAllThreadIdsMethod));
268
6
    RETURN_IF_ERROR(_threadMXBeanClass.get_method(env, "getThreadInfo",
269
6
                                                  "([JI)[Ljava/lang/management/ThreadInfo;",
270
6
                                                  &_getThreadInfoMethod));
271
6
    RETURN_IF_ERROR(_threadMXBeanClass.get_method(env, "getPeakThreadCount", "()I",
272
6
                                                  &_getPeakThreadCountMethod));
273
274
6
    RETURN_IF_ERROR(
275
6
            Jni::Util::find_class(env, "java/lang/management/ThreadInfo", &_threadInfoClass));
276
6
    RETURN_IF_ERROR(_threadInfoClass.get_method(env, "getThreadState", "()Ljava/lang/Thread$State;",
277
6
                                                &_getThreadStateMethod));
278
279
6
    RETURN_IF_ERROR(Jni::Util::find_class(env, "java/lang/Thread$State", &_threadStateClass));
280
6
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
281
6
            env, "NEW", "Ljava/lang/Thread$State;", &_newThreadStateObj));
282
6
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
283
6
            env, "RUNNABLE", "Ljava/lang/Thread$State;", &_runnableThreadStateObj));
284
6
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
285
6
            env, "BLOCKED", "Ljava/lang/Thread$State;", &_blockedThreadStateObj));
286
6
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
287
6
            env, "WAITING", "Ljava/lang/Thread$State;", &_waitingThreadStateObj));
288
6
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
289
6
            env, "TIMED_WAITING", "Ljava/lang/Thread$State;", &_timedWaitingThreadStateObj));
290
6
    RETURN_IF_ERROR(_threadStateClass.get_static_object_field(
291
6
            env, "TERMINATED", "Ljava/lang/Thread$State;", &_terminatedThreadStateObj));
292
293
6
    _init_complete = true;
294
6
    LOG(INFO) << "Start JVM monitoring.";
295
6
    return Status::OK();
296
6
}
297
298
634
Status JvmStats::refresh(JvmMetrics* jvm_metrics) const {
299
634
    if (!_init_complete) {
300
0
        return Status::InternalError("Jvm Stats not init complete.");
301
0
    }
302
303
634
    JNIEnv* env = nullptr;
304
634
    RETURN_IF_ERROR(Jni::Env::Get(&env));
305
306
634
    Jni::LocalObject memoryMXBeanObj;
307
634
    RETURN_IF_ERROR(_managementFactoryClass.call_static_object_method(env, _getMemoryMXBeanMethod)
308
634
                            .call(&memoryMXBeanObj));
309
310
634
    Jni::LocalObject heapMemoryUsageObj;
311
634
    RETURN_IF_ERROR(memoryMXBeanObj.call_object_method(env, _getHeapMemoryUsageMethod)
312
634
                            .call(&heapMemoryUsageObj));
313
314
634
    jlong heapMemoryUsed = 0;
315
634
    RETURN_IF_ERROR(heapMemoryUsageObj.call_long_method(env, _getMemoryUsageUsedMethod)
316
634
                            .call(&heapMemoryUsed));
317
318
634
    jlong heapMemoryCommitted = 0;
319
634
    RETURN_IF_ERROR(heapMemoryUsageObj.call_long_method(env, _getMemoryUsageCommittedMethod)
320
634
                            .call(&heapMemoryCommitted));
321
322
634
    jlong heapMemoryMax = 0;
323
634
    RETURN_IF_ERROR(heapMemoryUsageObj.call_long_method(env, _getMemoryUsageMaxMethod)
324
634
                            .call(&heapMemoryMax));
325
326
634
    jvm_metrics->jvm_heap_size_bytes_used->set_value(heapMemoryUsed < 0 ? 0 : heapMemoryUsed);
327
634
    jvm_metrics->jvm_heap_size_bytes_committed->set_value(
328
634
            heapMemoryCommitted < 0 ? 0 : heapMemoryCommitted);
329
634
    jvm_metrics->jvm_heap_size_bytes_max->set_value(heapMemoryMax < 0 ? 0 : heapMemoryMax);
330
331
634
    Jni::LocalObject nonHeapMemoryUsageObj;
332
634
    RETURN_IF_ERROR(memoryMXBeanObj.call_object_method(env, _getNonHeapMemoryUsageMethod)
333
634
                            .call(&nonHeapMemoryUsageObj));
334
335
634
    jlong nonHeapMemoryCommitted = 0;
336
634
    RETURN_IF_ERROR(nonHeapMemoryUsageObj.call_long_method(env, _getMemoryUsageCommittedMethod)
337
634
                            .call(&nonHeapMemoryCommitted));
338
339
634
    jlong nonHeapMemoryUsed = 0;
340
634
    RETURN_IF_ERROR(nonHeapMemoryUsageObj.call_long_method(env, _getMemoryUsageUsedMethod)
341
634
                            .call(&nonHeapMemoryUsed));
342
343
634
    jvm_metrics->jvm_non_heap_size_bytes_committed->set_value(
344
634
            nonHeapMemoryCommitted < 0 ? 0 : nonHeapMemoryCommitted);
345
634
    jvm_metrics->jvm_non_heap_size_bytes_used->set_value(nonHeapMemoryUsed < 0 ? 0
346
634
                                                                               : nonHeapMemoryUsed);
347
348
634
    Jni::LocalObject memoryPoolMXBeansList;
349
634
    RETURN_IF_ERROR(
350
634
            _managementFactoryClass.call_static_object_method(env, _getMemoryPoolMXBeansMethod)
351
634
                    .call(&memoryPoolMXBeansList));
352
353
634
    jint beanSize = 0;
354
634
    RETURN_IF_ERROR(memoryPoolMXBeansList.call_int_method(env, _getListSizeMethod).call(&beanSize));
355
356
5.70k
    for (int i = 0; i < beanSize; ++i) {
357
5.07k
        Jni::LocalObject memoryPoolMXBean;
358
5.07k
        RETURN_IF_ERROR(memoryPoolMXBeansList.call_object_method(env, _getListUseIndexMethod)
359
5.07k
                                .with_arg(i)
360
5.07k
                                .call(&memoryPoolMXBean));
361
362
5.07k
        Jni::LocalObject usageObject;
363
5.07k
        RETURN_IF_ERROR(memoryPoolMXBean.call_object_method(env, _getMemoryPoolMXBeanUsageMethod)
364
5.07k
                                .call(&usageObject));
365
366
5.07k
        jlong used = 0;
367
5.07k
        RETURN_IF_ERROR(usageObject.call_long_method(env, _getMemoryUsageUsedMethod).call(&used));
368
369
5.07k
        jlong max = 0;
370
5.07k
        RETURN_IF_ERROR(usageObject.call_long_method(env, _getMemoryUsageMaxMethod).call(&max));
371
372
5.07k
        Jni::LocalObject peakUsageObject;
373
5.07k
        RETURN_IF_ERROR(memoryPoolMXBean.call_object_method(env, _getMemoryPoolMXBeanPeakMethod)
374
5.07k
                                .call(&peakUsageObject));
375
376
5.07k
        jlong peakUsed = 0;
377
5.07k
        RETURN_IF_ERROR(
378
5.07k
                peakUsageObject.call_long_method(env, _getMemoryUsageUsedMethod).call(&peakUsed));
379
380
5.07k
        Jni::LocalString name;
381
5.07k
        RETURN_IF_ERROR(memoryPoolMXBean.call_object_method(env, _getMemoryPoolMXBeanNameMethod)
382
5.07k
                                .call(&name));
383
384
5.07k
        Jni::LocalStringBufferGuard nameStr;
385
5.07k
        RETURN_IF_ERROR(name.get_string_chars(env, &nameStr));
386
5.07k
        if (nameStr.get() != nullptr) {
387
5.07k
            auto it = _memoryPoolName.find(nameStr.get());
388
5.07k
            if (it == _memoryPoolName.end()) {
389
3.17k
                continue;
390
3.17k
            }
391
1.90k
            if (it->second == memoryPoolNameEnum::YOUNG) {
392
634
                jvm_metrics->jvm_young_size_bytes_used->set_value(used < 0 ? 0 : used);
393
634
                jvm_metrics->jvm_young_size_bytes_peak_used->set_value(peakUsed < 0 ? 0 : peakUsed);
394
634
                jvm_metrics->jvm_young_size_bytes_max->set_value(max < 0 ? 0 : max);
395
396
1.26k
            } else if (it->second == memoryPoolNameEnum::OLD) {
397
634
                jvm_metrics->jvm_old_size_bytes_used->set_value(used < 0 ? 0 : used);
398
634
                jvm_metrics->jvm_old_size_bytes_peak_used->set_value(peakUsed < 0 ? 0 : peakUsed);
399
634
                jvm_metrics->jvm_old_size_bytes_max->set_value(max < 0 ? 0 : max);
400
634
            }
401
1.90k
        }
402
5.07k
    }
403
404
634
    Jni::LocalObject threadMXBean;
405
634
    RETURN_IF_ERROR(_managementFactoryClass.call_static_object_method(env, _getThreadMXBeanMethod)
406
634
                            .call(&threadMXBean));
407
408
634
    Jni::LocalArray threadIds;
409
634
    RETURN_IF_ERROR(threadMXBean.call_object_method(env, _getAllThreadIdsMethod).call(&threadIds));
410
411
634
    jsize threadCount = 0;
412
634
    RETURN_IF_ERROR(threadIds.get_length(env, &threadCount));
413
414
634
    Jni::LocalArray threadInfos;
415
634
    RETURN_IF_ERROR(threadMXBean.call_object_method(env, _getThreadInfoMethod)
416
634
                            .with_arg(threadIds)
417
634
                            .with_arg(0)
418
634
                            .call(&threadInfos));
419
420
634
    int threadsNew = 0, threadsRunnable = 0, threadsBlocked = 0, threadsWaiting = 0,
421
634
        threadsTimedWaiting = 0, threadsTerminated = 0;
422
423
634
    jint peakThreadCount = 0;
424
634
    RETURN_IF_ERROR(
425
634
            threadMXBean.call_int_method(env, _getPeakThreadCountMethod).call(&peakThreadCount));
426
427
634
    jvm_metrics->jvm_thread_peak_count->set_value(peakThreadCount < 0 ? 0 : peakThreadCount);
428
634
    jvm_metrics->jvm_thread_count->set_value(threadCount < 0 ? 0 : threadCount);
429
430
61.4k
    for (int i = 0; i < threadCount; i++) {
431
60.7k
        Jni::LocalObject threadInfo;
432
60.7k
        RETURN_IF_ERROR(threadInfos.get_object_array_element(env, i, &threadInfo));
433
60.7k
        if (threadInfo.uninitialized()) {
434
0
            continue;
435
0
        }
436
60.7k
        Jni::LocalObject threadState;
437
60.7k
        RETURN_IF_ERROR(
438
60.7k
                threadInfo.call_object_method(env, _getThreadStateMethod).call(&threadState));
439
440
60.7k
        if (threadState.equal(env, _newThreadStateObj)) {
441
0
            threadsNew++;
442
60.7k
        } else if (threadState.equal(env, _runnableThreadStateObj)) {
443
57.9k
            threadsRunnable++;
444
57.9k
        } else if (threadState.equal(env, _blockedThreadStateObj)) {
445
0
            threadsBlocked++;
446
2.83k
        } else if (threadState.equal(env, _waitingThreadStateObj)) {
447
652
            threadsWaiting++;
448
2.18k
        } else if (threadState.equal(env, _timedWaitingThreadStateObj)) {
449
2.18k
            threadsTimedWaiting++;
450
2.18k
        } else if (threadState.equal(env, _terminatedThreadStateObj)) {
451
0
            threadsTerminated++;
452
0
        }
453
60.7k
    }
454
455
634
    jvm_metrics->jvm_thread_new_count->set_value(threadsNew < 0 ? 0 : threadsNew);
456
634
    jvm_metrics->jvm_thread_runnable_count->set_value(threadsRunnable < 0 ? 0 : threadsRunnable);
457
634
    jvm_metrics->jvm_thread_blocked_count->set_value(threadsBlocked < 0 ? 0 : threadsBlocked);
458
634
    jvm_metrics->jvm_thread_waiting_count->set_value(threadsWaiting < 0 ? 0 : threadsWaiting);
459
634
    jvm_metrics->jvm_thread_timed_waiting_count->set_value(
460
634
            threadsTimedWaiting < 0 ? 0 : threadsTimedWaiting);
461
634
    jvm_metrics->jvm_thread_terminated_count->set_value(threadsTerminated < 0 ? 0
462
634
                                                                              : threadsTerminated);
463
464
634
    Jni::LocalObject gcMXBeansList;
465
634
    RETURN_IF_ERROR(_managementFactoryClass
466
634
                            .call_static_object_method(env, _getGarbageCollectorMXBeansMethod)
467
634
                            .call(&gcMXBeansList));
468
634
    jint numCollectors = 0;
469
634
    RETURN_IF_ERROR(gcMXBeansList.call_int_method(env, _getListSizeMethod).call(&numCollectors));
470
471
1.90k
    for (int i = 0; i < numCollectors; i++) {
472
1.26k
        Jni::LocalObject gcMXBean;
473
1.26k
        RETURN_IF_ERROR(gcMXBeansList.call_object_method(env, _getListUseIndexMethod)
474
1.26k
                                .with_arg(i)
475
1.26k
                                .call(&gcMXBean));
476
477
1.26k
        Jni::LocalString gcName;
478
1.26k
        RETURN_IF_ERROR(gcMXBean.call_object_method(env, _getGCNameMethod).call(&gcName));
479
480
1.26k
        jlong gcCollectionCount = 0;
481
1.26k
        RETURN_IF_ERROR(gcMXBean.call_long_method(env, _getGCCollectionCountMethod)
482
1.26k
                                .call(&gcCollectionCount));
483
484
1.26k
        jlong gcCollectionTime = 0;
485
1.26k
        RETURN_IF_ERROR(
486
1.26k
                gcMXBean.call_long_method(env, _getGCCollectionTimeMethod).call(&gcCollectionTime));
487
488
1.26k
        Jni::LocalStringBufferGuard gcNameStr;
489
1.26k
        RETURN_IF_ERROR(gcName.get_string_chars(env, &gcNameStr));
490
491
1.26k
        if (gcNameStr.get() != nullptr) {
492
1.26k
            if (strcmp(gcNameStr.get(), "G1 Young Generation") == 0) {
493
634
                jvm_metrics->jvm_gc_g1_young_generation_count->set_value(gcCollectionCount);
494
634
                jvm_metrics->jvm_gc_g1_young_generation_time_ms->set_value(gcCollectionTime);
495
496
634
            } else {
497
634
                jvm_metrics->jvm_gc_g1_old_generation_count->set_value(gcCollectionCount);
498
634
                jvm_metrics->jvm_gc_g1_old_generation_time_ms->set_value(gcCollectionTime);
499
634
            }
500
1.26k
        }
501
1.26k
    }
502
503
634
    return Status::OK();
504
634
}
505
2
JvmStats::~JvmStats() {}
506
507
} // namespace doris