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"
31 #include "CicoSCConf.h"
32 #include "CicoSCSystemConfig.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 // double tVal = (double)g_samplingWaitTimer / 100;
60 // ecore_timer_add(g_samplingWaitTimer, ico_SCResourceWatch, data);
61 if (g_RC_LOG) ICO_TRA("end");
62 return ECORE_CALLBACK_RENEW;
69 CicoSCSysResourceController::CicoSCSysResourceController()
76 m_rConf = CicoSCSystemConfig::getInstance()->getResourceConf();
78 g_RC_LOG = m_rConf->m_bLog;
79 if (-1 != m_rConf->m_sampling) {
80 g_samplingWaitTimer = m_rConf->m_sampling;
83 if (false == m_rConf->m_bDoIt) {
87 // cpu,cpuacct cgroup init
88 init_cgroup(); // cgroup
89 m_monitor = new CicoSCSysResourceMonitor();
90 CicoSCSystemConfig* CSCSC = CicoSCSystemConfig::getInstance();
91 string filePath(CSCSC->getDefaultConf()->confdir);
92 filePath += string("/");
93 filePath += gstrStateMachineFileName;
95 CicoStateMachineCreator csmc;
96 m_stt = csmc.createFile(filePath);
97 if ((CicoStateMachine*)0 == m_stt) {
98 ICO_ERR("state machine create error(%s)", filePath.c_str());
102 m_SRCCPU_LOW = new CicoSRCCPU_LOW(this);
103 m_SRCCPU_HIGH = new CicoSRCCPU_HIGH(this);
104 m_SRCMEM_LOW = new CicoSRCMEM_LOW(this);
105 m_SRCMEM_HIGH = new CicoSRCMEM_HIGH(this);
107 CicoState* s = (CicoState*)m_stt->getState(gstrSRCCPU_LOW);
108 if ((CicoState*)0 != s) {
109 s->addEntryAction(m_SRCCPU_LOW);
110 s->addDoAction(m_SRCCPU_LOW);
112 s = (CicoState*)m_stt->getState(gstrSRCCPU_HIGH);
113 if ((CicoState*)0 != s) {
114 s->addEntryAction(m_SRCCPU_HIGH);
115 s->addDoAction(m_SRCCPU_HIGH);
117 s = (CicoState*)m_stt->getState(gstrSRCMEM_LOW);
118 if ((CicoState*)0 != s) {
119 s->addEntryAction(m_SRCMEM_LOW);
120 s->addDoAction(m_SRCMEM_LOW);
122 s = (CicoState*)m_stt->getState(gstrSRCMEM_HIGH);
123 if ((CicoState*)0 != s) {
124 s->addEntryAction(m_SRCMEM_HIGH);
125 s->addDoAction(m_SRCMEM_HIGH);
128 double tVal = (double)g_samplingWaitTimer / 100;
130 ICO_TRA("ECORE_TIMER(%f)", tVal);
131 g_SCResourceWatch = ecore_timer_add(tVal, ico_SCResourceWatch, this);
133 ICO_TRA("ECORE_TIMER(%f)", tVal);
134 g_SCResourceWatch = ecore_timer_add(tVal, ico_SCResourceWatch, 0);
142 CicoSCSysResourceController::~CicoSCSysResourceController()
145 if (0 != g_SCResourceWatch) {
146 ecore_timer_del(g_SCResourceWatch);
149 if ((CicoStateMachine*)0 != m_stt) {
152 m_stt = (CicoStateMachine*)0;
153 ICO_TRA("delete CicoStateMachine");
156 if ((CicoSRCCPU_LOW*)0 != m_SRCCPU_LOW) {
158 m_SRCCPU_LOW = (CicoSRCCPU_LOW*)0;
159 ICO_TRA("delete CicoSRCCPU_LOW");
162 if ((CicoSRCCPU_HIGH*)0 != m_SRCCPU_HIGH) {
163 delete m_SRCCPU_HIGH;
164 m_SRCCPU_HIGH = (CicoSRCCPU_HIGH*)0;
165 ICO_TRA("delete CicoSRCCPU_HIGH");
168 if ((CicoSRCMEM_LOW*)0 != m_SRCMEM_LOW) {
170 m_SRCMEM_LOW = (CicoSRCMEM_LOW*)0;
171 ICO_TRA("delete CicoSRCMEM_LOW");
174 if ((CicoSRCMEM_HIGH*)0 != m_SRCMEM_HIGH) {
175 delete m_SRCMEM_HIGH;
176 m_SRCMEM_HIGH = (CicoSRCMEM_HIGH*)0;
177 ICO_TRA("delete CicoSRCMEM_HIGH");
180 if ((CicoSCSysResourceMonitor*)0 != m_monitor) {
182 m_monitor = (CicoSCSysResourceMonitor*)0;
183 ICO_TRA("delete CicoSCSysResourceMonitor");
190 * @return true:change level false:no change
192 bool CicoSCSysResourceController::watch()
194 if (g_RC_LOG) ICO_TRA("start");
195 if ((CicoSCSysResourceMonitor*)0 == m_monitor) {
196 if (g_RC_LOG) ICO_TRA("end false");
200 m_monitor->monitoring();
201 int cpu = m_monitor->getCPUuser(); // application cpu
204 CicoEvent ev(gEV1000, cpu);
205 bool b = m_stt->eventEntry(ev);
206 if (g_RC_LOG) ICO_TRA("cpu %c, %d->%d", b? 't': 'f', m_cpu, cpu);
210 int mem = m_monitor->getUsageFreeMem(); // free memory
212 mem = (10000 - mem)/100; // free->used memory calc
213 CicoEvent ev(gEV1001, mem);
214 m_stt->eventEntry(ev);
218 if (g_RC_LOG) ICO_TRA("end %s", r? "true": "false");
223 * @brief CPU share control request
224 * @param pulus true:level up / false level down
226 void CicoSCSysResourceController::resourceControlCPUShares(bool bHight)
228 ICO_TRA("start %s", bHight? "true": "false");
229 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
233 if (true == m_rConf->m_cpuCtrl.empty()) {
238 int sz = m_rConf->m_cpuCtrl.size();
239 for (int i = 0; i < sz; i++) {
240 if (true == m_RCCpuShares[i].empty()) { // write target none
241 continue; // next target
243 int sharesVal = -1; // initialize cpu.shares value
244 if (true == bHight) {
245 if (m_cgrpCPU < (int)m_rConf->m_cpuCtrl[i]->m_hight.size()) {
246 sharesVal = m_rConf->m_cpuCtrl[i]->m_hight[m_cgrpCPU];
247 newCgrpCPU = m_cgrpCPU + 1;
248 ICO_DBG("m_hight[%d]=%d", m_cgrpCPU, sharesVal);
252 if (0 != m_cgrpCPU) {
253 int tmp = m_cgrpCPU - 1;
254 if (tmp < (int)m_rConf->m_cpuCtrl[i]->m_low.size()) {
255 sharesVal = m_rConf->m_cpuCtrl[i]->m_low[tmp];
257 ICO_DBG("m_low[%d]=%d", tmp, sharesVal);
261 if (0 <= sharesVal) {
262 replaceCgroupFile(m_RCCpuShares[i], sharesVal);
264 // else if (-9 == sharesVal) {
267 if (-1 != newCgrpCPU) { // control level value check
268 ICO_DBG("control level %d -> %d", m_cgrpCPU, newCgrpCPU);
269 m_cgrpCPU = newCgrpCPU; // set next control level
274 bool CicoSCSysResourceController::replaceCgroupFile(const std::string& tgt,
278 sprintf(sv, "%d", val);
279 return replaceCgroupFile(tgt, sv);
282 bool CicoSCSysResourceController::replaceCgroupFile(const std::string& tgt,
286 if (true == tgt.empty()) {
290 FILE *fp = fopen(tgt.c_str(), "wt");
292 ICO_TRA("end open error %s", tgt.c_str());
297 ICO_TRA("end Cmd(echo %s > %s", val, tgt.c_str());
302 * @brief memory limit control request
303 * @param pulus true:level up / false level down
305 void CicoSCSysResourceController::resourceControlMemLimit(bool bHight)
308 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
312 // TODO mk_k coming soon
313 if (true == bHight) {
315 ICO_TRA("hight (true)");
319 ICO_TRA("low (false)");
331 bool CicoSCSysResourceController::entryCgroupCPU(int pid, int grpNo)
333 ICO_TRA("start pid(%d), cgrpNo(%d)", pid, grpNo);
334 if ((0 > grpNo) || ((int)m_RCCpuTasks.size() <= grpNo)) {
335 ICO_TRA("end %d", m_RCCpuTasks.size());
341 sprintf(procdir, "/proc/%d/task", pid);
342 if (0 != stat(procdir, &statbuf)) {
343 ICO_TRA("end %s ng dir", procdir);
347 dp = opendir(procdir);
349 ICO_TRA("end opendir error");
353 while((ent = readdir(dp)) != NULL){
354 if (0 != isdigit(ent->d_name[0])) {
355 ICO_DBG("echo %s > %s", ent->d_name, m_RCCpuTasks[grpNo].c_str());
356 replaceCgroupFile(m_RCCpuTasks[grpNo], ent->d_name);
363 return replaceCgroupFile(m_RCCpuTasks[grpNo], pid);
368 * @brief initialize cgroup
370 void CicoSCSysResourceController::init_cgroup()
374 // memory cgroup directory get
375 init_cgroup_memory();
380 * @brief initialize cgroup cpu
382 void CicoSCSysResourceController::init_cgroup_cpu()
385 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
390 string sTasks("tasks");
391 string sShares("cpu.shares");
392 string t1(m_rConf->m_cpuCGRPPath);
393 if ('/' != t1[t1.size()-1]) {
397 d.push_back(m_rConf->m_cpuCGRPPath);
398 int sz = m_rConf->m_cpuCtrl.size();
399 for (int i = 0; i < sz; i++) {
400 CicoSCCpuResourceGrp* obj = m_rConf->m_cpuCtrl[i];
412 if (false == t2.empty()) {
413 if ('/' != t2[t2.size()-1]) {
420 m_RCCpuTasks.push_back(t3);
421 m_RCCpuShares.push_back(t4);
429 * @brief initialize cgroup memory
431 void CicoSCSysResourceController::init_cgroup_memory()
434 if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
435 ICO_TRA("end error config class");
444 * @brief make directory
445 * @param d create directory target
447 void CicoSCSysResourceController::make_directorys(std::vector<std::string> dir)
451 for (int i = 0; i < sz; i++) {
452 if (true == dir[i].empty()) {
455 const char* dpath = dir[i].c_str();
456 struct stat stat_buf;
457 if (0 != stat(dpath, &stat_buf)) {
459 int r = mkdir(dpath, S_IRUSR| S_IWUSR| S_IXUSR| S_IRGRP|
460 S_IXGRP| S_IROTH| S_IXOTH);
461 ICO_DBG("%s create status(%d)", dpath, r);
468 * @brief Class ico System Resource Controller CPU LOW level
471 CicoSRCCPU_LOW::CicoSRCCPU_LOW(CicoSCSysResourceController* obj):
474 ICO_TRA("constructor");
475 m_val = -1; // cpu or memory usage value
476 m_baseVal = -1; // cpu or memory base usage value
478 m_rConf = CicoSCSystemConfig::getInstance()->getResourceConf();
482 * @brief Class ico System Resource Controller CPU LOW level
485 CicoSRCCPU_LOW::~CicoSRCCPU_LOW()
487 ICO_TRA("destructor");
492 * @brief resource control entry state
493 * @param ev event data
494 * @param stt state object
496 void CicoSRCCPU_LOW::onEntry(const CicoEvent& ev, const CicoState* stt, int)
499 int v2 = ev.getGCVi();
500 if (g_RC_LOG) ICO_TRA("start %s,%d,%d CicoSRCCPU_LOW::", stt->getName().c_str(), v1, v2);
503 m_cnt = m_rConf->m_retryCnt;
504 if (g_RC_LOG) ICO_TRA("end cnt(%d) CicoSRCCPU_LOW::", m_cnt);
508 * @brief cpu usage low resource control
509 * @param ev event data
510 * @param stt state object
512 void CicoSRCCPU_LOW::onDo(const CicoEvent& ev, const CicoState* stt, int)
514 int valN = ev.getGCVi();
515 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)CicoSRCCPU_LOW::", m_val, valN);
518 // When the value is greater than the previous
519 if (g_RC_LOG) ICO_TRA("end n(%d)->o(%d) CicoSRCCPU_LOW::", m_val, valN);
522 // When the value is less than the previous
523 int t = ((double)valN / (double)m_baseVal)*100;
525 m_cntlr->resourceControlCPUShares(false);
526 if (g_RC_LOG) ICO_DBG("CHG %d->%d CicoSRCCPU_LOW::",m_baseVal, valN);
530 if (valN < m_rConf->m_lowLimitVal) {
533 m_cntlr->resourceControlCPUShares(false);
534 if (g_RC_LOG) ICO_DBG("CHG %d->%d CicoSRCCPU_LOW::",m_baseVal, valN);
535 m_cnt = m_rConf->m_retryCnt;
540 if (g_RC_LOG) ICO_DBG("cnt rst V(%d) CicoSRCCPU_LOW::", valN);
541 m_cnt = m_rConf->m_retryCnt;
545 if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_LOW::");
550 * @brief resource control exit state
551 * @param ev event data
552 * @param stt state object
554 void CicoSRCCPU_LOW::onExit(const CicoEvent&, const CicoState*, int)
556 if (g_RC_LOG) ICO_TRA("start CicoSRCCPU_LOW::");
559 if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_LOW::");
563 * @brief Class ico System Resource Controller CPU HIGH level
566 CicoSRCCPU_HIGH::CicoSRCCPU_HIGH(CicoSCSysResourceController* obj):
569 ICO_TRA("constructor");
573 * @brief Class ico System Resource Controller CPU HIGH level
576 CicoSRCCPU_HIGH::~CicoSRCCPU_HIGH()
578 ICO_TRA("destructor");
582 * @brief cpu usage high resource control
583 * @param ev event data
584 * @param stt state object
586 void CicoSRCCPU_HIGH::onDo(const CicoEvent& ev, const CicoState* stt, int)
588 int valN = ev.getGCVi();
589 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d) CicoSRCCPU_HIGH::", m_val, valN);
592 // When the value is less than the previous
593 if (g_RC_LOG) ICO_TRA("n(%d)<=o(%d) CicoSRCCPU_HIGH::", valN, m_val);
596 // When the value is greater than the previous
597 int t = ((double)(100-valN) / (double)(100-m_baseVal))*100;
599 m_cntlr->resourceControlCPUShares(true);
600 if (g_RC_LOG) ICO_DBG("%d->%d CicoSRCCPU_HIGH::", m_baseVal, valN);
604 if (valN > m_rConf->m_highLimitVal) {
606 if (g_RC_LOG) ICO_DBG("cnt(%d) CicoSRCCPU_HIGH::", m_cnt);
608 m_cntlr->resourceControlCPUShares(true);
609 if (g_RC_LOG) ICO_DBG("CicoSRCCPU_HIGH:: %d->%d",m_baseVal, valN);
610 m_cnt = m_rConf->m_retryCnt;
615 if (g_RC_LOG) ICO_DBG("CicoSRCCPU_HIGH:: (%d)", valN);
616 m_cnt = m_rConf->m_retryCnt;
620 if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_HIGH::");
624 * @brief Class ico System Resource Controller MEMory LOW level
627 CicoSRCMEM_LOW::CicoSRCMEM_LOW(CicoSCSysResourceController* obj):
630 ICO_TRA("constructor CicoSRCMEM_LOW::");
634 * @brief Class ico System Resource Controller MEMory LOW level
637 CicoSRCMEM_LOW::~CicoSRCMEM_LOW()
639 ICO_TRA("destructor CicoSRCMEM_LOW::");
643 * @brief memory usage low resource control
644 * @param ev event data
645 * @param stt state object
647 void CicoSRCMEM_LOW::onDo(const CicoEvent& ev, const CicoState* stt, int)
649 int valN = ev.getGCVi();
650 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)", m_val, valN);
652 // When the value is greater than the previous
653 if (g_RC_LOG) ICO_TRA("n(%d)>=o(%d)", valN, m_val);
656 // When the value is less than the previous
657 int t = ((double)valN / (double)m_baseVal)*100;
659 m_cntlr->resourceControlMemLimit(false);
660 if (g_RC_LOG) ICO_TRA("BASE CHG %d->%d",m_baseVal, valN);
664 if (g_RC_LOG) ICO_TRA("end");
669 * @brief Class ico System Resource Controller MEMory HIGH level
672 CicoSRCMEM_HIGH::CicoSRCMEM_HIGH(CicoSCSysResourceController* obj):
675 ICO_TRA("constructor");
679 * @brief Class ico System Resource Controller MEMory HIGH level
682 CicoSRCMEM_HIGH::~CicoSRCMEM_HIGH()
684 ICO_TRA("destructor");
688 * @brief memory usage high resource control
689 * @param ev event data
690 * @param stt state object
692 void CicoSRCMEM_HIGH::onDo(const CicoEvent& ev, const CicoState* stt, int)
694 int valN = ev.getGCVi();
695 if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)", m_val, valN);
697 // When the value is less than the previous
701 // When the value is greater than the previous
702 int t = ((double)(100-valN) / (double)(100-m_baseVal))*100;
704 m_cntlr->resourceControlMemLimit(true);
705 if (g_RC_LOG) ICO_TRA("BASE CHG %d -> %d", m_baseVal, valN);
709 if (g_RC_LOG) ICO_TRA("end");