From 341961b59eb08bfbbb1cc2a43704b8916142a5b2 Mon Sep 17 00:00:00 2001
From: dzwdz
Date: Mon, 1 Aug 2022 22:20:00 +0200
Subject: user/shell: add a `time` builtin

---
 src/user/app/shell/shell.c | 40 ++++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 14 deletions(-)

(limited to 'src')

diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c
index 35450a8..6eb328f 100644
--- a/src/user/app/shell/shell.c
+++ b/src/user/app/shell/shell.c
@@ -8,6 +8,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <user/lib/fs/misc.h>
+#include <x86intrin.h>
 
 int main();
 
@@ -32,17 +33,7 @@ static void execp(char **argv) {
 	free(s);
 }
 
-static void run(char *cmd) {
-	#define ARGV_MAX 16
-	char *argv[ARGV_MAX];
-	struct redir redir;
-
-	int argc = parse(cmd, argv, ARGV_MAX, &redir);
-	if (argc < 0) {
-		eprintf("error parsing command");
-		return;
-	}
-
+static void run_args(int argc, char **argv, struct redir *redir) {
 	if (!*argv) return;
 
 	/* "special" commands that can't be handled in a subprocess */
@@ -52,9 +43,16 @@ static void run(char *cmd) {
 		return;
 	} else if (!strcmp(argv[0], "whitelist")) {
 		MOUNT_AT("/") {
-			fs_whitelist(&argv[1]);
+			fs_whitelist((void*)&argv[1]);
 		}
 		return;
+	} else if (!strcmp(argv[0], "time")) {
+		uint64_t time = __rdtsc();
+		uint64_t div = 3000;
+		run_args(argc - 1, argv + 1, redir);
+		time = __rdtsc() - time;
+		printf("0x%x ns (assuming 3GHz)\n", time / div);
+		return;
 	} else if (!strcmp(argv[0], "exit")) {
 		exit(0);
 	}
@@ -64,8 +62,8 @@ static void run(char *cmd) {
 		return;
 	}
 
-	if (redir.stdout && !freopen(redir.stdout, redir.append ? "a" : "w", stdout)) {
-		eprintf("couldn't open %s for redirection", redir.stdout);
+	if (redir->stdout && !freopen(redir->stdout, redir->append ? "a" : "w", stdout)) {
+		eprintf("couldn't open %s for redirection", redir->stdout);
 		exit(0);
 	}
 
@@ -85,6 +83,20 @@ static void run(char *cmd) {
 	exit(0); /* kills the subprocess */
 }
 
+static void run(char *cmd) {
+	#define ARGV_MAX 16
+	char *argv[ARGV_MAX];
+	struct redir redir;
+
+	int argc = parse(cmd, argv, ARGV_MAX, &redir);
+	if (argc < 0) {
+		eprintf("error parsing command");
+		return;
+	}
+
+	run_args(argc, argv, &redir);
+}
+
 
 int main(int argc, char **argv) {
 	static char buf[256];
-- 
cgit v1.2.3