4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "module-data.h"
24 #include "file-helper.h"
26 #include "swap-common.h"
27 #include "memory-cgroup.h"
28 #include "config-parser.h"
29 #include "lowmem-handler.h"
32 #define FILESWAP_FULLNESS_RATIO 0.8
33 #define DEFAULT_SWAP_FILE_SIZE MBYTE_TO_BYTE(30)
35 struct swap_file_control {
36 char crypt_type[MAX_TYPE_LENGTH];
39 unsigned long swap_reclaim_bytes;
42 static struct swap_file_control file_control = {
44 .swapfile = SWAP_FILE_NAME,
45 .swap_file_size = DEFAULT_SWAP_FILE_SIZE,
46 .swap_reclaim_bytes = 0,
49 static int swap_file_activate(void *data)
51 struct swap_module_ops *swap = (struct swap_module_ops *)data;
53 file_control.swap_reclaim_bytes =
54 file_control.swap_file_size * FILESWAP_FULLNESS_RATIO;
56 return swap_set_file(file_control.swapfile, swap, file_control.crypt_type);
59 static int swap_file_reclaim(void *data)
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;
66 n = proc_get_swaps(&swaps);
70 for (i = 0; i < n; i++) {
71 if (streq(swaps[i]->filename, swap->path)) {
72 swap_size = swaps[i]->used;
77 swap_size = KBYTE_TO_BYTE(swap_size);
78 if (swap_size < file_control.swap_reclaim_bytes)
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
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.
98 lowmem_trigger_swap_reclaim(CGROUP_ROOT, swap_size);
102 /*static int swap_file_parse_config_file(void)
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 }
111 _cleanup_free_ char *crypt_type = NULL;
113 items[0].data = &crypt_type;
114 items[1].data = &file_control.swap_file_size;
116 r = config_parse_new(SWAP_CONF_FILE, (void*) items);
118 _E("Failed to parse configuration file: %d", r);
122 if (check_valid_compressor(crypt_type) == RESOURCED_ERROR_NONE)
123 strncpy(file_control.crypt_type, crypt_type, MAX_TYPE_LENGTH-1);
125 memset(file_control.crypt_type, 0, MAX_TYPE_LENGTH);
130 static int swap_file_init(void *data)
132 struct swap_module_ops *swap = (struct swap_module_ops *)data;
134 /* r = swap_file_parse_config_file();
136 _E("Failed to parse SwapFile config: %d", r);
140 swap->k_size = BYTE_TO_KBYTE(file_control.swap_file_size);
144 static int swap_file_conf(void *data)
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;
152 if (check_valid_compressor(fileswap_conf->type) == RESOURCED_ERROR_NONE)
153 strncpy(file_control.crypt_type, fileswap_conf->type, MAX_TYPE_LENGTH-1);
155 if (fileswap_conf->filesize > 0)
156 file_control.swap_file_size = fileswap_conf->filesize;
158 _I("[DEBUG] fileswap crypt type = %s", file_control.crypt_type);
159 _I("[DEBUG] fileswap file size = %ld", file_control.swap_file_size);
161 return RESOURCED_ERROR_NONE;
164 static struct swap_module_ops swap_file_ops = {
166 .type = SWAP_TYPE_FILE,
168 .priority = SWAP_PRI_LOW,
170 .init = swap_file_init,
171 .activate = swap_file_activate,
172 .reclaim = swap_file_reclaim,
173 .conf = swap_file_conf,
175 SWAP_MODULE_REGISTER(&swap_file_ops)