Skip to content

Commit cb68626

Browse files
committed
clock: refactor clock_gettime clock_settime
Signed-off-by: ligd <liguiding1@xiaomi.com>
1 parent 38f921f commit cb68626

File tree

3 files changed

+164
-193
lines changed

3 files changed

+164
-193
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: 93 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -34,180 +34,150 @@
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+
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+
4576
/****************************************************************************
4677
* Public Functions
4778
****************************************************************************/
4879

4980
/****************************************************************************
50-
* Name: clock_gettime
81+
* Name: nxclock_gettime
5182
*
5283
* Description:
53-
* Clock Functions based on POSIX APIs
84+
* Get the current value of the specified time clock.
5485
*
5586
****************************************************************************/
5687

57-
int clock_gettime(clockid_t clock_id, struct timespec *tp)
88+
void nxclock_gettime(clockid_t clock_id, struct timespec *tp)
5889
{
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)
8491
{
8592
/* The the time elapsed since the timer was initialized at power on
8693
* reset.
8794
*/
8895

89-
ret = clock_systime_timespec(tp);
96+
clock_systime_timespec(tp);
9097
}
98+
else if (clock_id == CLOCK_REALTIME)
99+
{
100+
#ifndef CONFIG_CLOCK_TIMEKEEPING
101+
struct timespec ts;
102+
irqstate_t flags;
91103

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);
97105

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.
104109
*/
105110

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);
108114
#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
149117
}
150-
else if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
118+
else
151119
{
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;
153123
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
160124

161125
if (pid == 0)
162126
{
163-
/* Fetch the PROCESS_CPUTIME for current process */
164-
165127
tcb = this_task();
166128
}
167129
else
168130
{
169131
tcb = nxsched_get_tcb(pid);
170132
}
171133

172-
if (tcb != NULL)
134+
if (tcb)
173135
{
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)
180137
{
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);
184143
}
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;
196144
}
197-
}
198145
#endif
199-
else
200-
{
201-
ret = -EINVAL;
202146
}
147+
}
203148

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+
****************************************************************************/
205172

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)
207176
{
208-
set_errno(-ret);
209-
ret = ERROR;
177+
set_errno(EINVAL);
178+
return ERROR;
210179
}
211180

212-
return ret;
181+
nxclock_gettime(clock_id, tp);
182+
return OK;
213183
}

0 commit comments

Comments
 (0)