Re: [RFC PATCH] preempt: Add a generic function to return the preemption string.

From: Peter Zijlstra
Date: Thu Jan 09 2025 - 06:43:57 EST


On Fri, Dec 06, 2024 at 12:34:31PM +0100, Sebastian Andrzej Siewior wrote:
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index 95e40895a5190..8f5517dbe07d4 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -7642,6 +7642,30 @@ static inline void preempt_dynamic_init(void) { }
>
> #endif /* CONFIG_PREEMPT_DYNAMIC */
>
> +const char *preempt_model_str(void)
> +{
> + if (IS_ENABLED(CONFIG_ARCH_HAS_PREEMPT_LAZY) && preempt_model_lazy()) {
> + if (preempt_model_rt())
> + return "PREEMPT_RT+LAZY";
> + if (preempt_model_full())
> + return "PREEMPT+LAZY";
> + if (preempt_model_voluntary())
> + return "VOLUNTARY+LAZY";
> + if (preempt_model_none())
> + return "NONE+LAZY";
> + } else {
> + if (preempt_model_rt())
> + return "PREEMPT_RT";
> + if (preempt_model_full())
> + return "PREEMPT";
> + if (preempt_model_voluntary())
> + return "VOLUNTARY";
> + if (preempt_model_none())
> + return "NONE";
> + }
> + return "UNKNOWN-PREEMPT";
> +}

Hurmph.. a bunch of those combinations are nonsense :/

Also, we have a string thing in sched_dynamic_show().

Can't we do something like:

const char *preempt_model_str(void)
{
static char buf[128];
size_t off = 0, len = sizeof(buf), r;
bool brace = IS_ENABLED(CONFIG_PREEMPT_RT) &&
(IS_ENABLED(CONFIG_PREEMPT_DYNAMIC) ||
IS_ENABLED(CONFIG_PREEMPT_LAZY));

if (IS_ENABLED(CONFIG_PREEMPT_BUILD)) {
r = snprintf(buf + off, len, "PREEMPT");
off += r;
len -= r;

if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
r = snprintf(buf + off, len, "%sRT%s",
brace ? "_{" : "_",
brace ? "," : "");
off += r;
len -= r;
}

if (IS_ENABLED(CONFIG_PREEMPT_DYNAMIC)) {
snprintf(buf + off, len, "(%s)%s",
preempt_modes[preempt_dynamic_mode],
brace ? "}" : "");
retun buf;
}

if (IS_ENABLED(CONFIG_PREEMPT_LAZY)) {
snprintf(buf + off, len, "LAZY%s",
brace ? "}" : "");
retun buf;
}

return buf;
}

if (IS_ENABLED(CONFIG_VOLUNTARY_BUILD))
return "VOLUNTARY";

return "NONE";
}