summaryrefslogtreecommitdiff
path: root/src/user/app
diff options
context:
space:
mode:
authordzwdz2022-08-01 22:20:00 +0200
committerdzwdz2022-08-01 22:20:00 +0200
commit341961b59eb08bfbbb1cc2a43704b8916142a5b2 (patch)
treec6120b39e6da14718647f3cbe88209484a52c64b /src/user/app
parent80bd52a861b6abb0e6b4a1685e4b96581dd09221 (diff)
user/shell: add a `time` builtin
Diffstat (limited to 'src/user/app')
-rw-r--r--src/user/app/shell/shell.c40
1 files changed, 26 insertions, 14 deletions
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];