Browse Source

new: shell中的free命令

fslongjin 2 years ago
parent
commit
7fd5330195
2 changed files with 45 additions and 1 deletions
  1. 36 1
      user/apps/shell/cmd.c
  2. 9 0
      user/apps/shell/cmd.h

+ 36 - 1
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命令
  *

+ 9 - 0
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命令
  *