|
34 | 34 | #include <nuttx/arch.h>
|
35 | 35 | #include <nuttx/sched.h>
|
36 | 36 | #include <nuttx/spinlock.h>
|
37 |
| -#include <nuttx/queue.h> |
38 | 37 |
|
39 | 38 | #include "clock/clock.h"
|
40 | 39 | #include "sched/sched.h"
|
41 | 40 | #ifdef CONFIG_CLOCK_TIMEKEEPING
|
42 | 41 | # include "clock/clock_timekeeping.h"
|
43 | 42 | #endif
|
44 | 43 |
|
| 44 | +/**************************************************************************** |
| 45 | + * Private Functions |
| 46 | + ****************************************************************************/ |
| 47 | + |
| 48 | +#ifdef CONFIG_SCHED_CRITMONITOR |
| 49 | +static clock_t clock_process_runtime(FAR struct tcb_s *tcb) |
| 50 | +{ |
| 51 | +# ifdef HAVE_GROUP_MEMBERS |
| 52 | + FAR struct task_group_s *group; |
| 53 | + FAR sq_entry_t *curr; |
| 54 | + FAR sq_entry_t *next; |
| 55 | + clock_t runtime = 0; |
| 56 | + irqstate_t flags; |
| 57 | + |
| 58 | + group = tcb->group; |
| 59 | + |
| 60 | + flags = spin_lock_irqsave(NULL); |
| 61 | + sq_for_every_safe(&group->tg_members, curr, next) |
| 62 | + { |
| 63 | + tcb = container_of(curr, struct tcb_s, member); |
| 64 | + |
| 65 | + runtime += tcb->run_time; |
| 66 | + } |
| 67 | + |
| 68 | + spin_unlock_irqrestore(NULL, flags); |
| 69 | + return runtime; |
| 70 | +# else /* HAVE_GROUP_MEMBERS */ |
| 71 | + return tcb->run_time; |
| 72 | +# endif /* HAVE_GROUP_MEMBERS */ |
| 73 | +} |
| 74 | +#endif |
| 75 | + |
45 | 76 | /****************************************************************************
|
46 | 77 | * Public Functions
|
47 | 78 | ****************************************************************************/
|
48 | 79 |
|
49 | 80 | /****************************************************************************
|
50 |
| - * Name: clock_gettime |
| 81 | + * Name: nxclock_gettime |
51 | 82 | *
|
52 | 83 | * Description:
|
53 |
| - * Clock Functions based on POSIX APIs |
| 84 | + * Get the current value of the specified time clock. |
54 | 85 | *
|
55 | 86 | ****************************************************************************/
|
56 | 87 |
|
57 |
| -int clock_gettime(clockid_t clock_id, struct timespec *tp) |
| 88 | +void nxclock_gettime(clockid_t clock_id, struct timespec *tp) |
58 | 89 | {
|
59 |
| -#ifndef CONFIG_CLOCK_TIMEKEEPING |
60 |
| - struct timespec ts; |
61 |
| -#endif |
62 |
| - int ret = OK; |
63 |
| - |
64 |
| - clockid_t clock_type = clock_id & CLOCK_MASK; |
65 |
| -#ifdef CONFIG_SCHED_CRITMONITOR |
66 |
| - pid_t pid = clock_id >> CLOCK_SHIFT; |
67 |
| -#endif |
68 |
| - |
69 |
| - DEBUGASSERT(tp != NULL); |
70 |
| - |
71 |
| - /* CLOCK_MONOTONIC is an optional under POSIX: "If the Monotonic Clock |
72 |
| - * option is supported, all implementations shall support a clock_id |
73 |
| - * of CLOCK_MONOTONIC defined in <time.h>. This clock represents the |
74 |
| - * monotonic clock for the system. For this clock, the value returned |
75 |
| - * by clock_gettime() represents the amount of time (in seconds and |
76 |
| - * nanoseconds) since an unspecified point in the past (for example, |
77 |
| - * system start-up time, or the Epoch). This point does not change |
78 |
| - * after system start-up time. The value of the CLOCK_MONOTONIC clock |
79 |
| - * cannot be set via clock_settime(). This function shall fail if it |
80 |
| - * is invoked with a clock_id argument of CLOCK_MONOTONIC." |
81 |
| - */ |
82 |
| - |
83 |
| - if (clock_type == CLOCK_MONOTONIC || clock_type == CLOCK_BOOTTIME) |
| 90 | + if (clock_id == CLOCK_MONOTONIC || clock_id == CLOCK_BOOTTIME) |
84 | 91 | {
|
85 | 92 | /* The the time elapsed since the timer was initialized at power on
|
86 | 93 | * reset.
|
87 | 94 | */
|
88 | 95 |
|
89 |
| - ret = clock_systime_timespec(tp); |
| 96 | + clock_systime_timespec(tp); |
90 | 97 | }
|
| 98 | + else if (clock_id == CLOCK_REALTIME) |
| 99 | + { |
| 100 | +#ifndef CONFIG_CLOCK_TIMEKEEPING |
| 101 | + struct timespec ts; |
| 102 | + irqstate_t flags; |
91 | 103 |
|
92 |
| - /* CLOCK_REALTIME - POSIX demands this to be present. CLOCK_REALTIME |
93 |
| - * represents the machine's best-guess as to the current wall-clock, |
94 |
| - * time-of-day time. This means that CLOCK_REALTIME can jump forward and |
95 |
| - * backward as the system time-of-day clock is changed. |
96 |
| - */ |
| 104 | + clock_systime_timespec(&ts); |
97 | 105 |
|
98 |
| - else if (clock_type == CLOCK_REALTIME) |
99 |
| - { |
100 |
| - /* Get the elapsed time since the time-of-day was last set. |
101 |
| - * clock_systime_timespec() provides the time since power was applied; |
102 |
| - * the bias value corresponds to the time when the time-of-day was |
103 |
| - * last set. |
| 106 | + /* Add the base time to this. The base time is the time-of-day |
| 107 | + * setting. When added to the elapsed time since the time-of-day |
| 108 | + * was last set, this gives us the current time. |
104 | 109 | */
|
105 | 110 |
|
106 |
| -#if defined(CONFIG_CLOCK_TIMEKEEPING) |
107 |
| - ret = clock_timekeeping_get_wall_time(tp); |
| 111 | + flags = spin_lock_irqsave(NULL); |
| 112 | + clock_timespec_add(&g_basetime, &ts, tp); |
| 113 | + spin_unlock_irqrestore(NULL, flags); |
108 | 114 | #else
|
109 |
| - ret = clock_systime_timespec(&ts); |
110 |
| - if (ret == OK) |
111 |
| - { |
112 |
| - irqstate_t flags; |
113 |
| - |
114 |
| - /* Add the base time to this. The base time is the time-of-day |
115 |
| - * setting. When added to the elapsed time since the time-of-day |
116 |
| - * was last set, this gives us the current time. |
117 |
| - */ |
118 |
| - |
119 |
| - flags = spin_lock_irqsave(NULL); |
120 |
| - clock_timespec_add(&g_basetime, &ts, tp); |
121 |
| - spin_unlock_irqrestore(NULL, flags); |
122 |
| - } |
123 |
| -#endif /* CONFIG_CLOCK_TIMEKEEPING */ |
124 |
| - } |
125 |
| -#ifdef CONFIG_SCHED_CRITMONITOR |
126 |
| - else if (clock_type == CLOCK_THREAD_CPUTIME_ID) |
127 |
| - { |
128 |
| - FAR struct tcb_s *tcb; |
129 |
| - |
130 |
| - if (pid == 0) |
131 |
| - { |
132 |
| - /* Fetch the THREAD_CPUTIME for current thread */ |
133 |
| - |
134 |
| - tcb = this_task(); |
135 |
| - } |
136 |
| - else |
137 |
| - { |
138 |
| - tcb = nxsched_get_tcb(pid); |
139 |
| - } |
140 |
| - |
141 |
| - if (tcb != NULL) |
142 |
| - { |
143 |
| - perf_convert(tcb->run_time, tp); |
144 |
| - } |
145 |
| - else |
146 |
| - { |
147 |
| - ret = -EFAULT; |
148 |
| - } |
| 115 | + clock_timekeeping_get_wall_time(tp); |
| 116 | +#endif |
149 | 117 | }
|
150 |
| - else if (clock_type == CLOCK_PROCESS_CPUTIME_ID) |
| 118 | + else |
151 | 119 | {
|
152 |
| - unsigned long runtime; |
| 120 | +#ifdef CONFIG_SCHED_CRITMONITOR |
| 121 | + clockid_t clock_type = clock_id & CLOCK_MASK; |
| 122 | + pid_t pid = clock_id >> CLOCK_SHIFT; |
153 | 123 | FAR struct tcb_s *tcb;
|
154 |
| -# ifdef HAVE_GROUP_MEMBERS |
155 |
| - FAR struct task_group_s *group; |
156 |
| - FAR sq_entry_t *curr; |
157 |
| - FAR sq_entry_t *next; |
158 |
| - irqstate_t flags; |
159 |
| -# endif |
160 | 124 |
|
161 | 125 | if (pid == 0)
|
162 | 126 | {
|
163 |
| - /* Fetch the PROCESS_CPUTIME for current process */ |
164 |
| - |
165 | 127 | tcb = this_task();
|
166 | 128 | }
|
167 | 129 | else
|
168 | 130 | {
|
169 | 131 | tcb = nxsched_get_tcb(pid);
|
170 | 132 | }
|
171 | 133 |
|
172 |
| - if (tcb != NULL) |
| 134 | + if (tcb) |
173 | 135 | {
|
174 |
| -# ifdef HAVE_GROUP_MEMBERS |
175 |
| - group = tcb->group; |
176 |
| - runtime = 0; |
177 |
| - |
178 |
| - flags = spin_lock_irqsave(NULL); |
179 |
| - sq_for_every_safe(&group->tg_members, curr, next) |
| 136 | + if (clock_type == CLOCK_PROCESS_CPUTIME_ID) |
180 | 137 | {
|
181 |
| - tcb = container_of(curr, struct tcb_s, member); |
182 |
| - |
183 |
| - runtime += tcb->run_time; |
| 138 | + up_perf_convert(clock_process_runtime(tcb), tp); |
| 139 | + } |
| 140 | + else if (clock_type == CLOCK_THREAD_CPUTIME_ID) |
| 141 | + { |
| 142 | + up_perf_convert(tcb->run_time, tp); |
184 | 143 | }
|
185 |
| - |
186 |
| - spin_unlock_irqrestore(NULL, flags); |
187 |
| -# else /* HAVE_GROUP_MEMBERS */ |
188 |
| - runtime = tcb->run_time; |
189 |
| -# endif /* HAVE_GROUP_MEMBERS */ |
190 |
| - |
191 |
| - perf_convert(runtime, tp); |
192 |
| - } |
193 |
| - else |
194 |
| - { |
195 |
| - ret = -EFAULT; |
196 | 144 | }
|
197 |
| - } |
198 | 145 | #endif
|
199 |
| - else |
200 |
| - { |
201 |
| - ret = -EINVAL; |
202 | 146 | }
|
| 147 | +} |
203 | 148 |
|
204 |
| - /* Check for errors and set the errno value if necessary */ |
| 149 | +/**************************************************************************** |
| 150 | + * Name: clock_gettime |
| 151 | + * |
| 152 | + * Description: |
| 153 | + * Clock Functions based on POSIX APIs |
| 154 | + * |
| 155 | + * CLOCK_MONOTONIC is an optional under POSIX: "If the Monotonic Clock |
| 156 | + * option is supported, all implementations shall support a clock_id |
| 157 | + * of CLOCK_MONOTONIC defined in <time.h>. This clock represents the |
| 158 | + * monotonic clock for the system. For this clock, the value returned |
| 159 | + * by clock_gettime() represents the amount of time (in seconds and |
| 160 | + * nanoseconds) since an unspecified point in the past (for example, |
| 161 | + * system start-up time, or the Epoch). This point does not change |
| 162 | + * after system start-up time. The value of the CLOCK_MONOTONIC clock |
| 163 | + * cannot be set via clock_settime(). This function shall fail if it |
| 164 | + * is invoked with a clock_id argument of CLOCK_MONOTONIC." |
| 165 | + * |
| 166 | + * CLOCK_REALTIME - POSIX demands this to be present. CLOCK_REALTIME |
| 167 | + * represents the machine's best-guess as to the current wall-clock, |
| 168 | + * time-of-day time. This means that CLOCK_REALTIME can jump forward and |
| 169 | + * backward as the system time-of-day clock is changed. |
| 170 | + * |
| 171 | + ****************************************************************************/ |
205 | 172 |
|
206 |
| - if (ret < 0) |
| 173 | +int clock_gettime(clockid_t clock_id, struct timespec *tp) |
| 174 | +{ |
| 175 | + if (tp == NULL || clock_id < 0 || clock_id > CLOCK_BOOTTIME) |
207 | 176 | {
|
208 |
| - set_errno(-ret); |
209 |
| - ret = ERROR; |
| 177 | + set_errno(EINVAL); |
| 178 | + return ERROR; |
210 | 179 | }
|
211 | 180 |
|
212 |
| - return ret; |
| 181 | + nxclock_gettime(clock_id, tp); |
| 182 | + return OK; |
213 | 183 | }
|
0 commit comments