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