Remove unused directories and files in video_in_hal
[staging/basesystem.git] / service / system / resource_manager / drop_caches / src / drop_caches.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <time.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "ss_resm_resourcemanagerlog.h"
28
29 #define DROP_CACHES "/proc/sys/vm/drop_caches"
30
31 // FRAMEWORKUNIFIEDLOG
32 FRAMEWORKUNIFIEDLOGPARAM g_FrameworkunifiedLogParams = {
33   FRAMEWORKUNIFIEDLOGOPTIONS,
34   {
35     ZONE_TEXT_10, ZONE_TEXT_11, ZONE_TEXT_12,
36     ZONE_TEXT_13, ZONE_TEXT_14, ZONE_TEXT_15,
37     ZONE_TEXT_16, ZONE_TEXT_17, ZONE_TEXT_18,
38     ZONE_TEXT_19, ZONE_TEXT_20, ZONE_TEXT_21,
39     ZONE_TEXT_22, ZONE_TEXT_23, ZONE_TEXT_24,
40     ZONE_TEXT_25, ZONE_TEXT_26, ZONE_TEXT_27,
41     ZONE_TEXT_28, ZONE_TEXT_29, ZONE_TEXT_30,
42     ZONE_TEXT_31
43   },
44   FRAMEWORKUNIFIEDLOGZONES
45 };
46
47 // echo 1 > /proc/sys/vm/drop_caches
48 #define DROP_CACHES_INTVAL_TIMESEC (1 * 10)  // 10 sec
49
50 #define MEMINFO_FILE "/proc/meminfo"
51
52 /* memFree threshold */
53 #define MEMFREE_THRETHOLD (100*1024)
54 /* LowFree - CmaFree threshold */
55 #define LOWFREE_THRETHOLD (50*1024)
56
57 static uint32_t mainFree_kib;
58 static uint32_t lowFree_kib;
59 static uint32_t cmaFree_kib;
60 // meminfo table
61 typedef struct {
62   const char* name;
63   uint32_t* value;
64 } meminfo_tbl;
65
66 static int32_t get_meminfo(void);
67 static int32_t comp_meminfo_tbl(const void* data1, const void* data2);
68 static bool judge_cachedrop(void);
69
70 static void
71 exec_drop_caches(void) {
72   int fd;
73
74   fd = open(DROP_CACHES, O_RDWR);
75   if (fd >= 0) {
76     write(fd, (const void *)"1", 1);
77     close(fd);
78   } else {
79     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "%s open error (fd:%d, errno:%d)", DROP_CACHES, fd, errno);
80   }
81 }
82
83 int
84 main(int argc, char* argv[]) {
85   const struct timespec drop_caches_intval = {DROP_CACHES_INTVAL_TIMESEC, 0};
86
87   FRAMEWORKUNIFIED_SET_ZONES();
88
89   while (1) {
90     /* Get meminfo */
91     get_meminfo();
92     if (judge_cachedrop()) {
93       exec_drop_caches();
94     }
95     nanosleep(&drop_caches_intval, NULL);
96   }
97
98   return 0;
99 }
100
101 /* Compare memory information */
102 static int32_t
103 comp_meminfo_tbl(const void* data1, const void* data2) {
104   return strcmp(((const meminfo_tbl*)data1)->name,
105                 ((const meminfo_tbl*)data2)->name);
106 }
107
108 /* Get memory information */
109 static int32_t
110 get_meminfo(void) {
111   int32_t meminfo_fd = -1;
112   char srch_name[16];
113   char buf[2048];
114   meminfo_tbl target = {srch_name, NULL};
115   meminfo_tbl* found;
116   char* head;
117   char* tail;
118   int32_t read_byte;
119   /* Strings must be in ascending order when adding entries to this table (for bsearch) */
120   static const meminfo_tbl mem_table[] = {
121     {"CmaFree",  &cmaFree_kib},
122     {"LowFree",  &lowFree_kib},
123     {"MemFree",  &mainFree_kib},
124   };
125   const int32_t mem_table_count = sizeof(mem_table)/sizeof(meminfo_tbl);
126
127   if (meminfo_fd == -1) {
128     meminfo_fd = open(MEMINFO_FILE, O_RDONLY);
129     if (meminfo_fd == -1) {
130       fflush(NULL);
131       return -1;
132     }
133   }
134
135   lseek(meminfo_fd, 0L, SEEK_SET);
136   read_byte = read (meminfo_fd, buf, sizeof(buf) - 1);
137   if (read_byte < 0) {
138     fflush(NULL);
139     close(meminfo_fd);
140     return -1;
141   }
142   buf[read_byte] = '\0';
143
144   head = buf;
145   while (1) {
146     tail = strchr(head, ':');
147     if (!tail) {
148       break;
149     }
150     *tail = '\0';
151     if (strlen (head) >= sizeof(srch_name)) {
152       head = tail + 1;
153     } else {
154       strcpy(srch_name, head);  // NOLINT
155       found = reinterpret_cast<meminfo_tbl *>(bsearch(&target, mem_table, mem_table_count,
156                                       sizeof(meminfo_tbl), comp_meminfo_tbl));
157       head = tail + 1;
158       if (found) {
159         *(found->value) = strtoul(head, &tail, 10);
160       }
161     }
162     tail = strchr(head, '\n');
163     if (!tail)
164       break;
165     head = tail + 1;
166   }
167   close(meminfo_fd);
168
169   return 0;
170 }
171
172 static bool
173 judge_cachedrop(void) {
174   if (mainFree_kib < MEMFREE_THRETHOLD) {
175     return true;
176   }
177
178   if ((lowFree_kib - cmaFree_kib) < LOWFREE_THRETHOLD) {
179     return true;
180   }
181
182   return false;
183 }