koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame^] | 1 | import re |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 2 | import logging |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 3 | import threading |
| 4 | import contextlib |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame^] | 5 | import subprocess |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 6 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 7 | |
| 8 | logger = logging.getLogger("io-perf-tool") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 9 | |
| 10 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 11 | def parse_creds(creds): |
| 12 | # parse user:passwd@host |
| 13 | user, passwd_host = creds.split(":", 1) |
| 14 | |
| 15 | if '@' not in passwd_host: |
| 16 | passwd, host = passwd_host, None |
| 17 | else: |
| 18 | passwd, host = passwd_host.rsplit('@', 1) |
| 19 | |
| 20 | return user, passwd, host |
| 21 | |
| 22 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 23 | class TaksFinished(Exception): |
| 24 | pass |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 25 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 26 | |
| 27 | class Barrier(object): |
| 28 | def __init__(self, count): |
| 29 | self.count = count |
| 30 | self.curr_count = 0 |
| 31 | self.cond = threading.Condition() |
| 32 | self.exited = False |
| 33 | |
| 34 | def wait(self, timeout=None): |
| 35 | with self.cond: |
| 36 | if self.exited: |
| 37 | raise TaksFinished() |
| 38 | |
| 39 | self.curr_count += 1 |
| 40 | if self.curr_count == self.count: |
| 41 | self.curr_count = 0 |
| 42 | self.cond.notify_all() |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame^] | 43 | return True |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 44 | else: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 45 | self.cond.wait(timeout=timeout) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame^] | 46 | return False |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 47 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 48 | def exit(self): |
| 49 | with self.cond: |
| 50 | self.exited = True |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 51 | |
| 52 | |
| 53 | @contextlib.contextmanager |
| 54 | def log_error(action, types=(Exception,)): |
| 55 | if not action.startswith("!"): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 56 | logger.debug("Starts : " + action) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 57 | else: |
| 58 | action = action[1:] |
| 59 | |
| 60 | try: |
| 61 | yield |
| 62 | except Exception as exc: |
| 63 | if isinstance(exc, types) and not isinstance(exc, StopIteration): |
| 64 | templ = "Error during {0} stage: {1}" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 65 | logger.debug(templ.format(action, exc.message)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 66 | raise |
| 67 | |
| 68 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 69 | SMAP = dict(k=1024, m=1024 ** 2, g=1024 ** 3, t=1024 ** 4) |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def ssize_to_b(ssize): |
| 73 | try: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 74 | ssize = ssize.lower() |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 75 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 76 | if ssize.endswith("b"): |
| 77 | ssize = ssize[:-1] |
| 78 | if ssize[-1] in SMAP: |
| 79 | return int(ssize[:-1]) * SMAP[ssize[-1]] |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 80 | return int(ssize) |
| 81 | except (ValueError, TypeError, AttributeError): |
koder aka kdanilov | 2e92802 | 2015-04-08 13:47:15 +0300 | [diff] [blame] | 82 | raise ValueError("Unknow size format {0!r}".format(ssize)) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame^] | 83 | |
| 84 | |
| 85 | def get_ip_for_target(target_ip): |
| 86 | cmd = 'ip route get to'.split(" ") + [target_ip] |
| 87 | data = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read() |
| 88 | |
| 89 | rr = r'{0} via [.0-9]+ dev (?P<dev>.*?) src (?P<ip>[.0-9]+)$' |
| 90 | rr = rr.replace(" ", r'\s+') |
| 91 | rr = rr.format(target_ip.replace('.', r'\.')) |
| 92 | |
| 93 | data_line = data.split("\n")[0].strip() |
| 94 | res = re.match(rr, data_line) |
| 95 | |
| 96 | if res is None: |
| 97 | raise OSError("Can't define interface for {0}".format(target_ip)) |
| 98 | |
| 99 | return res.group('ip') |