Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / resource-limiter / memory / lowmem-dbus.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 /*
20  * @file lowmem_dbus.c
21  *
22  * @desc lowmem dbus for oom triger
23  *
24  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
25  *
26  */
27
28 #include "trace.h"
29 #include "lowmem-handler.h"
30 #include "dbus-handler.h"
31 #include "resourced.h"
32 #include "macro.h"
33 #include "memory-cgroup.h"
34 #include "procfs.h"
35 #include "util.h"
36 #include "proc-common.h"
37
38 static void lowmem_dbus_oom_set_threshold(GVariant *params)
39 {
40         int level = -1, thres_mb = -1;
41         const char *const gtype = "(ii)";
42
43         ret_if_gvariant_type_mismatch(params, gtype);
44         g_variant_get(params, gtype, &level, &thres_mb);
45         ret_unless(level >= 0);
46         ret_unless(thres_mb >= 0);
47
48         memcg_set_threshold(CGROUP_ROOT, level, thres_mb);
49 }
50
51 static void lowmem_dbus_oom_set_leave_threshold(GVariant *params)
52 {
53         int thres_mb = -1;
54         const char *const gtype = "(i)";
55
56         ret_if_gvariant_type_mismatch(params, gtype);
57         g_variant_get(params, gtype, &thres_mb);
58         ret_unless(thres_mb >= 0);
59
60         memcg_set_leave_threshold(CGROUP_ROOT, thres_mb);
61 }
62
63 static void lowmem_dbus_oom_trigger(GVariant *params)
64 {
65         lowmem_trigger_reclaim(OOM_NOMEMORY_CHECK,
66                                 MAX_MEMORY_CGROUP_VICTIMS, CGROUP_LOW, 0);
67 }
68
69 static void lowmem_dbus_set_perceptible(GVariant *params)
70 {
71         pid_t pid = 0;
72         const char *const gtype = "(i)";
73
74         ret_if_gvariant_type_mismatch(params, gtype);
75         g_variant_get(params, gtype, &pid);
76         ret_unless(pid > 0);
77
78         proc_set_oom_score_adj(pid, OOMADJ_BACKGRD_PERCEPTIBLE, find_app_info(pid));
79 }
80
81 static void lowmem_dbus_set_platform(GVariant *params)
82 {
83         pid_t pid = 0;
84         const char *const gtype = "(i)";
85
86         ret_if_gvariant_type_mismatch(params, gtype);
87         g_variant_get(params, gtype, &pid);
88         ret_unless(pid > 0);
89
90         lowmem_trigger_swap(pid, get_memcg_info(CGROUP_LOW)->name, true);
91 }
92
93 static void lowmem_dbus_set_memlimit(GVariant *params)
94 {
95         int result;
96         pid_t pid = 0;
97         unsigned long limit_bytes = 0;
98         const char *const gtype = "(iu)";
99         struct proc_app_info *pai;
100
101         ret_if_gvariant_type_mismatch(params, gtype);
102         g_variant_get(params, gtype, &pid, &limit_bytes);
103         ret_unless(pid > 0);
104         ret_unless(limit_bytes > 0);
105
106         pai = find_app_info(pid);
107
108         if (pai) {
109                 if (pai->memory.memlimit_update_exclude)
110                         return;
111
112                 lowmem_limit_set_app(limit_bytes, pai, PROC_ACTION_KILL);
113         }
114         else {
115                 char appname[PROC_NAME_MAX];
116                 result = proc_get_cmdline(pid, appname, sizeof appname);
117                 if (result < 0) {
118                         _E("Failed to get cmdline basename of pid(%d)", pid);
119                         return;
120                 }
121                 lowmem_limit_set_system_service(pid, limit_bytes, appname, PROC_ACTION_KILL);
122         }
123 }
124
125 static const struct d_bus_signal dbus_signals[] = {
126         /* RESOURCED DBUS */
127         {RESOURCED_PATH_OOM, RESOURCED_INTERFACE_OOM,
128             SIGNAL_OOM_SET_THRESHOLD, lowmem_dbus_oom_set_threshold, NULL},
129         {RESOURCED_PATH_OOM, RESOURCED_INTERFACE_OOM,
130             SIGNAL_OOM_SET_LEAVE_THRESHOLD,
131             lowmem_dbus_oom_set_leave_threshold, NULL},
132         {RESOURCED_PATH_OOM, RESOURCED_INTERFACE_OOM,
133             SIGNAL_OOM_TRIGGER, lowmem_dbus_oom_trigger, NULL},
134         {RESOURCED_PATH_OOM, RESOURCED_INTERFACE_OOM,
135             SIGNAL_OOM_SET_PERCEPTIBLE, lowmem_dbus_set_perceptible, NULL},
136         {RESOURCED_PATH_OOM, RESOURCED_INTERFACE_OOM,
137             SIGNAL_OOM_SET_PLATFORM, lowmem_dbus_set_platform, NULL},
138         {RESOURCED_PATH_OOM, RESOURCED_INTERFACE_OOM,
139             SIGNAL_OOM_SET_MEMLIMIT, lowmem_dbus_set_memlimit, NULL},
140 };
141
142 void lowmem_dbus_init(void)
143 {
144         d_bus_register_signals(dbus_signals, ARRAY_SIZE(dbus_signals));
145 }