OSDN Git Service

05ab5422daa8293d15bad8be149d7a96770397bb
[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 <cstdef>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include <stdint.h>
30
31 #if defined(__GNUC__) || defined(__clang__)
32 #  define SM_LIKELY(x) __builtin_expect(!!(x),1)
33 #  define SM_UNLIKELY(x) __builtin_expect(!!(x), 0)
34 #else
35 #  define SM_LIKELY(x) (x)
36 #  define SM_UNLIKELY(x) (x)
37 #endif
38
39 struct cpu_times {
40     uint64_t idle = 0;
41     uint64_t user = 0;
42     uint64_t system = 0;
43 };
44
45 struct mem_info {
46     uint64_t total = 0;
47     uint64_t available = 0;
48     uint64_t free = 0;
49 };
50
51 struct process {
52     uint64_t pid = 0;
53     uint64_t ppid = 0;
54     std::string path;
55 };
56
57 class system_metrics {
58 public:
59     // platform specific:
60     system_metrics();
61     ~system_metrics();
62     struct cpu_times cpu_times();
63     std::vector<struct cpu_times> per_cpu_times();
64     uint64_t number_of_cpus();
65     uint64_t page_size();
66     struct mem_info mem_info();
67     std::vector<struct process> process_list();
68
69     // common
70     double cpu_usage();
71     std::vector<double> per_cpu_usage();
72
73 private:
74     struct cpu_times prev_cpu_times;
75     std::vector<struct cpu_times> prev_per_cpu_times;
76     std::unique_ptr<struct sm_private> _priv;
77 };