From 6ecdbb4eb9738aa9688d27969b7eb2d03a78722a Mon Sep 17 00:00:00 2001
From: Stefan Moll <stefan@stefaus.de>
Date: Wed, 14 Sep 2022 10:43:06 +0200
Subject: [PATCH] cleanup mod_gen folder, modify build file and add to readme

---
 pb_utils/.gitignore               |  1 -
 pb_utils/mod_gen/Makefile         |  7 ---
 pb_utils/mod_gen/build.sh         | 23 +++++----
 pb_utils/mod_gen/measure.c        | 29 -----------
 pb_utils/mod_gen/parse_dmesg.pl   | 80 -------------------------------
 pb_utils/mod_gen/tmpl/Makefile.tt |  4 ++
 pb_utils/mod_gen/tmpl/module.tt   |  2 +-
 pb_utils/readme.md                |  6 +++
 8 files changed, 24 insertions(+), 128 deletions(-)
 delete mode 100644 pb_utils/mod_gen/Makefile
 delete mode 100644 pb_utils/mod_gen/measure.c
 delete mode 100755 pb_utils/mod_gen/parse_dmesg.pl

diff --git a/pb_utils/.gitignore b/pb_utils/.gitignore
index 4cd39960308d..48ef480a33be 100644
--- a/pb_utils/.gitignore
+++ b/pb_utils/.gitignore
@@ -1,3 +1,2 @@
 build/
 mod_gen/mods
-mod_gen_new/test
\ No newline at end of file
diff --git a/pb_utils/mod_gen/Makefile b/pb_utils/mod_gen/Makefile
deleted file mode 100644
index aadd2f5d958b..000000000000
--- a/pb_utils/mod_gen/Makefile
+++ /dev/null
@@ -1,7 +0,0 @@
-obj-m += measure.o
-
-all:
-	make -C /home/kelvin/git/master_thesis_linux/ M=$(PWD) modules
-
-clean:
-	make -C /home/kelvin/git/master_thesis_linux/ M=$(PWD) clean
diff --git a/pb_utils/mod_gen/build.sh b/pb_utils/mod_gen/build.sh
index d977d82482c3..a4c1c8d36c9e 100755
--- a/pb_utils/mod_gen/build.sh
+++ b/pb_utils/mod_gen/build.sh
@@ -1,12 +1,15 @@
 #!/bin/bash
 
