Adapt the AIL filter used to new ones
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCAppResourceController.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 #include <string>
15 #include <vector>
16 #include <sstream>
17 #include <utility>
18
19 #include <dirent.h>
20 #include <sys/types.h>
21 #include <sys/stat.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>
26
27 #include "CicoLog.h"
28 #include "CicoSCCommand.h"
29 #include "CicoSCSysResourceController.h"
30 #include "CicoSCAppResourceController.h"
31 #include "CicoSCLifeCycleController.h"
32 #include "CicoSCUserManager.h"
33 #include "CicoConf.h"
34 #include "CicoSystemConfig.h"
35 #include "CicoLibString.h"
36 #include <Ecore.h>
37 //#include <Ecore_Evas.h>
38
39 /**
40  * @brief constructor
41  */
42 CicoSCAppResourceController::CicoSCAppResourceController()
43 {
44     ICO_TRA("start");
45     m_bDoItApp = false;
46     m_filepath.clear();
47     ICO_TRA("end");
48 }
49
50 /**
51  * @brief destructor
52  */
53 CicoSCAppResourceController::~CicoSCAppResourceController()
54 {
55     ICO_TRA("start");
56     ICO_TRA("end");
57 }
58
59 /**
60  * @brief startAppResource
61  */
62 bool CicoSCAppResourceController::initAppResource()
63 {
64     ICO_TRA("start");
65     if (true == m_bDoIt) {
66         ICO_TRA("end");
67         return false;
68     }
69     if (0 == m_rConf) {
70         ICO_TRA("end");
71         return false;
72     }
73     m_bDoItApp = m_rConf->m_bDoItApp;
74     if (false == m_bDoItApp) {
75         ICO_TRA("end");
76         return false;
77     }
78     m_filepath.clear();
79     ICO_TRA("end");
80     return true;
81 }
82
83 /**
84  * @brief startAppResource
85  */
86 bool CicoSCAppResourceController::startAppResource(const string& un)
87 {
88     ICO_TRA("start %s", un.c_str());
89     if (false == m_bDoItApp) {
90         ICO_TRA("end");
91         return false;
92     }
93
94     string fn;
95     getAppResourceFilePath(un, fn);
96     if (false == orderApps(fn)) {
97         ICO_TRA("end");
98         return false;
99     }
100     m_filepath = fn;
101     ICO_TRA("end");
102     return true;
103 }
104
105 /**
106  * @brief get application resouce file path
107  * @param un user name
108  * @param fi file path application resource file
109  */
110 void CicoSCAppResourceController::getAppResourceFilePath(const string& un,
111                                                          string& fp) const
112 {
113     CicoSCUserManager *cscum = CicoSCUserManager::getInstance();
114     cscum->getWorkingDir(un, fp);
115     fp += D_STRappResource;
116 }
117
118 /**
119  * @brief oder application resource
120  */
121 bool CicoSCAppResourceController::orderApps(const string& filepath)
122 {
123     ICO_TRA("start");
124     if (false == m_bDoItApp) {
125         ICO_TRA("end");
126         return false;
127     }
128     clearApps();
129
130     if (false == readApps(filepath)) {
131         ICO_TRA("end fail:read");
132         return false;
133     }
134
135     // read finish
136     if (0 == m_vAppid.size()) {
137         ICO_TRA("end fail read2");
138         return false;
139     }
140     init_cgroup_app_dir();
141     init_cgroup_app_cpushare();
142     return false;
143 }
144
145 /**
146  * @brief read app
147  * @param filepath file name (full path)
148  */
149 bool CicoSCAppResourceController::readApps(const string& filepath)
150 {
151     ICO_TRA("start file:%s", filepath.c_str());
152     if (false == m_bDoItApp) {
153         ICO_TRA("end");
154         return false;
155     }
156     bool bR = false;
157     double d1 = 1024.0;
158     double d2 = 100.0;
159 #if 0 // TODO
160     // text file read
161     ifstream ifs(filepath.c_str());
162     char sBuff[256];
163     while(ifs.getline(sBuff,sizeof(sBuff))) {
164         string tagApp(sBuff);
165         if (true == tagApp.empty()) {
166             continue;
167         }
168         vector<string> vTmp;
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());
174             if (0 != iShare) {
175                 if (100 < iShare) {
176                     ICO_DBG("change %d -> 100", iShare);
177                     iShare = 100;
178                 }
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);
183                 bR = true;
184             }
185             else {
186                 ICO_DBG("TARGET_NG %s, >> %s <<", appid.c_str(), share.c_str());
187             }
188
189         }
190         else {
191             ICO_DBG("TARGET_NG >> %s <<", sBuff);
192         }
193         vTmp.clear();
194     }
195     ifs.close();
196 #else
197     struct stat buff;
198     /* file check */
199     memset(&buff, 0, sizeof(buff));
200     if (0 != stat(filepath.c_str(), &buff)) {
201         ICO_TRA("end");
202         return false;
203     }
204     bool bStt=true;
205     // json read
206     try {
207         ptree pt;
208         read_json(filepath.c_str(), pt);
209         // cpu_shares
210         BOOST_FOREACH (const ptree::value_type& child,
211                        pt.get_child(DSTRcpu_shares)) {
212             const ptree& info = child.second;
213
214             string appid;
215             int iShareO;
216             // cpu_shares.share
217             if (boost::optional<int> iVal = info.get_optional<int>(DSTRshare)) {
218                 iShareO = iVal.get();
219             }
220             else {
221                 ICO_DBG("NG get share");
222                 bStt=false;
223                 break; // break of BOOST_FOREACH
224             }
225
226             // cpu_shares.appid
227             if (boost::optional<std::string> sVal =
228                     info.get_optional<std::string>(DSTRappid)) {
229                 appid = sVal.get();
230             }
231             else {
232                 ICO_DBG("NG get appid");
233                 bStt=false;
234                 break; // break of BOOST_FOREACH
235             }
236             // DATA get!
237             int iShare = iShareO;
238             if (100 < iShare) {
239                 iShare = 100;
240             }
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);
245             bR = true;
246         }
247     }
248     catch (...) {
249         ICO_WRN("NG read(%s)", filepath.c_str());
250         bStt = false;
251     }
252     if (false == bStt) {
253         m_vAppid.clear();
254         m_vShares.clear();
255         ICO_TRA("end %d", (int)bR);
256         return false;
257     }
258 #endif
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);
262     return bR;
263 }
264
265 /**
266  * @brief initialize cgroup application
267  */
268 void CicoSCAppResourceController::init_cgroup_app_dir()
269 {
270     ICO_TRA("start");
271     if (false == m_bDoItApp) {
272         ICO_TRA("end");
273         return;
274     }
275     string sTasks(D_STRtasks);
276     string sShares(D_STRcpushares);
277     string t1(m_rConf->m_cpuCGRPPath);
278     chkAndAddSlash(t1);
279
280     string tDir1(m_rConf->m_cpuCGRPPath);
281     vector<string> d;
282     d.push_back(tDir1);
283     chkAndAddSlash(tDir1);
284
285     vector<string>::iterator it = m_vAppid.begin();
286     for (; it != m_vAppid.end(); it++) {
287         // make cgroup apps directroy full path
288         string tDir2(tDir1);
289         tDir2 += *it;
290         d.push_back(tDir2);
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);
300     }
301     make_directorys(d);
302     ICO_TRA("end");
303     return;
304 }
305
306 /**
307  * @brief initialize cgroup application
308  */
309 void CicoSCAppResourceController::init_cgroup_app_cpushare()
310 {
311     ICO_TRA("start");
312     if (false == m_bDoItApp) {
313         ICO_TRA("end");
314         return;
315     }
316
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);
321         ++itDir;
322         ++itShare;
323     }
324     ICO_TRA("end");
325     return;
326 }
327
328 /**
329  * @brief clear Apps
330  */
331 bool CicoSCAppResourceController::clearApps()
332 {
333     ICO_TRA("start");
334     if (false == m_bDoItApp) {
335         ICO_TRA("end");
336         return false;
337     }
338     m_vAppid.clear();
339     m_vShares.clear();
340     m_RCCpuTasks.clear();
341     m_RCCpuShares.clear();
342     ICO_TRA("end fail:read");
343     return true;
344 }
345
346 /**
347  * @brief entry
348  * @param appid 
349  * @param pid
350  */
351 bool CicoSCAppResourceController::entryApp(const string& appid, int pid)
352 {
353     ICO_TRA("start appid(%s), pid(%d)", appid.c_str(), pid);
354     if (false == m_bDoItApp) {
355         ICO_TRA("end");
356         return false;
357     }
358     vector<string>::iterator itTasks = m_RCCpuTasks.begin();
359     vector<string>::iterator itAppid = m_vAppid.begin();
360     string tasks;
361     tasks.clear();
362     while (itTasks != m_RCCpuTasks.end()) {
363         if (0 == (*itAppid).compare(appid.c_str())) {
364             tasks = *itTasks;
365             break;
366         }
367         ++itTasks;
368         ++itAppid;
369     }
370     if (true == tasks.empty()) {
371         ICO_TRA("end no match appid");
372         return false;
373     }
374     replaceCgroupFile(tasks, pid);
375     ICO_TRA("end write cgroup %s, %d", tasks.c_str(), pid);
376     return true;
377 }
378
379 /**
380  * @brief entry application's
381  * @param vAulItem
382  */
383 bool CicoSCAppResourceController::entryApps(vector<CicoAulItems>& vAulItem)
384 {
385     ICO_TRA("start size(%d)", vAulItem.size());
386     if (false == m_bDoItApp) {
387         ICO_TRA("end");
388         return false;
389     }
390     bool bR = false;
391     vector<CicoAulItems>::iterator it = vAulItem.begin();
392     while (it != vAulItem.end()) {
393         entryApp(it->m_appid, it->m_pid);
394         bR = true;
395         ++it;
396     }
397     ICO_TRA("end %s", bR? "true": "false");
398     return bR;
399 }
400
401 /**
402  * @brief create applications Resource file
403  * @param file path create target
404  */
405 void CicoSCAppResourceController::createAppResourceFile(const string& filepath)
406 {
407     ICO_TRA("start %s", filepath.c_str());
408 #if 0
409     ofstream ofs;
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;
416     }
417     ofs.close();
418 #else
419     try{
420         ptree pt;
421         ptree child;
422
423         const vector<CicoAilItems>& vAil =
424             CicoSCLifeCycleController::getInstance()->getAilList();
425         vector<CicoAilItems>::const_iterator it = vAil.begin();
426         for (; it != vAil.end(); ++it) {
427             {
428                 ptree info;
429                 info.put(DSTRshare, 100);
430                 info.put(DSTRappid, (*it).m_appid.c_str());
431                 child.push_back(std::make_pair("", info));
432             }
433         }
434         pt.add_child(DSTRcpu_shares, child);
435         write_json(filepath.c_str(), pt);
436     }
437     catch (std::exception const& e) {
438         ICO_WRN("catch exception %s", e.what());
439     }
440     catch (...) {
441         ICO_WRN("NG write(%s)", filepath.c_str());
442     }
443
444 #endif
445     ICO_TRA("end");
446 }