Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix: implement clock_getcpuclockid() #67039

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/zephyr/posix/posix_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
extern "C" {
#endif

typedef int pid_t;

#ifndef __useconds_t_defined
typedef unsigned long useconds_t;
#endif
Expand Down
5 changes: 5 additions & 0 deletions include/zephyr/posix/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ extern "C" {
#define CLOCK_REALTIME 1
#endif

#ifndef CLOCK_PROCESS_CPUTIME_ID
#define CLOCK_PROCESS_CPUTIME_ID 2
#endif

#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 4
#endif
Expand All @@ -84,6 +88,7 @@ static inline int32_t _ts_to_ms(const struct timespec *to)

int clock_gettime(clockid_t clock_id, struct timespec *ts);
int clock_settime(clockid_t clock_id, const struct timespec *ts);
int clock_getcpuclockid(pid_t pid, clockid_t *clock_id);
/* Timer APIs */
int timer_create(clockid_t clockId, struct sigevent *evp, timer_t *timerid);
int timer_delete(timer_t timerid);
Expand Down
1 change: 1 addition & 0 deletions include/zephyr/posix/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern char *optarg;
extern int opterr, optind, optopt;
#endif

pid_t getpid(void);
unsigned sleep(unsigned int seconds);
int usleep(useconds_t useconds);

Expand Down
13 changes: 13 additions & 0 deletions lib/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <errno.h>
#include <zephyr/posix/time.h>
#include <zephyr/posix/sys/time.h>
#include <zephyr/posix/unistd.h>
#include <zephyr/internal/syscall_handler.h>
#include <zephyr/spinlock.h>

Expand Down Expand Up @@ -222,3 +223,15 @@ int gettimeofday(struct timeval *tv, void *tz)

return res;
}

int clock_getcpuclockid(pid_t pid, clockid_t *clock_id)
{
/* We don't allow any process ID but our own. */
if (pid != 0 && pid != getpid()) {
return EPERM;
}

*clock_id = CLOCK_PROCESS_CPUTIME_ID;

return 0;
}
17 changes: 17 additions & 0 deletions lib/posix/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <zephyr/logging/log.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/posix/pthread.h>
#include <zephyr/posix/unistd.h>
#include <zephyr/sys/slist.h>
#include <zephyr/sys/util.h>

Expand Down Expand Up @@ -158,6 +159,22 @@ int pthread_equal(pthread_t pt1, pthread_t pt2)
return (pt1 == pt2);
}

pid_t getpid(void)
{
/*
* To maintain compatibility with some other POSIX operating systems,
* a PID of zero is used to indicate that the process exists in another namespace.
* PID zero is also used by the scheduler in some cases.
* PID one is usually reserved for the init process.
* Also note, that negative PIDs may be used by kill()
* to send signals to process groups in some implementations.
*
* At the moment, getpid just returns an arbitrary number >= 2
*/

return 42;
jaiiarora marked this conversation as resolved.
Show resolved Hide resolved
}

static inline void __z_pthread_cleanup_init(struct __pthread_cleanup *c, void (*routine)(void *arg),
void *arg)
{
Expand Down
12 changes: 12 additions & 0 deletions tests/posix/common/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,15 @@ ZTEST(posix_apis, test_realtime)
CONFIG_TEST_CLOCK_RT_ERROR_MS, lo_wm, cma, hi_wm);
zassert_between_inclusive(cma, lo, hi);
}

ZTEST(posix_apis, test_clock_getcpuclockid)
{
int ret = 0;
clockid_t clock_id;

ret = clock_getcpuclockid((pid_t)0, &clock_id);
zassert_equal(ret, 0, "POSIX clock_getcpuclock id failed");

ret = clock_getcpuclockid((pid_t)2482, &clock_id);
zassert_equal(ret, EPERM, "POSIX clock_getcpuclock id failed");
}
Loading