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
16 #include <sys/types.h>
20 #include "CicoEvent.h"
21 #include "CicoEventInfo.h"
22 #include "CicoStateCore.h"
23 #include "CicoState.h"
24 #include "CicoHistoryState.h"
25 #include "CicoFinalState.h"
26 #include "CicoStateMachine.h"
27 #include "CicoStateMachineCreator.h"
28 #include "CicoSCSysResourceMonitor.h"
29 #include "CicoSCLifeCycleController.h"
30 #include "CicoSCSysResourceController.h"
32 #include "CicoSystemConfig.h"
34 //#include <Ecore_Evas.h>
36 const int gEV1000 = 1000; // CPU event
37 const int gEV1001 = 1001; // MEMORY event
39 const string gstrSRCCPU_LOW = "SRCCPU_LOW";
40 const string gstrSRCCPU_HIGH = "SRCCPU_HIGH";
41 const string gstrSRCMEM_LOW = "SRCMEM_LOW";
42 const string gstrSRCMEM_HIGH = "SRCMEM_HIGH";
44 static const string gstrStateMachineFileName = "CicoSCSysResourceSM.json";
46 bool g_RC_LOG = false;
47 int g_samplingWaitTimer = 200;
49 Ecore_Timer* g_SCResourceWatch = 0;
50 static Eina_Bool ico_SCResourceWatch(void *data)
52 if (g_RC_LOG) ICO_TRA("start data(%x)", data);
53 if ((NULL == data) || (0 == data)) {
54 if (g_RC_LOG) ICO_TRA("end");
55 return ECORE_CALLBACK_RENEW;
57 CicoSCSysResourceController* oCSCSRC = (CicoSCSysResourceController*)data;
59 if (g_RC_LOG) ICO_TRA("end");
60 return ECORE_CALLBACK_RENEW;
67 CicoSCSysResourceController::CicoSCSysResourceController()
75 m_rConf = CicoSystemConfig::getInstance()->getResourceConf();
77 g_RC_LOG = m_rConf->m_bLog;
78 if (-1 != m_rConf->m_sampling) {
79 g_samplingWaitTimer = m_rConf->m_sampling;
82 // cpu,cpuacct cgroup init
89 CicoSCSysResourceController::~CicoSCSysResourceController()
92 if (0 != g_SCResourceWatch) {
93 ecore_timer_del(g_SCResourceWatch);
96 if ((CicoStateMachine*)0 != m_stt) {
99 m_stt = (CicoStateMachine*)0;
100 ICO_TRA("delete CicoStateMachine");
103 if ((CicoSRCCPU_LOW*)0 != m_SRCCPU_LOW) {
105 m_SRCCPU_LOW = (CicoSRCCPU_LOW*)0;
106 ICO_TRA("delete CicoSRCCPU_LOW");
109 if ((CicoSRCCPU_HIGH*)0 != m_SRCCPU_HIGH) {
110 delete m_SRCCPU_HIGH;
111 m_SRCCPU_HIGH = (CicoSRCCPU_HIGH*)0;
112 ICO_TRA("delete CicoSRCCPU_HIGH");
115 if ((CicoSRCMEM_LOW*)0 != m_SRCMEM_LOW) {
117 m_SRCMEM_LOW = (CicoSRCMEM_LOW*)0;
118 ICO_TRA("delete CicoSRCMEM_LOW");
121 if ((CicoSRCMEM_HIGH*)0 != m_SRCMEM_HIGH) {
122 delete m_SRCMEM_HIGH;
123 m_SRCMEM_HIGH = (CicoSRCMEM_HIGH*)0;
124 ICO_TRA("delete CicoSRCMEM_HIGH");
127 if ((CicoSCSysResourceMonitor*)0 != m_monitor) {
129 m_monitor = (CicoSCSysResourceMonitor*)0;
130 ICO_TRA("delete CicoSCSysResourceMonitor");
138 bool CicoSCSysResourceController::startSysResource()
145 m_bDoIt = m_rConf->m_bDoIt;
146 if (false == m_bDoIt) {
150 // cpu,cpuacct cgroup init
151 init_cgroup(); // cgroup
152 m_monitor = new CicoSCSysResourceMonitor();
153 CicoSystemConfig* CSCSC = CicoSystemConfig::getInstance();
154 string filePath(CSCSC->getDefaultConf()->confdir);
155 filePath += string("/");
156 filePath += gstrStateMachineFileName;
158 CicoStateMachineCreator csmc;
159 m_stt = csmc.createFile(filePath);
160 if ((CicoStateMachine*)0 == m_stt) {
161 ICO_ERR("state machine create error(%s)", filePath.c_str());
166 m_SRCCPU_LOW = new CicoSRCCPU_LOW(this);
167 m_SRCCPU_HIGH = new CicoSRCCPU_HIGH(this);
168 m_SRCMEM_LOW = new CicoSRCMEM_LOW(this);
169 m_SRCMEM_HIGH = new CicoSRCMEM_HIGH(this);
171 CicoState* s = (CicoState*)m_stt->getState(gstrSRCCPU_LOW);
172 if ((CicoState*)0 != s) {
173 s->addEntryAction(m_SRCCPU_LOW);
174 s->addDoAction(m_SRCCPU_LOW);
176 s = (CicoState*)m_stt->getState(gstrSRCCPU_HIGH);
177 if ((CicoState*)0 != s) {
178 s->addEntryAction(m_SRCCPU_HIGH);
179 s->addDoAction(m_SRCCPU_HIGH);
181 s = (CicoState*)m_stt->getState(gstrSRCMEM_LOW);
182 if ((CicoState*)0 != s) {
183 s->addEntryAction(m_SRCMEM_LOW);
184 s->addDoAction(m_SRCMEM_LOW);
186 s = (CicoState*)m_stt->getState(gstrSRCMEM_HIGH);
187 if ((CicoState*)0 != s) {
188 s->addEntryAction(m_SRCMEM_HIGH);
189 s->addDoAction(m_SRCMEM_HIGH);
192 double tVal = (double)g_samplingWaitTimer / 100;
193 ICO_TRA("ECORE_TIMER(%f)", tVal);
194 g_SCResourceWatch = ecore_timer_add(tVal, ico_SCResourceWatch, this);
201 * @return true:change level false:no change
203 bool CicoSCSysResourceController::watch()
205 if (g_RC_LOG) ICO_TRA("start");
206 if ((CicoSCSysResourceMonitor*)0 == m_monitor) {
207 if (g_RC_LOG) ICO_TRA("end false");
211 m_monitor->monitoring();
212 int cpu = m_monitor->getCPUuser(); // application cpu
215 CicoEvent ev(gEV1000, cpu);
216 bool b = m_stt->eventEntry(ev);
217 if (g_RC_LOG) ICO_TRA("cpu %c, %d->%d", b? 't': 'f', m_cpu, cpu);
221 int mem = m_monitor->getUsageFreeMem(); // free memory
223 mem = (10000 - mem)/100; // free->used memory calc
224 CicoEvent ev(gEV1001, mem);
225 m_stt->eventEntry(ev);
229 if (g_RC_LOG) ICO_TRA("end %s", r? "true": "false");
234 * @brief CPU share control request
235 * @param pulus true:level up / false level down
237 void CicoSCSysResourceController::resourceControlCPUShares(bool bHight)
239 ICO_TRA("start %s", bHight? "true": "false");
240 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
244 if (true == m_rConf->m_cpuCtrl.empty()) {
249 int sz = m_rConf->m_cpuCtrl.size();
250 for (int i = 0; i < sz; i++) {
251 if (true == m_RCCpuShares[i].empty()) { // write target none
252 continue; // next target
254 int sharesVal = -1; // initialize cpu.shares value
255 if (true == bHight) {
256 if (m_cgrpCPU < (int)m_rConf->m_cpuCtrl[i]->m_hight.size()) {
257 sharesVal = m_rConf->m_cpuCtrl[i]->m_hight[m_cgrpCPU];
258 newCgrpCPU = m_cgrpCPU + 1;
259 ICO_DBG("m_hight[%d]=%d", m_cgrpCPU, sharesVal);
263 if (0 != m_cgrpCPU) {
264 int tmp = m_cgrpCPU - 1;
265 if (tmp < (int)m_rConf->m_cpuCtrl[i]->m_low.size()) {
266 sharesVal = m_rConf->m_cpuCtrl[i]->m_low[tmp];
268 ICO_DBG("m_low[%d]=%d", tmp, sharesVal);
272 if (0 <= sharesVal) {
273 replaceCgroupFile(m_RCCpuShares[i], sharesVal);
275 // else if (-9 == sharesVal) {
278 if (-1 != newCgrpCPU) { // control level value check
279 ICO_DBG("control level %d -> %d", m_cgrpCPU, newCgrpCPU);
280 m_cgrpCPU = newCgrpCPU; // set next control level
285 bool CicoSCSysResourceController::replaceCgroupFile(const std::string& tgt,
289 sprintf(sv, "%d", val);
290 return replaceCgroupFile(tgt, sv);
293 bool CicoSCSysResourceController::replaceCgroupFile(const std::string& tgt,
297 if (true == tgt.empty()) {
301 FILE *fp = fopen(tgt.c_str(), "wt");
303 ICO_TRA("end open error %s", tgt.c_str());
308 ICO_TRA("end Cmd(echo %s > %s", val, tgt.c_str());
313 * @brief memory limit control request
314 * @param pulus true:level up / false level down
316 void CicoSCSysResourceController::resourceControlMemLimit(bool bHight)
319 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
323 // TODO mk_k coming soon
324 if (true == bHight) {
326 ICO_TRA("hight (true)");
330 ICO_TRA("low (false)");
342 bool CicoSCSysResourceController::entryCgroupCPU(int pid, int grpNo)
344 ICO_TRA("start pid(%d), cgrpNo(%d)", pid, grpNo);
345 if (((0 > grpNo) || ((int)m_RCCpuTasks.size() <= grpNo)) ||
346 ((NULL == m_rConf) || (false == m_rConf->m_bDoIt))) {
347 ICO_TRA("end %d", m_RCCpuTasks.size());
353 sprintf(procdir, "/proc/%d/task", pid);
354 if (0 != stat(procdir, &statbuf)) {
355 ICO_TRA("end %s ng dir", procdir);
359 dp = opendir(procdir);
361 ICO_TRA("end opendir error");
365 while((ent = readdir(dp)) != NULL){
366 if (0 != isdigit(ent->d_name[0])) {
367 ICO_DBG("echo %s > %s", ent->d_name, m_RCCpuTasks[grpNo].c_str());
368 replaceCgroupFile(m_RCCpuTasks[grpNo], ent->d_name);
375 return replaceCgroupFile(m_RCCpuTasks[grpNo], pid);
380 * @brief initialize cgroup
382 void CicoSCSysResourceController::init_cgroup()
386 // memory cgroup directory get
387 init_cgroup_memory();
392 * @brief initialize cgroup cpu
394 void CicoSCSysResourceController::init_cgroup_cpu()
397 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
402 string sTasks(D_STRtasks);
403 string sShares(D_STRcpushares);
404 string t1(m_rConf->m_cpuCGRPPath);
405 if ('/' != t1[t1.size()-1]) {
409 d.push_back(m_rConf->m_cpuCGRPPath);
410 int sz = m_rConf->m_cpuCtrl.size();
411 for (int i = 0; i < sz; i++) {
412 CicoSCCpuResourceGrp* obj = m_rConf->m_cpuCtrl[i];
424 if (false == t2.empty()) {
425 if ('/' != t2[t2.size()-1]) {
432 m_RCCpuTasks.push_back(t3);
433 m_RCCpuShares.push_back(t4);
441 * @brief initialize cgroup memory
443 void CicoSCSysResourceController::init_cgroup_memory()
446 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
447 ICO_TRA("end error config class");
456 * @brief make directory
457 * @param d create directory target
459 void CicoSCSysResourceController::make_directorys(std::vector<std::string> dir)
463 for (int i = 0; i < sz; i++) {
464 if (true == dir[i].empty()) {
467 const char* dpath = dir[i].c_str();
468 struct stat stat_buf;
469 if (0 != stat(dpath, &stat_buf)) {
471 int r = mkdir(dpath, S_IRUSR| S_IWUSR| S_IXUSR| S_IRGRP|
472 S_IXGRP| S_IROTH| S_IXOTH);
473 ICO_DBG("%s create status(%d)", dpath, r);
480 * @brief Class ico System Resource Controller CPU LOW level
483 CicoSRCCPU_LOW::CicoSRCCPU_LOW(CicoSCSysResourceController* obj):
486 ICO_TRA("constructor");
487 m_val = -1; // cpu or memory usage value
488 m_baseVal = -1; // cpu or memory base usage value
490 m_rConf = CicoSystemConfig::getInstance()->getResourceConf();
494 * @brief Class ico System Resource Controller CPU LOW level
497 CicoSRCCPU_LOW::~CicoSRCCPU_LOW()
499 ICO_TRA("destructor");
504 * @brief resource control entry state
505 * @param ev event data
506 * @param stt state object
508 void CicoSRCCPU_LOW::onEntry(const CicoEvent& ev, const CicoState* stt, int)
511 int v2 = ev.getGCVi();
512 if (g_RC_LOG) ICO_TRA("start %s,%d,%d CicoSRCCPU_LOW::", stt->getName().c_str(), v1, v2);
515 m_cnt = m_rConf->m_retryCnt;
516 if (g_RC_LOG) ICO_TRA("end cnt(%d) CicoSRCCPU_LOW::", m_cnt);
520 * @brief cpu usage low resource control
521 * @param ev event data
522 * @param stt state object
524 void CicoSRCCPU_LOW::onDo(const CicoEvent& ev, const CicoState* stt, int)
526 int valN = ev.getGCVi();
527 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)CicoSRCCPU_LOW::", m_val, valN);
530 // When the value is greater than the previous
531 if (g_RC_LOG) ICO_TRA("end n(%d)->o(%d) CicoSRCCPU_LOW::", m_val, valN);
534 // When the value is less than the previous
535 int t = ((double)valN / (double)m_baseVal)*100;
537 m_cntlr->resourceControlCPUShares(false);
538 if (g_RC_LOG) ICO_DBG("CHG %d->%d CicoSRCCPU_LOW::",m_baseVal, valN);
542 if (valN < m_rConf->m_lowLimitVal) {
545 m_cntlr->resourceControlCPUShares(false);
546 if (g_RC_LOG) ICO_DBG("CHG %d->%d CicoSRCCPU_LOW::",m_baseVal, valN);
547 m_cnt = m_rConf->m_retryCnt;
552 if (g_RC_LOG) ICO_DBG("cnt rst V(%d) CicoSRCCPU_LOW::", valN);
553 m_cnt = m_rConf->m_retryCnt;
557 if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_LOW::");
562 * @brief resource control exit state
563 * @param ev event data
564 * @param stt state object
566 void CicoSRCCPU_LOW::onExit(const CicoEvent&, const CicoState*, int)
568 if (g_RC_LOG) ICO_TRA("start CicoSRCCPU_LOW::");
571 if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_LOW::");
575 * @brief Class ico System Resource Controller CPU HIGH level
578 CicoSRCCPU_HIGH::CicoSRCCPU_HIGH(CicoSCSysResourceController* obj):
581 ICO_TRA("constructor");
585 * @brief Class ico System Resource Controller CPU HIGH level
588 CicoSRCCPU_HIGH::~CicoSRCCPU_HIGH()
590 ICO_TRA("destructor");
594 * @brief cpu usage high resource control
595 * @param ev event data
596 * @param stt state object
598 void CicoSRCCPU_HIGH::onDo(const CicoEvent& ev, const CicoState* stt, int)
600 int valN = ev.getGCVi();
601 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d) CicoSRCCPU_HIGH::", m_val, valN);
604 // When the value is less than the previous
605 if (g_RC_LOG) ICO_TRA("n(%d)<=o(%d) CicoSRCCPU_HIGH::", valN, m_val);
608 // When the value is greater than the previous
609 int t = ((double)(100-valN) / (double)(100-m_baseVal))*100;
611 m_cntlr->resourceControlCPUShares(true);
612 if (g_RC_LOG) ICO_DBG("%d->%d CicoSRCCPU_HIGH::", m_baseVal, valN);
616 if (valN > m_rConf->m_highLimitVal) {
618 if (g_RC_LOG) ICO_DBG("cnt(%d) CicoSRCCPU_HIGH::", m_cnt);
620 m_cntlr->resourceControlCPUShares(true);
621 if (g_RC_LOG) ICO_DBG("CicoSRCCPU_HIGH:: %d->%d",m_baseVal, valN);
622 m_cnt = m_rConf->m_retryCnt;
627 if (g_RC_LOG) ICO_DBG("CicoSRCCPU_HIGH:: (%d)", valN);
628 m_cnt = m_rConf->m_retryCnt;
632 if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_HIGH::");
636 * @brief Class ico System Resource Controller MEMory LOW level
639 CicoSRCMEM_LOW::CicoSRCMEM_LOW(CicoSCSysResourceController* obj):
642 ICO_TRA("constructor CicoSRCMEM_LOW::");
646 * @brief Class ico System Resource Controller MEMory LOW level
649 CicoSRCMEM_LOW::~CicoSRCMEM_LOW()
651 ICO_TRA("destructor CicoSRCMEM_LOW::");
655 * @brief memory usage low resource control
656 * @param ev event data
657 * @param stt state object
659 void CicoSRCMEM_LOW::onDo(const CicoEvent& ev, const CicoState* stt, int)
661 int valN = ev.getGCVi();
662 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)", m_val, valN);
664 // When the value is greater than the previous
665 if (g_RC_LOG) ICO_TRA("n(%d)>=o(%d)", valN, m_val);
668 // When the value is less than the previous
669 int t = ((double)valN / (double)m_baseVal)*100;
671 m_cntlr->resourceControlMemLimit(false);
672 if (g_RC_LOG) ICO_TRA("BASE CHG %d->%d",m_baseVal, valN);
676 if (g_RC_LOG) ICO_TRA("end");
681 * @brief Class ico System Resource Controller MEMory HIGH level
684 CicoSRCMEM_HIGH::CicoSRCMEM_HIGH(CicoSCSysResourceController* obj):
687 ICO_TRA("constructor");
691 * @brief Class ico System Resource Controller MEMory HIGH level
694 CicoSRCMEM_HIGH::~CicoSRCMEM_HIGH()
696 ICO_TRA("destructor");
700 * @brief memory usage high resource control
701 * @param ev event data
702 * @param stt state object
704 void CicoSRCMEM_HIGH::onDo(const CicoEvent& ev, const CicoState* stt, int)
706 int valN = ev.getGCVi();
707 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)", m_val, valN);
709 // When the value is less than the previous
713 // When the value is greater than the previous
714 int t = ((double)(100-valN) / (double)(100-m_baseVal))*100;
716 m_cntlr->resourceControlMemLimit(true);
717 if (g_RC_LOG) ICO_TRA("BASE CHG %d -> %d", m_baseVal, valN);
721 if (g_RC_LOG) ICO_TRA("end");