2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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.
24 #include "launchpad_config.h"
25 #include "launchpad_memory_monitor.h"
26 #include "launchpad_proc.h"
27 #include "log_private.h"
29 #define INTERVAL_BASE_RATE 0.15f
31 struct memory_monitor_s {
32 unsigned int threshold;
33 unsigned int prev_used_ratio;
34 unsigned int base_interval;
35 unsigned int interval;
38 memory_monitor_cb callback;
42 static struct memory_monitor_s __monitor;
44 static void __memory_monitor_start(void);
46 static gboolean __memory_check_cb(gpointer data)
51 low_memory = _memory_monitor_is_low_memory();
52 if (__monitor.low_memory != low_memory && __monitor.callback)
53 __monitor.callback(low_memory, __monitor.user_data);
55 __monitor.low_memory = low_memory;
56 __memory_monitor_start();
58 return G_SOURCE_REMOVE;
61 static void __memory_monitor_stop(void)
66 g_source_remove(__monitor.tag);
70 static void __memory_monitor_start(void)
75 __monitor.tag = g_timeout_add(__monitor.interval,
76 __memory_check_cb, NULL);
78 __monitor.interval += __monitor.interval * INTERVAL_BASE_RATE;
81 int _memory_monitor_reset_timer(void)
84 __monitor.interval = __monitor.base_interval;
86 __memory_monitor_stop();
87 __memory_monitor_start();
92 bool _memory_monitor_is_low_memory(void)
94 unsigned int mem_used_ratio = 0;
96 _proc_get_mem_used_ratio(&mem_used_ratio);
98 _D("previous used ratio(%u), current used ratio(%u)",
99 __monitor.prev_used_ratio, mem_used_ratio);
101 __monitor.prev_used_ratio = mem_used_ratio;
103 if (mem_used_ratio > __monitor.threshold)
109 int _memory_monitor_set_event_cb(memory_monitor_cb callback, void *user_data)
111 __monitor.callback = callback;
112 __monitor.user_data = user_data;
117 int _memory_monitor_init(void)
121 _W("MEMORY_MONITOR_INIT");
123 __monitor.threshold = _config_get_int_value(
124 CONFIG_TYPE_MEMORY_MONITOR_THRESHOLD);
125 __monitor.base_interval = _config_get_int_value(
126 CONFIG_TYPE_MEMORY_MONITOR_INTERVAL);
127 __monitor.interval = __monitor.base_interval;
129 ret = _proc_get_mem_used_ratio(&__monitor.prev_used_ratio);
131 _E("Failed to get mem used ratio. error(%d)", ret);
135 __memory_monitor_start();
140 void _memory_monitor_fini(void)
142 _W("MEMORY_MONITOR_FINI");
144 __memory_monitor_stop();