From be1b9d6031a3ab632dbfd0fb627a81aace2d24a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugho389@student.liu.se>
Date: Mon, 21 Nov 2022 16:50:25 +0100
Subject: [PATCH] Random task select.

---
 robot_agent/src/scheduler.c | 108 ++++++++++++++++++++++++++++++------
 1 file changed, 90 insertions(+), 18 deletions(-)

diff --git a/robot_agent/src/scheduler.c b/robot_agent/src/scheduler.c
index 266c9bc..8ad478e 100644
--- a/robot_agent/src/scheduler.c
+++ b/robot_agent/src/scheduler.c
@@ -14,12 +14,14 @@
 #include <stdlib.h>
 #include <sys/time.h>
 #include <unistd.h>
+#include <locale.h>
+#include <limits.h>
+#include <sys/stat.h>
 /* project libraries */
 #include "scheduler.h"
 #include "task.h"
 #include "timelib.h"
 
-#include <locale.h>
 
 /* -- Defines -- */
 
@@ -129,6 +131,51 @@ void scheduler_exec_task(scheduler_t *ces, int task_id)
 	}
 }
 
+
+
+const char *const task_names[] = {
+  [s_TASK_NOP_ID]         = "NOP",
+  [s_TASK_MISSION_ID]     = "MISSION",
+  [s_TASK_NAVIGATE_ID]    = "NAVIGATE",
+  [s_TASK_CONTROL_ID]     = "CONTROL",
+  [s_TASK_REFINE_ID]      = "REFINE",
+  [s_TASK_REPORT_ID]      = "REPORT",
+  [s_TASK_COMMUNICATE_ID] = "COMMUNICATE",
+  [s_TASK_AVOID_ID]       = "AVOID",
+};
+
+/* Select by commenting and uncommenting here */
+int const tasks[] = {
+  // s_TASK_NOP_ID,
+  s_TASK_MISSION_ID,
+  s_TASK_NAVIGATE_ID,
+  s_TASK_CONTROL_ID,
+  s_TASK_REFINE_ID,
+  s_TASK_REPORT_ID,
+  s_TASK_COMMUNICATE_ID,
+  // s_TASK_AVOID_ID,
+};
+size_t task_count = sizeof tasks / sizeof(int);
+
+#define DO(x) \
+  start = timelib_unix_timestamp(); \
+  for (int i = 0; i < 1; i++) { \
+    scheduler_exec_task(ces, (x)); \
+  } \
+  end = timelib_unix_timestamp(); \
+  printf(";%.9f\n", end - start, (end - start) / 1000.);
+
+
+FILE *logfiles[255] = { 0 };
+
+void close_files (void) {
+  for (int i = 0; i < task_count; i++) {
+    if (logfiles[i]) {
+      fclose(logfiles[i]);
+    }
+  }
+}
+
 /**
  * Run scheduler
  * @param ces Pointer to scheduler structure
@@ -137,6 +184,20 @@ void scheduler_exec_task(scheduler_t *ces, int task_id)
 void scheduler_run(scheduler_t *ces)
 {
 	/* --- Local variables (define variables here) --- */
+  {
+    char *filename = malloc(PATH_MAX);
+    mkdir("/tmp/log", 0777);
+    for (int i = 0; i < task_count; i++) {
+      filename[0] = '\0';
+      strcat(filename, "/tmp/log/");
+      strcat(filename, task_names[tasks[i]]);
+      strcat(filename, ".csv");
+      logfiles[i] = fopen(filename, "w");
+    }
+    free(filename);
+  }
+
+  atexit(close_files);
 
 	/* --- Set minor cycle period --- */
 	ces->minor = 100;
@@ -146,24 +207,35 @@ void scheduler_run(scheduler_t *ces)
   setlocale(LC_NUMERIC, "sv_SE.UTF-8");
   double start, end;
 
-#define DO(x) \
-  start = timelib_unix_timestamp(); \
-  for (int i = 0; i < 100; i++) { \
-    scheduler_exec_task(ces, (x)); \
-  } \
-  end = timelib_unix_timestamp(); \
-  printf(";%.9f\n", end - start, (end - start) / 1000.);
-
 #if 1
-  for (int j = 0; j < 100; j++) {
-    printf("Run %d\n", j);
-    DO(s_TASK_MISSION_ID);      // 0.0005ms
-    DO(s_TASK_NAVIGATE_ID);     // 0.1ms
-    DO(s_TASK_CONTROL_ID);      // 0.6ms
-    DO(s_TASK_REFINE_ID);       // 10ms
-    DO(s_TASK_REPORT_ID);       // 0.00005ms
-    DO(s_TASK_COMMUNICATE_ID);  // 1.7ms
-    DO(s_TASK_AVOID_ID);        // 32ms
+  // printf("Collection loops;Communication time (ms)\n");
+  for (int i = 0; i < task_count; i++) {
+    fprintf(logfiles[i], "Cycle;Task;Runcount;Victims;Time\n");
+  }
+  for (int j = 0;; j++) {
+
+    if (j % 10 == 0/* timer_something */) {
+      printf("Avoiding\n");
+      scheduler_exec_task(ces, s_TASK_AVOID_ID);
+    }
+
+    start = timelib_unix_timestamp();
+    int selected_task = random() % task_count;
+    int runcount = random() % 10;
+
+    for (int i = 0; i < runcount; i++) {
+      scheduler_exec_task(ces, tasks[selected_task]);
+    }
+
+    end = timelib_unix_timestamp();
+    fprintf(logfiles[selected_task], "%d;%s;%d;%d;%.9f\n",
+        j,
+        task_names[selected_task],
+        runcount,
+        g_task_mission_data.victim_count,
+        end - start);
+
+    usleep(1e6 * 1./100);
   }
   // sum = 45ms
 
-- 
GitLab