From 463e2b321491e4e83e7bda7674f2847dc886cff1 Mon Sep 17 00:00:00 2001
From: Ollrogge <nils-ollrogge@outlook.de>
Date: Mon, 12 Sep 2022 14:48:18 +0200
Subject: [PATCH] pb_submitter: read plan from file

---
 pb_utils/mod_gen_new/Makefile        |   7 --
 pb_utils/mod_gen_new/build.sh        |  13 ----
 pb_utils/mod_gen_new/test.c          |  26 -------
 pb_utils/pb_submitter/build.sh       |   2 +-
 pb_utils/pb_submitter/example_plan   | 110 +++++++++++++++++++++++++++
 pb_utils/pb_submitter/example_run.sh |   2 +-
 pb_utils/pb_submitter/pb_submitter.c |  56 +++++++++++---
 pb_utils/pb_submitter/test_prog.c    |   5 +-
 8 files changed, 161 insertions(+), 60 deletions(-)
 delete mode 100644 pb_utils/mod_gen_new/Makefile
 delete mode 100755 pb_utils/mod_gen_new/build.sh
 delete mode 100644 pb_utils/mod_gen_new/test.c
 create mode 100644 pb_utils/pb_submitter/example_plan

diff --git a/pb_utils/mod_gen_new/Makefile b/pb_utils/mod_gen_new/Makefile
deleted file mode 100644
index 55503d9ce1a2..000000000000
--- a/pb_utils/mod_gen_new/Makefile
+++ /dev/null
@@ -1,7 +0,0 @@
-obj-m += pb_module.o
-
-all:
-	make -C $(PWD)/../../ M=$(PWD) modules
-
-clean:
-	make -C $(PWD)/../../ M=$(PWD) clean
diff --git a/pb_utils/mod_gen_new/build.sh b/pb_utils/mod_gen_new/build.sh
deleted file mode 100755
index 98a5f15fe0ad..000000000000
--- a/pb_utils/mod_gen_new/build.sh
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/bash
-
-#make
-mkdir -p mnt
-
-gcc -static -o test test.c
-
-sudo mount -o loop ../build/qemu-image.img ./mnt
-sudo cp test ./mnt/root
-
-sudo umount ./mnt
-
-echo "All done. Run ./run_qemu.sh now"
diff --git a/pb_utils/mod_gen_new/test.c b/pb_utils/mod_gen_new/test.c
deleted file mode 100644
index a7abb227be8d..000000000000
--- a/pb_utils/mod_gen_new/test.c
+++ /dev/null
@@ -1,26 +0,0 @@
-#include <stdio.h>
-#include <sys/syscall.h>
-#include <unistd.h>
-
-#define PB_SET_PLAN 0x1337
-
-typedef struct {
-    int tmp;
-} plan_t;
-
-// check kernel log with dmesg command afterwards
-int main(void)
-{
-    plan_t plan = {0};
-    plan.tmp = 0x414141;
-
-    int ret = syscall(PB_SET_PLAN, &plan);
-    if (ret < 0) {
-        perror("pb: ");
-        return -1;
-    }
-
-    puts("Done. Check kernel log with dmesg for message");
-
-    return 0;
-}
diff --git a/pb_utils/pb_submitter/build.sh b/pb_utils/pb_submitter/build.sh
index b2da7093921f..4b92faed1e92 100755
--- a/pb_utils/pb_submitter/build.sh
+++ b/pb_utils/pb_submitter/build.sh
@@ -7,7 +7,7 @@ gcc -static -o pb_submitter pb_submitter.c
 gcc -static -o test_prog test_prog.c
 
 sudo mount -o loop ../build/qemu-image.img ./mnt
-sudo cp pb_submitter test_prog example_run.sh ./mnt/root
+sudo cp pb_submitter test_prog example_run.sh example_plan ./mnt/root
 
 sudo umount ./mnt
 
