Created
April 18, 2012 13:13
-
-
Save r4um/2413468 to your computer and use it in GitHub Desktop.
Set io scheduler for mounts (SCSI devs only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import glob | |
import optparse | |
import os | |
import time | |
import stat | |
import string | |
import sys | |
class GetOptions: | |
def __init__(self): | |
parser = optparse.OptionParser() | |
parser.add_option("-s", "--sleep", action="store", | |
dest="sleep", type="int", | |
help="continously set and sleep") | |
parser.add_option("-m", "--mount", action="append", | |
dest="mounts", | |
help="Mounts to set scheduler for") | |
parser.add_option("--scheduler", action="store", | |
dest="scheduler", type="string", | |
help="Scheduler to set") | |
self.parser = parser | |
self.parse() | |
def parse(self): | |
(self.options, self.args) = self.parser.parse_args() | |
if not self.mounts: | |
print "No mountpoints provided" | |
self.print_help_exit() | |
if not self.scheduler: | |
print "No scheduler provided" | |
self.print_help_exit() | |
if self.scheduler not in ["cfq", "deadline", "noop", "anticipatory"]: | |
print "Invalid scheduler %s" % (self.scheduler) | |
self.print_help_exit() | |
def __getattr__(self, k): | |
return getattr(self.options, k) | |
def print_help_exit(self): | |
self.parser.print_help() | |
exit(1) | |
def setscheduler(mounts, scheduler="noop"): | |
# devices hash device path => "major:minor" | |
devices = {} | |
paths = glob.glob("/dev/sd*") | |
for path in paths: | |
if stat.S_ISBLK(os.stat(path).st_mode): | |
major = os.major(os.stat(path).st_rdev) | |
minor = os.minor(os.stat(path).st_rdev) | |
devices[path] = "%d:%d" % (major, minor) | |
for mount in mounts: | |
if os.path.ismount(mount): | |
match = False | |
major = os.major(os.stat(mount).st_dev) | |
minor = os.minor(os.stat(mount).st_dev) | |
for path in devices.keys(): | |
if devices[path] == "%d:%d" % (major,minor): | |
match = True | |
device = string.strip(os.path.basename(path), string.digits) | |
print "setting %s scheduler for device %s (%s)"\ | |
% (scheduler, device, mount) | |
open("/sys/block/%s/queue/scheduler" %(device)\ | |
, "w").write("%s\n" % scheduler) | |
if not match: | |
print "WARNING: No device found for mount %s" % (mount) | |
else: | |
print "WARNING: %s is not a mountpoint or mounted" % (mount) | |
def main(): | |
if os.geteuid() != 0: | |
print "Need to be root to run this." | |
sys.exit(1) | |
opts = GetOptions() | |
opts.parse() | |
if opts.sleep: | |
while True: | |
setscheduler(opts.mounts, opts.scheduler) | |
print "Sleeping for %d seconds" % (opts.sleep) | |
time.sleep(opts.sleep) | |
else: | |
setscheduler(opts.mounts, opts.scheduler) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment