Skip to content

Commit aca3b36

Browse files
ycsinnashif
authored andcommitted
shell: modules: kernel: add CPU mask affinity/pinning commands
Added commands to access the APIs in the kernel/cpu_mask.c Signed-off-by: Yong Cong Sin <[email protected]> Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 475fc91 commit aca3b36

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

subsys/shell/modules/kernel_service.c

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,193 @@ static int cmd_kernel_thread_unwind(const struct shell *sh, size_t argc, char **
349349

350350
#endif /* CONFIG_ARCH_STACKWALK */
351351

352+
#ifdef CONFIG_SCHED_CPU_MASK
353+
static int cmd_kernel_thread_mask_clear(const struct shell *sh, size_t argc, char **argv)
354+
{
355+
ARG_UNUSED(argc);
356+
357+
int rc, err = 0;
358+
struct k_thread *thread;
359+
360+
thread = UINT_TO_POINTER(shell_strtoull(argv[1], 16, &err));
361+
if (err != 0) {
362+
shell_error(sh, "Unable to parse thread ID %s (err %d)", argv[1], err);
363+
return err;
364+
}
365+
366+
if (!thread_is_valid(thread)) {
367+
shell_error(sh, "Invalid thread id %p", (void *)thread);
368+
return -EINVAL;
369+
}
370+
371+
rc = k_thread_cpu_mask_clear(thread);
372+
if (rc != 0) {
373+
shell_error(sh, "Failed - %d", rc);
374+
} else {
375+
shell_print(sh, "%p %s cpu_mask: 0x%x", (void *)thread, thread->name,
376+
thread->base.cpu_mask);
377+
}
378+
379+
return rc;
380+
}
381+
382+
static int cmd_kernel_thread_mask_enable_all(const struct shell *sh, size_t argc, char **argv)
383+
{
384+
ARG_UNUSED(argc);
385+
386+
int rc, err = 0;
387+
struct k_thread *thread;
388+
389+
thread = UINT_TO_POINTER(shell_strtoull(argv[1], 16, &err));
390+
if (err != 0) {
391+
shell_error(sh, "Unable to parse thread ID %s (err %d)", argv[1], err);
392+
return err;
393+
}
394+
395+
if (!thread_is_valid(thread)) {
396+
shell_error(sh, "Invalid thread id %p", (void *)thread);
397+
return -EINVAL;
398+
}
399+
400+
rc = k_thread_cpu_mask_enable_all(thread);
401+
if (rc != 0) {
402+
shell_error(sh, "Failed - %d", rc);
403+
} else {
404+
shell_print(sh, "%p %s cpu_mask: 0x%x", (void *)thread, thread->name,
405+
thread->base.cpu_mask);
406+
}
407+
408+
return rc;
409+
}
410+
411+
static int cmd_kernel_thread_mask_enable(const struct shell *sh, size_t argc, char **argv)
412+
{
413+
ARG_UNUSED(argc);
414+
415+
int rc, cpu, err = 0;
416+
struct k_thread *thread;
417+
418+
thread = UINT_TO_POINTER(shell_strtoull(argv[1], 16, &err));
419+
if (err != 0) {
420+
shell_error(sh, "Unable to parse thread ID %s (err %d)", argv[1], err);
421+
return err;
422+
}
423+
424+
if (!thread_is_valid(thread)) {
425+
shell_error(sh, "Invalid thread id %p", (void *)thread);
426+
return -EINVAL;
427+
}
428+
429+
cpu = (int)shell_strtol(argv[2], 10, &err);
430+
if (err != 0) {
431+
shell_error(sh, "Unable to parse CPU ID %s (err %d)", argv[2], err);
432+
return err;
433+
}
434+
435+
rc = k_thread_cpu_mask_enable(thread, cpu);
436+
if (rc != 0) {
437+
shell_error(sh, "Failed - %d", rc);
438+
} else {
439+
shell_print(sh, "%p %s cpu_mask: 0x%x", (void *)thread, thread->name,
440+
thread->base.cpu_mask);
441+
}
442+
443+
return rc;
444+
}
445+
446+
static int cmd_kernel_thread_mask_disable(const struct shell *sh, size_t argc, char **argv)
447+
{
448+
ARG_UNUSED(argc);
449+
450+
int rc, cpu, err = 0;
451+
struct k_thread *thread;
452+
453+
thread = UINT_TO_POINTER(shell_strtoull(argv[1], 16, &err));
454+
if (err != 0) {
455+
shell_error(sh, "Unable to parse thread ID %s (err %d)", argv[1], err);
456+
return err;
457+
}
458+
459+
if (!thread_is_valid(thread)) {
460+
shell_error(sh, "Invalid thread id %p", (void *)thread);
461+
return -EINVAL;
462+
}
463+
464+
cpu = (int)shell_strtol(argv[2], 10, &err);
465+
if (err != 0) {
466+
shell_error(sh, "Unable to parse CPU ID %s (err %d)", argv[2], err);
467+
return err;
468+
}
469+
470+
rc = k_thread_cpu_mask_disable(thread, cpu);
471+
if (rc != 0) {
472+
shell_error(sh, "Failed - %d", rc);
473+
} else {
474+
shell_print(sh, "%p %s cpu_mask: 0x%x", (void *)thread, thread->name,
475+
thread->base.cpu_mask);
476+
}
477+
478+
return rc;
479+
}
480+
481+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_kernel_thread_mask,
482+
SHELL_CMD_ARG(clear, NULL,
483+
"Sets all CPU enable masks to zero.\n"
484+
"Usage: kernel thread mask clear <thread ID>",
485+
cmd_kernel_thread_mask_clear, 2, 0),
486+
SHELL_CMD_ARG(enable_all, NULL,
487+
"Sets all CPU enable masks to one.\n"
488+
"Usage: kernel thread mask enable_all <thread ID>",
489+
cmd_kernel_thread_mask_enable_all, 2, 0),
490+
SHELL_CMD_ARG(enable, NULL,
491+
"Enable thread to run on specified CPU.\n"
492+
"Usage: kernel thread mask enable <thread ID> <CPU ID>",
493+
cmd_kernel_thread_mask_enable, 3, 0),
494+
SHELL_CMD_ARG(disable, NULL,
495+
"Prevent thread to run on specified CPU.\n"
496+
"Usage: kernel thread mask disable <thread ID> <CPU ID>",
497+
cmd_kernel_thread_mask_disable, 3, 0),
498+
SHELL_SUBCMD_SET_END /* Array terminated. */
499+
);
500+
501+
static int cmd_kernel_thread_pin(const struct shell *sh,
502+
size_t argc, char **argv)
503+
{
504+
ARG_UNUSED(argc);
505+
506+
int cpu, err = 0;
507+
struct k_thread *thread;
508+
509+
thread = UINT_TO_POINTER(shell_strtoull(argv[1], 16, &err));
510+
if (err != 0) {
511+
shell_error(sh, "Unable to parse thread ID %s (err %d)", argv[1], err);
512+
return err;
513+
}
514+
515+
if (!thread_is_valid(thread)) {
516+
shell_error(sh, "Invalid thread id %p", (void *)thread);
517+
return -EINVAL;
518+
}
519+
520+
cpu = shell_strtoul(argv[2], 10, &err);
521+
if (err != 0) {
522+
shell_error(sh, "Unable to parse CPU ID %s (err %d)", argv[2], err);
523+
return err;
524+
}
525+
526+
shell_print(sh, "Pinning %p %s to CPU %d", (void *)thread, thread->name, cpu);
527+
err = k_thread_cpu_pin(thread, cpu);
528+
if (err != 0) {
529+
shell_error(sh, "Failed - %d", err);
530+
} else {
531+
shell_print(sh, "%p %s cpu_mask: 0x%x", (void *)thread, thread->name,
532+
thread->base.cpu_mask);
533+
}
534+
535+
return err;
536+
}
537+
#endif /* CONFIG_SCHED_CPU_MASK */
538+
352539
SHELL_STATIC_SUBCMD_SET_CREATE(sub_kernel_thread,
353540
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO) && \
354541
defined(CONFIG_THREAD_MONITOR)
@@ -358,6 +545,14 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_kernel_thread,
358545
#if defined(CONFIG_ARCH_STACKWALK)
359546
SHELL_CMD_ARG(unwind, NULL, "Unwind a thread.", cmd_kernel_thread_unwind, 1, 1),
360547
#endif /* CONFIG_ARCH_STACKWALK */
548+
#if defined(CONFIG_SCHED_CPU_MASK)
549+
SHELL_CMD_ARG(mask, &sub_kernel_thread_mask, "Configure thread CPU mask affinity.", NULL, 2,
550+
0),
551+
SHELL_CMD_ARG(pin, NULL,
552+
"Pin thread to a CPU.\n"
553+
"Usage: kernel pin <thread ID> <CPU ID>",
554+
cmd_kernel_thread_pin, 3, 0),
555+
#endif /* CONFIG_SCHED_CPU_MASK */
361556
SHELL_SUBCMD_SET_END /* Array terminated. */
362557
);
363558

0 commit comments

Comments
 (0)