|
4 | 4 |
|
5 | 5 | load helpers
|
6 | 6 |
|
| 7 | +INITIAL_CPU_MASK="$(grep -F Cpus_allowed_list: /proc/self/status | awk '{ print $2 }')" |
| 8 | + |
7 | 9 | function setup() {
|
8 | 10 | requires smp cgroups_cpuset
|
9 | 11 | setup_busybox
|
| 12 | + |
| 13 | + echo "Initial CPU mask: $INITIAL_CPU_MASK" >&2 |
| 14 | + echo "---" >&2 |
10 | 15 | }
|
11 | 16 |
|
12 | 17 | function teardown() {
|
@@ -99,3 +104,107 @@ function cpus_to_mask() {
|
99 | 104 | [[ "$output" == *"nsexec"*": affinity: $mask"* ]]
|
100 | 105 | [[ "$output" == *"Cpus_allowed_list: $final"* ]] # Mind the literal tab.
|
101 | 106 | }
|
| 107 | + |
| 108 | +@test "runc run [CPU affinity should reset]" { |
| 109 | + # We need to use RUNC_CMDLINE since taskset requires a proper binary, not a |
| 110 | + # bash function (which is what runc and __runc are). |
| 111 | + setup_runc_cmdline |
| 112 | + |
| 113 | + first="$(first_cpu)" |
| 114 | + |
| 115 | + # Running without cpuset should result in an affinity for all CPUs. |
| 116 | + update_config '.process.args = [ "/bin/grep", "-F", "Cpus_allowed_list:", "/proc/self/status" ]' |
| 117 | + update_config 'del(.linux.resources.cpu)' |
| 118 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run ctr |
| 119 | + [ "$status" -eq 0 ] |
| 120 | + [[ "$output" != $'Cpus_allowed_list:\t'"$first" ]] |
| 121 | + [[ "$output" == $'Cpus_allowed_list:\t'"$INITIAL_CPU_MASK" ]] |
| 122 | +} |
| 123 | + |
| 124 | +@test "runc run [CPU affinity should reset to cgroup cpuset]" { |
| 125 | + [ $EUID -ne 0 ] && requires rootless_cgroup |
| 126 | + set_cgroups_path |
| 127 | + |
| 128 | + # We need to use RUNC_CMDLINE since taskset requires a proper binary, not a |
| 129 | + # bash function (which is what runc and __runc are). |
| 130 | + setup_runc_cmdline |
| 131 | + |
| 132 | + first="$(first_cpu)" |
| 133 | + second="$((first + 1))" # Hacky; might not work in all environments. |
| 134 | + |
| 135 | + # Running with a cpuset should result in an affinity that matches. |
| 136 | + update_config '.process.args = [ "/bin/grep", "-F", "Cpus_allowed_list:", "/proc/self/status" ]' |
| 137 | + update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$first-$second"'"}' |
| 138 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run ctr |
| 139 | + [ "$status" -eq 0 ] |
| 140 | + [[ "$output" != $'Cpus_allowed_list:\t'"$first" ]] |
| 141 | + # XXX: For some reason, systemd-cgroup leads to us using the all-set |
| 142 | + # cpumask rather than the cpuset we configured? |
| 143 | + [ -v RUNC_USE_SYSTEMD ] || [[ "$output" == $'Cpus_allowed_list:\t'"$first-$second" ]] |
| 144 | + |
| 145 | + # Ditto for a cpuset that has no overlap with the original cpumask. |
| 146 | + update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$second"'"}' |
| 147 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run ctr |
| 148 | + [ "$status" -eq 0 ] |
| 149 | + [[ "$output" != $'Cpus_allowed_list:\t'"$first" ]] |
| 150 | + # XXX: For some reason, systemd-cgroup leads to us using the all-set |
| 151 | + # cpumask rather than the cpuset we configured? |
| 152 | + [ -v RUNC_USE_SYSTEMD ] || [[ "$output" == $'Cpus_allowed_list:\t'"$second" ]] |
| 153 | +} |
| 154 | + |
| 155 | +@test "runc exec [default CPU affinity should reset]" { |
| 156 | + # We need to use RUNC_CMDLINE since taskset requires a proper binary, not a |
| 157 | + # bash function (which is what runc and __runc are). |
| 158 | + setup_runc_cmdline |
| 159 | + |
| 160 | + first="$(first_cpu)" |
| 161 | + |
| 162 | + # Running without cpuset should result in an affinity for all CPUs. |
| 163 | + update_config '.process.args = [ "/bin/sleep", "infinity" ]' |
| 164 | + update_config 'del(.linux.resources.cpu)' |
| 165 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run -d --console-socket "$CONSOLE_SOCKET" ctr3 |
| 166 | + [ "$status" -eq 0 ] |
| 167 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" exec ctr3 grep -F Cpus_allowed_list: /proc/self/status |
| 168 | + [ "$status" -eq 0 ] |
| 169 | + [[ "$output" != $'Cpus_allowed_list:\t'"$first" ]] |
| 170 | + [[ "$output" == $'Cpus_allowed_list:\t'"$INITIAL_CPU_MASK" ]] |
| 171 | +} |
| 172 | + |
| 173 | +@test "runc exec [default CPU affinity should reset to cgroup cpuset]" { |
| 174 | + [ $EUID -ne 0 ] && requires rootless_cgroup |
| 175 | + set_cgroups_path |
| 176 | + |
| 177 | + # We need to use RUNC_CMDLINE since taskset requires a proper binary, not a |
| 178 | + # bash function (which is what runc and __runc are). |
| 179 | + setup_runc_cmdline |
| 180 | + |
| 181 | + first="$(first_cpu)" |
| 182 | + second="$((first + 1))" # Hacky; might not work in all environments. |
| 183 | + |
| 184 | + # Running with a cpuset should result in an affinity that matches. |
| 185 | + update_config '.process.args = [ "/bin/sleep", "infinity" ]' |
| 186 | + update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$first-$second"'"}' |
| 187 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run -d --console-socket "$CONSOLE_SOCKET" ctr |
| 188 | + [ "$status" -eq 0 ] |
| 189 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" exec ctr grep -F Cpus_allowed_list: /proc/self/status |
| 190 | + [ "$status" -eq 0 ] |
| 191 | + [[ "$output" != $'Cpus_allowed_list:\t'"$first" ]] |
| 192 | + # XXX: For some reason, systemd-cgroup leads to us using the all-set |
| 193 | + # cpumask rather than the cpuset we configured? |
| 194 | + [ -v RUNC_USE_SYSTEMD ] || [[ "$output" == $'Cpus_allowed_list:\t'"$first-$second" ]] |
| 195 | + |
| 196 | + # Stop the container so we can reconfigure it. |
| 197 | + runc delete -f ctr |
| 198 | + [ "$status" -eq 0 ] |
| 199 | + |
| 200 | + # Ditto for a cpuset that has no overlap with the original cpumask. |
| 201 | + update_config '.linux.resources.cpu = {"mems": "0", "cpus": "'"$second"'"}' |
| 202 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" run -d --console-socket "$CONSOLE_SOCKET" ctr |
| 203 | + [ "$status" -eq 0 ] |
| 204 | + sane_run taskset -c "$first" "${RUNC_CMDLINE[@]}" exec ctr grep -F Cpus_allowed_list: /proc/self/status |
| 205 | + [ "$status" -eq 0 ] |
| 206 | + [[ "$output" != $'Cpus_allowed_list:\t'"$first" ]] |
| 207 | + # XXX: For some reason, systemd-cgroup leads to us using the all-set |
| 208 | + # cpumask rather than the cpuset we configured? |
| 209 | + [ -v RUNC_USE_SYSTEMD ] || [[ "$output" == $'Cpus_allowed_list:\t'"$second" ]] |
| 210 | +} |
0 commit comments