c04e1ad79a697a92a19ff18ccfc8e3f13380745d
[profile/ivi/ico-uxf-homescreen.git] / lib / common / CicoAulItems.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 <iostream>
10 #include <string>
11 #include <fstream>
12 #include <vector>
13 #include <memory>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <string.h>
17 #include <aul/aul.h>
18
19 #include <ico_log.h>
20 #include "CicoAulItems.h"
21 #include "CicoAilItems.h"
22 #include "CicoConf.h"
23 #include "CicoSystemConfig.h"
24
25 using namespace std;
26
27 /**
28  * @brief AUL Items class constructor
29  */
30 CicoAulItems::CicoAulItems()
31 {
32     ICO_TRA("start");
33     m_appid.clear(); // appid
34     m_pid = 0; // pid
35     m_category = DINITm_categoryID;
36     m_cpucgroup = -1;
37     m_memcgroup = -1;
38     m_defCgrpCpu.clear();
39     m_defCgrpMem.clear();
40     m_aulstt = AUL_R_OK;
41     ICO_TRA("end");
42 }
43
44 /**
45  * @brief AUL Items class constructor
46  */
47 CicoAulItems::CicoAulItems(const char* appid, int pid, int ctgry,
48                                int aulstt, const void* obj)
49     :m_appid(appid), m_pid(pid), m_category(ctgry), m_aulstt(aulstt)
50 {
51     ICO_TRA("start %s, %d, %x", appid? appid: "(NIL)", pid, obj);
52     enterWindow(obj);
53     getPidCgroupInfo(pid, m_defCgrpMem, m_defCgrpCpu);
54     m_cpucgroup = -1;
55     m_memcgroup = -1;
56     if (DINITm_categoryID != m_category) {
57         CicoSystemConfig* conf = CicoSystemConfig::getInstance();
58         const CicoSCCategoryConf* objX = 
59             conf->getCategoryObjbyCaategoryID(m_category);
60         if (NULL != objX) {
61             m_cpucgroup = objX->rctrl;
62             ICO_DBG("categ:%d -> cgroup: %d", m_category, m_cpucgroup);
63         }
64     }
65     ICO_TRA("end");
66 }
67
68 /**
69  * @brief AUL Items class constructor
70  */
71 CicoAulItems::CicoAulItems(const CicoAulItems& s)
72 {
73     m_appid = s.m_appid;
74     m_pid = s.m_pid;
75     m_category = s.m_category;
76     m_cpucgroup = s.m_cpucgroup;
77     m_memcgroup = s.m_memcgroup;
78 #if 1 // TODO mk_k
79     m_CSCWptrs = s.m_CSCWptrs;
80 #else
81     int sz = s.m_CSCWptrs.size();
82     for (int i = 0; i < sz; i++) {
83         m_CSCWptrs.push_back(s.m_CSCWptrs[i]);
84     }
85 #endif
86     m_defCgrpCpu = s.m_defCgrpCpu;
87     m_defCgrpMem = s.m_defCgrpMem;
88     m_aulstt = s.m_aulstt;
89 }
90
91 /**
92  * @brief AUL Items class destructor
93  */
94 CicoAulItems::~CicoAulItems()
95 {
96 //  ICO_TRA("CicoAulItems::~CicoAulItems");
97     m_CSCWptrs.clear();
98 }
99
100 /**
101  * @brief window information pointer entry
102  * @param obj entry pointer
103  */
104 void CicoAulItems::enterWindow(const void* obj)
105 {
106     ICO_TRA("CicoAulItems::enterWindow %x", obj);
107     if ((NULL == obj) || (0 == obj)) {
108         ICO_TRA("CicoAulItems::enterWindow");
109         return;
110     }
111     bool bingo = false; // Registered flag off
112 #if 1 // TODO mk_k
113     vector<const void*>::iterator it = m_CSCWptrs.begin();
114     vector<const void*>::iterator theEnd = m_CSCWptrs.end();
115     for(; it != theEnd; ++it) {
116         if (obj == *it) { // if Registered ?
117             bingo = true; // Registered flag on
118             break; // break of for
119         }
120     }
121 #else
122     int sz = m_CSCWptrs.size();
123     for (int i = 0; i < sz; i++) {
124         if (obj == m_CSCWptrs[i]) {
125             bingo = true;
126             break; // break of for
127         }
128     }
129 #endif
130     if (false == bingo) {
131         ICO_TRA("add window pointer");
132         m_CSCWptrs.push_back(obj);
133     }
134     ICO_TRA("CicoAulItems::enterWindow");
135     return;
136 }
137
138 /**
139  * @brief remove window information pointer
140  * @param obj remove target
141  */
142 void CicoAulItems::rmWindow(const void* obj)
143 {
144     ICO_TRA("CicoAulItems::rmWindow %x", obj);
145     vector<const void*>::iterator it = m_CSCWptrs.begin();
146     vector<const void*>::iterator theEnd = m_CSCWptrs.end();
147     for(; it != theEnd; ++it) {
148         if (obj == *it) {
149             ICO_TRA("CicoAulItems::rmWindow");
150             m_CSCWptrs.erase(it);
151             break; // break of for
152         }
153     }
154     ICO_TRA("CicoAulItems::rmWindow");
155     return;
156 }
157
158 /**
159  * @brief get cgroup data by /proc/[pid]/cgroup file
160  * @parm pid target pid number
161  * @param m store cgroup memory directory data
162  * @param c store cgroup cpu,cpuacct directory data
163  */
164 static const char* g_procPidCgroupFileFmt="/proc/%d/cgroup";
165 static const char* g_cpuWord = "cpuacct,cpu:";
166 static const char* g_memWord = "memory:";
167 bool CicoAulItems::getPidCgroupInfo(int pid, string& m, string& c)
168 {
169     ICO_TRA("start");
170     char fn[64];
171     sprintf(fn, g_procPidCgroupFileFmt, pid);
172     const size_t cpuWdSz = strlen(g_cpuWord);
173     const size_t memWdSz = strlen(g_memWord);
174     string tmp;
175     ifstream ifs(fn);
176     bool bR = false;
177     const char* pC = 0;
178     const char* pM = 0;
179     while (ifs >> tmp) {
180         if (true == tmp.empty()) {
181             continue;
182         }
183         const char* pT = tmp.c_str();
184         const char* pS = pT;
185         for (;pS != '\0'; pS++) {
186             if (':' == *pS) {
187                 pS++;
188                 if (0 == strncmp(pS, g_cpuWord, cpuWdSz)) { //cpu
189                     pC = pS + cpuWdSz; // get cgroup cpu directory
190                 }
191                 else if (0 == strncmp(pS, g_memWord, memWdSz)) { // memory
192                     pM = pS + memWdSz; // get cgroup memory directory
193                 }
194                 break; // break of for
195             }
196         }
197         if ((0 != pC) && (0 != pM)) {
198             ICO_DBG("CicoAulItems::getPidCgroupInfo m=%s, c=%s", pM, pC);
199             m = pM;
200             c = pC;
201             bR = true;
202             break;  // break of while
203         }
204     }
205     ifs.close();
206     ICO_TRA("end %s", bR? "true": "false");
207     return bR;
208 }
209
210 /**
211  * @brief appid update
212  */
213 void CicoAulItems::update_appid()
214 {
215     if (AUL_R_OK == m_aulstt) {
216         return;
217     }
218     ICO_TRA("update start %d, %s", m_aulstt, m_appid.c_str());
219     char buf[255];
220     buf[0] = '\0'; // STOP CODE
221     m_aulstt = aul_app_get_appid_bypid(m_pid, buf, sizeof(buf));
222     if (AUL_R_OK == m_aulstt) {
223         m_appid = buf;
224     }
225     ICO_TRA("update end %d, %s", m_aulstt, m_appid.c_str());
226     return ;
227 }
228 // vim:set expandtab ts=4 sw=4: