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