Skip to content

Commit e83480a

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

File tree

3 files changed

+163
-194
lines changed

3 files changed

+163
-194
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: 92 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -30,184 +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"
4038
#include "sched/sched.h"
4139
#ifdef CONFIG_CLOCK_TIMEKEEPING
4240
# include "clock/clock_timekeeping.h"
4341
#endif
4442

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+
4574
/****************************************************************************
4675
* Public Functions
4776
****************************************************************************/
4877

4978
/****************************************************************************
50-
* Name: clock_gettime
79+
* Name: nxclock_gettime
5180
*
5281
* Description:
53-
* Clock Functions based on POSIX APIs
82+
* Get the current value of the specified time clock.
5483
*
5584
****************************************************************************/
5685

57-
int clock_gettime(clockid_t clock_id, struct timespec *tp)
86+
void nxclock_gettime(clockid_t clock_id, struct timespec *tp)
5887
{
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)
88+
if (clock_id == CLOCK_MONOTONIC || clock_id == CLOCK_BOOTTIME)
8489
{
8590
/* The the time elapsed since the timer was initialized at power on
8691
* reset.
8792
*/
8893

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

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

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.
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.
104107
*/
105108

106-
#if defined(CONFIG_CLOCK_TIMEKEEPING)
107-
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);
108112
#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-
}
113+
clock_timekeeping_get_wall_time(tp);
114+
#endif
149115
}
150-
else if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
116+
else
151117
{
152-
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;
153121
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
160122

161123
if (pid == 0)
162124
{
163-
/* Fetch the PROCESS_CPUTIME for current process */
164-
165125
tcb = this_task();
166126
}
167127
else
168128
{
169129
tcb = nxsched_get_tcb(pid);
170130
}
171131

172-
if (tcb != NULL)
132+
if (tcb)
173133
{
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)
134+
if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
180135
{
181-
tcb = container_of(curr, struct tcb_s, member);
182-
183-
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);
184141
}
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;
196142
}
197-
}
198143
#endif
199-
else
200-
{
201-
ret = -EINVAL;
202144
}
145+
}
203146

204-
/* 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+
****************************************************************************/
205170

206-
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)
207174
{
208-
set_errno(-ret);
209-
ret = ERROR;
175+
set_errno(EINVAL);
176+
return ERROR;
210177
}
211178

212-
return ret;
179+
nxclock_gettime(clock_id, tp);
180+
return OK;
213181
}

0 commit comments

Comments
 (0)