d48dc510f9d62f92e6ba6ddc2e48fbffb136ed04
[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 #define D_STRtasks      "tasks"
36 #define D_STRcpushares  "cpu.shares"
37
38 class CicoSCSysResourceController;
39
40 /**
41  * @brief Class ico System Resource Controller CPU LOW level
42  */
43 class CicoSRCCPU_LOW : public CicoStateAction
44 {
45 public: // member method
46     CicoSRCCPU_LOW(CicoSCSysResourceController* obj);
47     virtual ~CicoSRCCPU_LOW();
48
49     virtual void onEntry(const CicoEvent& ev, const CicoState* stt, int addval);
50     virtual void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
51     virtual void onExit(const CicoEvent& ev, const CicoState* stt,
52                         int addval);
53 protected: // member Variable
54     int m_val;      // cpu or memory usage value
55     int m_baseVal;  // cpu or memory base usage value
56     int m_cnt;
57     CicoSCSysResourceController* m_cntlr;
58     const CicoSCResourceConf* m_rConf;
59 };
60
61 /**
62  * @brief Class ico System Resource Controller CPU HIGH level
63  */
64 class CicoSRCCPU_HIGH : public CicoSRCCPU_LOW
65 {
66 public: // member method
67     CicoSRCCPU_HIGH(CicoSCSysResourceController* obj);
68     virtual ~CicoSRCCPU_HIGH();
69     void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
70
71 protected: // member Variable
72 };
73
74 /**
75  * @brief Class ico System Resource Controller MEMory LOW level
76  */
77 class CicoSRCMEM_LOW : public CicoSRCCPU_LOW
78 {
79 public: // member method
80     CicoSRCMEM_LOW(CicoSCSysResourceController* obj);
81     virtual ~CicoSRCMEM_LOW();
82     void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
83 protected: // member Variable
84 };
85
86 /**
87  * @brief Class ico System Resource Controller MEMory HIGH level
88  */
89 class CicoSRCMEM_HIGH : public CicoSRCCPU_LOW
90 {
91 public: // member method
92     CicoSRCMEM_HIGH(CicoSCSysResourceController* obj);
93     virtual ~CicoSRCMEM_HIGH();
94     void onDo(const CicoEvent& ev, const CicoState* stt, int addval);
95 protected: // member Variable
96 };
97
98 /**
99  * @brief System Controller System Resource Controller class
100  */
101 class CicoSCSysResourceController
102 {
103 public:
104     CicoSCSysResourceController();
105     ~CicoSCSysResourceController();
106
107     bool isWactchAllReady() const;
108
109     bool startSysResource(); // start cgroup controll
110
111     bool watch();   // watch request (please interval call)
112     const CicoSCSysResourceMonitor& getMonitor() const;
113     int getCpuUsge() const;
114     int getMemoryUsge() const;
115
116     void resourceControlCPUShares(bool bHight);
117     void resourceControlMemLimit(bool bHight);
118     bool entryCgroupCPU(int pid, int grpNo);
119 protected:
120     void init_cgroup();
121     void init_cgroup_cpu();
122     void init_cgroup_memory();
123     void make_directorys(std::vector<std::string> dir);
124     bool replaceCgroupFile(const std::string& tgt, int val);
125     bool replaceCgroupFile(const std::string& tgt, const char* val);
126
127 protected:
128     CicoSCSysResourceMonitor* m_monitor;
129     const CicoSCResourceConf* m_rConf;
130     std::vector<std::string> m_RCCpuTasks;
131     std::vector<std::string> m_RCCpuShares;
132     bool m_bDoIt;
133 private:
134     CicoStateMachine* m_stt;
135
136     CicoSRCCPU_LOW* m_SRCCPU_LOW;
137     CicoSRCCPU_HIGH* m_SRCCPU_HIGH;
138     CicoSRCMEM_LOW* m_SRCMEM_LOW;
139     CicoSRCMEM_HIGH* m_SRCMEM_HIGH;
140
141     int m_cpu;
142     int m_mem;
143     int m_cgrpCPU;
144     int m_cgrpMEM;
145 };
146
147 /**
148  * @brief
149  * @return
150  */
151 inline
152 bool CicoSCSysResourceController::isWactchAllReady() const
153 {
154     if ((-1 != m_cpu) && (-1 != m_mem)) {
155         return true;
156     }
157     return false;
158 }
159
160 /**
161  * @brief get monitor class
162  * return monitor class
163  */
164 inline const CicoSCSysResourceMonitor&
165     CicoSCSysResourceController::getMonitor() const
166 {
167     return *m_monitor;
168 }
169
170 /**
171  * @brief get CPU usage
172  * @return CPU usage
173  */
174 inline int CicoSCSysResourceController::getCpuUsge() const
175 {
176     return m_cpu;
177 }
178
179 /**
180  * @brief get CPU usage
181  * @return CPU usage
182  */
183 inline int CicoSCSysResourceController::getMemoryUsge() const
184 {
185     return m_mem;
186 }
187
188 #endif // CICOSCSYSRESOURCECONTROLLER_H