1 // Copyright (c) 2020 Tomasz Konojacki
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
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.
21 // SPDX-License-Identifier: BSD-1-Clause
29 #include <unordered_map>
31 #include <string_view>
33 #include "sm_mapify.hxx"
35 #if defined(__GNUC__) || defined(__clang__)
36 # define SM_LIKELY(x) __builtin_expect(!!(x),1)
37 # define SM_UNLIKELY(x) __builtin_expect(!!(x), 0)
39 # define SM_LIKELY(x) (x)
40 # define SM_UNLIKELY(x) (x)
51 uint64_t available = 0;
59 double cpu_usage = -1;
62 struct process_list_fields {
64 bool cpu_usage = false;
66 SM_MAPIFY_STRUCT(bool, pid, cpu_usage, name)
69 class system_metrics {
74 struct cpu_times cpu_times();
75 std::vector<struct cpu_times> per_cpu_times();
76 uint64_t number_of_cpus();
78 struct mem_info mem_info();
79 std::vector<struct process> process_list(const process_list_fields &fields);
83 std::vector<double> per_cpu_usage();
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;
92 double calc_cpu_usage(struct cpu_times &prev_ct, const struct cpu_times &ct);