Coverage Report

Created: 2026-03-13 03:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/common/metrics/system_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/system_metrics.h"
19
20
#include <absl/strings/str_split.h>
21
#include <glog/logging.h>
22
23
#include <functional>
24
#include <ostream>
25
#include <unordered_map>
26
#include <utility>
27
28
#include "common/cast_set.h"
29
#include "runtime/memory/jemalloc_control.h"
30
#include "util/cgroup_util.h"
31
#include "util/perf_counters.h"
32
33
namespace doris {
34
#include "common/compile_check_begin.h"
35
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(avail_cpu_num, MetricUnit::NOUNIT);
36
37
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(host_cpu_num, MetricUnit::NOUNIT);
38
struct CpuNumberMetrics {
39
5
    CpuNumberMetrics(MetricEntity* ent) : entity(ent) {
40
5
        INT_COUNTER_METRIC_REGISTER(entity, host_cpu_num);
41
5
        INT_COUNTER_METRIC_REGISTER(entity, avail_cpu_num);
42
5
    }
43
44
    IntCounter* host_cpu_num {nullptr};
45
    IntCounter* avail_cpu_num {nullptr};
46
    MetricEntity* entity = nullptr;
47
};
48
49
#define DEFINE_CPU_COUNTER_METRIC(metric)                                            \
50
    DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(cpu_##metric, MetricUnit::PERCENT, "", cpu, \
51
                                         Labels({{"mode", #metric}}));
52
DEFINE_CPU_COUNTER_METRIC(user);
53
DEFINE_CPU_COUNTER_METRIC(nice);
54
DEFINE_CPU_COUNTER_METRIC(system);
55
DEFINE_CPU_COUNTER_METRIC(idle);
56
DEFINE_CPU_COUNTER_METRIC(iowait);
57
DEFINE_CPU_COUNTER_METRIC(irq);
58
DEFINE_CPU_COUNTER_METRIC(soft_irq);
59
DEFINE_CPU_COUNTER_METRIC(steal);
60
DEFINE_CPU_COUNTER_METRIC(guest);
61
DEFINE_CPU_COUNTER_METRIC(guest_nice);
62
63
// /proc/stat: http://www.linuxhowtos.org/System/procstat.htm
64
struct CpuMetrics {
65
84
    CpuMetrics(MetricEntity* ent) : entity(ent) {
66
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_user);
67
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_nice);
68
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_system);
69
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_idle);
70
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_iowait);
71
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_irq);
72
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_soft_irq);
73
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_steal);
74
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_guest);
75
84
        INT_COUNTER_METRIC_REGISTER(entity, cpu_guest_nice);
76
77
84
        metrics[0] = cpu_user;
78
84
        metrics[1] = cpu_nice;
79
84
        metrics[2] = cpu_system;
80
84
        metrics[3] = cpu_idle;
81
84
        metrics[4] = cpu_iowait;
82
84
        metrics[5] = cpu_irq;
83
84
        metrics[6] = cpu_soft_irq;
84
84
        metrics[7] = cpu_steal;
85
84
        metrics[8] = cpu_guest;
86
84
        metrics[9] = cpu_guest_nice;
87
84
    }
88
89
    static constexpr int cpu_num_metrics = 10;
90
91
    MetricEntity* entity = nullptr;
92
    IntCounter* cpu_user;
93
    IntCounter* cpu_nice;
94
    IntCounter* cpu_system;
95
    IntCounter* cpu_idle;
96
    IntCounter* cpu_iowait;
97
    IntCounter* cpu_irq;
98
    IntCounter* cpu_soft_irq;
99
    IntCounter* cpu_steal;
100
    IntCounter* cpu_guest;
101
    IntCounter* cpu_guest_nice;
102
103
    IntCounter* metrics[cpu_num_metrics];
104
};
105
106
#define DEFINE_MEMORY_GAUGE_METRIC(metric, unit) \
107
    DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(memory_##metric, unit);
108
DEFINE_MEMORY_GAUGE_METRIC(allocated_bytes, MetricUnit::BYTES);
109
DEFINE_MEMORY_GAUGE_METRIC(pgpgin, MetricUnit::NOUNIT);
110
DEFINE_MEMORY_GAUGE_METRIC(pgpgout, MetricUnit::NOUNIT);
111
DEFINE_MEMORY_GAUGE_METRIC(pswpin, MetricUnit::NOUNIT);
112
DEFINE_MEMORY_GAUGE_METRIC(pswpout, MetricUnit::NOUNIT);
113
#ifndef USE_JEMALLOC
114
DEFINE_MEMORY_GAUGE_METRIC(tcmalloc_allocated_bytes, MetricUnit::BYTES);
115
DEFINE_MEMORY_GAUGE_METRIC(tcmalloc_total_thread_cache_bytes, MetricUnit::BYTES);
116
DEFINE_MEMORY_GAUGE_METRIC(tcmalloc_central_cache_free_bytes, MetricUnit::BYTES);
117
DEFINE_MEMORY_GAUGE_METRIC(tcmalloc_transfer_cache_free_bytes, MetricUnit::BYTES);
118
DEFINE_MEMORY_GAUGE_METRIC(tcmalloc_thread_cache_free_bytes, MetricUnit::BYTES);
119
DEFINE_MEMORY_GAUGE_METRIC(tcmalloc_pageheap_free_bytes, MetricUnit::BYTES);
120
DEFINE_MEMORY_GAUGE_METRIC(tcmalloc_pageheap_unmapped_bytes, MetricUnit::BYTES);
121
#else
122
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_allocated_bytes, MetricUnit::BYTES);
123
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_active_bytes, MetricUnit::BYTES);
124
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_metadata_bytes, MetricUnit::BYTES);
125
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_resident_bytes, MetricUnit::BYTES);
126
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_mapped_bytes, MetricUnit::BYTES);
127
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_retained_bytes, MetricUnit::BYTES);
128
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_tcache_bytes, MetricUnit::BYTES);
129
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_pactive_num, MetricUnit::NOUNIT);
130
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_pdirty_num, MetricUnit::NOUNIT);
131
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_pmuzzy_num, MetricUnit::NOUNIT);
132
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_dirty_purged_num, MetricUnit::NOUNIT);
133
DEFINE_MEMORY_GAUGE_METRIC(jemalloc_muzzy_purged_num, MetricUnit::NOUNIT);
134
#endif
135
136
struct MemoryMetrics {
137
5
    MemoryMetrics(MetricEntity* ent) : entity(ent) {
138
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_allocated_bytes);
139
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_pgpgin);
140
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_pgpgout);
141
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_pswpin);
142
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_pswpout);
143
144
5
#ifndef USE_JEMALLOC
145
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_tcmalloc_allocated_bytes);
146
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_tcmalloc_total_thread_cache_bytes);
147
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_tcmalloc_central_cache_free_bytes);
148
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_tcmalloc_transfer_cache_free_bytes);
149
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_tcmalloc_thread_cache_free_bytes);
150
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_tcmalloc_pageheap_free_bytes);
151
5
        INT_GAUGE_METRIC_REGISTER(entity, memory_tcmalloc_pageheap_unmapped_bytes);
