Additional functions of policy and resource management.
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCSysResourceMonitor.h
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  */
9 /**
10  * @brief   CicoSCSysResourceMonitor
11  *          SystemController resource monitor
12  *          CPU/FreeMemory monitoring
13  *
14  * @date    Jul-02-2013 create start
15  */
16
17 #ifndef CICOSCSYSRESOURCEMONITOR_H
18 #define CICOSCSYSRESOURCEMONITOR_H
19
20 #include <sys/types.h>
21 #include <vector>
22
23
24 /**
25  * /proc/stat cpu/cpuX data struct type
26  */
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
38                  // 8) stolen time
39     int guest;   // 9) Time guest OS
40     int guest_nice; // 10) Time guest OS(nice)
41 } stat_data_t;
42
43 class CicoSCSysResourceMonitorP {
44 public: // member method
45     CicoSCSysResourceMonitorP(pid_t pid);
46     CicoSCSysResourceMonitorP(const CicoSCSysResourceMonitorP& s);
47
48
49
50
51
52 public: // member Variable
53     pid_t m_pid;
54     int m_utime_o;
55     int m_stime_o;
56     int m_vsize_o;
57 /*
58
59   /proc/[pid]/stat
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
67   44)cguest_time
68 */
69 };
70
71
72
73
74
75 /**
76  * @brief Resource monitoring
77  */
78 class CicoSCSysResourceMonitor {
79 public:
80     CicoSCSysResourceMonitor();
81     ~CicoSCSysResourceMonitor();
82
83     enum E_USAGETYPE{
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)
94     };
95     bool isAllReady() const;
96     // cpu
97     int  getCPUCount() const; // get number of cpu cores
98
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;
104
105     // memory
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
109
110     // controller
111     void monitoring();
112
113     // process monitoring entry and remove
114     void addTarget(pid_t pid);
115     void removeTarget(pid_t pid);
116 //    std::vector<Xxxxx>& get
117 protected:
118     void getResource();
119     void getResourceCPU();
120     void getResourceMEM();
121 private:
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,
126                       stat_data_t& calc);
127     void readProcMeminfo(bool ttlMem=false);
128 protected:
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
136 private:
137     bool m_allready;
138 };
139
140 /**
141  * @brief monitoring all ready
142  * @return true:ok false:not good
143  */
144 inline bool CicoSCSysResourceMonitor::isAllReady() const
145 {
146     return m_allready;
147 }
148
149 /**
150  * @brief cpu core count
151  * @return cpum_usageFreeMem core count
152  */
153 inline int CicoSCSysResourceMonitor::getCPUCount() const
154 {
155     return m_CPUcnt;
156 }
157
158 /**
159  * @brief user usage
160  * @param cpu core cpu position -1:all 0-n:cpu position
161  * @return cpu usage
162  * @retval 0 - 10000 (value/100 = 0.00 - 100.00%)
163  * @retval -1:get fail
164  */
165 inline int CicoSCSysResourceMonitor::getCPUuser(int cpu) const
166 {
167     return getCPUtime(E_user,cpu);
168 }
169
170 /**
171  * @brief system usage
172  * @param cpu core cpu position -1:all 0-n:cpu position
173  * @return cpu usage
174  * @retval 0 - 10000 (value/100 = 0.00 - 100.00%)
175  * @retval -1:get fail
176  */
177 inline int CicoSCSysResourceMonitor::getCPUsystem(int cpu) const
178 {
179     return getCPUtime(E_system,cpu);
180 }
181
182 /**
183  * @brief free memory size
184  * @return free memory size maybe(kB)
185  */
186 inline unsigned int CicoSCSysResourceMonitor::getFreeMemSize() const
187 {
188     return m_freeMem;
189 }
190
191 /**
192  * @brief total memory size
193  * @return free memory size maybe(kB)
194  */
195 inline unsigned int CicoSCSysResourceMonitor::getTotalMemSize() const
196 {
197     return m_totalMem;
198 }
199
200 /**
201  * @brief usage free memory
202  * @return usage
203  * @retval -1:during measurement
204  * @retval 0 - 10000(value / 100 = 0.00% - 100.00%)
205  */
206 inline int CicoSCSysResourceMonitor::getUsageFreeMem() const // usage free memory
207 {
208     return m_usageFreeMem;
209 }
210
211 #endif // CICOSCSYSRESOURCEMONITOR_H