2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
27 #include "ss_resm_resourcemanagerlog.h"
29 #define DROP_CACHES "/proc/sys/vm/drop_caches"
31 // FRAMEWORKUNIFIEDLOG
32 FRAMEWORKUNIFIEDLOGPARAM g_FrameworkunifiedLogParams = {
33 FRAMEWORKUNIFIEDLOGOPTIONS,
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,
44 FRAMEWORKUNIFIEDLOGZONES
47 // echo 1 > /proc/sys/vm/drop_caches
48 #define DROP_CACHES_INTVAL_TIMESEC (1 * 10) // 10 sec
50 #define MEMINFO_FILE "/proc/meminfo"
52 /* memFree threshold */
53 #define MEMFREE_THRETHOLD (100*1024)
54 /* LowFree - CmaFree threshold */
55 #define LOWFREE_THRETHOLD (50*1024)
57 static uint32_t mainFree_kib;
58 static uint32_t lowFree_kib;
59 static uint32_t cmaFree_kib;
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);
71 exec_drop_caches(void) {
74 fd = open(DROP_CACHES, O_RDWR);
76 write(fd, (const void *)"1", 1);
79 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "%s open error (fd:%d, errno:%d)", DROP_CACHES, fd, errno);
84 main(int argc, char* argv[]) {
85 const struct timespec drop_caches_intval = {DROP_CACHES_INTVAL_TIMESEC, 0};
87 FRAMEWORKUNIFIED_SET_ZONES();
92 if (judge_cachedrop()) {
95 nanosleep(&drop_caches_intval, NULL);
101 /* Compare memory information */
103 comp_meminfo_tbl(const void* data1, const void* data2) {
104 return strcmp(((const meminfo_tbl*)data1)->name,
105 ((const meminfo_tbl*)data2)->name);
108 /* Get memory information */
111 int32_t meminfo_fd = -1;
114 meminfo_tbl target = {srch_name, NULL};
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},
125 const int32_t mem_table_count = sizeof(mem_table)/sizeof(meminfo_tbl);
127 if (meminfo_fd == -1) {
128 meminfo_fd = open(MEMINFO_FILE, O_RDONLY);
129 if (meminfo_fd == -1) {
135 lseek(meminfo_fd, 0L, SEEK_SET);
136 read_byte = read (meminfo_fd, buf, sizeof(buf) - 1);
142 buf[read_byte] = '\0';
146 tail = strchr(head, ':');
151 if (strlen (head) >= sizeof(srch_name)) {
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));
159 *(found->value) = strtoul(head, &tail, 10);
162 tail = strchr(head, '\n');
173 judge_cachedrop(void) {
174 if (mainFree_kib < MEMFREE_THRETHOLD) {
178 if ((lowFree_kib - cmaFree_kib) < LOWFREE_THRETHOLD) {