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

kernel: Add k_thread_runtime_stats_is_enabled function #80450

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions include/zephyr/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6418,6 +6418,16 @@ int k_thread_runtime_stats_enable(k_tid_t thread);
*/
int k_thread_runtime_stats_disable(k_tid_t thread);

/**
* @brief Check if runtime statistics gathering is enabled for a thread
*
* This routine checks whether the specified thread has enabled runtime statistics.
*
* @param thread ID of thread
* @return true if usage statistics are enabled for the given thread, otherwise false
*/
bool k_thread_runtime_stats_is_enabled(k_tid_t thread);

/**
* @brief Enable gathering of system runtime statistics
*
Expand Down
5 changes: 5 additions & 0 deletions kernel/usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ int k_thread_runtime_stats_disable(k_tid_t thread)

return 0;
}

bool k_thread_runtime_stats_is_enabled(k_tid_t thread)
{
return thread->base.usage.track_usage;
}
#endif /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */

#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
Expand Down
2 changes: 2 additions & 0 deletions lib/posix/options/Kconfig.timer
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ if POSIX_TIMERS

config POSIX_THREAD_CPUTIME
bool "POSIX per-thread CPU-time clocks"
select THREAD_RUNTIME_STATS
select SCHED_THREAD_USAGE_ANALYSIS
help
This enables CLOCK_THREAD_CPUTIME_ID.

Expand Down
24 changes: 24 additions & 0 deletions lib/posix/options/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ int z_impl___posix_clock_get_base(clockid_t clock_id, struct timespec *base)
*base = rt_clock_base;
}
break;
#ifdef CONFIG_POSIX_THREAD_CPUTIME
case CLOCK_THREAD_CPUTIME_ID: {
uint64_t ns;
k_thread_runtime_stats_t stats;
k_tid_t thread = k_current_get();

if (!k_thread_runtime_stats_is_enabled(thread)) {
errno = EINVAL;
return -1;
}
k_thread_runtime_stats_get(thread, &stats);
ns = k_cyc_to_ns_floor64(stats.execution_cycles);
base->tv_sec = ns / NSEC_PER_SEC;
base->tv_nsec = ns % NSEC_PER_SEC;
break;
}
#endif /* CONFIG_POSIX_THREAD_CPUTIME */

default:
errno = EINVAL;
Expand Down Expand Up @@ -75,6 +92,13 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts)
(void)__posix_clock_get_base(clock_id, &base);
break;

#ifdef CONFIG_POSIX_THREAD_CPUTIME
case CLOCK_THREAD_CPUTIME_ID:
(void)__posix_clock_get_base(clock_id, &base);
*ts = base;
return 0;
#endif /* CONFIG_POSIX_THREAD_CPUTIME */

default:
errno = EINVAL;
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ ZTEST(usage_api, test_thread_stats_enable_disable)

k_thread_runtime_stats_get(_current, &stats1);
k_thread_runtime_stats_get(tid, &helper_stats1);
zassert_true(k_thread_runtime_stats_is_enabled(tid));
k_thread_runtime_stats_disable(tid);
zassert_false(k_thread_runtime_stats_is_enabled(tid));
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved
rruuaanng marked this conversation as resolved.
Show resolved Hide resolved

/*
* Busy wait for the remaining tick before re-enabling the thread
Expand Down
6 changes: 6 additions & 0 deletions tests/posix/timers/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ static const struct timespec ref_ts = {1514821501, NSEC_PER_SEC / 2U};
static const clockid_t clocks[] = {
CLOCK_MONOTONIC,
CLOCK_REALTIME,
#ifdef CONFIG_POSIX_THREAD_CPUTIME
CLOCK_THREAD_CPUTIME_ID,
#endif /* CONFIG_POSIX_THREAD_CPUTIME */
};

static const bool settable[] = {
false,
true,
#ifdef CONFIG_POSIX_THREAD_CPUTIME
false,
#endif /* CONFIG_POSIX_THREAD_CPUTIME */
};

static inline int64_t ts_to_ns(const struct timespec *ts)
Expand Down
Loading