Remove useless source code
[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_init(void *data)
103 {
104         struct swap_module_ops *swap = (struct swap_module_ops *)data;
105
106         swap->k_size = BYTE_TO_KBYTE(file_control.swap_file_size);
107         return 0;
108 }
109
110 static int swap_file_conf(void *data)
111 {
112         struct fileswap_conf *fileswap_conf = (struct fileswap_conf *)data;
113         if (!fileswap_conf) {
114                 _E("Fileswap configuration should not be NULL");
115                 return RESOURCED_ERROR_FAIL;
116         }
117
118         if (check_valid_compressor(fileswap_conf->type) == RESOURCED_ERROR_NONE)
119                 strncpy(file_control.crypt_type, fileswap_conf->type, MAX_TYPE_LENGTH-1);
120
121         if (fileswap_conf->filesize > 0)
122                 file_control.swap_file_size = fileswap_conf->filesize;
123
124         _I("[DEBUG] fileswap crypt type = %s", file_control.crypt_type);
125         _I("[DEBUG] fileswap file size = %ld", file_control.swap_file_size);
126
127         return RESOURCED_ERROR_NONE;
128 }
129
130 static struct swap_module_ops swap_file_ops = {
131         .name = "FILESWAP",
132         .type = SWAP_TYPE_FILE,
133         .path = "",
134         .priority = SWAP_PRI_LOW,
135         .k_size = 0,
136         .init = swap_file_init,
137         .activate = swap_file_activate,
138         .reclaim = swap_file_reclaim,
139         .conf = swap_file_conf,
140 };
141 SWAP_MODULE_REGISTER(&swap_file_ops)