diff --git a/pb_utils/pb_submitter/example_plan b/pb_utils/pb_submitter/example_plan
new file mode 100644
index 000000000000..d3a1ca1428a9
--- /dev/null
+++ b/pb_utils/pb_submitter/example_plan
@@ -0,0 +1,110 @@
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
+100000
diff --git a/pb_utils/pb_submitter/example_run.sh b/pb_utils/pb_submitter/example_run.sh
index b399f1bf4924..f4ebc4074e57 100755
--- a/pb_utils/pb_submitter/example_run.sh
+++ b/pb_utils/pb_submitter/example_run.sh
@@ -1,3 +1,3 @@
 #!/bin/sh
 
-./pb_submitter ./test 16 16
+./pb_submitter test example_plan
diff --git a/pb_utils/pb_submitter/pb_submitter.c b/pb_utils/pb_submitter/pb_submitter.c
index 61de4581ffc4..3a677d3c30b6 100644
--- a/pb_utils/pb_submitter/pb_submitter.c
+++ b/pb_utils/pb_submitter/pb_submitter.c
@@ -14,41 +14,75 @@ typedef struct {
 
 static void usage(void)
 {
-    puts("Usage: ./pb_submitter <prog_name> <inst_cnt> <num_tasks>");
+    puts("Usage: ./pb_submitter <prog_name> <plan_file>");
+}
+
+static size_t count_lines(FILE *fp)
+{
+    char c;
+    size_t lines = 0;
+    while (!feof(fp)) {
+        c = fgetc(fp);
+        if (c == '\n') {
+            lines++;
+        }
+    }
+
+    rewind(fp);
+
+    return lines;
 }
 
 int main(int argc, char** argv)
 {
-    if (argc < 0x4) {
+    if (argc < 0x3) {
         usage();
         return -1;
     }
 
     int ret;
     pb_plan_t plan = { 0 };
-
-    ret = sscanf(argv[3], "%zu", &plan.num_tasks);
-    if (ret != 1) {
+    FILE *fp;
+    char *line = NULL;
+    size_t len = 0;
+    ssize_t read;
+    
+    fp = fopen(argv[2], "r");
+    if (fp == NULL) {
         usage();
         return -1;
     }
 
+    plan.num_tasks = count_lines(fp);
+
+    printf("num_tasks: %zu \n", plan.num_tasks);
+
     uint64_t* inst_cnt = calloc(plan.num_tasks, sizeof(uint64_t));
     if (inst_cnt == NULL) {
         perror("calloc");
         return -1;
     }
 
-    // todo only for testing:
-    // need to pass in a file that contains all the inst_cnts at some point
-    for (int i = 0; i < 0x10; i++) {
-        inst_cnt[i] = 0x10000;
+    for (size_t i = 0; i < plan.num_tasks; i++) {
+        read = getline(&line, &len, fp);
+
+        if (read < 0) {
+            perror("getline");
+            return -1;
+        }
+
+        uint64_t cnt = strtoull(line, NULL, 10);
+
+        if (inst_cnt == 0) {
+            puts("Inst_cnt conversion failure");
+            return -1;
+        }
+
+        inst_cnt[i] = cnt;
     }
 
     plan.inst_cnt = inst_cnt;
 
-    printf("inst_cnt address: %p \n", inst_cnt);
-
     plan.pid = getpid();
 
     ret = syscall(PB_SET_PLAN, &plan);
diff --git a/pb_utils/pb_submitter/test_prog.c b/pb_utils/pb_submitter/test_prog.c
index 902617de5e32..07e16b3618d0 100644
--- a/pb_utils/pb_submitter/test_prog.c
+++ b/pb_utils/pb_submitter/test_prog.c
@@ -5,8 +5,11 @@ int main(void)
     unsigned int c = 0;
     // printk(KERN_WARNING "Hello from Module.\n");
     int a = 0;
+    int b = 0;
     // printk(KERN_WARNING "A.\n");
-    for (;a < 200000; a++){asm("");}
+    for (;b < 100; b++) {
+      for (;a < 100000; a++){asm("");}
+    }
     // printk(KERN_WARNING "B.\n");
     c++;
     // printk(KERN_WARNING "Bye from module.\n");
-- 
GitLab