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
20 #include <sys/types.h>
22 #include <boost/property_tree/ptree.hpp>
23 #include <boost/property_tree/json_parser.hpp>
24 #include <boost/foreach.hpp>
25 #include <boost/optional.hpp>
28 #include "CicoSCCommand.h"
29 #include "CicoSCSysResourceController.h"
30 #include "CicoSCAppResourceController.h"
31 #include "CicoSCLifeCycleController.h"
32 #include "CicoSCUserManager.h"
34 #include "CicoSystemConfig.h"
35 #include "CicoLibString.h"
37 //#include <Ecore_Evas.h>
42 CicoSCAppResourceController::CicoSCAppResourceController()
53 CicoSCAppResourceController::~CicoSCAppResourceController()
60 * @brief startAppResource
62 bool CicoSCAppResourceController::initAppResource()
65 if (true == m_bDoIt) {
73 m_bDoItApp = m_rConf->m_bDoItApp;
74 if (false == m_bDoItApp) {
84 * @brief startAppResource
86 bool CicoSCAppResourceController::startAppResource(const string& un)
88 ICO_TRA("start %s", un.c_str());
89 if (false == m_bDoItApp) {
95 getAppResourceFilePath(un, fn);
96 if (false == orderApps(fn)) {
106 * @brief get application resouce file path
107 * @param un user name
108 * @param fi file path application resource file
110 void CicoSCAppResourceController::getAppResourceFilePath(const string& un,
113 CicoSCUserManager *cscum = CicoSCUserManager::getInstance();
114 cscum->getWorkingDir(un, fp);
115 fp += D_STRappResource;
119 * @brief oder application resource
121 bool CicoSCAppResourceController::orderApps(const string& filepath)
124 if (false == m_bDoItApp) {
130 if (false == readApps(filepath)) {
131 ICO_TRA("end fail:read");
136 if (0 == m_vAppid.size()) {
137 ICO_TRA("end fail read2");
140 init_cgroup_app_dir();
141 init_cgroup_app_cpushare();
147 * @param filepath file name (full path)
149 bool CicoSCAppResourceController::readApps(const string& filepath)
151 ICO_TRA("start file:%s", filepath.c_str());
152 if (false == m_bDoItApp) {
161 ifstream ifs(filepath.c_str());
163 while(ifs.getline(sBuff,sizeof(sBuff))) {
164 string tagApp(sBuff);
165 if (true == tagApp.empty()) {
169 split(tagApp, ',', vTmp);
170 if (2 == vTmp.size()) {
171 string appid = trim(vTmp[0]);
172 string share = trim(vTmp[1]);
173 int iShare = atoi(share.c_str());
176 ICO_DBG("change %d -> 100", iShare);
179 iShare = (int)(d1 * ((double)iShare) / d2);
180 ICO_DBG("TARGET_OK %s, %d", appid.c_str(), iShare);
181 m_vAppid.push_back(appid);
182 m_vShares.push_back(iShare);
186 ICO_DBG("TARGET_NG %s, >> %s <<", appid.c_str(), share.c_str());
191 ICO_DBG("TARGET_NG >> %s <<", sBuff);
199 memset(&buff, 0, sizeof(buff));
200 if (0 != stat(filepath.c_str(), &buff)) {
208 read_json(filepath.c_str(), pt);
210 BOOST_FOREACH (const ptree::value_type& child,
211 pt.get_child(DSTRcpu_shares)) {
212 const ptree& info = child.second;
217 if (boost::optional<int> iVal = info.get_optional<int>(DSTRshare)) {
218 iShareO = iVal.get();
221 ICO_DBG("NG get share");
223 break; // break of BOOST_FOREACH
227 if (boost::optional<std::string> sVal =
228 info.get_optional<std::string>(DSTRappid)) {
232 ICO_DBG("NG get appid");
234 break; // break of BOOST_FOREACH
237 int iShare = iShareO;
241 iShare = (int)(d1 * ((double)iShare) / d2);
242 ICO_DBG("TARGET_GET %d(%d), %s", iShare, iShareO, appid.c_str());
243 m_vAppid.push_back(appid);
244 m_vShares.push_back(iShare);
249 ICO_WRN("NG read(%s)", filepath.c_str());
255 ICO_TRA("end %d", (int)bR);
259 ICO_DBG("read %d, %d, %d, %d", m_vAppid.size(), m_vShares.size(),
260 m_RCCpuTasks.size(), m_RCCpuShares.size());
261 ICO_TRA("end %d", (int)bR);
266 * @brief initialize cgroup application
268 void CicoSCAppResourceController::init_cgroup_app_dir()
271 if (false == m_bDoItApp) {
275 string sTasks(D_STRtasks);
276 string sShares(D_STRcpushares);
277 string t1(m_rConf->m_cpuCGRPPath);
280 string tDir1(m_rConf->m_cpuCGRPPath);
283 chkAndAddSlash(tDir1);
285 vector<string>::iterator it = m_vAppid.begin();
286 for (; it != m_vAppid.end(); it++) {
287 // make cgroup apps directroy full path
291 chkAndAddSlash(tDir2);
292 // make cgroup tasks file full path
293 string tDir3tasks(tDir2);
294 tDir3tasks += sTasks;
295 m_RCCpuTasks.push_back(tDir3tasks);
296 // make cgroup cpu.shares file full path
297 string tDir4cpushare(tDir2);
298 tDir4cpushare += sShares;
299 m_RCCpuShares.push_back(tDir4cpushare);
307 * @brief initialize cgroup application
309 void CicoSCAppResourceController::init_cgroup_app_cpushare()
312 if (false == m_bDoItApp) {
317 vector<string>::iterator itDir = m_RCCpuShares.begin();
318 vector<int>::iterator itShare = m_vShares.begin();
319 while (itDir != m_RCCpuShares.end()) {
320 replaceCgroupFile(*itDir, *itShare);
331 bool CicoSCAppResourceController::clearApps()
334 if (false == m_bDoItApp) {
340 m_RCCpuTasks.clear();
341 m_RCCpuShares.clear();
342 ICO_TRA("end fail:read");
351 bool CicoSCAppResourceController::entryApp(const string& appid, int pid)
353 ICO_TRA("start appid(%s), pid(%d)", appid.c_str(), pid);
354 if (false == m_bDoItApp) {
358 vector<string>::iterator itTasks = m_RCCpuTasks.begin();
359 vector<string>::iterator itAppid = m_vAppid.begin();
362 while (itTasks != m_RCCpuTasks.end()) {
363 if (0 == (*itAppid).compare(appid.c_str())) {
370 if (true == tasks.empty()) {
371 ICO_TRA("end no match appid");
374 replaceCgroupFile(tasks, pid);
375 ICO_TRA("end write cgroup %s, %d", tasks.c_str(), pid);
380 * @brief entry application's
383 bool CicoSCAppResourceController::entryApps(vector<CicoAulItems>& vAulItem)
385 ICO_TRA("start size(%d)", vAulItem.size());
386 if (false == m_bDoItApp) {
391 vector<CicoAulItems>::iterator it = vAulItem.begin();
392 while (it != vAulItem.end()) {
393 entryApp(it->m_appid, it->m_pid);
397 ICO_TRA("end %s", bR? "true": "false");
402 * @brief create applications Resource file
403 * @param file path create target
405 void CicoSCAppResourceController::createAppResourceFile(const string& filepath)
407 ICO_TRA("start %s", filepath.c_str());
410 ofs.open(filepath.c_str(), ios::trunc);
411 const vector<CicoAilItems>& vAil =
412 CicoSCLifeCycleController::getInstance()->getAilList();
413 vector<CicoAilItems>::const_iterator it = vAil.begin();
414 for (; it != vAil.end(); ++it) {
415 ofs << (*it).m_appid << ",100" << endl;
423 const vector<CicoAilItems>& vAil =
424 CicoSCLifeCycleController::getInstance()->getAilList();
425 vector<CicoAilItems>::const_iterator it = vAil.begin();
426 for (; it != vAil.end(); ++it) {
429 info.put(DSTRshare, 100);
430 info.put(DSTRappid, (*it).m_appid.c_str());
431 child.push_back(std::make_pair("", info));
434 pt.add_child(DSTRcpu_shares, child);
435 write_json(filepath.c_str(), pt);
437 catch (std::exception const& e) {
438 ICO_WRN("catch exception %s", e.what());
441 ICO_WRN("NG write(%s)", filepath.c_str());