297fa13ae757a99c8e916b692cafb6d58bd2811e
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCSysResourceController.cpp
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 #include <memory>
10 #include <iostream>
11 #include <fstream>
12 #include <cstdlib>
13 #include <cstdio>
14
15 #include <dirent.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18
19 #include "CicoLog.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 "CicoConf.h"
32 #include "CicoSystemConfig.h"
33 #include <Ecore.h>
34 //#include <Ecore_Evas.h>
35
36 const int gEV1000 = 1000; // CPU event
37 const int gEV1001 = 1001; // MEMORY event
38
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";
43
44 static const string gstrStateMachineFileName = "CicoSCSysResourceSM.json";
45
46 bool g_RC_LOG = false;
47 int  g_samplingWaitTimer = 200;
48
49 Ecore_Timer* g_SCResourceWatch = 0;
50 static Eina_Bool ico_SCResourceWatch(void *data)
51 {
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;
56     }
57     CicoSCSysResourceController* oCSCSRC = (CicoSCSysResourceController*)data;
58     oCSCSRC->watch();
59     if (g_RC_LOG) ICO_TRA("end");
60     return ECORE_CALLBACK_RENEW;
61 }
62
63
64 /**
65  * @brief constructor
66  */
67 CicoSCSysResourceController::CicoSCSysResourceController()
68 {
69     ICO_TRA("start");
70     m_cgrpCPU = 0;
71     m_cgrpMEM = 0;
72     m_cpu = -1;
73     m_mem = -1;
74     m_bDoIt = false;
75     m_rConf = CicoSystemConfig::getInstance()->getResourceConf();
76     if (0 != m_rConf) {
77         g_RC_LOG = m_rConf->m_bLog;
78         if (-1 != m_rConf->m_sampling) {
79             g_samplingWaitTimer = m_rConf->m_sampling;
80         }
81     }
82     // cpu,cpuacct cgroup init
83     ICO_TRA("end");
84 }
85
86 /**
87  * @brief destructor
88  */
89 CicoSCSysResourceController::~CicoSCSysResourceController()
90 {
91     ICO_TRA("start");
92     if (0 != g_SCResourceWatch) {
93         ecore_timer_del(g_SCResourceWatch);
94     }
95
96     if ((CicoStateMachine*)0 != m_stt) {
97         m_stt->stop();
98         delete m_stt;
99         m_stt = (CicoStateMachine*)0;
100         ICO_TRA("delete CicoStateMachine");
101     }
102
103     if ((CicoSRCCPU_LOW*)0 != m_SRCCPU_LOW) {
104         delete m_SRCCPU_LOW;
105         m_SRCCPU_LOW = (CicoSRCCPU_LOW*)0;
106         ICO_TRA("delete CicoSRCCPU_LOW");
107     }
108
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");
113     }
114
115     if ((CicoSRCMEM_LOW*)0 != m_SRCMEM_LOW) {
116         delete m_SRCMEM_LOW;
117         m_SRCMEM_LOW = (CicoSRCMEM_LOW*)0;
118         ICO_TRA("delete CicoSRCMEM_LOW");
119     }
120
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");
125     }
126
127     if ((CicoSCSysResourceMonitor*)0 != m_monitor) {
128         delete m_monitor;
129         m_monitor = (CicoSCSysResourceMonitor*)0;
130         ICO_TRA("delete CicoSCSysResourceMonitor");
131     }
132     ICO_TRA("end");
133 }
134
135 /**
136  * @brief startWatch
137  */
138 bool CicoSCSysResourceController::startSysResource()
139 {
140     ICO_TRA("start");
141     if (0 == m_rConf) {
142         ICO_TRA("end");
143         return false;
144     }
145     m_bDoIt = m_rConf->m_bDoIt;
146     if (false == m_bDoIt) {
147         ICO_TRA("end");
148         return false;
149     }
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;
157
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());
162         ICO_TRA("end");
163         return false;
164     }
165
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);
170
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);
175     }
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);
180     }
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);
185     }
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);
190     }
191     m_stt->start();
192     double tVal = (double)g_samplingWaitTimer / 100;
193     ICO_TRA("ECORE_TIMER(%f)", tVal);
194     g_SCResourceWatch = ecore_timer_add(tVal, ico_SCResourceWatch, this);
195     ICO_TRA("end");
196     return true;
197 }
198
199 /**
200  * @brief watch
201  * @return true:change level false:no change
202  */
203 bool CicoSCSysResourceController::watch()
204 {
205     if (g_RC_LOG) ICO_TRA("start");
206     if ((CicoSCSysResourceMonitor*)0 == m_monitor) {
207         if (g_RC_LOG) ICO_TRA("end false");
208         return false;
209     }
210     bool r = false;
211     m_monitor->monitoring();
212     int cpu = m_monitor->getCPUuser(); // application cpu
213     if (-1 != cpu) {
214         cpu /= 100;
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);
218         m_cpu = cpu;
219         r = true;
220     }
221     int mem = m_monitor->getUsageFreeMem(); // free memory
222     if (-1 != mem) {
223         mem = (10000 - mem)/100;  // free->used memory calc
224         CicoEvent ev(gEV1001, mem);
225         m_stt->eventEntry(ev);
226         m_mem = mem;
227         r = true;
228     }
229     if (g_RC_LOG) ICO_TRA("end %s", r? "true": "false");
230     return r;
231 }
232
233 /**
234  * @brief CPU share control request
235  * @param pulus true:level up / false level down
236  */
237 void CicoSCSysResourceController::resourceControlCPUShares(bool bHight)
238 {
239     ICO_TRA("start %s", bHight? "true": "false");
240     if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
241         ICO_TRA("end");
242         return;
243     }
244     if (true == m_rConf->m_cpuCtrl.empty()) {
245         ICO_TRA("end");
246         return;
247     }
248     int newCgrpCPU = -1;
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
253         }
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);
260             }
261         }
262         else {
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];
267                     newCgrpCPU = tmp;
268                     ICO_DBG("m_low[%d]=%d", tmp, sharesVal);
269                 }
270             }
271         }
272         if (0 <= sharesVal) {
273             replaceCgroupFile(m_RCCpuShares[i], sharesVal);
274         }
275 //        else if (-9 == sharesVal) {
276 //        }
277     }
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
281     }
282     ICO_TRA("end");
283 }
284
285 bool CicoSCSysResourceController::replaceCgroupFile(const std::string& tgt,
286                                                     int val)
287 {
288     char sv[64];
289     sprintf(sv, "%d", val);
290     return replaceCgroupFile(tgt, sv);
291 }
292
293 bool CicoSCSysResourceController::replaceCgroupFile(const std::string& tgt,
294                                                     const char* val)
295 {
296     ICO_TRA("start");
297     if (true == tgt.empty()) {
298         ICO_TRA("end");
299         return false;
300     }
301     FILE *fp = fopen(tgt.c_str(), "wt");
302     if (0 == fp) {
303         ICO_TRA("end open error %s", tgt.c_str());
304         return false;
305     }
306     fprintf(fp, val);
307     fclose(fp);
308     ICO_TRA("end Cmd(echo %s > %s", val, tgt.c_str());
309     return true;
310 }
311
312 /**
313  * @brief memory limit control request
314  * @param pulus true:level up / false level down
315  */
316 void CicoSCSysResourceController::resourceControlMemLimit(bool bHight)
317 {
318     ICO_TRA("start");
319     if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
320         ICO_TRA("end");
321         return;
322     }
323     // TODO mk_k coming soon
324     if (true == bHight) {
325         // coming soon
326         ICO_TRA("hight (true)");
327     }
328     else {
329         // coming soon
330         ICO_TRA("low (false)");
331     }
332 //    m_cgrpMEM++;
333     ICO_TRA("end");
334 }
335
336
337
338 /**
339  * @brief
340  *
341  */
342 bool CicoSCSysResourceController::entryCgroupCPU(int pid, int grpNo)
343 {
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());
348         return false;
349     }
350 #if 1
351     struct stat statbuf;
352     char procdir[128];
353     sprintf(procdir, "/proc/%d/task", pid);
354     if (0 != stat(procdir, &statbuf)) {
355         ICO_TRA("end %s ng dir", procdir);
356         return false;
357     }
358     DIR *dp;
359     dp = opendir(procdir);
360     if(dp == NULL) {
361         ICO_TRA("end opendir error");
362         return false;
363     }
364     struct dirent *ent;
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);
369         }
370     }
371     closedir(dp);
372     ICO_TRA("end");
373     return true;
374 #else
375     return replaceCgroupFile(m_RCCpuTasks[grpNo], pid);
376 #endif
377 }
378
379 /**
380  * @brief initialize cgroup
381  */
382 void CicoSCSysResourceController::init_cgroup()
383 {
384     ICO_TRA("start");
385     init_cgroup_cpu();
386     // memory cgroup directory get
387     init_cgroup_memory();
388     ICO_TRA("end");
389 }
390
391 /**
392  * @brief initialize cgroup cpu
393  */
394 void CicoSCSysResourceController::init_cgroup_cpu()
395 {
396     ICO_TRA("start");
397     if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
398         ICO_TRA("end");
399         return;
400     }
401     string ss("/");
402     string sTasks(D_STRtasks);
403     string sShares(D_STRcpushares);
404     string t1(m_rConf->m_cpuCGRPPath);
405     if ('/' != t1[t1.size()-1]) {
406         t1 += ss;
407     }
408     vector<string> d;
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];
413         string t2;
414         if (0 != obj) {
415             t2 = t1;
416             t2 += obj->m_grpNm;
417         }
418         else {
419             t2.clear();
420         }
421         d.push_back(t2);
422         string t3(t2);
423         string t4(t2);
424         if (false == t2.empty()) {
425             if ('/' != t2[t2.size()-1]) {
426                 t3 += ss;
427                 t4 += ss;
428             }
429             t3 += sTasks;
430             t4 += sShares;
431         }
432         m_RCCpuTasks.push_back(t3);
433         m_RCCpuShares.push_back(t4);
434     }
435     make_directorys(d);
436     ICO_TRA("end");
437     return;
438 }
439
440 /**
441  * @brief initialize cgroup memory
442  */
443 void CicoSCSysResourceController::init_cgroup_memory()
444 {
445     ICO_TRA("start");
446     if ((NULL == m_rConf) || (false == m_rConf->m_bDoIt)) {
447         ICO_TRA("end error config class");
448         return;
449     }
450     // Coming Soon
451     ICO_TRA("end");
452     return;
453 }
454
455 /**
456  * @brief make directory
457  * @param d create directory target
458  */
459 void CicoSCSysResourceController::make_directorys(std::vector<std::string> dir)
460 {
461     ICO_TRA("start");
462     int sz = dir.size();
463     for (int i = 0; i < sz; i++) {
464         if (true == dir[i].empty()) {
465             continue;
466         }
467         const char* dpath = dir[i].c_str();
468         struct stat stat_buf;
469         if (0 != stat(dpath, &stat_buf)) {
470             // drwxr-xr-x
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);
474         }
475     }
476     ICO_TRA("end");
477 }
478
479 /**
480  * @brief Class ico System Resource Controller CPU LOW level
481  *        constructor
482  */
483 CicoSRCCPU_LOW::CicoSRCCPU_LOW(CicoSCSysResourceController* obj):
484     m_cntlr(obj)
485 {
486     ICO_TRA("constructor");
487     m_val = -1;      // cpu or memory usage value
488     m_baseVal = -1;  // cpu or memory base usage value
489     m_cnt = 0;
490     m_rConf = CicoSystemConfig::getInstance()->getResourceConf();
491 }
492
493 /**
494  * @brief Class ico System Resource Controller CPU LOW level
495  *        destructor
496  */
497 CicoSRCCPU_LOW::~CicoSRCCPU_LOW()
498 {
499     ICO_TRA("destructor");
500     m_cntlr = NULL;
501 }
502
503 /**
504  * @brief resource control entry state
505  * @param ev event data
506  * @param stt state object
507  */
508 void CicoSRCCPU_LOW::onEntry(const CicoEvent& ev, const CicoState* stt, int)
509 {
510     int v1 = ev.getEV();
511     int v2 = ev.getGCVi();
512     if (g_RC_LOG) ICO_TRA("start %s,%d,%d CicoSRCCPU_LOW::", stt->getName().c_str(), v1, v2);
513     m_baseVal = v2;
514     m_val = m_baseVal;
515     m_cnt = m_rConf->m_retryCnt;
516     if (g_RC_LOG) ICO_TRA("end cnt(%d) CicoSRCCPU_LOW::", m_cnt);
517 }
518
519 /**
520  * @brief cpu usage low resource control
521  * @param ev event data
522  * @param stt state object
523  */
524 void CicoSRCCPU_LOW::onDo(const CicoEvent& ev, const CicoState* stt, int)
525 {
526     int valN = ev.getGCVi();
527     if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)CicoSRCCPU_LOW::", m_val, valN);
528 #if 0
529     if (valN > m_val) {
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);
532         return;
533     }
534     // When the value is less than the previous
535     int t = ((double)valN / (double)m_baseVal)*100;
536     if (50 <= t) {
537         m_cntlr->resourceControlCPUShares(false);
538         if (g_RC_LOG) ICO_DBG("CHG %d->%d CicoSRCCPU_LOW::",m_baseVal, valN);
539         m_baseVal = valN;
540     }
541 #else
542     if (valN < m_rConf->m_lowLimitVal) {
543         m_cnt--;
544         if (0 >= m_cnt) {
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;
548             m_baseVal = valN;
549         }
550     }
551     else {
552         if (g_RC_LOG) ICO_DBG("cnt rst V(%d) CicoSRCCPU_LOW::", valN);
553         m_cnt = m_rConf->m_retryCnt;
554     }
555 #endif
556     m_val = valN;
557     if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_LOW::");
558 }
559
560
561 /**
562  * @brief resource control exit state
563  * @param ev event data
564  * @param stt state object
565  */
566 void CicoSRCCPU_LOW::onExit(const CicoEvent&, const CicoState*, int)
567 {
568     if (g_RC_LOG) ICO_TRA("start CicoSRCCPU_LOW::");
569     m_baseVal = -1;
570     m_val = -1;
571     if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_LOW::");
572 }
573
574 /**
575  * @brief Class ico System Resource Controller CPU HIGH level
576  *        constructor
577  */
578 CicoSRCCPU_HIGH::CicoSRCCPU_HIGH(CicoSCSysResourceController* obj):
579         CicoSRCCPU_LOW(obj)
580 {
581     ICO_TRA("constructor");
582 }
583
584 /**
585  * @brief Class ico System Resource Controller CPU HIGH level
586  *        destructor
587  */
588 CicoSRCCPU_HIGH::~CicoSRCCPU_HIGH()
589 {
590     ICO_TRA("destructor");
591 }
592
593 /**
594  * @brief cpu usage high resource control
595  * @param ev event data
596  * @param stt state object
597  */
598 void CicoSRCCPU_HIGH::onDo(const CicoEvent& ev, const CicoState* stt, int)
599 {
600     int valN = ev.getGCVi();
601     if (g_RC_LOG) ICO_TRA("start o(%d), n(%d) CicoSRCCPU_HIGH::", m_val, valN);
602 #if 0
603     if (valN <= m_val) {
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);
606         return;
607     }
608     // When the value is greater than the previous
609     int t = ((double)(100-valN) / (double)(100-m_baseVal))*100;
610     if (50 <= t) {
611         m_cntlr->resourceControlCPUShares(true);
612         if (g_RC_LOG) ICO_DBG("%d->%d CicoSRCCPU_HIGH::", m_baseVal, valN);
613         m_baseVal = valN;
614     }
615 #else
616     if (valN > m_rConf->m_highLimitVal) {
617         m_cnt--;
618         if (g_RC_LOG) ICO_DBG("cnt(%d) CicoSRCCPU_HIGH::", m_cnt);
619         if (0 >= 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;
623             m_baseVal = valN;
624         }
625     }
626     else {
627         if (g_RC_LOG) ICO_DBG("CicoSRCCPU_HIGH:: (%d)", valN);
628         m_cnt = m_rConf->m_retryCnt;
629     }
630 #endif
631     m_val = valN;
632     if (g_RC_LOG) ICO_TRA("end CicoSRCCPU_HIGH::");
633 }
634
635 /**
636  * @brief Class ico System Resource Controller MEMory LOW level
637  *        constructor
638  */
639 CicoSRCMEM_LOW::CicoSRCMEM_LOW(CicoSCSysResourceController* obj):
640         CicoSRCCPU_LOW(obj)
641 {
642     ICO_TRA("constructor CicoSRCMEM_LOW::");
643 }
644
645 /**
646  * @brief Class ico System Resource Controller MEMory LOW level
647  *        constructor
648  */
649 CicoSRCMEM_LOW::~CicoSRCMEM_LOW()
650 {
651     ICO_TRA("destructor CicoSRCMEM_LOW::");
652 }
653
654 /**
655  * @brief memory usage low resource control
656  * @param ev event data
657  * @param stt state object
658  */
659 void CicoSRCMEM_LOW::onDo(const CicoEvent& ev, const CicoState* stt, int)
660 {
661     int valN = ev.getGCVi();
662     if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)", m_val, valN);
663     if (valN >= m_val) {
664         // When the value is greater than the previous
665         if (g_RC_LOG) ICO_TRA("n(%d)>=o(%d)", valN, m_val);
666         return;
667     }
668     // When the value is less than the previous
669     int t = ((double)valN / (double)m_baseVal)*100;
670     if (50 <= t) {
671         m_cntlr->resourceControlMemLimit(false);
672         if (g_RC_LOG) ICO_TRA("BASE CHG %d->%d",m_baseVal, valN);
673         m_baseVal = valN;
674     }
675     m_val = valN;
676     if (g_RC_LOG) ICO_TRA("end");
677
678 }
679
680 /**
681  * @brief Class ico System Resource Controller MEMory HIGH level
682  *        constructor
683  */
684 CicoSRCMEM_HIGH::CicoSRCMEM_HIGH(CicoSCSysResourceController* obj):
685     CicoSRCCPU_LOW(obj)
686 {
687     ICO_TRA("constructor");
688 }
689
690 /**
691  * @brief Class ico System Resource Controller MEMory HIGH level
692  *        destructor
693  */
694 CicoSRCMEM_HIGH::~CicoSRCMEM_HIGH()
695 {
696     ICO_TRA("destructor");
697 }
698
699 /**
700  * @brief memory usage high resource control
701  * @param ev event data
702  * @param stt state object
703  */
704 void CicoSRCMEM_HIGH::onDo(const CicoEvent& ev, const CicoState* stt, int)
705 {
706     int valN = ev.getGCVi();
707     if (g_RC_LOG) ICO_TRA("start o(%d), n(%d)", m_val, valN);
708     if (valN <= m_val) {
709         // When the value is less than the previous
710         ICO_TRA("end");
711         return;
712     }
713     // When the value is greater than the previous
714     int t = ((double)(100-valN) / (double)(100-m_baseVal))*100;
715     if (50 <= t) {
716         m_cntlr->resourceControlMemLimit(true);
717         if (g_RC_LOG) ICO_TRA("BASE CHG %d -> %d", m_baseVal, valN);
718         m_baseVal = valN;
719     }
720     m_val = valN;
721     if (g_RC_LOG) ICO_TRA("end");
722 }