Fix directory hierarchy & cmake configuration files
[platform/core/appfw/launchpad.git] / src / launchpad-process-pool / src / launchpad_memory_monitor.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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 #define _GNU_SOURCE
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <glib.h>
23
24 #include "launchpad_config.h"
25 #include "launchpad_memory_monitor.h"
26 #include "launchpad_proc.h"
27 #include "log_private.h"
28
29 #define INTERVAL_BASE_RATE 0.15f
30
31 struct memory_monitor_s {
32         unsigned int threshold;
33         unsigned int prev_used_ratio;
34         unsigned int base_interval;
35         unsigned int interval;
36         bool low_memory;
37         guint tag;
38         memory_monitor_cb callback;
39         void *user_data;
40 };
41
42 static struct memory_monitor_s __monitor;
43
44 static void __memory_monitor_start(void);
45
46 static gboolean __memory_check_cb(gpointer data)
47 {
48         bool low_memory;
49
50         __monitor.tag = 0;
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);
54
55         __monitor.low_memory = low_memory;
56         __memory_monitor_start();
57
58         return G_SOURCE_REMOVE;
59 }
60
61 static void __memory_monitor_stop(void)
62 {
63         if (!__monitor.tag)
64                 return;
65
66         g_source_remove(__monitor.tag);
67         __monitor.tag = 0;
68 }
69
70 static void __memory_monitor_start(void)
71 {
72         if (__monitor.tag)
73                 return;
74
75         __monitor.tag = g_timeout_add(__monitor.interval,
76                         __memory_check_cb, NULL);
77
78         __monitor.interval += __monitor.interval * INTERVAL_BASE_RATE;
79 }
80
81 int _memory_monitor_reset_timer(void)
82 {
83         _W("Reset");
84         __monitor.interval = __monitor.base_interval;
85
86         __memory_monitor_stop();
87         __memory_monitor_start();
88
89         return 0;
90 }
91
92 bool _memory_monitor_is_low_memory(void)
93 {
94         unsigned int mem_used_ratio = 0;
95
96         _proc_get_mem_used_ratio(&mem_used_ratio);
97
98         _D("previous used ratio(%u), current used ratio(%u)",
99                         __monitor.prev_used_ratio, mem_used_ratio);
100
101         __monitor.prev_used_ratio = mem_used_ratio;
102
103         if (mem_used_ratio > __monitor.threshold)
104                 return true;
105
106         return false;
107 }
108
109 int _memory_monitor_set_event_cb(memory_monitor_cb callback, void *user_data)
110 {
111         __monitor.callback = callback;
112         __monitor.user_data = user_data;
113
114         return 0;
115 }
116
117 int _memory_monitor_init(void)
118 {
119         int ret;
120
121         _W("MEMORY_MONITOR_INIT");
122
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;
128
129         ret = _proc_get_mem_used_ratio(&__monitor.prev_used_ratio);
130         if (ret != 0) {
131                 _E("Failed to get mem used ratio. error(%d)", ret);
132                 return ret;
133         }
134
135         __memory_monitor_start();
136
137         return 0;
138 }
139
140 void _memory_monitor_fini(void)
141 {
142         _W("MEMORY_MONITOR_FINI");
143
144         __memory_monitor_stop();
145 }