COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
devicecpu.h
Go to the documentation of this file.
1 /********************************************************************
2 * Copyright (C) 2015 by Interstel Technologies, Inc.
3 * and Hawaii Space Flight Laboratory.
4 *
5 * This file is part of the COSMOS/core that is the central
6 * module for COSMOS. For more information on COSMOS go to
7 * <http://cosmos-project.com>
8 *
9 * The COSMOS/core software is licenced under the
10 * GNU Lesser General Public License (LGPL) version 3 licence.
11 *
12 * You should have received a copy of the
13 * GNU Lesser General Public License
14 * If not, go to <http://www.gnu.org/licenses/>
15 *
16 * COSMOS/core is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public License
18 * as published by the Free Software Foundation, either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * COSMOS/core is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
25 *
26 * Refer to the "licences" folder for further information on the
27 * condititons and terms to use this software.
28 ********************************************************************/
29 #ifndef DEVICECPU_H
30 #define DEVICECPU_H
31 
36 
42 
43 #include "support/configCosmos.h"
44 #include "support/stringlib.h"
45 
46 // for current process CPU utilization
47 #if defined (COSMOS_LINUX_OS)
48 #include <sys/times.h>
49 #include <sys/vtimes.h>
50 #include <sys/sysinfo.h>
51 
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #endif
55 
56 #if defined (COSMOS_WIN_OS)
57 #include "windows.h"
58 #include <tchar.h>
59 #endif
60 
61 #include <fstream> // std::ifstream
62 #include <algorithm> // std::unique
63 
64 using std::string;
65 using std::ifstream;
66 using std::cout;
67 using std::endl;
68 
69 #ifdef COSMOS_WIN_OS
70 class DeviceCpuWindows
71 {
72 
73 public:
74  DeviceCpuWindows();
75 
76  double getLoad();
77  double CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks);
78  unsigned long long FileTimeToInt64(const FILETIME & ft);
79  string getHostName();
80  double getVirtualMemoryUsed();
81  double getVirtualMemoryTotal();
82  pid_t getPidOf(string processName);
83 };
84 
85 #endif
86 
87 
88 #if defined(COSMOS_LINUX_OS)
89 class DeviceCpuLinux
90 {
91 
92  double lastCPUtime;
93  double tic, toc;
94  float percentCpu;
95 
96 public:
97  DeviceCpuLinux();
98 
99  // variables
100  float load1minAverage;
101  int numProcessors = 0;
102  string processName;
103 
104  // functions
105  double getLoad1minAverage();
106  float getPercentUseForCurrentProcess();
107  float getPercentUseForCurrentProcessOverLifetime();
108  void initCpuUtilization();
109  double getVirtualMemoryUsed();
110  double getVirtualMemoryTotal();
111  double getVirtualMemoryFree();
112  string getCurrentProcessName();
113  string getHostName();
114  //double GetLinuxCPULoad(), GetLinuxUsedDisk(), GetLinuxVirtualMem();
115  //double GetLinuxTotalDisk(), GetLinuxTotalVirtualMem();
116  //double CalculateLinuxCPULoad (float *out);
117  double getMemoryUsed();
118  double getMemoryTotal();
119  pid_t getPidOf(string processName);
120  double getMemoryUsedOf(string processName);
121  float getPercentCpuOf(string processName);
122  int32_t getCpuCount();
123  int32_t getBootCount();
124  int32_t getUptime();
125 
126  class procPidStat {
127 
128  struct stat {
129  // stat vars
130  string pid, comm, state, ppid, pgrp, session, tty_nr;
131  string tpgid, flags, minflt, cminflt, majflt, cmajflt;
132  string utime, stime, cutime, cstime, priority, nice;
133  string O, itrealvalue, starttime;
134  unsigned long vsize;
135  long rss;
136  };
137 
138 
139 
140  public:
141  //int pid, comm, state, ppid, pgrp, session, tty_nr;
142  //int tpgid, flags, minflt, cminflt, majflt, cmajflt;
143  float utime, stime; //, cutime, cstime, priority, nice;
144  //string O, itrealvalue, starttime;
145 
146  bool fileExists = false;
147 
148  procPidStat(string processName);
149  };
150 
151  class procStat {
152 
153  public:
154  // stat vars
155  string cpu, user, nice, system, idle, iowait, irq, softirq, steal,guest, guest_nice;
156  int time_total;
157  procStat();
158  };
159  float getPercentMemoryOf(string processName);
160 };
161 
162 #endif
163 
164 
165 #if defined(COSMOS_MAC_OS)
166 class DeviceCpuMac
167 {
168  double lastCPUtime;
169  double tic, toc;
170  float percentCpu;
171 
172 public:
173  DeviceCpuMac();
174 
175  // variables
176  float load1minAverage;
177  int numProcessors = 0;
178  string processName;
179 
180  // functions
181  double getLoad1minAverage();
182  float getPercentUseForCurrentProcess();
183  float getPercentUseForCurrentProcessOverLifetime();
184  void initCpuUtilization();
185  double getVirtualMemoryUsed();
186  double getVirtualMemoryTotal();
187  double getVirtualMemoryFree();
188  string getCurrentProcessName();
189  string getHostName();
190  //double GetLinuxCPULoad(), GetLinuxUsedDisk(), GetLinuxVirtualMem();
191  //double GetLinuxTotalDisk(), GetLinuxTotalVirtualMem();
192  //double CalculateLinuxCPULoad (float *out);
193  double getMemoryUsed();
194  double getMemoryTotal();
195  pid_t getPidOf(string processName);
196  double getMemoryUsedOf(string processName);
197  float getPercentCpuOf(string processName);
198 
199  class procPidStat {
200 
201  struct stat {
202  // stat vars
203  string pid, comm, state, ppid, pgrp, session, tty_nr;
204  string tpgid, flags, minflt, cminflt, majflt, cmajflt;
205  string utime, stime, cutime, cstime, priority, nice;
206  string O, itrealvalue, starttime;
207  unsigned long vsize;
208  long rss;
209  };
210 
211 
212 
213  public:
214  //int pid, comm, state, ppid, pgrp, session, tty_nr;
215  //int tpgid, flags, minflt, cminflt, majflt, cmajflt;
216  float utime, stime; //, cutime, cstime, priority, nice;
217  //string O, itrealvalue, starttime;
218 
219  bool fileExists = false;
220 
221  procPidStat(string processName);
222  };
223 
224  class procStat {
225 
226  public:
227  // stat vars
228  string cpu, user, nice, system, idle, iowait, irq, softirq, steal,guest, guest_nice;
229  int time_total;
230  procStat();
231  };
232  float getPercentMemoryOf(string processName);
233 };
234 
235 #endif
236 
237 
238 
240 {
241 public:
242  DeviceCpu();
243 
246  string processName;
247  string hostName;
248  double memoryUsed;
249  double memoryTotal;
250  double memoryFree;
255  double load;
256 
257  void stress();
258 
259  // API functions (generic names)
260  double getLoad();
261  uint16_t getCpuCount();
262  uint32_t getUptime();
263  uint16_t getBootCount();
264  double getVirtualMemoryTotal();
265  double getVirtualMemoryUsed();
266  double getVirtualMemoryFree();
267  double getVirtualMemoryUsedPercent();
268  double getPercentUseForCurrentProcess();
269  string getHostName();
270 
271  // move to math_cpu.cpp?
272  double BytesToKiB(double bytes);
273  double BytesToMiB(double bytes);
274  double BytesToMB(double bytes);
275 
276 #if defined(COSMOS_LINUX_OS)
277  DeviceCpuLinux cpuLinux;
278 #endif
279 
280 #if defined(COSMOS_MAC_OS)
281  DeviceCpuMac cpuMac;
282 #endif
283 
284 #if defined(COSMOS_WIN_OS)
285  DeviceCpuWindows cpuWin;
286 #endif
287 
288  double getVirtualMemoryUsedKiB();
289  double getVirtualMemoryUsedMiB();
290 
291  double getVirtualMemoryUsedMB();
292  double getMemoryUsed();
293  double getMemoryTotal();
294  double getMemoryUsedKiB();
295  double getMemoryUsedMiB();
296  double getMemoryTotalKiB();
297  double getMemoryTotalMiB();
298  double getMemoryUsedMB();
299  double getMemoryTotalMB();
300  pid_t getPidOf(string processName);
301  pid_t getMemoryUsedOf(string processName);
302  float getPercentCpuOf(string processName);
303  float getPercentMemoryOf(string processName);
304 };
305 
306 
307 #endif // DEVICECPU_H
double virtualMemoryFree
Definition: devicecpu.h:254
double memoryBuffers
Definition: devicecpu.h:251
double virtualMemoryTotal
Definition: devicecpu.h:253
int numProcessors
Definition: devicecpu.h:245
Definition: devicecpu.h:239
double memoryUsed
Definition: devicecpu.h:248
Headers and definitions common to all COSMOS.
double load
Definition: devicecpu.h:255
double memoryFree
Definition: devicecpu.h:250
double virtualMemoryUsed
Definition: devicecpu.h:252
string processName
Definition: devicecpu.h:246
DeviceCpu cpu
Definition: agent_example.cpp:58
stringlib include file A library providing functions for handling various types of string...
string hostName
Definition: devicecpu.h:247
double percentUseForCurrentProcess
Definition: devicecpu.h:244
double memoryTotal
Definition: devicecpu.h:249