1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/kernel.h>
7 #include <zephyr/timing/timing.h>
8 #include "utils.h"
9 
10 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
11 /* stack used by the threads */
12 static K_THREAD_STACK_DEFINE(t1_stack, STACK_SIZE);
13 
14 static struct k_thread t1;
15 
16 timing_t timestamp_start_create_c;
17 timing_t timestamp_end_create_c;
18 timing_t timestamp_start_start_c;
19 timing_t timestamp_end_start_c;
20 timing_t timestamp_start_suspend_c;
21 timing_t timestamp_end_suspend_c;
22 timing_t timestamp_start_resume_c;
23 timing_t timestamp_end_resume_c;
24 
25 timing_t timestamp_start_abort_1;
26 timing_t timestamp_end_abort_1;
27 
thread_suspend_resume(void * p1,void * p2,void * p3)28 void thread_suspend_resume(void *p1, void *p2, void *p3)
29 {
30 	timestamp_start_suspend_c = timing_counter_get();
31 	k_thread_suspend(_current);
32 
33 	/* comes to this line once its resumed*/
34 	timestamp_start_resume_c = timing_counter_get();
35 
36 }
37 
suspend_resume(void)38 int suspend_resume(void)
39 {
40 	uint32_t diff;
41 
42 	timing_start();
43 
44 	timestamp_start_create_c = timing_counter_get();
45 
46 	k_tid_t t1_tid = k_thread_create(&t1, t1_stack, STACK_SIZE,
47 					 thread_suspend_resume, NULL, NULL,
48 					 NULL, K_PRIO_PREEMPT(6), 0, K_FOREVER);
49 
50 	timestamp_end_create_c = timing_counter_get();
51 	k_thread_name_set(t1_tid, "t1");
52 
53 	timestamp_start_start_c = timing_counter_get();
54 	k_thread_start(t1_tid);
55 
56 	timestamp_end_suspend_c = timing_counter_get();
57 	k_thread_resume(t1_tid);
58 	timestamp_end_resume_c = timing_counter_get();
59 
60 
61 	diff = timing_cycles_get(&timestamp_start_create_c,
62 				 &timestamp_end_create_c);
63 	PRINT_STATS("Time to create a thread (without start)", diff);
64 
65 	diff = timing_cycles_get(&timestamp_start_start_c,
66 				 &timestamp_start_suspend_c);
67 	PRINT_STATS("Time to start a thread", diff);
68 
69 	diff = timing_cycles_get(&timestamp_start_suspend_c,
70 				 &timestamp_end_suspend_c);
71 	PRINT_STATS("Time to suspend a thread", diff);
72 
73 	diff = timing_cycles_get(&timestamp_start_resume_c,
74 				 &timestamp_end_resume_c);
75 	PRINT_STATS("Time to resume a thread", diff);
76 
77 	timestamp_start_abort_1 = timing_counter_get();
78 	k_thread_abort(t1_tid);
79 	timestamp_end_abort_1 = timing_counter_get();
80 
81 	diff = timing_cycles_get(&timestamp_start_abort_1,
82 				 &timestamp_end_abort_1);
83 	PRINT_STATS("Time to abort a thread (not running)", diff);
84 
85 	timing_stop();
86 	return 0;
87 }
88