152
#else
153
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_allocated_bytes);
154
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_active_bytes);
155
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_metadata_bytes);
156
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_resident_bytes);
157
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_mapped_bytes);
158
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_retained_bytes);
159
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_tcache_bytes);
160
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_pactive_num);
161
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_pdirty_num);
162
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_pmuzzy_num);
163
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_dirty_purged_num);
164
        INT_GAUGE_METRIC_REGISTER(entity, memory_jemalloc_muzzy_purged_num);
165
#endif
166
5
    }
167
168
    MetricEntity* entity = nullptr;
169
    IntGauge* memory_allocated_bytes;
170
    IntGauge* memory_pgpgin;
171
    IntGauge* memory_pgpgout;
172
    IntGauge* memory_pswpin;
173
    IntGauge* memory_pswpout;
174
175
#ifndef USE_JEMALLOC
176
    IntGauge* memory_tcmalloc_allocated_bytes;
177
    IntGauge* memory_tcmalloc_total_thread_cache_bytes;
178
    IntGauge* memory_tcmalloc_central_cache_free_bytes;
179
    IntGauge* memory_tcmalloc_transfer_cache_free_bytes;
180
    IntGauge* memory_tcmalloc_thread_cache_free_bytes;
181
    IntGauge* memory_tcmalloc_pageheap_free_bytes;
182
    IntGauge* memory_tcmalloc_pageheap_unmapped_bytes;
183
#else
184
    IntGauge* memory_jemalloc_allocated_bytes;
185
    IntGauge* memory_jemalloc_active_bytes;
186
    IntGauge* memory_jemalloc_metadata_bytes;
187
    IntGauge* memory_jemalloc_resident_bytes;
188
    IntGauge* memory_jemalloc_mapped_bytes;
189
    IntGauge* memory_jemalloc_retained_bytes;
190
    IntGauge* memory_jemalloc_tcache_bytes;
191
    IntGauge* memory_jemalloc_pactive_num;
192
    IntGauge* memory_jemalloc_pdirty_num;
193
    IntGauge* memory_jemalloc_pmuzzy_num;
194
    IntGauge* memory_jemalloc_dirty_purged_num;
195
    IntGauge* memory_jemalloc_muzzy_purged_num;
196
#endif
197
};
198
199
#define DEFINE_DISK_COUNTER_METRIC(metric, unit) \
200
    DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(disk_##metric, unit);
201
DEFINE_DISK_COUNTER_METRIC(reads_completed, MetricUnit::OPERATIONS);
202
DEFINE_DISK_COUNTER_METRIC(bytes_read, MetricUnit::BYTES);
203
DEFINE_DISK_COUNTER_METRIC(read_time_ms, MetricUnit::MILLISECONDS);
204
DEFINE_DISK_COUNTER_METRIC(writes_completed, MetricUnit::OPERATIONS);
205
DEFINE_DISK_COUNTER_METRIC(bytes_written, MetricUnit::BYTES);
206
DEFINE_DISK_COUNTER_METRIC(write_time_ms, MetricUnit::MILLISECONDS);
207
DEFINE_DISK_COUNTER_METRIC(io_time_ms, MetricUnit::MILLISECONDS);
208
DEFINE_DISK_COUNTER_METRIC(io_time_weigthed, MetricUnit::MILLISECONDS);
209
210
struct DiskMetrics {
211
5
    DiskMetrics(MetricEntity* ent) : entity(ent) {
212
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_reads_completed);
213
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_bytes_read);
214
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_read_time_ms);
215
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_writes_completed);
216
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_bytes_written);
217
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_write_time_ms);
218
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_io_time_ms);
219
5
        INT_COUNTER_METRIC_REGISTER(entity, disk_io_time_weigthed);
220
5
    }
221
222
    MetricEntity* entity = nullptr;
223
    IntCounter* disk_reads_completed;
224
    IntCounter* disk_bytes_read;
225
    IntCounter* disk_read_time_ms;
226
    IntCounter* disk_writes_completed;
227
    IntCounter* disk_bytes_written;
228
    IntCounter* disk_write_time_ms;
229
    IntCounter* disk_io_time_ms;
230
    IntCounter* disk_io_time_weigthed;
