• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 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 #include "stdlib.h"
33 #include "shcmd.h"
34 #include "shell.h"
35 #include "los_memory.h"
36 
37 #define MEM_SIZE_1K 0x400
38 #define MEM_SIZE_1M 0x100000
39 
40 #define MEM_SIZE_TO_KB(size) (((size) + (MEM_SIZE_1K >> 1)) / MEM_SIZE_1K)
41 #define MEM_SIZE_TO_MB(size) (((size) + (MEM_SIZE_1M >> 1)) / MEM_SIZE_1M)
42 
OsShellCmdFreeInfo(INT32 argc,const CHAR * argv[])43 LITE_OS_SEC_TEXT_MINOR STATIC UINT32 OsShellCmdFreeInfo(INT32 argc, const CHAR *argv[])
44 {
45     UINT32 memUsed = LOS_MemTotalUsedGet(m_aucSysMem0);
46     UINT32 totalMem = LOS_MemPoolSizeGet(m_aucSysMem0);
47     UINT32 freeMem = totalMem - memUsed;
48 
49     if ((argc == 0) ||
50         ((argc == 1) && (strcmp(argv[0], "-k") == 0)) ||
51         ((argc == 1) && (strcmp(argv[0], "-m") == 0))) {
52         PRINTK("\n        total        used          free\n");
53     }
54 
55     if ((argc == 1) && (strcmp(argv[0], "-k") == 0)) {
56         PRINTK("Mem:    %-9u    %-10u    %-10u\n", MEM_SIZE_TO_KB(totalMem), MEM_SIZE_TO_KB(memUsed),
57                MEM_SIZE_TO_KB(freeMem));
58     } else if ((argc == 1) && (strcmp(argv[0], "-m") == 0)) {
59         PRINTK("Mem:    %-9u    %-10u    %-10u\n", MEM_SIZE_TO_MB(totalMem), MEM_SIZE_TO_MB(memUsed),
60                MEM_SIZE_TO_MB(freeMem));
61     } else if (argc == 0) {
62         PRINTK("Mem:    %-9u    %-10u    %-10u\n", totalMem, memUsed, freeMem);
63     } else {
64         PRINTK("\nUsage: free or free [-k/-m]\n");
65         return OS_ERROR;
66     }
67     return 0;
68 }
69 
OsShellCmdFree(INT32 argc,const CHAR * argv[])70 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdFree(INT32 argc, const CHAR *argv[])
71 {
72     if (argc > 1) {
73         PRINTK("\nUsage: free or free [-k/-m]\n");
74         return OS_ERROR;
75     }
76     if (OsShellCmdFreeInfo(argc, argv) != 0) {
77         return OS_ERROR;
78     }
79     return 0;
80 }
81 
82 #if (LOSCFG_MEM_WATERLINE == 1)
OsShellCmdWaterLine(INT32 argc,const CHAR * argv[])83 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdWaterLine(INT32 argc, const CHAR *argv[])
84 {
85     LOS_MEM_POOL_STATUS poolStatus;
86 
87     if (argc > 1) {
88         PRINTK("\nUsage: memusage or memusage [-k/-m]\n");
89         return OS_ERROR;
90     }
91 
92     if (LOS_MemInfoGet(m_aucSysMem0, &poolStatus) != LOS_OK) {
93         return OS_ERROR;
94     }
95 
96     if ((argc == 1) && (strcmp(argv[0], "-k") == 0)) {
97         PRINTK("Mem WaterLine:    %-9u\n", MEM_SIZE_TO_KB(poolStatus.usageWaterLine));
98     } else if ((argc == 1) && (strcmp(argv[0], "-m") == 0)) {
99         PRINTK("Mem WaterLine:    %-9u\n", MEM_SIZE_TO_MB(poolStatus.usageWaterLine));
100     } else if (argc == 0) {
101         PRINTK("Mem WaterLine:    %-9u\n", poolStatus.usageWaterLine);
102     } else {
103         PRINTK("\nUsage: memusage or memusage [-k/-m]\n");
104         return OS_ERROR;
105     }
106 
107     return 0;
108 }
109 #endif
110