Skip to content

Instantly share code, notes, and snippets.

@ethercflow
Last active May 28, 2018 16:39
Show Gist options
  • Select an option

  • Save ethercflow/8b10b7a450dfe021d7cf27e4182c5a3f to your computer and use it in GitHub Desktop.

Select an option

Save ethercflow/8b10b7a450dfe021d7cf27e4182c5a3f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import logging
import os
import threading
def parse_opts():
parser = argparse.ArgumentParser(prog='iops')
parser.add_argument('--dev', help='dev(major:minor)' )
parser.add_argument('--base', type = int, help='iops')
parser.add_argument('--step', type = int, help='step')
parser.add_argument('--cycle', type = int, help='cycle')
parser.add_argument('--type', choices='rw', help='riops or wiops')
subparsers = parser.add_subparsers(help='sub-command help')
parser_inc = subparsers.add_parser('inc', help='inc iops')
parser_inc.set_defaults(func=inc)
parser_dec = subparsers.add_parser('dec', help='dec iops')
parser_dec.set_defaults(func=dec)
return parser.parse_args()
def inc(args):
item = 'write'
if args.type == 'r':
item = "read"
run(args.cycle, item, args.dev, args.base, args.step)
def dec(args):
item = 'write'
if args.type == 'r':
item = "read"
run(args.cycle, item, args.dev, args.base, -args.step)
def run(cycle, item, dev, iops, step):
cmd = 'cgset -r blkio.throttle.%s_iops_device="%s %d" iothrottle' % (item, dev, iops)
logging.info(cmd)
ret = os.system(cmd) != 0
if ret != 0:
logging.fatal(cmd)
exit(1)
iops += step
if iops < 0:
iops = 0
t = threading.Timer(cycle, run, (cycle, item, dev, iops, step))
t.start()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
args = parse_opts()
args.func(args)
@ethercflow
Copy link
Copy Markdown
Author

eg:

./iops.py --dev 8:0 --base 1000 --step 5 --cycle 1 --type=w dec
./iops.py --dev 8:0 --base 10 --step 5 --cycle 1 --type=r inc

参数说明:

  1. dev是设备号 major:minor;
  2. base是初始iops;
  3. step是iops变化步长;
  4. cycle是变化周期,单位是s;
  5. type只有两个选择,w代表写iops,r代表读iops;
  6. dec是周期性减少,inc是周期性增。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment