bbadc8be729653597088b2948af31efad5ad3513
[platform/core/system/resourced.git] / src / resource-optimizer / memory / swap / fileswap.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 #include "macro.h"
20 #include "module.h"
21 #include "module-data.h"
22 #include "trace.h"
23 #include "util.h"
24 #include "file-helper.h"
25 #include "procfs.h"
26 #include "swap-common.h"
27 #include "memory-cgroup.h"
28 #include "config-parser.h"
29 #include "lowmem-handler.h"
30 #include "losetup.h"
31
32 #define FILESWAP_FULLNESS_RATIO 0.8
33 #define DEFAULT_SWAP_FILE_SIZE MBYTE_TO_BYTE(30)
34
35 struct swap_file_control {
36         char crypt_type[MAX_TYPE_LENGTH];
37         char swapfile[64];
38         long swap_file_size;
39         unsigned long swap_reclaim_bytes;
40 };
41
42 static struct swap_file_control file_control = {
43         .crypt_type = "aes",
44         .swapfile = SWAP_FILE_NAME,
45         .swap_file_size = DEFAULT_SWAP_FILE_SIZE,
46         .swap_reclaim_bytes = 0,
47 };
48
49 static int swap_file_activate(void *data)
50 {
51         struct swap_module_ops *swap = (struct swap_module_ops *)data;
52
53         file_control.swap_reclaim_bytes =
54                         file_control.swap_file_size * FILESWAP_FULLNESS_RATIO;
55
56         return swap_set_file(file_control.swapfile, swap, file_control.crypt_type);
57 }
58
59 static int swap_file_reclaim(void *data)
60 {
61         struct swap_module_ops *swap = (struct swap_module_ops *)data;
62         _cleanup_proc_swaps_free_ struct proc_swaps **swaps = NULL;
63         unsigned int swap_size = 0;
64         int n, i;
65
66         n = proc_get_swaps(&swaps);
67         if (n <= 0)
68                 return -ENOENT;
69
70         for (i = 0; i < n; i++) {
71                 if (streq(swaps[i]->filename, swap->path)) {
72                         swap_size = swaps[i]->used;
73                         break;
74                 }
75         }
76
77         swap_size = KBYTE_TO_BYTE(swap_size);
78         if (swap_size < file_control.swap_reclaim_bytes)
79                 return 0;
80
81         /*
82          * if current swap has multiple devices,
83          * it is no necessary to trigger reclaiming
84          * because there is no impact to save memory
85          * in spite of killing victims
86          */
87         if (n > 1)
88                 return 0;
89
90         /*
91          * In this case, swap storage is almost full
92          * when using only storage for swap.
93          * It means that there are many background processes or
94          * some process makes memory leak.
95          * So, it requires to trigger proactive oom killer
96          * with CGROUP_ROOT type.
97          */
98         lowmem_trigger_swap_reclaim(CGROUP_ROOT, swap_size);
99         return -ENOSPC;
100 }
101
102 /*static int swap_file_parse_config_file(void)
103 {
104         ConfigTableItem items[] = {
105                 { "FILE",               "CryptType",            config_parse_string,    0,      NULL    },
106                 { "FILE",               "FileSize",                     config_parse_bytes,     0,      NULL    },
107                 { NULL,         NULL,                   NULL,                           0,      NULL    }
108         };
109
110         int r;
111         _cleanup_free_ char *crypt_type = NULL;
112
113         items[0].data = &crypt_type;
114         items[1].data = &file_control.swap_file_size;
115
116         r = config_parse_new(SWAP_CONF_FILE, (void*) items);
117         if (r < 0) {
118                 _E("Failed to parse configuration file: %d", r);
119                 return r;
120         }
121
122         if (check_valid_compressor(crypt_type) == RESOURCED_ERROR_NONE)
123                 strncpy(file_control.crypt_type, crypt_type, MAX_TYPE_LENGTH-1);
124         else
125                 memset(file_control.crypt_type, 0, MAX_TYPE_LENGTH);
126
127         return 0;
128 }*/
129
130 static int swap_file_init(void *data)
131 {
132         struct swap_module_ops *swap = (struct swap_module_ops *)data;
133
134 /*      r = swap_file_parse_config_file();
135         if (r < 0) {
136                 _E("Failed to parse SwapFile config: %d", r);
137                 return r;
138         }*/
139
140         swap->k_size = BYTE_TO_KBYTE(file_control.swap_file_size);
141         return 0;
142 }
143
144 static int swap_file_conf(void *data)
145 {
146         struct fileswap_conf *fileswap_conf = (struct fileswap_conf *)data;
147         if (!fileswap_conf) {
148                 _E("Fileswap configuration should not be NULL");
149                 return RESOURCED_ERROR_FAIL;
150         }
151
152         if (check_valid_compressor(fileswap_conf->type) == RESOURCED_ERROR_NONE)
153                 strncpy(file_control.crypt_type, fileswap_conf->type, MAX_TYPE_LENGTH-1);
154
155         if (fileswap_conf->filesize > 0)
156                 file_control.swap_file_size = fileswap_conf->filesize;
157
158         _I("[DEBUG] fileswap crypt type = %s", file_control.crypt_type);
159         _I("[DEBUG] fileswap file size = %ld", file_control.swap_file_size);
160
161         return RESOURCED_ERROR_NONE;
162 }
163
164 static struct swap_module_ops swap_file_ops = {
165         .name = "FILESWAP",
166         .type = SWAP_TYPE_FILE,
167         .path = "",
168         .priority = SWAP_PRI_LOW,
169         .k_size = 0,
170         .init = swap_file_init,
171         .activate = swap_file_activate,
172         .reclaim = swap_file_reclaim,
173         .conf = swap_file_conf,
174 };
175 SWAP_MODULE_REGISTER(&swap_file_ops)