1 /*
2  * Copyright (c) 2024 Meta Platforms
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "kernel_shell.h"
8 
9 #include <zephyr/kernel.h>
10 
cmd_kernel_thread_pin(const struct shell * sh,size_t argc,char ** argv)11 static int cmd_kernel_thread_pin(const struct shell *sh,
12 				  size_t argc, char **argv)
13 {
14 	ARG_UNUSED(argc);
15 
16 	int cpu, err = 0;
17 	struct k_thread *thread;
18 
19 	thread = UINT_TO_POINTER(shell_strtoull(argv[1], 16, &err));
20 	if (err != 0) {
21 		shell_error(sh, "Unable to parse thread ID %s (err %d)", argv[1], err);
22 		return err;
23 	}
24 
25 	if (!z_thread_is_valid(thread)) {
26 		shell_error(sh, "Invalid thread id %p", (void *)thread);
27 		return -EINVAL;
28 	}
29 
30 	cpu = shell_strtoul(argv[2], 10, &err);
31 	if (err != 0) {
32 		shell_error(sh, "Unable to parse CPU ID %s (err %d)", argv[2], err);
33 		return err;
34 	}
35 
36 	shell_print(sh, "Pinning %p %s to CPU %d", (void *)thread, thread->name, cpu);
37 	err = k_thread_cpu_pin(thread, cpu);
38 	if (err != 0) {
39 		shell_error(sh, "Failed - %d", err);
40 	} else {
41 		shell_print(sh, "%p %s cpu_mask: 0x%x", (void *)thread, thread->name,
42 			    thread->base.cpu_mask);
43 	}
44 
45 	return err;
46 }
47 
48 KERNEL_THREAD_CMD_ARG_ADD(pin, NULL,
49 			  "Pin thread to a CPU.\n"
50 			  "Usage: kernel pin <thread ID> <CPU ID>",
51 			  cmd_kernel_thread_pin, 3, 0);
52