RDK Documentation (Open Sourced RDK Components)
dcacpu.c
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 
22 /**
23 * @defgroup dca
24 * @{
25 **/
26 
27 
28 
29 
30 /**
31 * @defgroup dca
32 * @{
33 * @defgroup src
34 * @{
35 **/
36 
37 //cpu and free memory
38 #include <stdio.h>
39 #include "safec_lib.h"
40 #include <string.h>
41 #include <unistd.h>
42 
43 #define MAXLEN 512
44 
45 int getCpuUsage(char * cpuUtil);
46 
47 /**
48  * @addtogroup DCA_APIS
49  * @{
50  */
51 int main()
52 {
53  char cpuUsage[MAXLEN] = {'\0'};
54 
55  if(getCpuUsage(cpuUsage))
56  {
57  printf("USED_CPU:%s\n", cpuUsage);
58  }
59  else
60  {
61  printf("USED_CPU:NOT AVAILABLE\n");
62  }
63 
64  return 0;
65 }
66 
67 /**
68  * @brief To get CPU usage of the device.
69  *
70  * @param[out] cpuUtil CPU usage of the device.
71  *
72  * @return Returns status of operation.
73  * @retval Return 1 on success.
74  */
75 int getCpuUsage(char * cpuUtil)
76 {
77  long double a[10], b[10],usr_cpu,total_time;
78  FILE *fp;
79  char cpuUtilization[MAXLEN]={'\0'};
80  int i=0;
81  errno_t rc = -1;
82 
83  for(i=0; i<5; i++)
84  {
85  fp = fopen("/proc/stat","r");
86  if(!fp)
87  {
88  return 0;
89  }
90  /*Coverity Fix CID:18504 CHECKED_RETURN */
91  if( fscanf(fp,"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
92  &a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9]) != 10 )
93  printf("%s:Error in fscanf()\n",__FUNCTION__);
94 
95  fclose(fp);
96  sleep(1);
97 
98  fp = fopen("/proc/stat","r");
99  if(!fp)
100  {
101  return 0;
102  }
103  /*Coverity Fix CID:18504 CHECKED_RETURN */
104  if( fscanf(fp,"%*s %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf %Lf",
105  &b[0],&b[1],&b[2],&b[3],&b[4],&b[5],&b[6],&b[7],&b[8],&b[9]) != 10 )
106  printf("Error in fscanf()\n");
107 
108  fclose(fp);
109 
110  total_time=(b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9])
111  -
112  (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]);
113 
114  usr_cpu=((b[0]-a[0])/total_time)*100;
115 
116  rc = sprintf_s(cpuUtilization,sizeof(cpuUtilization),"%Lf",usr_cpu);
117  if(rc < EOK)
118  {
119  ERR_CHK(rc);
120  return 0;
121  }
122  }
123 
124  if(cpuUtil)
125  {
126  rc = strcpy_s(cpuUtil,MAXLEN,cpuUtilization);
127  if(rc != EOK)
128  {
129  ERR_CHK(rc);
130  return 0;
131  }
132  return 1;
133  }
134  else
135  {
136  printf("Exit from getCPUusage due to NULL pointer\n");
137  return 0;
138  }
139 }
140 /** @} */ //END OF GROUP DCA_APIS
141 
142 /** @} */
143 
144 
145 /** @} */
146 /** @} */
getCpuUsage
int getCpuUsage(char *cpuUtil)
To get CPU usage of the device.
Definition: dcacpu.c:75