231
};
232
233
#define DEFINE_NETWORK_COUNTER_METRIC(metric, unit) \
234
    DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(network_##metric, unit);
235
DEFINE_NETWORK_COUNTER_METRIC(receive_bytes, MetricUnit::BYTES);
236
DEFINE_NETWORK_COUNTER_METRIC(receive_packets, MetricUnit::PACKETS);
237
DEFINE_NETWORK_COUNTER_METRIC(send_bytes, MetricUnit::BYTES);
238
DEFINE_NETWORK_COUNTER_METRIC(send_packets, MetricUnit::PACKETS);
239
240
struct NetworkMetrics {
241
18
    NetworkMetrics(MetricEntity* ent) : entity(ent) {
242
18
        INT_COUNTER_METRIC_REGISTER(entity, network_receive_bytes);
243
18
        INT_COUNTER_METRIC_REGISTER(entity, network_receive_packets);
244
18
        INT_COUNTER_METRIC_REGISTER(entity, network_send_bytes);
245
18
        INT_COUNTER_METRIC_REGISTER(entity, network_send_packets);
246
18
    }
247
248
    MetricEntity* entity = nullptr;
249
    IntCounter* network_receive_bytes;
250
    IntCounter* network_receive_packets;
251
    IntCounter* network_send_bytes;
252
    IntCounter* network_send_packets;
253
};
254
255
#define DEFINE_SNMP_COUNTER_METRIC(metric, unit, desc) \
256
    DEFINE_COUNTER_METRIC_PROTOTYPE_3ARG(snmp_##metric, unit, desc);
257
DEFINE_SNMP_COUNTER_METRIC(tcp_in_errs, MetricUnit::NOUNIT,
258
                           "The number of all problematic TCP packets received");
259
DEFINE_SNMP_COUNTER_METRIC(tcp_retrans_segs, MetricUnit::NOUNIT, "All TCP packets retransmitted");
260
DEFINE_SNMP_COUNTER_METRIC(tcp_in_segs, MetricUnit::NOUNIT, "All received TCP packets");
261
DEFINE_SNMP_COUNTER_METRIC(tcp_out_segs, MetricUnit::NOUNIT, "All send TCP packets with RST mark");
262
263
// metrics read from /proc/net/snmp
264
struct SnmpMetrics {
265
5
    SnmpMetrics(MetricEntity* ent) : entity(ent) {
266
5
        INT_COUNTER_METRIC_REGISTER(entity, snmp_tcp_in_errs);
267
5
        INT_COUNTER_METRIC_REGISTER(entity, snmp_tcp_retrans_segs);
268
5
        INT_COUNTER_METRIC_REGISTER(entity, snmp_tcp_in_segs);
269
5
        INT_COUNTER_METRIC_REGISTER(entity, snmp_tcp_out_segs);
270
5
    }
271
272
    MetricEntity* entity = nullptr;
273
    IntCounter* snmp_tcp_in_errs;
274
    IntCounter* snmp_tcp_retrans_segs;
275
    IntCounter* snmp_tcp_in_segs;
276
    IntCounter* snmp_tcp_out_segs;
277
};
278
279
#define DEFINE_FD_COUNTER_METRIC(metric, unit) \
280
    DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(fd_##metric, unit);
281
DEFINE_FD_COUNTER_METRIC(num_limit, MetricUnit::NOUNIT);
282
DEFINE_FD_COUNTER_METRIC(num_used, MetricUnit::NOUNIT);
283
284
struct FileDescriptorMetrics {
285
5
    FileDescriptorMetrics(MetricEntity* ent) : entity(ent) {
286
5
        INT_GAUGE_METRIC_REGISTER(entity, fd_num_limit);
287
5
        INT_GAUGE_METRIC_REGISTER(entity, fd_num_used);
288
5
    }
289
290
    MetricEntity* entity = nullptr;
291
    IntGauge* fd_num_limit;
292
    IntGauge* fd_num_used;
293
};
294
295
#define DEFINE_LOAD_AVERAGE_DOUBLE_METRIC(metric)                                     \
296
    DEFINE_GAUGE_METRIC_PROTOTYPE_5ARG(load_average_##metric, MetricUnit::NOUNIT, "", \
297
                                       load_average, Labels({{"mode", #metric}}));
298
DEFINE_LOAD_AVERAGE_DOUBLE_METRIC(1_minutes);
299
DEFINE_LOAD_AVERAGE_DOUBLE_METRIC(5_minutes);
300
DEFINE_LOAD_AVERAGE_DOUBLE_METRIC(15_minutes);
301
302
struct LoadAverageMetrics {
303
5
    LoadAverageMetrics(MetricEntity* ent) : entity(ent) {
304
5
        DOUBLE_GAUGE_METRIC_REGISTER(entity, load_average_1_minutes);
305
5
        DOUBLE_GAUGE_METRIC_REGISTER(entity, load_average_5_minutes);
306
5
        DOUBLE_GAUGE_METRIC_REGISTER(entity, load_average_15_minutes);
307
5
    }
308
309
    MetricEntity* entity = nullptr;
310
    DoubleGauge* load_average_1_minutes;
311
    DoubleGauge* load_average_5_minutes;
312
    DoubleGauge* load_average_15_minutes;
313
};
314
315
#define DEFINE_PROC_STAT_COUNTER_METRIC(metric)                                       \
316
    DEFINE_COUNTER_METRIC_PROTOTYPE_5ARG(proc_##metric, MetricUnit::NOUNIT, "", proc, \
317
                                         Labels({{"mode", #metric}}));
318
DEFINE_PROC_STAT_COUNTER_METRIC(interrupt);
319
DEFINE_PROC_STAT_COUNTER_METRIC(ctxt_switch);
320
DEFINE_PROC_STAT_COUNTER_METRIC(procs_running);
321
DEFINE_PROC_STAT_COUNTER_METRIC(procs_blocked);
322
323
struct ProcMetrics {
324
5
    ProcMetrics(MetricEntity* ent) : entity(ent) {
325
5
        INT_COUNTER_METRIC_REGISTER(entity, proc_interrupt);
326
5
        INT_COUNTER_METRIC_REGISTER(entity, proc_ctxt_switch);
327
5
        INT_COUNTER_METRIC_REGISTER(entity, proc_procs_running);
328
5
        INT_COUNTER_METRIC_REGISTER(entity, proc_procs_blocked);
329
5
    }
330
331
    MetricEntity* entity = nullptr;
332
333
    IntCounter* proc_interrupt;
334
    IntCounter* proc_ctxt_switch;
335
    IntCounter* proc_procs_running;
336
    IntCounter* proc_procs_blocked;
337
};
338
339
DEFINE_GAUGE_CORE_METRIC_PROTOTYPE_2ARG(max_disk_io_util_percent, MetricUnit::PERCENT);
340
DEFINE_GAUGE_CORE_METRIC_PROTOTYPE_2ARG(max_network_send_bytes_rate, MetricUnit::BYTES);
341
DEFINE_GAUGE_CORE_METRIC_PROTOTYPE_2ARG(max_network_receive_bytes_rate, MetricUnit::BYTES);
342
343
const char* SystemMetrics::_s_hook_name = "system_metrics";
344
345
SystemMetrics::SystemMetrics(MetricRegistry* registry, const std::set<std::string>& disk_devices,
346
5
                             const std::vector<std::string>& network_interfaces) {
347
5
    DCHECK(registry != nullptr);
348
5
    _registry = registry;
349
5
    _server_entity = _registry->register_entity("server");
350
5
    DCHECK(_server_entity != nullptr);
351
5
    _server_entity->register_hook(_s_hook_name, std::bind(&SystemMetrics::update, this));
352
5
    _install_cpu_metrics();
353
5
    _install_memory_metrics(_server_entity.get());
354
5
    _install_disk_metrics(disk_devices);
355
5
    _install_net_metrics(network_interfaces);
356
5
    _install_fd_metrics(_server_entity.get());
357
5
    _install_snmp_metrics(_server_entity.get());
358
5
    _install_load_avg_metrics(_server_entity.get());
359
5
    _install_proc_metrics(_server_entity.get());
360
361
5
    INT_GAUGE_METRIC_REGISTER(_server_entity.get(), max_disk_io_util_percent);
362
5
    INT_GAUGE_METRIC_REGISTER(_server_entity.get(), max_network_send_bytes_rate);
363
5
    INT_GAUGE_METRIC_REGISTER(_server_entity.get(), max_network_receive_bytes_rate);
364
5
}
365
366
3
SystemMetrics::~SystemMetrics() {
367
3
    DCHECK(_server_entity != nullptr);
368
3
    _server_entity->deregister_hook(_s_hook_name);
369
370
42
    for (auto& it : _cpu_metrics) {
371
42
        delete it.second;
372
42
    }
373
3
    for (auto& it : _disk_metrics) {
374
3
        delete it.second;
375
3
    }
376
8
    for (auto& it : _network_metrics) {
377
8
        delete it.second;
378
8
    }
379
3
    if (_line_ptr != nullptr) {
380
2
        free(_line_ptr);
381
2
    }
382
3
}
383
384
330
void SystemMetrics::update() {
385
330
    _update_cpu_metrics();
386
330
    _update_memory_metrics();
387
330
    _update_disk_metrics();
388
330
    _update_net_metrics();
389
330
    _update_fd_metrics();
390
330
    _update_snmp_metrics();
391
330
    _update_load_avg_metrics();
392
330
    _update_proc_metrics();
393
330
}
394
395
5
void SystemMetrics::_install_cpu_metrics() {
396
5
    get_cpu_name();
397
398
5
    int cpu_num = 0;
399
84
    for (auto cpu_name : _cpu_names) {
400
        // NOTE: cpu_name comes from /proc/stat which named 'cpu' is not a real cpu name, it should be skipped.
401
84
        if (cpu_name != "cpu") {
402
80
            cpu_num++;
403
80
        }
404
84
        auto cpu_entity = _registry->register_entity(cpu_name, {{"device", cpu_name}});
405
84
        CpuMetrics* metrics = new CpuMetrics(cpu_entity.get());
406
84
        _cpu_metrics.emplace(cpu_name, metrics);
407
84
    }
408
409
5
    auto cpu_num_entity = _registry->register_entity("doris_be_host_cpu_num");
410
5
    _cpu_num_metrics = std::make_unique<CpuNumberMetrics>(cpu_num_entity.get());
411
412
5
    _cpu_num_metrics->host_cpu_num->set_value(cpu_num);
413
5
}
414
415
#ifdef BE_TEST
416
const char* k_ut_stat_path;
417
const char* k_ut_diskstats_path;
418
const char* k_ut_net_dev_path;
419
const char* k_ut_fd_path;
420
const char* k_ut_net_snmp_path;
421
const char* k_ut_load_avg_path;
422
const char* k_ut_vmstat_path;
423
#endif
424
425
330
void SystemMetrics::_update_cpu_metrics() {
426
#ifdef BE_TEST
427
    FILE* fp = fopen(k_ut_stat_path, "r");
428
#else
429
330
    FILE* fp = fopen("/proc/stat", "r");
430
330
#endif
431
330
    if (fp == nullptr) {
432
0
        char buf[64];
433
0
        LOG(WARNING) << "open /proc/stat failed, errno=" << errno
434
0
                     << ", message=" << strerror_r(errno, buf, 64);
435
0
        return;
436
0
    }
437
438
12.5k
    while (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
439
12.1k
        char cpu[16];
440
12.1k
        int64_t values[CpuMetrics::cpu_num_metrics];
441
12.1k
        memset(values, 0, sizeof(values));
442
12.1k
        int num = sscanf(_line_ptr,
443
12.1k
                         "%15s"
444
12.1k
                         " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64
445
12.1k
                         " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
446
12.1k
                         cpu, &values[0], &values[1], &values[2], &values[3], &values[4],
447
12.1k
                         &values[5], &values[6], &values[7], &values[8], &values[9]);
448
12.1k
        if (num < 4) {
449
1.65k
            continue;
450
1.65k
        }
451
452
10.5k
        std::string cpu_name(cpu);
453
10.5k
        auto it = _cpu_metrics.find(cpu_name);
454
10.5k
        if (it == _cpu_metrics.end()) {
455
660
            continue;
456
660
        }
457
458
108k
        for (int i = 0; i < CpuMetrics::cpu_num_metrics; ++i) {
459
98.8k
            it->second->metrics[i]->set_value(values[i]);
460
98.8k
        }
461
9.88k
    }
462
463
330
    if (ferror(fp) != 0) {
464
0
        char buf[64];
465
0
        LOG(WARNING) << "getline failed, errno=" << errno
466
0
                     << ", message=" << strerror_r(errno, buf, 64);
467
0
    }
468
469
330
    fclose(fp);
470
330
}
471
472
5
void SystemMetrics::_install_memory_metrics(MetricEntity* entity) {
473
5
    _memory_metrics.reset(new MemoryMetrics(entity));
474
5
}
475
476
330
void SystemMetrics::_update_memory_metrics() {
477
330
    _memory_metrics->memory_allocated_bytes->set_value(PerfCounters::get_vm_rss());
478
330
    get_metrics_from_proc_vmstat();
479
330
}
480
481
0
void SystemMetrics::update_allocator_metrics() {
482
0
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER)
483
0
    LOG(INFO) << "Memory tracking is not available with address sanitizer builds.";
484
#elif defined(USE_JEMALLOC)
485
    _memory_metrics->memory_jemalloc_allocated_bytes->set_value(
486
            JemallocControl::get_jemallctl_value<int64_t>("stats.allocated"));
487
    _memory_metrics->memory_jemalloc_active_bytes->set_value(
488
            JemallocControl::get_jemallctl_value<int64_t>("stats.active"));
489
    _memory_metrics->memory_jemalloc_metadata_bytes->set_value(
490
            JemallocControl::get_jemallctl_value<int64_t>("stats.metadata"));
491
    _memory_metrics->memory_jemalloc_resident_bytes->set_value(
492
            JemallocControl::get_jemallctl_value<int64_t>("stats.resident"));
493
    _memory_metrics->memory_jemalloc_mapped_bytes->set_value(
494
            JemallocControl::get_jemallctl_value<int64_t>("stats.mapped"));
495
    _memory_metrics->memory_jemalloc_retained_bytes->set_value(
496
            JemallocControl::get_jemallctl_value<int64_t>("stats.retained"));
497
    _memory_metrics->memory_jemalloc_tcache_bytes->set_value(
498
            JemallocControl::get_je_all_arena_metrics("tcache_bytes"));
499
    _memory_metrics->memory_jemalloc_pactive_num->set_value(
500
            JemallocControl::get_je_all_arena_metrics("pactive"));
501
    _memory_metrics->memory_jemalloc_pdirty_num->set_value(
502
            JemallocControl::get_je_all_arena_metrics("pdirty"));
503
    _memory_metrics->memory_jemalloc_pmuzzy_num->set_value(
504
            JemallocControl::get_je_all_arena_metrics("pmuzzy"));
505
    _memory_metrics->memory_jemalloc_dirty_purged_num->set_value(
506
            JemallocControl::get_je_all_arena_metrics("dirty_purged"));
507
    _memory_metrics->memory_jemalloc_muzzy_purged_num->set_value(
508
            JemallocControl::get_je_all_arena_metrics("muzzy_purged"));
509
#else
510
    _memory_metrics->memory_tcmalloc_allocated_bytes->set_value(
511
            JemallocControl::get_tc_metrics("generic.total_physical_bytes"));
512
    _memory_metrics->memory_tcmalloc_total_thread_cache_bytes->set_value(
513
            JemallocControl::je_cache_bytes());
514
    _memory_metrics->memory_tcmalloc_central_cache_free_bytes->set_value(
515
            JemallocControl::get_tc_metrics("tcmalloc.central_cache_free_bytes"));
516
    _memory_metrics->memory_tcmalloc_transfer_cache_free_bytes->set_value(
517
            JemallocControl::get_tc_metrics("tcmalloc.transfer_cache_free_bytes"));
518
    _memory_metrics->memory_tcmalloc_thread_cache_free_bytes->set_value(
519
            JemallocControl::get_tc_metrics("tcmalloc.thread_cache_free_bytes"));
520
    _memory_metrics->memory_tcmalloc_pageheap_free_bytes->set_value(
521
            JemallocControl::get_tc_metrics("tcmalloc.pageheap_free_bytes"));
522
    _memory_metrics->memory_tcmalloc_pageheap_unmapped_bytes->set_value(
523
            JemallocControl::get_tc_metrics("tcmalloc.pageheap_unmapped_bytes"));
524
#endif
525
0
}
526
527
5
void SystemMetrics::_install_disk_metrics(const std::set<std::string>& disk_devices) {
528
5
    for (auto& disk_device : disk_devices) {
529
5
        auto disk_entity = _registry->register_entity(std::string("disk_metrics.") + disk_device,
530
5
                                                      {{"device", disk_device}});
531
5
        DiskMetrics* metrics = new DiskMetrics(disk_entity.get());
532
5
        _disk_metrics.emplace(disk_device, metrics);
533
5
    }
534
5
}
535
536
330
void SystemMetrics::_update_disk_metrics() {
537
#ifdef BE_TEST
538
    FILE* fp = fopen(k_ut_diskstats_path, "r");
539
#else
540
330
    FILE* fp = fopen("/proc/diskstats", "r");
541
330
#endif
542
330
    if (fp == nullptr) {
543
0
        char buf[64];
544
0
        LOG(WARNING) << "open /proc/diskstats failed, errno=" << errno
545
0
                     << ", message=" << strerror_r(errno, buf, 64);
546
0
        return;
547
0
    }
548
549
    // /proc/diskstats: https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats
550
    // 1 - major number
551
    // 2 - minor mumber
552
    // 3 - device name
553
    // 4 - reads completed successfully
554
    // 5 - reads merged
555
    // 6 - sectors read
556
    // 7 - time spent reading (ms)
557
    // 8 - writes completed
558
    // 9 - writes merged
559
    // 10 - sectors written
560
    // 11 - time spent writing (ms)
561
    // 12 - I/Os currently in progress
562
    // 13 - time spent doing I/Os (ms)
563
    // 14 - weighted time spent doing I/Os (ms)
564
    // I think 1024 is enough for device name
565
330
    int major = 0;
566
330
    int minor = 0;
567
330
    char device[1024];
568
330
    int64_t values[11];
569
3.98k
    while (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
570
3.65k
        memset(values, 0, sizeof(values));
571
3.65k
        int num = sscanf(_line_ptr,
572
3.65k
                         "%d %d %1023s"
573
3.65k
                         " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64
574
3.65k
                         " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
575
3.65k
                         &major, &minor, device, &values[0], &values[1], &values[2], &values[3],
576
3.65k
                         &values[4], &values[5], &values[6], &values[7], &values[8], &values[9],
577
3.65k
                         &values[10]);
578
3.65k
        if (num < 4) {
579
0
            continue;
580
0
        }
581
3.65k
        auto it = _disk_metrics.find(device);
582
3.65k
        if (it == _disk_metrics.end()) {
583
3.32k
            continue;
584
3.32k
        }
585
        // update disk metrics
586
        // reads_completed: 4 reads completed successfully
587
330
        it->second->disk_reads_completed->set_value(values[0]);
588
        // bytes_read: 6 sectors read * 512; 5 reads merged is ignored
589
330
        it->second->disk_bytes_read->set_value(values[2] * 512);
590
        // read_time_ms: 7 time spent reading (ms)
591
330
        it->second->disk_read_time_ms->set_value(values[3]);
592
        // writes_completed: 8 writes completed
593
330
        it->second->disk_writes_completed->set_value(values[4]);
594
        // bytes_written: 10 sectors write * 512; 9 writes merged is ignored
595
330
        it->second->disk_bytes_written->set_value(values[6] * 512);
596
        // write_time_ms: 11 time spent writing (ms)
597
330
        it->second->disk_write_time_ms->set_value(values[7]);
598
        // io_time_ms: 13 time spent doing I/Os (ms)
599
330
        it->second->disk_io_time_ms->set_value(values[9]);
600
        // io_time_weigthed: 14 - weighted time spent doing I/Os (ms)
601
330
        it->second->disk_io_time_weigthed->set_value(values[10]);
602
330
    }
603
330
    if (ferror(fp) != 0) {
604
0
        char buf[64];
605
0
        LOG(WARNING) << "getline failed, errno=" << errno
606
0
                     << ", message=" << strerror_r(errno, buf, 64);
607
0
    }
608
330
    fclose(fp);
609
330
}
610
611
5
void SystemMetrics::_install_net_metrics(const std::vector<std::string>& interfaces) {
612
18
    for (auto& interface : interfaces) {
613
18
        auto interface_entity = _registry->register_entity(
614
18
                std::string("network_metrics.") + interface, {{"device", interface}});
615
18
        NetworkMetrics* metrics = new NetworkMetrics(interface_entity.get());
616
18
        _network_metrics.emplace(interface, metrics);
617
18
    }
618
5
}
619
620
5
void SystemMetrics::_install_snmp_metrics(MetricEntity* entity) {
621
5
    _snmp_metrics.reset(new SnmpMetrics(entity));
622
5
}
623
624
330
void SystemMetrics::_update_net_metrics() {
625
#ifdef BE_TEST
626
    // to mock proc
627
    FILE* fp = fopen(k_ut_net_dev_path, "r");
628
#else
629
330
    FILE* fp = fopen("/proc/net/dev", "r");
630
330
#endif
631
330
    if (fp == nullptr) {
632
0
        char buf[64];
633
0
        LOG(WARNING) << "open /proc/net/dev failed, errno=" << errno
634
0
                     << ", message=" << strerror_r(errno, buf, 64);
635
0
        return;
636
0
    }
637
638
    // Ignore header
639
330
    if (getline(&_line_ptr, &_line_buf_size, fp) < 0 ||
640
330
        getline(&_line_ptr, &_line_buf_size, fp) < 0) {
641
0
        char buf[64];
642
0
        LOG(WARNING) << "read /proc/net/dev first two line failed, errno=" << errno
643
0
                     << ", message=" << strerror_r(errno, buf, 64);
644
0
        fclose(fp);
645
0
        return;
646
0
    }
647
330
    if (_proc_net_dev_version == 0) {
648
4
        if (strstr(_line_ptr, "compressed") != nullptr) {
649
4
            _proc_net_dev_version = 3;
650
4
        } else if (strstr(_line_ptr, "bytes") != nullptr) {
651
0
            _proc_net_dev_version = 2;
652
0
        } else {
653
0
            _proc_net_dev_version = 1;
654
0
        }
655
4
    }
656
657
2.37k
    while (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
658
2.04k
        char* ptr = strrchr(_line_ptr, ':');
659
2.04k
        if (ptr == nullptr) {
660
0
            continue;
661
0
        }
662
2.04k
        char* start = _line_ptr;
663
4.02k
        while (isspace(*start)) {
664
1.98k
            start++;
665
1.98k
        }
666
2.04k
        std::string interface(start, ptr - start);
667
2.04k
        auto it = _network_metrics.find(interface);
668
2.04k
        if (it == _network_metrics.end()) {
669
918
            continue;
670
918
        }
671
1.12k
        ptr++;
672
1.12k
        int64_t receive_bytes = 0;
673
1.12k
        int64_t receive_packets = 0;
674
1.12k
        int64_t send_bytes = 0;
675
1.12k
        int64_t send_packets = 0;
676
1.12k
        switch (_proc_net_dev_version) {
677
1.12k
        case 3:
678
            // receive: bytes packets errs drop fifo frame compressed multicast
679
            // send:    bytes packets errs drop fifo colls carrier compressed
680
1.12k
            sscanf(ptr,
681
1.12k
                   " %" PRId64 " %" PRId64
682
1.12k
                   " %*d %*d %*d %*d %*d %*d"
683
1.12k
                   " %" PRId64 " %" PRId64 " %*d %*d %*d %*d %*d %*d",
684
1.12k
                   &receive_bytes, &receive_packets, &send_bytes, &send_packets);
685
1.12k
            break;
686
0
        case 2:
687
            // receive: bytes packets errs drop fifo frame
688
            // send:    bytes packets errs drop fifo colls carrier
689
0
            sscanf(ptr,
690
0
                   " %" PRId64 " %" PRId64
691
0
                   " %*d %*d %*d %*d"
692
0
                   " %" PRId64 " %" PRId64 " %*d %*d %*d %*d %*d",
693
0
                   &receive_bytes, &receive_packets, &send_bytes, &send_packets);
694
0
            break;
695
0
        case 1:
696
            // receive: packets errs drop fifo frame
697
            // send: packets errs drop fifo colls carrier
698
0
            sscanf(ptr,
699
0
                   " %" PRId64
700
0
                   " %*d %*d %*d %*d"
701
0
                   " %" PRId64 " %*d %*d %*d %*d %*d",
702
0
                   &receive_packets, &send_packets);
703
0
            break;
704
0
        default:
705
0
            break;
706
1.12k
        }
707
1.12k
        it->second->network_receive_bytes->set_value(receive_bytes);
708
1.12k
        it->second->network_receive_packets->set_value(receive_packets);
709
1.12k
        it->second->network_send_bytes->set_value(send_bytes);
710
1.12k
        it->second->network_send_packets->set_value(send_packets);
711
1.12k
    }
712
330
    if (ferror(fp) != 0) {
713
0
        char buf[64];
714
0
        LOG(WARNING) << "getline failed, errno=" << errno
715
0
                     << ", message=" << strerror_r(errno, buf, 64);
716
0
    }
717
330
    fclose(fp);
718
330
}
719
720
330
void SystemMetrics::_update_snmp_metrics() {
721
#ifdef BE_TEST
722
    // to mock proc
723
    FILE* fp = fopen(k_ut_net_snmp_path, "r");
724
#else
725
330
    FILE* fp = fopen("/proc/net/snmp", "r");
726
330
#endif
727
330
    if (fp == nullptr) {
728
0
        char buf[64];
729
0
        LOG(WARNING) << "open /proc/net/snmp failed, errno=" << errno
730
0
                     << ", message=" << strerror_r(errno, buf, 64);
731
0
        return;
732
0
    }
733
734
    // We only care about Tcp lines, so skip other lines in front of Tcp line
735
330
    int64_t res = 0;
736
2.31k
    while ((res = getline(&_line_ptr, &_line_buf_size, fp)) > 0) {
737
2.31k
        if (strstr(_line_ptr, "Tcp") != nullptr) {
738
330
            break;
739
330
        }
740
2.31k
    }
741
330
    if (res <= 0) {
742
0
        char buf[64];
743
0
        LOG(WARNING) << "failed to skip lines of /proc/net/snmp, errno=" << errno
744
0
                     << ", message=" << strerror_r(errno, buf, 64);
745
0
        fclose(fp);
746
0
        return;
747
0
    }
748
749
    // parse the Tcp header
750
    // Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts InCsumErrors
751
330
    std::vector<std::string> headers = absl::StrSplit(_line_ptr, " ");
752
330
    std::unordered_map<std::string, int32_t> header_map;
753
330
    int32_t pos = 0;
754
5.28k
    for (auto& h : headers) {
755
5.28k
        header_map.emplace(h, pos++);
756
5.28k
    }
757
758
    // read the metrics of TCP
759
330
    if (getline(&_line_ptr, &_line_buf_size, fp) < 0) {
760
0
        char buf[64];
761
0
        LOG(WARNING) << "failed to skip Tcp header line of /proc/net/snmp, errno=" << errno
762
0
                     << ", message=" << strerror_r(errno, buf, 64);
763
0
        fclose(fp);
764
0
        return;
765
0
    }
766
767
    // metric line looks like:
768
    // Tcp: 1 200 120000 -1 47849374 38601877 3353843 2320314 276 1033354613 1166025166 825439 12694 23238924 0
769
330
    std::vector<std::string> metrics = absl::StrSplit(_line_ptr, " ");
770
330
    if (metrics.size() != headers.size()) {
771
0
        LOG(WARNING) << "invalid tcp metrics line: " << _line_ptr;
772
0
        fclose(fp);
773
0
        return;
774
0
    }
775
330
    int64_t retrans_segs = std::stoll(metrics[header_map["RetransSegs"]]);
776
330
    int64_t in_errs = std::stoll(metrics[header_map["InErrs"]]);
777
330
    int64_t in_segs = std::stoll(metrics[header_map["InSegs"]]);
778
330
    int64_t out_segs = std::stoll(metrics[header_map["OutSegs"]]);
779
330
    _snmp_metrics->snmp_tcp_retrans_segs->set_value(retrans_segs);
780
330
    _snmp_metrics->snmp_tcp_in_errs->set_value(in_errs);
781
330
    _snmp_metrics->snmp_tcp_in_segs->set_value(in_segs);
782
330
    _snmp_metrics->snmp_tcp_out_segs->set_value(out_segs);
783
784
330
    if (ferror(fp) != 0) {
785
0
        char buf[64];
786
0
        LOG(WARNING) << "getline failed, errno=" << errno
787
0
                     << ", message=" << strerror_r(errno, buf, 64);
788
0
    }
789
330
    fclose(fp);
790
330
}
791
792
5
void SystemMetrics::_install_fd_metrics(MetricEntity* entity) {
793
5
    _fd_metrics.reset(new FileDescriptorMetrics(entity));
794
5
}
795
796
330
void SystemMetrics::_update_fd_metrics() {
797
#ifdef BE_TEST
798
    FILE* fp = fopen(k_ut_fd_path, "r");
799
#else
800
330
    FILE* fp = fopen("/proc/sys/fs/file-nr", "r");
801
330
#endif
802
330
    if (fp == nullptr) {
803
0
        char buf[64];
804
0
        LOG(WARNING) << "open /proc/sys/fs/file-nr failed, errno=" << errno
805
0
                     << ", message=" << strerror_r(errno, buf, 64);
806
0
        return;
807
0
    }
808
809
    // /proc/sys/fs/file-nr: https://www.kernel.org/doc/Documentation/sysctl/fs.txt
810
    // 1 - the number of allocated file handles
811
    // 2 - the number of allocated but unused file handles
812
    // 3 - the maximum number of file handles
813
814
330
    int64_t values[3];
815
330
    if (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
816
330
        memset(values, 0, sizeof(values));
817
330
        int num = sscanf(_line_ptr, "%" PRId64 " %" PRId64 " %" PRId64, &values[0], &values[1],
818
330
                         &values[2]);
819
330
        if (num == 3) {
820
330
            _fd_metrics->fd_num_limit->set_value(values[2]);
821
330
            _fd_metrics->fd_num_used->set_value(values[0] - values[1]);
822
330
        }
823
330
    }
824
825
330
    if (ferror(fp) != 0) {
826
0
        char buf[64];
827
0
        LOG(WARNING) << "getline failed, errno=" << errno
828
0
                     << ", message=" << strerror_r(errno, buf, 64);
829
0
    }
830
330
    fclose(fp);
831
330
}
832
833
5
void SystemMetrics::_install_load_avg_metrics(MetricEntity* entity) {
834
5
    _load_average_metrics.reset(new LoadAverageMetrics(entity));
835
5
}
836
837
330
void SystemMetrics::_update_load_avg_metrics() {
838
#ifdef BE_TEST
839
    FILE* fp = fopen(k_ut_load_avg_path, "r");
840
#else
841
330
    FILE* fp = fopen("/proc/loadavg", "r");
842
330
#endif
843
330
    if (fp == nullptr) {
844
0
        char buf[64];
845
0
        LOG(WARNING) << "open /proc/loadavg failed, errno=" << errno
846
0
                     << ", message=" << strerror_r(errno, buf, 64);
847
0
        return;
848
0
    }
849
850
330
    double values[3];
851
330
    if (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
852
330
        memset(values, 0, sizeof(values));
853
330
        int num = sscanf(_line_ptr, "%lf %lf %lf", &values[0], &values[1], &values[2]);
854
330
        if (num == 3) {
855
330
            _load_average_metrics->load_average_1_minutes->set_value(values[0]);
856
330
            _load_average_metrics->load_average_5_minutes->set_value(values[1]);
857
330
            _load_average_metrics->load_average_15_minutes->set_value(values[2]);
858
330
        }
859
330
    }
860
861
330
    if (ferror(fp) != 0) {
862
0
        char buf[64];
863
0
        LOG(WARNING) << "getline failed, errno=" << errno
864
0
                     << ", message=" << strerror_r(errno, buf, 64);
865
0
    }
866
330
    fclose(fp);
867
330
}
868
869
int64_t SystemMetrics::get_max_io_util(const std::map<std::string, int64_t>& lst_value,
870
326
                                       int64_t interval_sec) {
871
326
    int64_t max = 0;
872
326
    for (auto& it : _disk_metrics) {
873
326
        int64_t cur = it.second->disk_io_time_ms->value();
874
326
        const auto find = lst_value.find(it.first);
875
326
        if (find == lst_value.end()) {
876
0
            continue;
877
0
        }
878
326
        int64_t incr = cur - find->second;
879
326
        if (incr > max) max = incr;
880
326
    }
881
326
    return max / interval_sec / 10;
882
326
}
883
884
329
void SystemMetrics::get_disks_io_time(std::map<std::string, int64_t>* map) {
885
329
    map->clear();
886
329
    for (auto& it : _disk_metrics) {
887
329
        map->emplace(it.first, it.second->disk_io_time_ms->value());
888
329
    }
889
329
}
890
891
948
double SystemMetrics::get_load_average_1_min() {
892
948
    if (_load_average_metrics) {
893
948
        return _load_average_metrics->load_average_1_minutes->value();
894
948
    } else {
895
0
        return 0;
896
0
    }
897
948
}
898
899
void SystemMetrics::get_network_traffic(std::map<std::string, int64_t>* send_map,
900
329
                                        std::map<std::string, int64_t>* rcv_map) {
901
329
    send_map->clear();
902
329
    rcv_map->clear();
903
1.40k
    for (auto& it : _network_metrics) {
904
1.40k
        if (it.first == "lo") {
905
329
            continue;
906
329
        }
907
1.07k
        send_map->emplace(it.first, it.second->network_send_bytes->value());
908
1.07k
        rcv_map->emplace(it.first, it.second->network_receive_bytes->value());
909
1.07k
    }
910
329
}
911
912
void SystemMetrics::get_max_net_traffic(const std::map<std::string, int64_t>& lst_send_map,
913
                                        const std::map<std::string, int64_t>& lst_rcv_map,
914
                                        int64_t interval_sec, int64_t* send_rate,
915
326
                                        int64_t* rcv_rate) {
916
326
    int64_t max_send = 0;
917
326
    int64_t max_rcv = 0;
918
1.38k
    for (auto& it : _network_metrics) {
919
1.38k
        int64_t cur_send = it.second->network_send_bytes->value();
920
1.38k
        int64_t cur_rcv = it.second->network_receive_bytes->value();
921
922
1.38k
        const auto find_send = lst_send_map.find(it.first);
923
1.38k
        if (find_send != lst_send_map.end()) {
924
1.05k
            int64_t incr = cur_send - find_send->second;
925
1.05k
            if (incr > max_send) max_send = incr;
926
1.05k
        }
927
1.38k
        const auto find_rcv = lst_rcv_map.find(it.first);
928
1.38k
        if (find_rcv != lst_rcv_map.end()) {
929
1.05k
            int64_t incr = cur_rcv - find_rcv->second;
930
1.05k
            if (incr > max_rcv) max_rcv = incr;
931
1.05k
        }
932
1.38k
    }
933
934
326
    *send_rate = max_send / interval_sec;
935
326
    *rcv_rate = max_rcv / interval_sec;
936
326
}
937
938
void SystemMetrics::update_max_disk_io_util_percent(const std::map<std::string, int64_t>& lst_value,
939
326
                                                    int64_t interval_sec) {
940
326
    max_disk_io_util_percent->set_value(get_max_io_util(lst_value, interval_sec));
941
326
}
942
943
326
void SystemMetrics::update_max_network_send_bytes_rate(int64_t max_send_bytes_rate) {
944
326
    max_network_send_bytes_rate->set_value(max_send_bytes_rate);
945
326
}
946
947
326
void SystemMetrics::update_max_network_receive_bytes_rate(int64_t max_receive_bytes_rate) {
948
326
    max_network_receive_bytes_rate->set_value(max_receive_bytes_rate);
949
326
}
950
951
5
void SystemMetrics::_install_proc_metrics(MetricEntity* entity) {
952
5
    _proc_metrics.reset(new ProcMetrics(entity));
953
5
}
954
955
330
void SystemMetrics::_update_proc_metrics() {
956
#ifdef BE_TEST
957
    FILE* fp = fopen(k_ut_stat_path, "r");
958
#else
959
330
    FILE* fp = fopen("/proc/stat", "r");
960
330
#endif
961
330
    if (fp == nullptr) {
962
0
        char buf[64];
963
0
        LOG(WARNING) << "open /proc/stat failed, errno=" << errno
964
0
                     << ", message=" << strerror_r(errno, buf, 64);
965
0
        return;
966
0
    }
967
968
330
    uint64_t inter = 0, ctxt = 0, procs_r = 0, procs_b = 0;
969
12.5k
    while (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
970
12.1k
        char* start_pos = nullptr;
971
12.1k
        start_pos = strstr(_line_ptr, "intr ");
972
12.1k
        if (start_pos) {
973
330
            sscanf(start_pos, "intr %" PRIu64, &inter);
974
330
            _proc_metrics->proc_interrupt->set_value(inter);
975
330
        }
976
977
12.1k
        start_pos = strstr(_line_ptr, "ctxt ");
978
12.1k
        if (start_pos) {
979
330
            sscanf(start_pos, "ctxt %" PRIu64, &ctxt);
980
330
            _proc_metrics->proc_ctxt_switch->set_value(ctxt);
981
330
        }
982
983
12.1k
        start_pos = strstr(_line_ptr, "procs_running ");
984
12.1k
        if (start_pos) {
985
330
            sscanf(start_pos, "procs_running %" PRIu64, &procs_r);
986
330
            _proc_metrics->proc_procs_running->set_value(procs_r);
987
330
        }
988
989
12.1k
        start_pos = strstr(_line_ptr, "procs_blocked ");
990
12.1k
        if (start_pos) {
991
330
            sscanf(start_pos, "procs_blocked %" PRIu64, &procs_b);
992
330
            _proc_metrics->proc_procs_blocked->set_value(procs_b);
993
330
        }
994
12.1k
    }
995
996
330
    if (ferror(fp) != 0) {
997
0
        char buf[64];
998
0
        LOG(WARNING) << "getline failed, errno=" << errno
999
0
                     << ", message=" << strerror_r(errno, buf, 64);
1000
0
    }
1001
1002
330
    fclose(fp);
1003
330
}
1004
1005
326
void SystemMetrics::update_be_avail_cpu_num() {
1006
326
    int64_t physical_cpu_num = _cpu_num_metrics->host_cpu_num->value();
1007
326
    if (physical_cpu_num > 0) {
1008
326
        physical_cpu_num =
1009
326
                CGroupUtil::get_cgroup_limited_cpu_number(cast_set<int32_t>(physical_cpu_num));
1010
326
        _cpu_num_metrics->avail_cpu_num->set_value(physical_cpu_num);
1011
326
    }
1012
326
}
1013
1014
330
void SystemMetrics::get_metrics_from_proc_vmstat() {
1015
#ifdef BE_TEST
1016
    FILE* fp = fopen(k_ut_vmstat_path, "r");
1017
#else
1018
330
    FILE* fp = fopen("/proc/vmstat", "r");
1019
330
#endif
1020
330
    if (fp == nullptr) {
1021
0
        char buf[64];
1022
0
        LOG(WARNING) << "open /proc/vmstat failed, errno=" << errno
1023
0
                     << ", message=" << strerror_r(errno, buf, 64);
1024
0
        return;
1025
0
    }
1026
1027
45.7k
    while (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
1028
45.4k
        uint64_t value;
1029
45.4k
        char name[64];
1030
45.4k
        int num = sscanf(_line_ptr, "%s %" PRIu64, name, &value);
1031
45.4k
        if (num < 2) {
1032
0
            continue;
1033
0
        }
1034
1035
45.4k
        if (strcmp(name, "pgpgin") == 0) {
1036
330
            _memory_metrics->memory_pgpgin->set_value(value);
1037
45.0k
        } else if (strcmp(name, "pgpgout") == 0) {
1038
330
            _memory_metrics->memory_pgpgout->set_value(value);
1039
44.7k
        } else if (strcmp(name, "pswpin") == 0) {
1040
330
            _memory_metrics->memory_pswpin->set_value(value);
1041
44.4k
        } else if (strcmp(name, "pswpout") == 0) {
1042
330
            _memory_metrics->memory_pswpout->set_value(value);
1043
330
        }
1044
45.4k
    }
1045
1046
330
    if (ferror(fp) != 0) {
1047
0
        char buf[64];
1048
0
        LOG(WARNING) << "getline failed, errno=" << errno
1049
0
                     << ", message=" << strerror_r(errno, buf, 64);
1050
0
    }
1051
1052
330
    fclose(fp);
1053
330
}
1054
1055
5
void SystemMetrics::get_cpu_name() {
1056
#ifdef BE_TEST
1057
    FILE* fp = fopen(k_ut_stat_path, "r");
1058
#else
1059
5
    FILE* fp = fopen("/proc/stat", "r");
1060
5
#endif
1061
5
    if (fp == nullptr) {
1062
1
        char buf[64];
1063
1
        LOG(WARNING) << "open /proc/stat failed, errno=" << errno
1064
1
                     << ", message=" << strerror_r(errno, buf, 64);
1065
1
        return;
1066
1
    }
1067
1068
116
    while (getline(&_line_ptr, &_line_buf_size, fp) > 0) {
1069
112
        char cpu[16];
1070
112
        char* start_pos = nullptr;
1071
112
        start_pos = strstr(_line_ptr, "cpu");
1072
112
        if (start_pos) {
1073
84
            sscanf(_line_ptr, "%15s", cpu);
1074
84
            std::string cpu_name(cpu);
1075
84
            _cpu_names.push_back(cpu_name);
1076
84
        }
1077
112
    }
1078
1079
4
    if (ferror(fp) != 0) {
1080
0
        char buf[64];
1081
0
        LOG(WARNING) << "getline failed, errno=" << errno
1082
0
                     << ", message=" << strerror_r(errno, buf, 64);
1083
0
    }
1084
1085
4
    fclose(fp);
1086
4
}
1087
1088
} // namespace doris