Skip to content

Commit 4e897e8

Browse files
committed
sched_getaffinity: multicore userland breakpoint actually worked this time
Move docs to README
1 parent d34a8a3 commit 4e897e8

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

README.adoc

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,16 +1919,85 @@ See also: https://github.com/cirosantilli/linux-kernel-module-cheat/issues/19
19191919

19201920
=== GDB step debug multicore
19211921

1922-
https://stackoverflow.com/questions/42800801/how-to-use-gdb-to-debug-qemu-with-smp-symmetric-multiple-processors
1922+
We can set and get which cores the Linux kernel allows a program to run on with `sched_getaffinity` and `sched_setaffinity`:
19231923

1924-
Modify the number of cores: <<number-of-cores>>
1924+
....
1925+
./run -c2 -F '/sched_getaffinity.out'
1926+
....
1927+
1928+
Source: link:kernel_module/user/sched_getaffinity.c[]
1929+
1930+
Sample output:
1931+
1932+
....
1933+
sched_getaffinity = 1 1
1934+
sched_getcpu = 1
1935+
sched_getaffinity = 1 0
1936+
sched_getcpu = 0
1937+
....
1938+
1939+
Which shows us that:
1940+
1941+
* initially:
1942+
** all 2 cores were enabled as shown by `sched_getaffinity = 1 1`
1943+
** the process was randomly assigned to run on core 1 (the second one) as shown by `sched_getcpu = 1`. If we run this several times, it will also run on core 0 sometimes.
1944+
* then we restrict the affinity to just core 0, and we see that the program was actually moved to core 0
1945+
1946+
The number of cores is modified as explained at: <<number-of-cores>>
1947+
1948+
`taskset` from the util-linux package sets the initial core affinity of a program:
1949+
1950+
....
1951+
taskset -c 1,1 /sched_getaffinity.out
1952+
....
1953+
1954+
output:
1955+
1956+
....
1957+
sched_getaffinity = 0 1
1958+
sched_getcpu = 1
1959+
sched_getaffinity = 1 0
1960+
sched_getcpu = 0
1961+
....
1962+
1963+
so we see that the affinity was restricted to the second core from the start.
19251964

1926-
TODO: how to do something cool to see that in action?
1965+
Let's do a QEMU observation to justify this example being in the repository with <<gdb-step-debug-userland-non-init,userland breakpoints>>:
19271966

1928-
I tried to play around with `taskset`, but when I have two CPUs the <<gdb-step-debug-userland-non-init,userland breakpoints>> don't work... Why?
1967+
....
1968+
./run -c2 -d -F 'i=0; while true; do taskset -c $i,$i /sched_getaffinity.out; i=$((! $i)); done'
1969+
....
1970+
1971+
on another shell:
1972+
1973+
....
1974+
./rungdb-user kernel_module-1.0/user/sched_getaffinity.out main
1975+
....
1976+
1977+
Then, inside GDB:
1978+
1979+
....
1980+
(gdb) info threads
1981+
Id Target Id Frame
1982+
* 1 Thread 1 (CPU#0 [running]) main () at sched_getaffinity.c:30
1983+
2 Thread 2 (CPU#1 [halted ]) native_safe_halt () at ./arch/x86/include/asm/irqflags.h:55
1984+
(gdb) c
1985+
(gdb) info threads
1986+
Id Target Id Frame
1987+
1 Thread 1 (CPU#0 [halted ]) native_safe_halt () at ./arch/x86/include/asm/irqflags.h:55
1988+
* 2 Thread 2 (CPU#1 [running]) main () at sched_getaffinity.c:30
1989+
(gdb) c
1990+
....
1991+
1992+
So we observe that `info threads` shows the actual correct core on which the process was restricted to run by `taskset`!
19291993

19301994
We should also try it out with kernel modules: https://stackoverflow.com/questions/28347876/set-cpu-affinity-on-a-loadable-linux-kernel-module
19311995

1996+
Bibliography:
1997+
1998+
* https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787
1999+
* https://stackoverflow.com/questions/42800801/how-to-use-gdb-to-debug-qemu-with-smp-symmetric-multiple-processors
2000+
19322001
== KGDB
19332002

19342003
TODO: only working with <<graphic-mode>>. Without it, nothing shows on the terminal. So likely something linked to the option `console=ttyS0`.

kernel_module/irq.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#irq-ko */
2+
13
#include <linux/fs.h>
24
#include <linux/interrupt.h>
35
#include <linux/kernel.h>

kernel_module/user/sched_getaffinity.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/*
2-
upstream; https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c/50117787#50117787
3-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#gdb-step-debug-multicore */
42

53
#define _GNU_SOURCE
64
#include <assert.h>

0 commit comments

Comments
 (0)