[Linux] CPU Hotplug の変更 (動的にCPUの特定coreの有効化/無効化)

Linuxでは動的に特定CPUの無効化が出来る機能があるらしいのでやってみたメモ

# 使用方法
```bash:使用方法(cpuXのXはCPU番号)
# 無効化
$ echo 0 > /sys/devices/system/cpu/cpuX/online
# 有効化
$ echo 1 > /sys/devices/system/cpu/cpuX/online
```

# 使用例
VirtualBox上のUbuntu16.04で確認

```console:元々の構成
root@koara-VirtualBox:/home/koara# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
stepping	: 3
cpu MHz		: 3392.162
cache size	: 8192 KB
physical id	: 0
siblings	: 2
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm
bugs		:
bogomips	: 6784.32
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
stepping	: 3
cpu MHz		: 3392.162
cache size	: 8192 KB
physical id	: 0
siblings	: 2
core id		: 1
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm
bugs		:
bogomips	: 6784.32
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
```

cpu1を無効化してみる。

```console:cpu1の無効化
root@koara-VirtualBox:/home/koara# echo 0 > /sys/devices/system/cpu/cpu1/online
root@koara-VirtualBox:/home/koara# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
stepping	: 3
cpu MHz		: 3392.162
cache size	: 8192 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm
bugs		:
bogomips	: 6784.32
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
```

CPU1を無効化できていることを確認できた。

```console:無効にしたCPUを有効化
root@koara-VirtualBox:/home/koara# echo 1 > /sys/devices/system/cpu/cpu1/online
root@koara-VirtualBox:/home/koara# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
stepping	: 3
cpu MHz		: 3392.162
cache size	: 8192 KB
physical id	: 0
siblings	: 2
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm
bugs		:
bogomips	: 6784.32
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
stepping	: 3
cpu MHz		: 3392.162
cache size	: 8192 KB
physical id	: 0
siblings	: 2
core id		: 1
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx rdrand hypervisor lahf_lm abm
bugs		:
bogomips	: 6784.32
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
```

再度１を書き込むと有効化できる。

# 参考
+ [CPU hotplug Support in Linux(tm) Kernel](https://www.kernel.org/doc/Documentation/cpu-hotplug.txt)
+ [Linux Hotplug a CPU and Disable CPU Cores At Run Time](https://www.cyberciti.biz/faq/debian-rhel-centos-redhat-suse-hotplug-cpu/)
+ [CPUのホットプラグ | VirtualBox Mania](http://vboxmania.net/content/cpu%E3%81%AE%E3%83%9B%E3%83%83%E3%83%88%E3%83%97%E3%83%A9%E3%82%B0)
+ [Linuxブート時のcpu数認識の処理を読む - φ(・・*)ゞ ｳｰﾝ　カーネルとか弄ったりのメモ](http://kernhack.hatenablog.com/entry/2013/12/14/191400)