1 /*
2  * Copyright (c) 2020 BayLibre, SAS
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr.h>
8 #include <stdio.h>
9 
10 #include "main.h"
11 
12 /*
13  * 0xB00 is CSR mcycle
14  * 0xB02 is CSR minstret
15  */
supervisor_thread_function(void * p1,void * p2,void * p3)16 void supervisor_thread_function(void *p1, void *p2, void *p3)
17 {
18 	register unsigned long cycle_before, cycle_count;
19 	register unsigned long inst_before, inst_count;
20 
21 	printf("Supervisor thread started\n");
22 
23 	while (1) {
24 		k_sleep(K_MSEC(2000));
25 
26 		inst_before = csr_read(0xB02);
27 		cycle_before = csr_read(0xB00);
28 		k_current_get();
29 		cycle_count = csr_read(0xB00);
30 		inst_count = csr_read(0xB02);
31 
32 		if (cycle_count > cycle_before) {
33 			cycle_count -= cycle_before;
34 		} else {
35 			cycle_count += 0xFFFFFFFF - cycle_before;
36 		}
37 
38 		if (inst_count > inst_before) {
39 			inst_count -= inst_before;
40 		} else {
41 			inst_count += 0xFFFFFFFF - inst_before;
42 		}
43 
44 		/* Remove CSR accesses to be more accurate */
45 		inst_count -= 3;
46 
47 		printf("Supervisor thread:\t%8lu cycles\t%8lu instructions\n",
48 			cycle_count, inst_count);
49 	}
50 }
51