From fee4f32730fc71b1b2b7b237bf7538cdd768f5f1 Mon Sep 17 00:00:00 2001 From: R4ken Date: Tue, 12 Aug 2025 10:58:31 +0200 Subject: [PATCH 1/2] syscalls: add getThreadCpuTime function Add getThreadCpuTime function based on syscall with same name JIRA: RTOS-1088 --- include/sys/threads.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/sys/threads.h b/include/sys/threads.h index 04d47197..7bd66ebd 100644 --- a/include/sys/threads.h +++ b/include/sys/threads.h @@ -86,6 +86,9 @@ static inline int beginthread(void (*start)(void *), unsigned int priority, void extern int threadsinfo(int n, threadinfo_t *info); +extern time_t getThreadCpuTime(void); + + extern int priority(int priority); From ba96e09ce82d4a2c1bc49e41632c3ae4be9c91cf Mon Sep 17 00:00:00 2001 From: R4ken Date: Tue, 12 Aug 2025 11:02:00 +0200 Subject: [PATCH 2/2] posix: handle CLOCK_THREAD_CPUTIME_ID flag in clock_gettime Implement handling of missing flag in clock_gettime JIRA: RTOS-1088 --- include/time.h | 9 +++++---- time/time.c | 24 +++++++++++++++--------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/time.h b/include/time.h index e4ad6c57..be44e0d3 100644 --- a/include/time.h +++ b/include/time.h @@ -17,7 +17,7 @@ #define _LIBPHOENIX_TIME_H_ -#define SECS_TO_USECS_T(secs) (1000000ULL * (secs)) +#define SECS_TO_USECS_T(secs) (1000000ULL * (secs)) #define MSECS_TO_USECS_T(msecs) (1000ULL * (msecs)) #include @@ -26,9 +26,10 @@ #define CLOCKS_PER_SEC 1000000 -#define CLOCK_MONOTONIC 0 -#define CLOCK_MONOTONIC_RAW 1 -#define CLOCK_REALTIME 2 +#define CLOCK_MONOTONIC 0 +#define CLOCK_MONOTONIC_RAW 1 +#define CLOCK_REALTIME 2 +#define CLOCK_THREAD_CPUTIME_ID 3 #ifdef __cplusplus diff --git a/time/time.c b/time/time.c index bccce176..a3262cfd 100644 --- a/time/time.c +++ b/time/time.c @@ -21,6 +21,7 @@ #include #include #include +#include char *tzname[2]; @@ -91,26 +92,31 @@ time_t time(time_t *tp) int clock_gettime(clockid_t clk_id, struct timespec *tp) { - time_t now, offs; + time_t time, offs; if (tp == NULL) { errno = EINVAL; return -1; } - if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW) { + if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW && clk_id != CLOCK_THREAD_CPUTIME_ID) { errno = EINVAL; return -1; } - gettime(&now, &offs); - - if (clk_id == CLOCK_REALTIME) - now += offs; + if (clk_id == CLOCK_THREAD_CPUTIME_ID) { + time = (unsigned int)getThreadCpuTime(); + } + else { + gettime(&time, &offs); + if (clk_id == CLOCK_REALTIME) { + time += offs; + } + } - tp->tv_sec = now / (1000 * 1000); - now -= tp->tv_sec * 1000 * 1000; - tp->tv_nsec = now * 1000; + tp->tv_sec = time / (1000 * 1000); + time -= tp->tv_sec * 1000 * 1000; + tp->tv_nsec = time * 1000; return EOK; }