testsuite: ztress: Make cpu load calculation multi-cpu ready

Use idle threads on all cores for cpu load calculation.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
diff --git a/subsys/testsuite/ztest/src/ztress.c b/subsys/testsuite/ztest/src/ztress.c
index 766caae..2829c12 100644
--- a/subsys/testsuite/ztest/src/ztress.c
+++ b/subsys/testsuite/ztest/src/ztress.c
@@ -39,7 +39,7 @@
 static k_timeout_t backoff[CONFIG_ZTRESS_MAX_THREADS];
 static k_timeout_t init_backoff[CONFIG_ZTRESS_MAX_THREADS];
 K_THREAD_STACK_ARRAY_DEFINE(stacks, CONFIG_ZTRESS_MAX_THREADS, CONFIG_ZTRESS_STACK_SIZE);
-static k_tid_t idle_tid;
+static k_tid_t idle_tid[CONFIG_MP_NUM_CPUS];
 
 #define THREAD_NAME(i, _) STRINGIFY(ztress_##i)
 
@@ -107,16 +107,21 @@
 
 static void control_load(void)
 {
-	static uint64_t prev_cycles;
+	static uint64_t prev_idle_cycles;
 	static uint64_t total_cycles;
-
-	k_thread_runtime_stats_t rt_stats_thread;
+	uint64_t idle_cycles = 0;
 	k_thread_runtime_stats_t rt_stats_all;
 	int err = 0;
 
-	err = k_thread_runtime_stats_get(idle_tid, &rt_stats_thread);
-	if (err < 0) {
-		return;
+	for (int i = 0; i < CONFIG_MP_NUM_CPUS; i++) {
+		k_thread_runtime_stats_t thread_stats;
+
+		err = k_thread_runtime_stats_get(idle_tid[i], &thread_stats);
+		if (err < 0) {
+			return;
+		}
+
+		idle_cycles += thread_stats.execution_cycles;
 	}
 
 	err = k_thread_runtime_stats_all_get(&rt_stats_all);
@@ -124,10 +129,10 @@
 		return;
 	}
 
-	int load = 1000 - (1000 * (rt_stats_thread.execution_cycles - prev_cycles) /
+	int load = 1000 - (1000 * (idle_cycles - prev_idle_cycles) /
 			(rt_stats_all.execution_cycles - total_cycles));
 
-	prev_cycles = rt_stats_thread.execution_cycles;
+	prev_idle_cycles = idle_cycles;
 	total_cycles = rt_stats_all.execution_cycles;
 
 	int avg_load = (rt.cpu_load * rt.cpu_load_measurements + load) /
@@ -259,11 +264,15 @@
 
 static void thread_cb(const struct k_thread *cthread, void *user_data)
 {
+#define GET_IDLE_TID(i, tid) do {\
+	if (strcmp(tname, "idle 0" STRINGIFY(i)) == 0) { \
+		idle_tid[i] = tid; \
+	} \
+} while (0)
+
 	const char *tname = k_thread_name_get((struct k_thread *)cthread);
 
-	if (strcmp(tname, "idle 00") == 0) {
-		idle_tid = (struct k_thread *)cthread;
-	}
+	LISTIFY(CONFIG_MP_NUM_CPUS, GET_IDLE_TID, (;), (k_tid_t)cthread);
 }
 
 static void ztress_init(struct ztress_context_data *thread_data)