Skip to content

Commit 07a4233

Browse files
GUIDINGLIxiaoxiang781216
authored andcommitted
clock: refactor clock_gettime clock_settime
Signed-off-by: ligd <liguiding1@xiaomi.com>
1 parent 6a2c037 commit 07a4233

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, FAR 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
@@ -36,180 +36,149 @@
3636
#include <nuttx/arch.h>
3737
#include <nuttx/sched.h>
3838
#include <nuttx/spinlock.h>
39-
#include <nuttx/queue.h>
4039

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

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+
4777
/****************************************************************************
4878
* Public Functions
4979
****************************************************************************/
5080

5181
/****************************************************************************
52-
* Name: clock_gettime
82+
* Name: nxclock_gettime
5383
*
5484
* Description:
55-
* Clock Functions based on POSIX APIs
85+
* Get the current value of the specified time clock.
5686
*
5787
****************************************************************************/
5888

59-
int clock_gettime(clockid_t clock_id, struct timespec *tp)
89+
void nxclock_gettime(clockid_t clock_id, FAR struct timespec *tp)
6090
{
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)
8692
{
8793
/* The the time elapsed since the timer was initialized at power on
8894
* reset.
8995
*/
9096

91-
ret = clock_systime_timespec(tp);
97+
clock_systime_timespec(tp);
9298
}
99+
else if (clock_id == CLOCK_REALTIME)
100+
{
101+
#ifndef CONFIG_CLOCK_TIMEKEEPING
102+
struct timespec ts;
103+
irqstate_t flags;
93104

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

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.
106110
*/
107111

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);
110115
#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
151118
}
152-
else if (clock_type == CLOCK_PROCESS_CPUTIME_ID)
119+
else
153120
{
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;
155124
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
162125

163126
if (pid == 0)
164127
{
165-
/* Fetch the PROCESS_CPUTIME for current process */
166-
167128
tcb = this_task();
168129
}
169130
else
170131
{
171132
tcb = nxsched_get_tcb(pid);
172133
}
173134

174-
if (tcb != NULL)
135+
if (tcb)
175136
{
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)
182138
{
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);
186144
}
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;
198145
}
199-
}
200146
#endif
201-
else
202-
{
203-
ret = -EINVAL;
204147
}
148+
}
205149

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

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)
209177
{
210-
set_errno(-ret);
211-
ret = ERROR;
178+
set_errno(EINVAL);
179+
return ERROR;
212180
}
213181

214-
return ret;
182+
nxclock_gettime(clock_id, tp);
183+
return OK;
215184
}

0 commit comments

Comments
 (0)