1 /*
2  * Copyright (c) 2012 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include <stdio.h>
24 #include <rand.h>
25 #include <err.h>
26 #include <app/tests.h>
27 #include <kernel/thread.h>
28 #include <kernel/mutex.h>
29 #include <kernel/semaphore.h>
30 #include <kernel/event.h>
31 #include <platform.h>
32 
clock_tests(void)33 void clock_tests(void)
34 {
35     uint32_t c;
36     lk_time_t t;
37     lk_bigtime_t t2;
38 
39     thread_sleep(100);
40     c = arch_cycle_count();
41     t = current_time();
42     c = arch_cycle_count() - c;
43     printf("%u cycles per current_time()\n", c);
44 
45     thread_sleep(100);
46     c = arch_cycle_count();
47     t2 = current_time_hires();
48     c = arch_cycle_count() - c;
49     printf("%u cycles per current_time_hires()\n", c);
50 
51     printf("making sure time never goes backwards\n");
52     {
53         printf("testing current_time()\n");
54         lk_time_t start = current_time();
55         lk_time_t last = start;
56         for (;;) {
57             t = current_time();
58             //printf("%lu %lu\n", last, t);
59             if (time_lt(t, last)) {
60                 printf("WARNING: time ran backwards: %lu < %lu\n", t, last);
61                 last = t;
62                 continue;
63             }
64             last = t;
65             if (last - start > 5000)
66                 break;
67         }
68     }
69     {
70         printf("testing current_time_hires()\n");
71         lk_bigtime_t start = current_time_hires();
72         lk_bigtime_t last = start;
73         for (;;) {
74             t2 = current_time_hires();
75             //printf("%llu %llu\n", last, t2);
76             if (t2 < last) {
77                 printf("WARNING: time ran backwards: %llu < %llu\n", t2, last);
78                 last = t2;
79                 continue;
80             }
81             last = t2;
82             if (last - start > 5000000)
83                 break;
84         }
85     }
86 
87     printf("making sure current_time() and current_time_hires() are always the same base\n");
88     {
89         lk_time_t start = current_time();
90         for (;;) {
91             t = current_time();
92             t2 = current_time_hires();
93             if (t > ((t2 + 500) / 1000)) {
94                 printf("WARNING: current_time() ahead of current_time_hires() %lu %llu\n", t, t2);
95             }
96             if (t - start > 5000)
97                 break;
98         }
99     }
100 
101     printf("counting to 5, in one second intervals\n");
102     for (int i = 0; i < 5; i++) {
103         thread_sleep(1000);
104         printf("%d\n", i + 1);
105     }
106 
107     printf("measuring cpu clock against current_time_hires()\n");
108     for (int i = 0; i < 5; i++) {
109         uint cycles = arch_cycle_count();
110         lk_bigtime_t start = current_time_hires();
111         while ((current_time_hires() - start) < 1000000)
112             ;
113         cycles = arch_cycle_count() - cycles;
114         printf("%u cycles per second\n", cycles);
115     }
116 }
117