Skip to content

Commit 195d181

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

File tree

3 files changed

+165
-195
lines changed

3 files changed

+165
-195
lines changed

include/nuttx/clock.h

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

692692
unsigned long perf_getfreq(void);
693693

694+
/****************************************************************************
695+
* Name: nxclock_settime
696+
*
697+
* Description:
698+
* Clock Functions based on POSIX APIs
699+
*
700+
* CLOCK_REALTIME - POSIX demands this to be present. This is the wall
701+
* time clock.
702+
*
703+
****************************************************************************/
704+
705+
void nxclock_settime(clockid_t clock_id, FAR const struct timespec *tp);
706+
707+
/****************************************************************************
708+
* Name: nxclock_gettime
709+
*
710+
* Description:
711+
* Get the current value of the specified time clock.
712+
*
713+
****************************************************************************/
714+
715+
void nxclock_gettime(clockid_t clock_id, struct timespec *tp);
716+
694717
#undef EXTERN
695718
#ifdef __cplusplus
696719
}

sched/clock/clock_gettime.c

Lines changed: 94 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -30,183 +30,152 @@
3030
#include <errno.h>
3131
#include <debug.h>
3232

33-
#include <nuttx/nuttx.h>
3433
#include <nuttx/arch.h>
3534
#include <nuttx/sched.h>
3635
#include <nuttx/spinlock.h>
37-
#include <nuttx/queue.h>
3836

3937
#include "clock/clock.h"
38+
#include "sched/sched.h"
4039
#ifdef CONFIG_CLOCK_TIMEKEEPING
4140
# include "clock/clock_timekeeping.h"
4241
#endif
4342

43+
/****************************************************************************
44+
* Private Functions
45+
****************************************************************************/
46+
47+
#ifdef CONFIG_SCHED_CRITMONITOR
48+
static clock_t clock_process_runtime(FAR struct tcb_s *tcb)
49+
{
50+
# ifdef HAVE_GROUP_MEMBERS
51+
FAR struct task_group_s *group;
52+
clock_t runtime = 0;
53+
irqstate_t flags;
54+
int i;
55+
56+
group = tcb->group;
57+
58+
flags = spin_lock_irqsave(NULL);
59+
sq_for_every_safe(&group->tg_members, curr, next)
60+
{
61+
tcb = container_of(curr, struct tcb_s, member);
62+
63+
runtime += tcb->run_time;
64+
}
65+
66+
spin_unlock_irqrestore(NULL, flags);
67+
return runtime;
68+
# else /* HAVE_GROUP_MEMBERS */
69+
return tcb->run_time;
70+
# endif /* HAVE_GROUP_MEMBERS */
71+
}
72+
#endif
73+
4474
/****************************************************************************
4575
* Public Functions
4676
****************************************************************************/
4777

4878
/****************************************************************************
49-
* Name: clock_gettime
79+
* Name: nxclock_gettime
5080
*
5181
* Description:
52-
* Clock Functions based on POSIX APIs
82+
* Get the current value of the specified time clock.
5383
*
5484
****************************************************************************/
5585

