@@ -0,0 +1,2031 @@
+#!/usr/bin/python%0A#%0Aimport optparse%0Aimport threading%0Aimport subprocess%0Afrom timeout import timeout%0A%0Aparser = optparse.OptionParser()%0Aparser.add_option(%22-T%22, %22--task-set%22, dest=%22task%22, help=%22task input file%22, action=%22store%22, default=%22./task.dat%22)%0A%0A(options, args) = parser.parse_args()%0A%0Astop_command = '''%0Afor n in %60cat ~/io.pid%60; do kill -STOP $n; done%0A'''%0A%0Acont_command = '''%0Afor n in %60cat ~/io.pid%60; do kill -CONT $n; done%0A'''%0A%0A# a 30-element list.%0A# each element stands for disk util for certain 2 seconds within the last 60 seconds%0Autil_last_min = %5B0%5D * 30%0A%0Adef populateUtilList():%0A    i = 0%0A    while(True):%0A        command = %22/usr/bin/iostat -d -x 1 2 %7C grep sda %7C tail -1 %7C awk '%7Bprint $12%7D'%22%0A        util_value = float(subprocess.check_output(command, shell=True))/100  # percentage to value%0A        util_last_min%5Bi %25 30%5D = util_value%0A        i += 1%0A%0Adef getDiskUtil():%0A    return sum(util_last_min, 0.0) / len(util_last_min)%0A%0A%0Adef run(command):%0A    return subprocess.check_output(command, shell=True)%0A%0Adef wrapTimeOut(target, time):%0A%09@timeout(time)%0A%09def func(target):%0A%09%09while(True):%0A%09%09%09util = getDiskUtil()%0A%09%09%09if(util %3C target):%0A%09%09%09%09print %22Disk util(%25s) lower than target, iostress.sh will be running.%22 %25 util%0A%09%09%09%09go = run(cont_command)%0A%09%09%09elif(util %3E target):%0A%09%09%09%09print %22Disk util(%25s) higher than target, iostress.sh will be stopped.%22 %25 util%0A%09%09%09%09go = run(stop_command)%0A%09%09%09sleep(2)%0A%09return func%0A%0A    %0A# start a thread to maintain the last minute disk util list%0At = threading.Thread(target=populateUtilList, args = ())%0At.daemon = True%0At.start()%0A%0Aall_tasks = %5B%5D%0Atry:%0A    with open(options.task, 'r') as f:%0A        for line in f:%0A            lines = line%5B:-2%5D.split(' ')%0A            if(len(lines) == 2):%0A                all_tasks.append(%5B(int(lines%5B0%5D), float(lines%5B1%5D)) for line in f%5D)%0Aexcept Exception as e:%0A    print e%0A%0Afor l in all_tasks:%0A    if(len(l) != 2):%0A        continue%0A    target = l%5B1%5D%0A    time = l%5B0%5D%0A    try:%0A        curr_func = wrapTimeOut(target, time)%0A        curr_func(target)%0A    except:%0A        continue%0A

Create iostress.py