-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProcessMonitor.h
More file actions
129 lines (100 loc) · 3.71 KB
/
ProcessMonitor.h
File metadata and controls
129 lines (100 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
///
/// \file ProcessMonitor.h
/// \author Adam Wegrzynek <adam.wegrzynek@cern.ch>
///
#ifndef ALICEO2_MONITORING_CORE_PROCESSMONITOR_H
#define ALICEO2_MONITORING_CORE_PROCESSMONITOR_H
#include <atomic>
#include <iostream>
#include <string>
#include <vector>
#include <sys/resource.h>
#include "Monitoring/Metric.h"
namespace o2
{
/// ALICE O2 Monitoring system
namespace monitoring
{
enum class PmMeasurement : short {
Cpu,
Mem,
Smaps
};
/// Monitors current process and/or other processes running at the same machien
class ProcessMonitor
{
friend class Monitoring;
public:
enum {
MEMORY_USAGE_PERCENTAGE = 0,
VIRTUAL_MEMORY_SIZE,
RESIDENT_SET_SIZE,
CPU_USED_PERCENTAGE,
INVOLUNTARY_CONTEXT_SWITCHES,
VOLUNTARY_CONTEXT_SWITCHES,
CPU_USED_ABSOLUTE,
AVG_RESIDENT_SET_SIZE,
AVG_VIRTUAL_MEMORY_SIZE,
AVG_CPU_USED_PERCENTAGE,
ACCUMULATED_CPU_TIME,
PSS,
PRIVATE_CLEAN,
PRIVATE_DIRTY,
AVAILABLE_METRICS_SIZE
};
static std::vector<std::string> getAvailableMetricsNames();
std::vector<Metric> getPerformanceMetrics();
/// Sets PID and total memory
ProcessMonitor();
/// Default destructor
~ProcessMonitor() = default;
/// Set initial variables for CPU usage calculations
void init();
/// Enable given measurement
void enable(PmMeasurement measurement);
private:
/// States which measurements are enabled
std::array<bool, 3> mEnabledMeasurements;
double splitStatusLineAndRetriveValue(const std::string& line) const;
/// Retrievs total memory size from /proc/meminfo
void setTotalMemory();
static constexpr const char* metricsNames[] = {"memoryUsagePercentage", "virtualMemorySize", "residentSetSize",
"cpuUsedPercentage", "involuntaryContextSwitches", "voluntaryContextSwitches", "cpuUsedAbsolute",
"averageResidentSetSize", "averageVirtualMemorySize", "averageCpuUsedPercentage",
"cpuTimeConsumedByProcess", "proportionalSetSize", "memoryPrivateClean", "memoryPrivateDirty"};
static constexpr unsigned int VM_SIZE_INDEX = 18;
static constexpr unsigned int VM_RSS_INDEX = 22;
/// PIDs that are monitored
unsigned int mPid;
/// Total memory size
unsigned int mTotalMemory;
/// 'getrusage' values from last execution
struct rusage mPreviousGetrUsage;
///each measurement will be saved to compute average/accumulation usage
std::vector<double> mVmSizeMeasurements;
std::vector<double> mVmRssMeasurements;
std::vector<uint64_t> mCpuMicroSeconds;
std::vector<double> mCpuPerctange;
/// Timestamp when process monitoring was executed last time
std::chrono::high_resolution_clock::time_point mTimeLastRun;
/// Retrieves virtual memory and resident set size usage
std::vector<Metric> getMemoryUsage();
/// Retrieves memory maping metrics
std::vector<Metric> getSmaps();
/// Retrieves CPU usage (%) and number of context switches during the interval
std::vector<Metric> getCpuAndContexts();
std::vector<Metric> makeLastMeasurementAndGetMetrics();
};
} // namespace monitoring
} // namespace o2
#endif // ALICEO2_MONITORING_CORE_PROCESSMONITOR_H