1 /*
2 * Copyright (c) 2012 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #include <stdio.h>
9 #include <rand.h>
10 #include <lk/err.h>
11 #include <app/tests.h>
12 #include <kernel/thread.h>
13 #include <kernel/mutex.h>
14 #include <kernel/semaphore.h>
15 #include <kernel/event.h>
16 #include <platform.h>
17
clock_tests(int argc,const console_cmd_args * argv)18 int clock_tests(int argc, const console_cmd_args *argv) {
19 ulong c;
20 lk_time_t t;
21 lk_bigtime_t t2;
22
23 #define CYCLE_COUNT_TRIES 1000000
24 thread_sleep(100);
25 c = arch_cycle_count();
26 for (int i = 0; i < CYCLE_COUNT_TRIES; i++) {
27 t = current_time();
28 }
29 c = arch_cycle_count() - c;
30 printf("%lu cycles per current_time()\n", c / CYCLE_COUNT_TRIES);
31
32 thread_sleep(100);
33 c = arch_cycle_count();
34 for (int i = 0; i < CYCLE_COUNT_TRIES; i++) {
35 t2 = current_time_hires();
36 }
37 c = arch_cycle_count() - c;
38 printf("%lu cycles per current_time_hires()\n", c / CYCLE_COUNT_TRIES);
39
40 printf("making sure time never goes backwards\n");
41 {
42 printf("testing current_time()\n");
43 lk_time_t start = current_time();
44 lk_time_t last = start;
45 for (;;) {
46 t = current_time();
47 //printf("%lu %lu\n", last, t);
48 if (TIME_LT(t, last)) {
49 printf("WARNING: time ran backwards: %u < %u\n", t, last);
50 last = t;
51 continue;
52 }
53 last = t;
54 if (last - start > 5000)
55 break;
56 }
57 }
58 {
59 printf("testing current_time_hires()\n");
60 lk_bigtime_t start = current_time_hires();
61 lk_bigtime_t last = start;
62 for (;;) {
63 t2 = current_time_hires();
64 //printf("%llu %llu\n", last, t2);
65 if (t2 < last) {
66 printf("WARNING: time ran backwards: %llu < %llu\n", t2, last);
67 last = t2;
68 continue;
69 }
70 last = t2;
71 if (last - start > 5000000)
72 break;
73 }
74 }
75
76 printf("making sure current_time() and current_time_hires() are always the same base\n");
77 {
78 lk_time_t start = current_time();
79 for (;;) {
80 t = current_time();
81 t2 = current_time_hires();
82 if (t > ((t2 + 500) / 1000)) {
83 printf("WARNING: current_time() ahead of current_time_hires() %u %llu\n", t, t2);
84 }
85 if (t - start > 5000)
86 break;
87 }
88 }
89
90 printf("counting to 5, in one second intervals\n");
91 for (int i = 0; i < 5; i++) {
92 thread_sleep(1000);
93 printf("%d\n", i + 1);
94 }
95
96 printf("measuring cpu clock against current_time_hires()\n");
97 for (int i = 0; i < 5; i++) {
98 ulong cycles = arch_cycle_count();
99 lk_bigtime_t start = current_time_hires();
100 while ((current_time_hires() - start) < 1000000)
101 ;
102 cycles = arch_cycle_count() - cycles;
103 printf("%lu cycles per second\n", cycles);
104 }
105
106 return NO_ERROR;
107 }
108