OSDN Git Service

sddsasad
[btop/system-metrics.git] / cxx / system_metrics.hxx
1 // Copyright (c) 2020 Tomasz Konojacki
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  * Redistributions of source code must retain the above copyright notice,
7 //    this list of conditions and the following disclaimer.
8 //
9 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
12 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
13 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
14 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
15 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
16 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
17 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
18 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
19 // POSSIBILITY OF SUCH DAMAGE.
20 //
21 // SPDX-License-Identifier: BSD-1-Clause
22
23 #pragma once
24
25 #include <cstddef>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include <unordered_map>
30 #include <stdint.h>
31 #include <string_view>
32
33 #include "sm_mapify.hxx"
34
35 #if defined(__GNUC__) || defined(__clang__)
36 #  define SM_LIKELY(x) __builtin_expect(!!(x),1)
37 #  define SM_UNLIKELY(x) __builtin_expect(!!(x), 0)
38 #else
39 #  define SM_LIKELY(x) (x)
40 #  define SM_UNLIKELY(x) (x)
41 #endif
42
43 struct cpu_times {
44     uint64_t idle = 0;
45     uint64_t user = 0;
46     uint64_t system = 0;
47 };
48
49 struct mem_info {
50     uint64_t total = 0;
51     uint64_t available = 0;
52     uint64_t free = 0;
53 };
54
55 struct process {
56     uint64_t pid = 0;
57     uint64_t ppid = 0;
58     std::string path;
59     double cpu_usage = -1;
60 };
61
62 struct process_list_fields {
63     bool pid = true;
64     bool cpu_usage = false;
65     bool name = false;
66     SM_MAPIFY_STRUCT(bool, pid, cpu_usage, name)
67 };
68
69 class system_metrics {
70 public:
71     // platform specific:
72     system_metrics();
73     ~system_metrics();
74     struct cpu_times cpu_times();
75     std::vector<struct cpu_times> per_cpu_times();
76     uint64_t number_of_cpus();
77     uint64_t page_size();
78     struct mem_info mem_info();
79     std::vector<struct process> process_list(const process_list_fields &fields);
80
81     // common
82     double cpu_usage();
83     std::vector<double> per_cpu_usage();
84
85 private:
86     struct cpu_times prev_cpu_times;
87     std::vector<struct cpu_times> prev_per_cpu_times;
88     std::unique_ptr<struct sm_private> _priv;
89     std::unordered_map<uint64_t, struct cpu_times > prev_proc_cpu_times;
90 };
91
92 double calc_cpu_usage(struct cpu_times &prev_ct, const struct cpu_times &ct);