Additional functions of policy and resource management.
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCSysResourceController.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   CicoSCSysResourceController
11  *          SystemController resource watcher
12  *          CPU/FreeMemory monitoring
13  *
14  * @date    Aug-05-2013 create start
15  */
16
17 #ifndef CICOSCSYSRESOURCECONTROLLER_H
18 #define CICOSCSYSRESOURCECONTROLLER_H
19 #include <cstddef>
20
21 #include "CicoStateAction.h"
22
23 #ifndef CICOSTATEMACHINE_H
24 class CicoStateMachine;
25 #endif
26
27 #ifndef CICOSCSYSRESOURCEMONITOR_H
28 class CicoSCSysResourceMonitor;
29 #endif
30
31 #ifndef __CICO_SC_CONF_H__
32 class CicoSCResourceConf;
33 #endif
34
35 class CicoSCSysResourceController;
36
37 /**
38  * @brief Class ico System Resource Controller CPU LOW level
39  */
40 class CicoSRCCPU_LOW : public CicoStateAction
41 {
42 public: // member method
43     CicoSRCCPU_LOW(CicoSCSysResourceController* obj);
44     virtual ~CicoSRCCPU_LOW();
45
46     virtual void onEntry(const CicoEvent& ev, const CicoState* stt, int addval);
47     virtual void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
48     virtual void onExit(const CicoEvent& ev, const CicoState* stt,
49                         int addval);
50 protected: // member Variable
51     int m_val;      // cpu or memory usage value
52     int m_baseVal;  // cpu or memory base usage value
53     int m_cnt;
54     CicoSCSysResourceController* m_cntlr;
55     const CicoSCResourceConf* m_rConf;
56 };
57
58 /**
59  * @brief Class ico System Resource Controller CPU HIGH level
60  */
61 class CicoSRCCPU_HIGH : public CicoSRCCPU_LOW
62 {
63 public: // member method
64     CicoSRCCPU_HIGH(CicoSCSysResourceController* obj);
65     virtual ~CicoSRCCPU_HIGH();
66     void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
67
68 protected: // member Variable
69 };
70
71 /**
72  * @brief Class ico System Resource Controller MEMory LOW level
73  */
74 class CicoSRCMEM_LOW : public CicoSRCCPU_LOW
75 {
76 public: // member method
77     CicoSRCMEM_LOW(CicoSCSysResourceController* obj);
78     virtual ~CicoSRCMEM_LOW();
79     void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
80 protected: // member Variable
81 };
82
83 /**
84  * @brief Class ico System Resource Controller MEMory HIGH level
85  */
86 class CicoSRCMEM_HIGH : public CicoSRCCPU_LOW
87 {
88 public: // member method
89     CicoSRCMEM_HIGH(CicoSCSysResourceController* obj);
90     virtual ~CicoSRCMEM_HIGH();
91     void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
92 protected: // member Variable
93 };
94
95 /**
96  * @brief System Controller System Resource Controller class
97  */
98 class CicoSCSysResourceController
99 {
100 public:
101     CicoSCSysResourceController();
102     ~CicoSCSysResourceController();
103
104     bool isWactchAllReady() const;
105
106     bool watch();   // watch request (please interval call)
107     const CicoSCSysResourceMonitor& getMonitor() const;
108     int getCpuUsge() const;
109     int getMemoryUsge() const;
110
111     void resourceControlCPUShares(bool bHight);
112     void resourceControlMemLimit(bool bHight);
113     bool entryCgroupCPU(int pid, int grpNo);
114 protected:
115     void init_cgroup();
116     void init_cgroup_cpu();
117     void init_cgroup_memory();
118     void make_directorys(std::vector<std::string> dir);
119     bool replaceCgroupFile(const std::string& tgt, int val);
120     bool replaceCgroupFile(const std::string& tgt, const char* val);
121
122 private:
123     CicoSCSysResourceMonitor* m_monitor;
124     const CicoSCResourceConf* m_rConf;
125     CicoStateMachine* m_stt;
126
127     CicoSRCCPU_LOW* m_SRCCPU_LOW;
128     CicoSRCCPU_HIGH* m_SRCCPU_HIGH;
129     CicoSRCMEM_LOW* m_SRCMEM_LOW;
130     CicoSRCMEM_HIGH* m_SRCMEM_HIGH;
131
132     int m_cpu;
133     int m_mem;
134     std::vector<std::string> m_RCCpuTasks;
135     std::vector<std::string> m_RCCpuShares;
136     int m_cgrpCPU;
137     int m_cgrpMEM;
138
139 };
140
141 /**
142  * @brief
143  * @return
144  */
145 inline
146 bool CicoSCSysResourceController::isWactchAllReady() const
147 {
148     if ((-1 != m_cpu) && (-1 != m_mem)) {
149         return true;
150     }
151     return false;
152 }
153
154 /**
155  * @brief get monitor class
156  * return monitor class
157  */
158 inline const CicoSCSysResourceMonitor&
159     CicoSCSysResourceController::getMonitor() const
160 {
161     return *m_monitor;
162 }
163
164 /**
165  * @brief get CPU usage
166  * @return CPU usage
167  */
168 inline int CicoSCSysResourceController::getCpuUsge() const
169 {
170     return m_cpu;
171 }
172
173 /**
174  * @brief get CPU usage
175  * @return CPU usage
176  */
177 inline int CicoSCSysResourceController::getMemoryUsge() const
178 {
179     return m_mem;
180 }
181
182 #endif // CICOSCSYSRESOURCECONTROLLER_H