cee56f9a79ef0c27e7da40f16977a54c2b322988
[platform/core/system/pass.git] / src / resource / resource-memory.c
1 /*
2  * PASS (Power Aware System Service) - Memory Resource Driver
3  *
4  * Copyright (c) 2022 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 /**
20  * @file        resource-memory.c
21  * @brief       TBD
22  * @ingroup     TBD
23  */
24
25 #include <glib.h>
26
27 #include <util/common.h>
28 #include <util/log.h>
29 #include <util/kernel.h>
30
31 #include <libsyscommon/resource-manager.h>
32 #include <libsyscommon/resource-type.h>
33
34 #include <resource-monitor/resource-monitor.h>
35
36 #define PROC_MEM_INFO_MEM_AVAILABLE "MemAvailable"
37 #define PROC_MEM_INFO_MEM_FREE "MemFree"
38 #define PROC_MEM_INFO_BUFFER "Buffers"
39 #define PROC_MEM_INFO_CACHED "Cached"
40 #define PROC_MEM_INFO_CMA_FREE "CmaFree"
41 #define PROC_MEM_INFO_SWAP_FREE "SwapFree"
42
43 static int memory_get_cma_total(u_int64_t *val)
44 {
45         static u_int64_t cma_total = 0;
46         int ret;
47
48         if (!cma_total) {
49                 ret = kernel_get_memory_info("CmaTotal", &cma_total);
50                 if (ret < 0) {
51                         _E("failed to get system CMA total memory\n");
52                         return -EINVAL;
53                 }
54         }
55
56         *val = cma_total;
57
58         return 0;
59 }
60
61 static int memory_get_swap_total(u_int64_t *val)
62 {
63         static u_int64_t swap_total = 0;
64         int ret;
65
66         if (!swap_total) {
67                 ret = kernel_get_memory_info("SwapTotal", &swap_total);
68                 if (ret < 0) {
69                         _E("failed to get system SWAP total memory\n");
70                         return -EINVAL;
71                 }
72         }
73
74         *val = swap_total;
75
76         return 0;
77 }
78
79 static int memory_get_memory_info(struct syscommon_resman_resource *res,
80                                 const struct syscommon_resman_resource_attribute *attr,
81                                 void *data)
82 {
83         u_int64_t *val = (u_int64_t *)data;
84         int ret;
85
86         if (!res || !attr || !data)
87                 return -EINVAL;
88
89         switch (attr->id) {
90         case MEMORY_ATTR_TOTAL:
91                 ret = kernel_get_memory_total(val);
92                 break;
93         case MEMORY_ATTR_AVAILABLE:
94                 ret = kernel_get_memory_info(PROC_MEM_INFO_MEM_AVAILABLE, val);
95                 break;
96         case MEMORY_ATTR_FREE:
97                 ret = kernel_get_memory_info(PROC_MEM_INFO_MEM_FREE, val);
98                 break;
99         case MEMORY_ATTR_BUFFER:
100                 ret = kernel_get_memory_info(PROC_MEM_INFO_BUFFER, val);
101                 break;
102         case MEMORY_ATTR_CACHED:
103                 ret = kernel_get_memory_info(PROC_MEM_INFO_CACHED, val);
104                 break;
105         case MEMORY_ATTR_CMA_TOTAL:
106                 ret = memory_get_cma_total(val);
107                 break;
108         case MEMORY_ATTR_CMA_FREE:
109                 ret = kernel_get_memory_info(PROC_MEM_INFO_CMA_FREE, val);
110                 break;
111         case MEMORY_ATTR_SWAP_TOTAL:
112                 ret = memory_get_swap_total(val);
113                 break;
114         case MEMORY_ATTR_SWAP_FREE:
115                 ret = kernel_get_memory_info(PROC_MEM_INFO_SWAP_FREE, val);
116                 break;
117         default:
118                 _E("wrong memory resource attribute\n");
119                 ret = -EINVAL;
120         }
121         if (ret < 0)
122                 return -EINVAL;
123
124         return 0;
125 }
126
127 static const struct syscommon_resman_resource_attribute memory_attrs[] = {
128         {
129                 .name   = "MEMORY_ATTR_TOTAL",
130                 .id     = MEMORY_ATTR_TOTAL,
131                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
132                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
133                 .ops    = {
134                         .get = memory_get_memory_info,
135                 },
136         }, {
137                 .name   = "MEMORY_ATTR_AVAILABLE",
138                 .id     = MEMORY_ATTR_AVAILABLE,
139                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
140                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
141                 .ops    = {
142                         .get = memory_get_memory_info,
143                 },
144         }, {
145                 .name   = "MEMORY_ATTR_FREE",
146                 .id     = MEMORY_ATTR_FREE,
147                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
148                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
149                 .ops    = {
150                         .get = memory_get_memory_info,
151                 }
152         }, {
153                 .name   = "MEMORY_ATTR_BUFFER",
154                 .id     = MEMORY_ATTR_BUFFER,
155                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
156                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
157                 .ops    = {
158                         .get = memory_get_memory_info,
159                 }
160         }, {
161                 .name   = "MEMORY_ATTR_CACHED",
162                 .id     = MEMORY_ATTR_CACHED,
163                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
164                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
165                 .ops    = {
166                         .get = memory_get_memory_info,
167                 }
168         }, {
169                 .name   = "MEMORY_ATTR_CMA_TOTAL",
170                 .id     = MEMORY_ATTR_CMA_TOTAL,
171                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
172                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
173                 .ops    = {
174                         .get = memory_get_memory_info,
175                 }
176         }, {
177                 .name   = "MEMORY_ATTR_CMA_FREE",
178                 .id     = MEMORY_ATTR_CMA_FREE,
179                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
180                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
181                 .ops    = {
182                         .get = memory_get_memory_info,
183                 }
184         }, {
185                 .name   = "MEMORY_ATTR_SWAP_TOTAL",
186                 .id     = MEMORY_ATTR_SWAP_TOTAL,
187                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
188                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
189                 .ops    = {
190                         .get = memory_get_memory_info,
191                 }
192         }, {
193                 .name   = "MEMORY_ATTR_SWAP_FREE",
194                 .id     = MEMORY_ATTR_SWAP_FREE,
195                 .type   = SYSCOMMON_RESMAN_DATA_TYPE_UINT64,
196                 .flag   = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PUBLIC,
197                 .ops    = {
198                         .get = memory_get_memory_info,
199                 }
200         },
201 };
202
203 static const struct syscommon_resman_resource_driver memory_resource_driver = {
204         .name           = "MEMORY",
205         .type           = RESOURCE_TYPE_MEMORY,
206         .flag           = SYSCOMMON_RESMAN_RESOURCE_DRIVER_FLAG_COUNT_ONLY_ONE,
207         .attrs          = memory_attrs,
208         .num_attrs      = ARRAY_SIZE(memory_attrs),
209 };
210 SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(&memory_resource_driver)