Article Title: simple adaptive CPU utilization control in linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Recently, we are working on a virtualization-related demo involving Server Load balancer in a virtualized environment. We use a simple case to perform live migration when the load is "unbalanced. Because it is only required by the demo, we only consider the cpu usage for the moment. In actual preparation, it is difficult to precisely control the CPU utilization because there is no client pressure. A simple script program is written here. Through adaptive adjustment, the CPU utilization of the server can be controlled within a certain range, so as to ensure that when the CPU usage of a server is too high, the VM live migration is automatically prepared for testing on other machines with low CPU utilization.
The python implementation script is as follows. This script requires the input of five numeric parameters:
Minimum CPU utilization, maximum CPU utilization, initial number of threads, number of threads adjusted each time, and sleep time of each thread (MS)
Of course, these parameters are highly empirical due to different hardware environments.
#! /Usr/bin/python Import threading Import time Import OS Import string Import sys Class ControlThread (threading. Thread ): Def _ init _ (self ): Threading. Thread. _ init _ (self) Self. runflag = True # indicates the thread run. It is used to stop a thread normally when it is reduced in the future.
Def run (self ): While self. runflag: OS. popen ('usleep '+ sys. argv [5]) # Time. sleep (string. atof (sys. argv [5]) |
# Here we use usleep in the shell in linux, instead of the sleep function that comes with python.
# In contrast, usleep is still quite powerful, while python sleep is measured in seconds. Although floating point numbers can be input, it is still relatively weak.
Def stop (self ): Self. runflag = False # Let it terminate the loop normally ThreadList = []Print 'start Thread Number: '+ sys. argv [3] +' \ tSleep Time (MS): '+ sys. argv [5] |
# Initialize a certain number of threads. Otherwise, it may take a long time to reach the specified range from scratch.
For I in range (0, string. atoi (sys. argv [3]): Thread = ControlThread () ThreadList. append (thread) Thread. start () |
# Here, sar is used to capture the cpu utilization, which indicates the total cpu utilization. Then compare and make adaptive adjustments
While True: Output = 100-string. atof (OS. popen ('Sar 1 1 | grep ^ Average | awk \ '{print $8} \ ''). read ()) Print 'cpu Usage: '+ str (output) +' \ tCurrent Thread Number: '+ str (len (threadList ))If output <string. atoi (sys. argv [1]): # Add a thread For I in range (0, string. atoi (sys. argv [4]): Thread = ControlThread () Thread. start () ThreadList. append (thread) Print "++" If output> string. atoi (sys. argv [2]): # reduce threads For I in range (0, string. atoi (sys. argv [4]): Thread = threadList. pop () Thread. stop () Print "-----" |
In general, this script is relatively simple and requires the operator's experience to initialize on different machines to achieve the best effect. But after all, this is my first python program and has achieved the expected goal. The above is only the core code that can be run. As for the usage, error handling, exit the program, and so on, it is not provided :)