56-
int clock_gettime(clockid_t clock_id, struct timespec *tp)
86+
void nxclock_gettime(clockid_t clock_id, struct timespec *tp)
5787
{
58-
#ifndef CONFIG_CLOCK_TIMEKEEPING
59-
struct timespec ts;
60-
#endif
61-
int ret = OK;
62-
63-
clockid_t clock_type = clock_id & CLOCK_MASK;
64-
#ifdef CONFIG_SCHED_CRITMONITOR
65-
pid_t pid = clock_id >> CLOCK_SHIFT;
66-
#endif
67-
68-
DEBUGASSERT(tp != NULL);
69-
70-
/* CLOCK_MONOTONIC is an optional under POSIX: "If the Monotonic Clock
71-
* option is supported, all implementations shall support a clock_id
72-
* of CLOCK_MONOTONIC defined in <time.h>. This clock represents the
73-
* monotonic clock for the system. For this clock, the value returned
74-
* by clock_gettime() represents the amount of time (in seconds and
75-
* nanoseconds) since an unspecified point in the past (for example,
76-
* system start-up time, or the Epoch). This point does not change
77-
* after system start-up time. The value of the CLOCK_MONOTONIC clock
78-
* cannot be set via clock_settime(). This function shall fail if it
79-
* is invoked with a clock_id argument of CLOCK_MONOTONIC."
80-
*/
81-
82-
if (clock_type == CLOCK_MONOTONIC || clock_type == CLOCK_BOOTTIME)
88+
if (clock_id == CLOCK_MONOTONIC || clock_id == CLOCK_BOOTTIME)
8389
{
8490
/* The the time elapsed since the timer was initialized at power on
8591
* reset.
8692
*/
8793

88-
ret = clock_systime_timespec(tp);
94+
clock_systime_timespec(tp);
8995
}
96+
else if (clock_id == CLOCK_REALTIME)
97+
{
98+
#ifndef CONFIG_CLOCK_TIMEKEEPING
99+
struct timespec ts;
100+
irqstate_t flags;
90101

91-
/* CLOCK_REALTIME - POSIX demands this to be present. CLOCK_REALTIME
92-
* represents the machine's best-guess as to the current wall-clock,
93-
* time-of-day time. This means that CLOCK_REALTIME can jump forward and
94-
* backward as the system time-of-day clock is changed.
95-
*/
102+
clock_systime_timespec(&ts);
96103

97-
else if (clock_type == CLOCK_REALTIME)
98-
{
99-
/* Get the elapsed time since the time-of-day was last set.
100-
* clock_systime_timespec() provides the time since power was applied;
101-
* the bias value corresponds to the time when the time-of-day was
102-
* last set.
104+
/* Add the base time to this. The base time is the time-of-day
105+
* setting. When added to the elapsed time since the time-of-day
106+
* was last set, this gives us the current time.
103107
*/
104108

105-
#if defined(CONFIG_CLOCK_TIMEKEEPING)
106-
ret = clock_timekeeping_get_wall_time(tp);
109+
flags = spin_lock_irqsave(NULL);
110+
clock_timespec_add(&g_basetime, &ts, tp);
111+
spin_unlock_irqrestore(NULL, flags);
107112
#else
108-
ret = clock_systime_timespec(&ts);
109-
if (ret == OK)
110-
{
111-
irqstate_t flags;
112-
113-
/* Add the base time to this. The base time is the time-of-day
114-
* setting. When added to the elapsed time since the time-of-day
115-
* was last set, this gives us the current time.
116-
*/
117-
118-
flags = spin_lock_irqsave(NULL);
119-
clock_timespec_add(&g_basetime, &ts, tp);
120-
spin_unlock_irqrestore(NULL, flags);
121-
}
122-
#endif /* CONFIG_CLOCK_TIMEKEEPING */
123-
}
124-
#ifdef CONFIG_SCHED_CRITMONITOR
125-
else if (clock_type == CLOCK_THREAD_CPUTIME_ID)
126-
{
127-
FAR struct tcb_s *tcb;
128-
129-
if (pid == 0)
130-
{
131-
/* Fetch the THREAD_CPUTIME for current thread */
132-
133-
tcb = nxsched_self();
134-
}
135-
else
136-
{
137-
tcb = nxsched_get_tcb(pid);
138-
}
139-
140-
if (tcb != NULL)
141-
{
142-
perf_convert(tcb->run_time, tp);
143-
}
144-
else
145-
{
146-
ret = -EFAULT;
147-
}
113+
clock_timekeeping_get_wall_time(tp);
114+
#endif
148115
}
149-
else if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
116+
else
150117
{
151-
unsigned long runtime;
118+
#ifdef CONFIG_SCHED_CRITMONITOR
119+
clockid_t clock_type = clock_id & CLOCK_MASK;
120+
pid_t pid = clock_id >> CLOCK_SHIFT;
152121
FAR struct tcb_s *tcb;
153-
# ifdef HAVE_GROUP_MEMBERS
154-
FAR struct task_group_s *group;
155-
FAR sq_entry_t *curr;
156-
FAR sq_entry_t *next;
157-
irqstate_t flags;
158-
# endif
159122

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

171-
if (tcb != NULL)
132+
if (tcb)
172133
{
173-
# ifdef HAVE_GROUP_MEMBERS
174-
group = tcb->group;
175-
runtime = 0;
176-
177-
flags = spin_lock_irqsave(NULL);
178-
sq_for_every_safe(&group->tg_members, curr, next)
134+
if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
179135
{
180-
tcb = container_of(curr, struct tcb_s, member);
181-
182-
runtime += tcb->run_time;
136+
up_perf_convert(clock_process_runtime(tcb), tp);
137+
}
138+
else if (clock_type == CLOCK_THREAD_CPUTIME_ID)
139+
{
140+
up_perf_convert(tcb->run_time, tp);
183141
}
184-
185-
spin_unlock_irqrestore(NULL, flags);
186-
# else /* HAVE_GROUP_MEMBERS */
187-
runtime = tcb->run_time;
188-
# endif /* HAVE_GROUP_MEMBERS */
189-
190-
perf_convert(runtime, tp);
191-
}
192-
else
193-
{
194-
ret = -EFAULT;
195142
}
196-
}
197143
#endif
198-
else
199-
{
200-
ret = -EINVAL;
201144
}
145+
}
202146

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

205-
if (ret < 0)
171+
int clock_gettime(clockid_t clock_id, struct timespec *tp)
172+
{
173+
if (tp == NULL || clock_id < 0 || clock_id > CLOCK_BOOTTIME)
206174
{
207-
set_errno(-ret);
208-
ret = ERROR;
175+
set_errno(EINVAL);
176+
return ERROR;
209177
}
210178

211-
return ret;
179+
nxclock_gettime(clock_id, tp);
180+
return OK;
212181
}

0 commit comments

Comments
 (0)