RDK Documentation (Open Sourced RDK Components)
Device_DeviceInfo_ProcessStatus.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file Device_DeviceInfo_ProcessStatus.cpp
22  * @brief This source file contains the APIs for getting device processor status information.
23  */
24 
25 
26 
27 /*****************************************************************************
28  * STANDARD INCLUDE FILES
29  *****************************************************************************/
30 
31 
32 /**
33 * @defgroup tr69hostif
34 * @{
35 * @defgroup hostif
36 * @{
37 **/
38 
39 
41 #include "safec_lib.h"
42 
43 GHashTable* hostIf_DeviceProcessStatusInterface::ifHash = NULL;
44 GMutex* hostIf_DeviceProcessStatusInterface::m_mutex = NULL;
45 
46 hostIf_DeviceProcessStatusInterface* hostIf_DeviceProcessStatusInterface::getInstance(int dev_id)
47 {
49 
50  if(ifHash)
51  {
52  pRet = (hostIf_DeviceProcessStatusInterface *)g_hash_table_lookup(ifHash,(gpointer) dev_id);
53  }
54  else
55  {
56  ifHash = g_hash_table_new(NULL,NULL);
57  }
58 
59  if(!pRet)
60  {
61  try {
62  pRet = new hostIf_DeviceProcessStatusInterface(dev_id);
63  } catch(int e)
64  {
65  RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,"Caught exception, not able create Ethernet Interface instance..\n");
66  }
67  g_hash_table_insert(ifHash, (gpointer)dev_id, pRet);
68  }
69  return pRet;
70 }
71 
72 GList* hostIf_DeviceProcessStatusInterface::getAllInstances()
73 {
74  if(ifHash)
75  return g_hash_table_get_keys(ifHash);
76  return NULL;
77 }
78 
79 void hostIf_DeviceProcessStatusInterface::closeInstance(hostIf_DeviceProcessStatusInterface *pDev)
80 {
81  if(pDev)
82  {
83  g_hash_table_remove(ifHash, (gconstpointer)pDev->dev_id);
84  delete pDev;
85  }
86 }
87 
88 void hostIf_DeviceProcessStatusInterface::closeAllInstances()
89 {
90  if(ifHash)
91  {
92  GList* tmp_list = g_hash_table_get_values (ifHash);
93 
94  while(tmp_list)
95  {
97  tmp_list = tmp_list->next;
98  closeInstance(pDev);
99  }
100  }
101 }
102 
103 void hostIf_DeviceProcessStatusInterface::getLock()
104 {
105  if(!m_mutex)
106  {
107  m_mutex = g_mutex_new();
108  }
109  g_mutex_lock(m_mutex);
110 }
111 
112 void hostIf_DeviceProcessStatusInterface::releaseLock()
113 {
114  g_mutex_unlock(m_mutex);
115 }
116 
117 hostIf_DeviceProcessStatusInterface::hostIf_DeviceProcessStatusInterface(int devid)
118 {
119  dev_id = devid;
120  bCalledCPUUsage = false;
121  bCalledProcessNumberOfEntries = false;
122  backupCPUUsage = 0;
123  backupProcessNumberOfEntries = 0;
124 }
125 
126 int hostIf_DeviceProcessStatusInterface::getProcessStatusCPUUsage() {
127 
128  errno_t rc = -1;
129  char cmd[64] = {'\0'};
130  FILE *fp = NULL;
131  char resultBuffer[1024]= {'\0'};
132  unsigned long total_jiffies_1 = 0, total_jiffies_2 = 0;
133  unsigned long work_jiffies_1 = 0, work_jiffies_2 = 0;
134  long long unsigned int mUser = 0, mNice = 0, mSystem = 0, mIdle = 0, mIOwait = 0, mIrq = 0, mSoftirq = 0;
135  unsigned int uiCpuUsage = 0;
136  unsigned int temp = 0;
137  //CPUUsage
138  memset(cmd,'\0', sizeof(cmd));
139  rc=strcpy_s(cmd,sizeof(cmd),"grep '^cpu ' /proc/stat");
140  if(rc!=EOK)
141  {
142  ERR_CHK(rc);
143  }
144  fp = popen(cmd,"r");
145 
146  if(fp == NULL) {
147  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"%s(): Error popen\n", __FUNCTION__);
148  return -1;
149  }
150 
151  if(fgets(resultBuffer,1024,fp)!=NULL) {
152  sscanf(resultBuffer,"%*s %llu %llu %llu %llu %llu %llu %llu",&mUser,&mNice,&mSystem,&mIdle, &mIOwait, &mIrq, &mSoftirq);
153 
154  }
155 
156  pclose(fp);
157 
158  total_jiffies_1 = mUser + mNice + mSystem + mIdle + mIOwait + mIrq + mSoftirq;
159  work_jiffies_1 = mUser + mNice + mSystem;
160 
161  sleep(1);
162 
163  fp = popen(cmd,"r");
164 
165  if(fp == NULL) {
166  RDK_LOG(RDK_LOG_ERROR,LOG_TR69HOSTIF,"%s(): Error popen\n", __FUNCTION__);
167  return -1;
168  }
169 
170  if(fgets(resultBuffer,1024,fp)!=NULL) {
171  sscanf(resultBuffer,"%*s %llu %llu %llu %llu %llu %llu %llu",&mUser,&mNice,&mSystem,&mIdle, &mIOwait, &mIrq, &mSoftirq);
172  }
173 
174  pclose(fp);
175 
176  total_jiffies_2 = mUser + mNice + mSystem + mIdle + mIOwait + mIrq + mSoftirq;
177  work_jiffies_2 = mUser + mNice + mSystem;
178 
179  temp = total_jiffies_2 - total_jiffies_1;
180 
181  if(temp != 0)
182  {
183  uiCpuUsage = ((work_jiffies_2 - work_jiffies_1)*100)/(temp);
184 
185  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"%s(): CpuUsage = [%u]\n", __FUNCTION__, uiCpuUsage);
186  }
187  return uiCpuUsage;
188 
189 }
190 
191 
192 /****************************************************************************************************************************************************/
193 // Device_DeviceInfo_ProcessStatus. Profile. Getters:
194 /****************************************************************************************************************************************************/
195 
196 /**
197  * @brief This function provides the total amount of cpu usage, in percentage.
198  *
199  * @param[out] stMsgData TR-069 Host interface message request.
200  * @param[in] pChanged Status of the operation.
201  *
202  * @return Returns the status of the operation.
203  *
204  * @retval OK if is successfully fetch the data from the device.
205  * @retval ERR_INTERNAL_ERROR if not able to fetch the data.
206  * @ingroup TR69_HOSTIF_DEVICE_PROCESSORSTATUS_API
207  */
209 {
210  int uiCpuUsage = getProcessStatusCPUUsage();
211  if (uiCpuUsage < 0)
212  uiCpuUsage = 0;
213 
214  put_int(stMsgData->paramValue,uiCpuUsage);
215  stMsgData->paramtype = hostIf_IntegerType;
216  stMsgData->paramLen = 32;
217  if(bCalledCPUUsage && pChanged &&(backupCPUUsage != uiCpuUsage))
218  {
219  *pChanged = true;
220  }
221  bCalledCPUUsage = true;
222  backupCPUUsage = uiCpuUsage;
223 
224 
225  return OK;
226 }
227 
228 /* End of doxygen group */
229 /**
230  * @}
231  */
232 
233 /* End of file xxx_api.c. */
234 
235 
236 /** @} */
237 /** @} */
Device_DeviceInfo_ProcessStatus.h
The header file provides TR069 device processor status information APIs.
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
hostIf_DeviceProcessStatusInterface::get_Device_DeviceInfo_ProcessStatus_CPUUsage
int get_Device_DeviceInfo_ProcessStatus_CPUUsage(HOSTIF_MsgData_t *stMsgData, bool *pChanged=NULL)
This function provides the total amount of cpu usage, in percentage.
Definition: Device_DeviceInfo_ProcessStatus.cpp:208
_HostIf_MsgData_t::paramtype
HostIf_ParamType_t paramtype
Definition: hostIf_tr69ReqHandler.h:177
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
_HostIf_MsgData_t::paramValue
char paramValue[(4 *1024)]
Definition: hostIf_tr69ReqHandler.h:172
hostIf_DeviceProcessStatusInterface
This class provides the interface for getting device processor status information.
Definition: Device_DeviceInfo_ProcessStatus.h:67
put_int
void put_int(char *ptr, int val)
This function converts the input data to integer type.
Definition: hostIf_utils.cpp:152
_HostIf_MsgData_t::paramLen
short paramLen
Definition: hostIf_tr69ReqHandler.h:175