Skip to content

Commit e0356c9

Browse files
committed
clock: refactor clock_gettime clock_settime
Signed-off-by: ligd <[email protected]>
1 parent 2b25141 commit e0356c9

File tree

3 files changed

+163
-193
lines changed

3 files changed

+163
-193
lines changed

include/nuttx/clock.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,29 @@ void perf_convert(clock_t elapsed, FAR struct timespec *ts);
732732

733733
unsigned long perf_getfreq(void);
734734

735+
/****************************************************************************
736+
* Name: nxclock_settime
737+
*
738+
* Description:
739+
* Clock Functions based on POSIX APIs
740+
*
741+
* CLOCK_REALTIME - POSIX demands this to be present. This is the wall
742+
* time clock.
743+
*
744+
****************************************************************************/
745+
746+
void nxclock_settime(clockid_t clock_id, FAR const struct timespec *tp);
747+
748+
/****************************************************************************
749+
* Name: nxclock_gettime
750+
*
751+
* Description:
752+
* Get the current value of the specified time clock.
753+
*
754+
****************************************************************************/
755+
756+
void nxclock_gettime(clockid_t clock_id, struct timespec *tp);
757+
735758
#undef EXTERN
736759
#ifdef __cplusplus
737760
}

sched/clock/clock_gettime.c

Lines changed: 92 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -34,180 +34,149 @@
3434
#include <nuttx/arch.h>
3535
#include <nuttx/sched.h>
3636
#include <nuttx/spinlock.h>
37-
#include <nuttx/queue.h>
3837

3938
#include "clock/clock.h"
4039
#include "sched/sched.h"
4140
#ifdef CONFIG_CLOCK_TIMEKEEPING
4241
# include "clock/clock_timekeeping.h"
4342
#endif
4443

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+
clock_t runtime = 0;
55+
irqstate_t flags;
56+
57+
group = tcb->group;
58+
59+
flags = spin_lock_irqsave(NULL);
60+
sq_for_every(&group->tg_members, curr)
61+
{
62+
tcb = container_of(curr, struct tcb_s, member);
63+
64+
runtime += tcb->run_time;
65+
}
66+
67+
spin_unlock_irqrestore(NULL, flags);
68+
return runtime;
69+
# else /* HAVE_GROUP_MEMBERS */
70+
return tcb->run_time;
71+
# endif /* HAVE_GROUP_MEMBERS */
72+
}
73+
#endif
74+
4575
/****************************************************************************
4676
* Public Functions
4777
****************************************************************************/
4878

4979
/****************************************************************************
50-
* Name: clock_gettime
80+
* Name: nxclock_gettime
5181
*
5282
* Description:
53-
* Clock Functions based on POSIX APIs
83+
* Get the current value of the specified time clock.
5484
*
5585
****************************************************************************/
5686

57-
int clock_gettime(clockid_t clock_id, struct timespec *tp)
87+
void nxclock_gettime(clockid_t clock_id, struct timespec *tp)
5888
{
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)
89+
if (clock_id == CLOCK_MONOTONIC || clock_id == CLOCK_BOOTTIME)
8490
{
8591
/* The the time elapsed since the timer was initialized at power on
8692
* reset.
8793
*/
8894

89-
ret = clock_systime_timespec(tp);
95+
clock_systime_timespec(tp);
9096
}
97+
else if (clock_id == CLOCK_REALTIME)
98+
{
99+
#ifndef CONFIG_CLOCK_TIMEKEEPING
100+
struct timespec ts;
101+
irqstate_t flags;
91102

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-
*/
103+
clock_systime_timespec(&ts);
97104

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.
105+
/* Add the base time to this. The base time is the time-of-day
106+
* setting. When added to the elapsed time since the time-of-day
107+
* was last set, this gives us the current time.
104108
*/
105109

106-
#if defined(CONFIG_CLOCK_TIMEKEEPING)
107-
ret = clock_timekeeping_get_wall_time(tp);
110+
flags = spin_lock_irqsave(NULL);
111+
clock_timespec_add(&g_basetime, &ts, tp);
112+
spin_unlock_irqrestore(NULL, flags);
108113
#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-
}
114+
clock_timekeeping_get_wall_time(tp);
115+
#endif
149116
}
150-
else if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
117+
else
151118
{
152-
unsigned long runtime;
119+
#ifdef CONFIG_SCHED_CRITMONITOR
120+
clockid_t clock_type = clock_id & CLOCK_MASK;
121+
pid_t pid = clock_id >> CLOCK_SHIFT;
153122
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
160123

161124
if (pid == 0)
162125
{
163-
/* Fetch the PROCESS_CPUTIME for current process */
164-
165126
tcb = this_task();
166127
}
167128
else
168129
{
169130
tcb = nxsched_get_tcb(pid);
170131
}
171132

172-
if (tcb != NULL)
133+
if (tcb)
173134
{
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)
135+
if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
180136
{
181-
tcb = container_of(curr, struct tcb_s, member);
182-
183-
runtime += tcb->run_time;
137+
up_perf_convert(clock_process_runtime(tcb), tp);
138+
}
139+
else if (clock_type == CLOCK_THREAD_CPUTIME_ID)
140+
{
141+
up_perf_convert(tcb->run_time, tp);
184142
}
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;
196143
}
197-
}
198144
#endif
199-
else
200-
{
201-
ret = -EINVAL;
202145
}
146+
}
203147

204-
/* Check for errors and set the errno value if necessary */
148+
/****************************************************************************
149+
* Name: clock_gettime
150+
*
151+
* Description:
152+
* Clock Functions based on POSIX APIs
153+
*
154+
* CLOCK_MONOTONIC is an optional under POSIX: "If the Monotonic Clock
155+
* option is supported, all implementations shall support a clock_id
156+
* of CLOCK_MONOTONIC defined in <time.h>. This clock represents the
157+
* monotonic clock for the system. For this clock, the value returned
158+
* by clock_gettime() represents the amount of time (in seconds and
159+
* nanoseconds) since an unspecified point in the past (for example,
160+
* system start-up time, or the Epoch). This point does not change
161+
* after system start-up time. The value of the CLOCK_MONOTONIC clock
162+
* cannot be set via clock_settime(). This function shall fail if it
163+
* is invoked with a clock_id argument of CLOCK_MONOTONIC."
164+
*
165+
* CLOCK_REALTIME - POSIX demands this to be present. CLOCK_REALTIME
166+
* represents the machine's best-guess as to the current wall-clock,
167+
* time-of-day time. This means that CLOCK_REALTIME can jump forward and
168+
* backward as the system time-of-day clock is changed.
169+
*
170+
****************************************************************************/
205171

206-
if (ret < 0)
172+
int clock_gettime(clockid_t clock_id, struct timespec *tp)
173+
{
174+
if (tp == NULL || clock_id < 0 || clock_id > CLOCK_BOOTTIME)
207175
{
208-
set_errno(-ret);
209-
ret = ERROR;
176+
set_errno(EINVAL);
177+
return ERROR;
210178
}
211179

212-
return ret;
180+
nxclock_gettime(clock_id, tp);
181+
return OK;
213182
}

0 commit comments

Comments
 (0)