From 7fd53301958e1a7db2225480e51a2c84f246a1bf Mon Sep 17 00:00:00 2001 From: fslongjin Date: Sun, 7 Aug 2022 21:17:02 +0800 Subject: [PATCH] =?UTF-8?q?new:=20shell=E4=B8=AD=E7=9A=84free=E5=91=BD?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/apps/shell/cmd.c | 37 ++++++++++++++++++++++++++++++++++++- user/apps/shell/cmd.h | 9 +++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/user/apps/shell/cmd.c b/user/apps/shell/cmd.c index 12f929c0..6d0bf2c0 100644 --- a/user/apps/shell/cmd.c +++ b/user/apps/shell/cmd.c @@ -32,6 +32,7 @@ struct built_in_cmd_t shell_cmds[] = {"reboot", shell_cmd_reboot}, {"touch", shell_cmd_touch}, {"about", shell_cmd_about}, + {"free", shell_cmd_free}, {"help", shell_help}, }; @@ -460,12 +461,46 @@ int shell_cmd_about(int argc, char **argv) * @param argv * @return int */ -// todo: int shell_cmd_reboot(int argc, char **argv) { return syscall_invoke(SYS_REBOOT, 0, 0, 0, 0, 0, 0, 0, 0); } +int shell_cmd_free(int argc, char **argv) +{ + int retval = 0; + if (argc == 2 && strcmp("-m", argv[1]) != 0) + { + retval = -EINVAL; + printf("Invalid argument: %s\n", argv[1]); + goto done; + } + + struct mstat_t mst = {0}; + retval = mstat(&mst); + if(retval!=0) + { + printf("Failed: retval=%d", retval); + goto done; + } + + printf("\ttotal\tused\tfree\tshared\tcache\tavailable\n"); + printf("Mem:\t"); + if(argc==1) // 按照kb显示 + { + printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total>>10,mst.used>>10,mst.free>>10, mst.shared>>10, mst.cache_used>>10, mst.available>>10); + } + else // 按照MB显示 + { + printf("%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t\n", mst.total>>20,mst.used>>20,mst.free>>20, mst.shared>>20, mst.cache_used>>20, mst.available>>20); + } + +done:; + if (argv != NULL) + free(argv); + return retval; +} + /** * @brief 解析shell命令 * diff --git a/user/apps/shell/cmd.h b/user/apps/shell/cmd.h index a74f11f7..2bf030cb 100644 --- a/user/apps/shell/cmd.h +++ b/user/apps/shell/cmd.h @@ -132,6 +132,15 @@ int shell_cmd_reboot(int argc, char **argv); */ int shell_cmd_about(int argc, char **argv); +/** + * @brief 显示系统内存空间信息的命令 + * + * @param argc + * @param argv + * @return int + */ +int shell_cmd_free(int argc, char **argv); + /** * @brief 解析shell命令 *