1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LOS_LMK_H
33 #define _LOS_LMK_H
34 
35 #include "los_config.h"
36 #include "los_compiler.h"
37 #include "los_list.h"
38 #include "los_error.h"
39 
40 typedef UINT32 (*FreeMemByKillingTask)(VOID);
41 typedef UINT32 (*RestoreKilledTask)(VOID);
42 
43 /**
44  * @ingroup los_lmk
45  * Lmk error code: Invalid parameter.
46  *
47  * Value: 0x02002101
48  *
49  */
50 #define LOS_ERRNO_LMK_INVALID_PARAMETER        LOS_ERRNO_OS_ERROR(LOS_MOD_LMK, 0x01)
51 
52 /**
53  * @ingroup los_lmk
54  * Lmk error code: LosLmkOpsNode already registered.
55  *
56  * Value: 0x02002102
57  *
58  */
59 #define LOS_ERRNO_LMK_ALREADY_REGISTERED        LOS_ERRNO_OS_ERROR(LOS_MOD_LMK, 0x02)
60 
61 /**
62  * @ingroup los_lmk
63  * Lmk error code: LosLmkOpsNode not yet registered.
64  *
65  * Value: 0x02002103
66  *
67  */
68 #define LOS_ERRNO_LMK_NOT_REGISTERED            LOS_ERRNO_OS_ERROR(LOS_MOD_LMK, 0x03)
69 
70 /**
71  * @ingroup los_lmk
72  * Lmk error code: Failed to free memory by invoking the registered functions.
73  *
74  * Value: 0x02002104
75  *
76  */
77 #define LOS_ERRNO_LMK_FREE_MEMORY_FAILURE       LOS_ERRNO_OS_ERROR(LOS_MOD_LMK, 0x04)
78 
79 /**
80  * @ingroup los_lmk
81  * Lmk error code: The registered free memory functions have been invoked.
82  *
83  * Value: 0x02002105
84  *
85  */
86 #define LOS_ERRNO_LMK_MEMORY_ALREADY_FREED     LOS_ERRNO_OS_ERROR(LOS_MOD_LMK, 0x05)
87 
88 /**
89  * @ingroup los_lmk
90  * Lmk error code: Failed to restore the killed tasks by invoking the registered functions.
91  *
92  * Value: 0x02002106
93  *
94  */
95 #define LOS_ERRNO_LMK_RESTORE_TASKS_FAILURE     LOS_ERRNO_OS_ERROR(LOS_MOD_LMK, 0x06)
96 
97 /**
98  * @ingroup los_lmk
99  * Lmk error code: No need to restore when no free memory functions have been invoked.
100  *
101  * Value: 0x02002107
102  *
103  */
104 #define LOS_ERRNO_LMK_RESTORE_NOT_NEEDED        LOS_ERRNO_OS_ERROR(LOS_MOD_LMK, 0x07)
105 
106 typedef struct {
107     UINT32 priority;             /**< The priority in the LMK list, the higher priority with a smaller number. */
108     UINT32 (*freeMem)(VOID);     /**< Release the memory of tasks in the LMK list. Return LOS_OK for a successful release. */
109     UINT32 (*restoreTask)(VOID); /**< Restore the tasks killed by freeMem(). Return LOS_OK for a successful restore. */
110     LOS_DL_LIST node;            /**< LosLmkOpsNode node.  */
111 } LosLmkOpsNode;
112 
113 typedef struct {
114     LOS_DL_LIST lmkOpsList;     /**< The registered LosLmkOpsNode will be inserted in this list. */
115     BOOL isMemFreed;            /**< Flag that if LOS_LmkTasksKill has been invoked. */
116 } LosLmkOps;
117 
118 /**
119  * @ingroup los_lmk
120  * @brief Register a low memory killer node.
121  *
122  * @par Description:
123  * This API is used to register a low memory killer node. A LosLmkOpsNode node
124  * can be registered only once.
125  *
126  * @attention None.
127  *
128  * @param  lmkNode [IN] The LosLmkOpsNode node to be registered.
129  *
130  * @retval LOS_OK The LosLmkOpsNode node is registered successfully.
131  * @retval LOS_ERRNO_LMK_INVALID_PARAMETER  The parameter is invalid.
132  * @retval LOS_ERRNO_LMK_ALREADY_REGISTERED The LosLmkOpsNode node already registered.
133  * @par Dependency:
134  * <ul><li>los_lmk.h: the header file that contains the API declaration.</li></ul>
135  * @see
136  */
137 UINT32 LOS_LmkOpsNodeRegister(LosLmkOpsNode *lmkNode);
138 
139 /**
140  * @ingroup los_lmk
141  * @brief Unregister a low memory killer node.
142  *
143  * @par Description:
144  * This API is used to unregister a low memory killer node.
145  *
146  * @attention None.
147  *
148  * @param  lmkNode [IN] The LosLmkOpsNode node to be registered.
149  *
150  * @retval LOS_OK The LosLmkOpsNode node is unregistered successfully.
151  * @retval LOS_ERRNO_LMK_NOT_REGISTERED The LosLmkOpsNode node is not yet registered.
152  * @par Dependency:
153  * <ul><li>los_lmk.h: the header file that contains the API declaration.</li></ul>
154  * @see
155  */
156 UINT32 LOS_LmkOpsNodeUnregister(LosLmkOpsNode *lmkNode);
157 
158 /**
159  * @ingroup los_lmk
160  * @brief Initialize low memory killer framework.
161  *
162  * @par Description:
163  * This API is used to initialize the low memory killer framework.
164  *
165  * @attention None.
166  *
167  * @param  None.
168  *
169  * @retval None.
170  * @par Dependency:
171  * <ul><li>los_lmk.h: the header file that contains the API declaration.</li></ul>
172  * @see
173  */
174 VOID OsLmkInit(VOID);
175 
176 /**
177  * @ingroup los_lmk
178  * @brief Restore the tasks killed by the task which triggers low memory killer.
179  *
180  * @par Description:
181  * This API is used to restore the tasks killed by the task which triggers low memory killer.
182  * This function will be invoked by the developer as needed.
183  *
184  * @attention None.
185  *
186  * @param  None.
187  *
188  * @retval LOS_OK  All the restore killed tasks functions are invoked successfully.
189  * @retval LOS_ERRNO_LMK_RESTORE_NOT_NEEDED No need to restore since no tasks killed to free memory.
190  * @retval LOS_ERRNO_LMK_RESTORE_TASKS_FAILURE Failed to restore the killed tasks by invoking the registered functions.
191  * @par Dependency:
192  * <ul><li>los_lmk.h: the header file that contains the API declaration.</li></ul>
193  * @see
194  */
195 UINT32 LOS_LmkTasksRestore(VOID);
196 
197 /**
198  * @ingroup los_lmk
199  * @brief Kill the tasks to release the used memory.
200  *
201  * @par Description:
202  *  This API is used to kill the tasks to release the used memory when low memory killer is triggered.
203  *
204  * @attention None.
205  *
206  * @param  None.
207  *
208  * @retval LOS_OK All the free memory functions are invoked successfully.
209  * @retval LOS_ERRNO_LMK_MEMORY_ALREADY_FREED The registered free memory functions have been invoked.
210  * @retval LOS_ERRNO_LMK_FREE_MEMORY_FAILURE Failed to free memory by invoking the registered functions.
211  * @par Dependency:
212  * <ul><li>los_lmk.h: the header file that contains the API declaration.</li></ul>
213  * @see
214  */
215 UINT32 LOS_LmkTasksKill(VOID);
216 
217 #if (LOSCFG_KERNEL_LMK_DEBUG == 1)
218 /**
219  * @ingroup los_lmk
220  * @brief Output the low memory killer node priorities.
221  *
222  * @par Description:
223  *  This API is used to output the low memory killer node priorities.
224  *
225  * @attention None.
226  *
227  * @param  None.
228  *
229  * @retval None.
230  * @par Dependency:
231  * <ul><li>los_lmk.h: the header file that contains the API declaration.</li></ul>
232  * @see
233  */
234 VOID LOS_LmkOpsNodeInfoShow(VOID);
235 #endif
236 #endif
237