2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 * @brief CicoSCSysResourceMonitor
11 * SystemController resource monitor
12 * CPU/FreeMemory monitoring
14 * @date Jul-02-2013 create start
17 #ifndef CICOSCSYSRESOURCEMONITOR_H
18 #define CICOSCSYSRESOURCEMONITOR_H
20 #include <sys/types.h>
25 * /proc/stat cpu/cpuX data struct type
27 typedef struct t_stat_data {
28 int user; // 1) Time spent in user mode.
29 int nice; // 2) Time spent in user mode with low priority (nice).
30 int system; // 3) Time spent in system mode.
31 int idle; // 4) Time spent in the idle task.
32 // This value should be USER_HZ times the second entry
33 // in the /proc/uptime pseudo-file.
34 int iowait; // 5) Time waiting for I/O to complete.
35 int irq; // 6) Time servicing interrupts.
36 int softirq; // 7) Time servicing softirqs.
37 int steal; // 8) Time stolen
39 int guest; // 9) Time guest OS
40 int guest_nice; // 10) Time guest OS(nice)
43 class CicoSCSysResourceMonitorP {
44 public: // member method
45 CicoSCSysResourceMonitorP(pid_t pid);
46 CicoSCSysResourceMonitorP(const CicoSCSysResourceMonitorP& s);
52 public: // member Variable
60 01)pid, 02)comm, 03)state, 04)ppid, 05)pgrp, 06)session, 07)tty_nr, 08)tpgid
61 09)flags, 10)minflt, 11)cminflt, 12)majflt, 13)cmajflt, 14)utime, 15)stime
62 16)cutime, 17)cstime, 18)priority, 19)nice, 20)num_threads, 21)itrealvalue
63 22)starttime, 23)vsize, 24)rss, 25)rsslim, 26)startcode, 27)endcode
64 28)startstack, 29)kstkesp, 30)kstkeip, 31)signal, 32)blocked, 33)sigignore
65 34)sigcatch, 35)wchan, 36)nswap, 37)cnswap, 38)exit_signal, 39)processor
66 40)rt_priority, 41)policy, 42)delayacct_blkio_ticks, 43)guest_time
76 * @brief Resource monitoring
78 class CicoSCSysResourceMonitor {
80 CicoSCSysResourceMonitor();
81 ~CicoSCSysResourceMonitor();
84 E_user = 0, // 1) Time spent in user mode.
85 E_nice, // 2) Time spent in user mode with low priority (nice).
86 E_system, // 3) Time spent in system mode.
87 E_idle, // 4) Time spent in the idle task.
88 E_iowait, // 5) Time waiting for I/O to complete.
89 E_irq, // 6) Time servicing interrupts.
90 E_softirq, // 7) Time servicing softirqs.
91 E_steal, // 8) Time stolen
92 E_guest, // 9) Time Guest OS
93 E_guest_nice // 10) Time guest OS(nice)
95 bool isAllReady() const;
97 int getCPUCount() const; // get number of cpu cores
99 // @param cpu -1:total 0:cpu0 1:cpu1 n:cpuN
100 // @retval 0 - 10000(retval/100 = 0.00 - 100.00%)
101 int getCPUuser(int cpu=-1) const;
102 int getCPUsystem(int cpu=-1) const;
103 int getCPUtime(E_USAGETYPE e, int cpu=-1) const;
106 unsigned int getFreeMemSize() const; // free memory size maybe kB
107 unsigned int getTotalMemSize() const; // total memory size maybe kB
108 int getUsageFreeMem() const; // usage free memory
113 // process monitoring entry and remove
114 void addTarget(pid_t pid);
115 void removeTarget(pid_t pid);
116 // std::vector<Xxxxx>& get
119 void getResourceCPU();
120 void getResourceMEM();
122 int createCurrentCPU();
123 void readCurrentCPU();
124 int readProcStatCPU(stat_data_t* sdtp, const int cpucnt);
125 bool usageCalcCPU(const stat_data_t& old, const stat_data_t& current,
127 void readProcMeminfo(bool ttlMem=false);
129 int m_CPUcnt; // CPU core count
130 stat_data_t* m_old; // CPU time of previous value
131 stat_data_t* m_current; // CPU time of this time
132 stat_data_t* m_calc; // usage CPU time
133 unsigned int m_totalMem; // total memory size maybe kB
134 unsigned int m_freeMem; // free memory size maybe kB
135 int m_usageFreeMem; // usage free memory
141 * @brief monitoring all ready
142 * @return true:ok false:not good
144 inline bool CicoSCSysResourceMonitor::isAllReady() const
150 * @brief cpu core count
151 * @return cpum_usageFreeMem core count
153 inline int CicoSCSysResourceMonitor::getCPUCount() const
160 * @param cpu core cpu position -1:all 0-n:cpu position
162 * @retval 0 - 10000 (value/100 = 0.00 - 100.00%)
163 * @retval -1:get fail
165 inline int CicoSCSysResourceMonitor::getCPUuser(int cpu) const
167 return getCPUtime(E_user,cpu);
171 * @brief system usage
172 * @param cpu core cpu position -1:all 0-n:cpu position
174 * @retval 0 - 10000 (value/100 = 0.00 - 100.00%)
175 * @retval -1:get fail
177 inline int CicoSCSysResourceMonitor::getCPUsystem(int cpu) const
179 return getCPUtime(E_system,cpu);
183 * @brief free memory size
184 * @return free memory size maybe(kB)
186 inline unsigned int CicoSCSysResourceMonitor::getFreeMemSize() const
192 * @brief total memory size
193 * @return free memory size maybe(kB)
195 inline unsigned int CicoSCSysResourceMonitor::getTotalMemSize() const
201 * @brief usage free memory
203 * @retval -1:during measurement
204 * @retval 0 - 10000(value / 100 = 0.00% - 100.00%)
206 inline int CicoSCSysResourceMonitor::getUsageFreeMem() const // usage free memory
208 return m_usageFreeMem;
211 #endif // CICOSCSYSRESOURCEMONITOR_H