RDK Documentation (Open Sourced RDK Components)
dcamem.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 <stdlib.h>
42 
43 #define MAXLEN 512
44 
45 int getMemoryUsage(char *memoryUtilization);
46 
47 /**
48  * @addtogroup DCA_APIS
49  * @{
50  */
51 
52 int main()
53 {
54 
55  char memoryUtilization[MAXLEN]={'\0'};
56 
57  if(getMemoryUsage(memoryUtilization))
58  {
59  printf("USED_MEM:%s\n", memoryUtilization);
60  }
61  else
62  {
63  printf("USED_MEM:NOT AVAILABLE\n");
64  }
65  return 0;
66 }
67 
68 
69 /**
70  * @brief To get memory usage of the device.
71  *
72  * @param[out] memoryUtilization Memory usage of the device.
73  *
74  * @return Returns status of operation.
75  * @retval Return 1 on success.
76  */
77 int getMemoryUsage(char *memoryUtilization)
78 {
79  FILE *memoryinfo;
80  char line[MAXLEN];
81  char tmp[MAXLEN];
82  long long memTotal = 0;
83  long long memFree = 0;
84  long long memoryInUse=0;
85  int Total_flag = 0;
86  int Free_flag = 0;
87  errno_t rc = -1;
88  int ind = -1;
89  int mem_total_len = 0, mem_free_len = 0 ;
90 
91  /* Open /proc/cpuinfo file*/
92  if ((memoryinfo = fopen("/proc/meminfo", "r")) == NULL)
93  {
94  printf("Failed to get Memory Utilization mode\n");
95  return 0;
96  }
97 
98  mem_total_len = strlen("MemTotal:");
99  mem_free_len = strlen("MemFree:");
100  /* Search until the "MemTotal" entry is found*/
101  while(fgets(line, MAXLEN, memoryinfo))
102  {
103  sscanf(line, "%512s", tmp);
104  rc = strcmp_s("MemTotal:",mem_total_len,tmp, &ind);
105  ERR_CHK(rc);
106  if((!ind) && (rc == EOK))
107  {
108  sscanf(line, "%*s %512s", tmp);
109  memTotal = atoll(tmp);
110  Total_flag = 1;
111  }
112  else
113  {
114  rc = strcmp_s("MemFree:",mem_free_len,tmp, &ind);
115  ERR_CHK(rc);
116  if((!ind) && (rc == EOK))
117  {
118  sscanf(line, "%*s %512s", tmp);
119  memFree = atoll(tmp);
120  Free_flag = 1;
121  }
122  }
123  if (Total_flag == 1 && Free_flag == 1) {
124  break;
125  }
126  }
127 
128  fclose(memoryinfo);
129  memoryInUse = (memTotal - memFree);
130  if(memoryUtilization)
131  {
132  rc = sprintf_s(memoryUtilization,MAXLEN,"%lld",memoryInUse);
133  if(rc < EOK)
134  {
135  ERR_CHK(rc);
136  return 0;
137  }
138  }
139  else
140  {
141  printf("Exit from get Memory Utilization due to NULL pointer");
142  return 0;
143  }
144  return 1;
145 }
146 
147 /** @} */ //END OF GROUP DCA_APIS
148 
149 /** @} */
150 
151 
152 /** @} */
153 /** @} */
getMemoryUsage
int getMemoryUsage(char *memoryUtilization)
To get memory usage of the device.
Definition: dcamem.c:77