-#make
-mkdir -p mnt
-
-sudo mount -o loop ../build/qemu-image.img ./mnt
-sudo cp ./mods/plan_same_v.ko ./mnt/root
-sudo cp ./mods/random_v.ko ./mnt/root
-
-sudo umount ./mnt
-
-echo "All done. Run ./run_qemu.sh now"
+if [ ! -d "/mnt/pb_utils/mod_gen" ]; then
+    echo "are you running this in chroot or the build system?"
+    exit 1
+fi
+
+cd /mnt/pb_utils/mod_gen/
+rm -rf mods
+./plan_to_module.pl
+cd mods && make
+
+echo "################################################"
+echo "modules created and copied to /root"
+echo "use 'insmod <file' to run within the test system"
diff --git a/pb_utils/mod_gen/measure.c b/pb_utils/mod_gen/measure.c
deleted file mode 100644
index 009f8f0d93d7..000000000000
--- a/pb_utils/mod_gen/measure.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/init.h>
-#include <linux/kthread.h>
-#include <../kernel/sched/sched.h>
-
-
-MODULE_LICENSE("GPL");
-
-static int __init pb_client_init(void)
-{
-    struct rq *rq;
-
-    rq = this_rq();
-    set_pb_measure_on(&rq->pb);
-
-    return 0;
-}
-
-static void __exit pb_client_cleanup(void)
-{
-    struct rq *rq;
-    rq = this_rq();
-    set_pb_measure_off(&rq->pb);
-
-}
-
-module_init(pb_client_init);
-module_exit(pb_client_cleanup);
diff --git a/pb_utils/mod_gen/parse_dmesg.pl b/pb_utils/mod_gen/parse_dmesg.pl
deleted file mode 100755
index 905c2e7469f4..000000000000
--- a/pb_utils/mod_gen/parse_dmesg.pl
+++ /dev/null
@@ -1,80 +0,0 @@
-#!/usr/bin/perl
-
-use Plan;
-
-my $argc = @ARGV;
-
-my $dmesg_log = $ARGV[0];
-
-my $last_time = 0;
-my $last_nr = 0;
-my @results;
-
-open (FILE, $dmesg_log);
-
-while (<FILE>)
-{
-    unless (/^.*(EXEC|IDLE),(START|STOP),\d*,\d*.*/)
-    {
-        next;
-    }
-    chomp;
-
-    ($dirty_mode, $state, $entry, $time) = split(/,/);
-
-    $mode = "";
-    if ($dirty_mode =~ /.*(EXEC|IDLE).*/)
-    {
-        $mode = $1;
-    }
-
-    if ( $state eq "START" )
-    {
-        $last_time = $time;
-        $last_nr = $entry;
-    }
-
-    if ( $state eq "STOP" )
-    {
-        $result = $time - $last_time;
-        push(@results, { mode => $mode, nr => $last_nr, time => $result });
-    }
-}
-close (FILE);
-
-if ( $argc > 1 )
-{
-    my $plan_csv = $ARGV[1];
-    print_compared_result ($plan_csv, @results);
-}
-else
-{
-    print_simple_result (@results);
-}
-
-sub print_simple_result
-{
-    my (@results) = @_;
-
-    foreach (@results)
-    {
-        print ($_->{mode}.'['.$_->{nr}.']'.': '.$_->{time}."\n");
-    }
-}
-
-sub print_compared_result
-{
-    my ($plan_csv_file, @results) = @_;
-    my @expected_results = parse_plan_csv($plan_csv_file);
-
-    push(@results, {mode => 'IDLE', nr => $results[-1]{nr}, time => 0});
-
-    for (my $i=0; $i <= $#results; $i++)
-    {
-        my $expected = $expected_results[$i]->{time};
-        my $actual = $results[$i]->{time};
-        my $diff = abs($expected - $actual);
-
-        print ($results[$i]->{mode}.'['.$results[$i]->{nr}.']'.' actual: '.$actual.', expected: '.$expected.', diff: '.$diff."\n");
-    }
-}
diff --git a/pb_utils/mod_gen/tmpl/Makefile.tt b/pb_utils/mod_gen/tmpl/Makefile.tt
index 9cdbc1ac98b4..362fe2c616d3 100644
--- a/pb_utils/mod_gen/tmpl/Makefile.tt
+++ b/pb_utils/mod_gen/tmpl/Makefile.tt
@@ -2,6 +2,10 @@
 [% END %]
 all:
 	make -C [% linux_src_dir %] M=$(PWD) modules
+	[% FOREACH module_name IN module_names %]cp [% module_name %].ko /root
+	[% END %]
 
 clean:
 	make -C [% linux_src_dir %] M=$(PWD) clean
+	[% FOREACH module_name IN module_names %]rm /root/[% module_name %].ko
+	[% END %]
diff --git a/pb_utils/mod_gen/tmpl/module.tt b/pb_utils/mod_gen/tmpl/module.tt
index efa4adb9d615..4855ac59bed0 100644
--- a/pb_utils/mod_gen/tmpl/module.tt
+++ b/pb_utils/mod_gen/tmpl/module.tt
@@ -27,7 +27,7 @@ static void init_rq(struct rq *rq)
 {
     struct pb_rq *pb_rq = &rq->pb;
     struct task_struct *proxy_task;
-    int i;
+
     proxy_task = kthread_create(loop_thread_func, NULL, "PB proxy thread");
     proxy_task->sched_class = &pb_sched_class;
     set_pb_plan_size(pb_rq, [% plan_size %]);
diff --git a/pb_utils/readme.md b/pb_utils/readme.md
index eb5f75153bd9..f7f06bf91c68 100644
--- a/pb_utils/readme.md
+++ b/pb_utils/readme.md
@@ -20,6 +20,12 @@ cd /mnt
 ./build
 ```
 
+## chroot into debian rootfs to build test modules
+run `./chroot_image.sh`, this drops you in a shell on the created debian and run
+```bash
+/mnt/pb_utils/mod_gen/build.sh
+```
+
 # run the build kernel with the created rootfs
 run `./run_qemu.sh`
 
-- 
GitLab