Dynamically switch the number of CPUs used in Linux

Source: Internet
Author: User

Number of CPUs used for dynamic Linux switching Abstract: to test some code, the running result will be affected by multi-core parallel execution, so we hope to adjust the number of CPUs used. The preceding method on the network is to add a maxcpus parameter to the kernel startup parameter. However, if this is the case, it is too troublesome to restart each time the kernel is switched. Think... to test some code, the running result will be affected by multi-core parallel processing, so we hope to adjust the number of CPUs used. The preceding method on the network is to add a maxcpus parameter to the kernel startup parameter. However, if this is the case, it is too troublesome to restart each time the kernel is switched. Think about Linux, it should be very powerful, so you can modify the number of CPUs dynamically. Inadvertently saw a file named cpu-hotplug.txt under the Documentation folder of Linux code, so I looked at it, we can see in/sys/devices/system/cpu that the folders of each CPU are named by cpuX, such as cpu0, cpu1, and cpu2. These folders contain an online file. If the value is 0, the CPU is disabled. If the value is 1, the CPU is enabled. Note: The root permission is required. Because I only need to switch between single-core and multi-core, I wrote two scripts in/usr/local/sbin: singlecore #! /Bin/bashcpus_dir = "/sys/devices/system/cpu" for cpu in $ (ls "$ cpus_dir" | grep 'cpu [0-9] \ + ') docpu_online = "$ cpus_dir/$ cpu/online" if [-e "$ cpu_online" & $ (cat $ cpu_online) = 1] thenecho 0> "$ cpu_online" fidonemulticore #! /Bin/bashcpus_dir = "/sys/devices/system/cpu" for cpu in $ (ls "$ cpus_dir" | grep 'cpu [0-9] \ + ') docpu_online = "$ cpus_dir/$ cpu/online" if [-e "$ cpu_online" & $ (cat $ cpu_online) = 0] thenecho 1> "$ cpu_online" after fidone, you only need to run sudo singlecore or sudo multicore ~ By the way, I was wondering what would happen if I disabled all the CPUs? The result shows that cpu0 does not have an online file, that is, at least one CPU is available in Linux.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.