koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 1 | import time |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 2 | import socket |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 3 | import logging |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 4 | import threading |
| 5 | import contextlib |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 6 | import multiprocessing |
| 7 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 8 | import paramiko |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 9 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 10 | |
| 11 | logger = logging.getLogger("io-perf-tool") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def get_barrier(count, threaded=False): |
| 15 | if threaded: |
| 16 | class val(object): |
| 17 | value = count |
| 18 | cond = threading.Condition() |
| 19 | else: |
| 20 | val = multiprocessing.Value('i', count) |
| 21 | cond = multiprocessing.Condition() |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 22 | |
| 23 | def closure(timeout): |
| 24 | with cond: |
| 25 | val.value -= 1 |
| 26 | if val.value == 0: |
| 27 | cond.notify_all() |
| 28 | else: |
| 29 | cond.wait(timeout) |
| 30 | return val.value == 0 |
| 31 | |
| 32 | return closure |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 33 | |
| 34 | |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 35 | def ssh_connect(creds, retry_count=60, timeout=1): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 36 | ssh = paramiko.SSHClient() |
| 37 | ssh.load_host_keys('/dev/null') |
| 38 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 39 | ssh.known_hosts = None |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 40 | for i in range(retry_count): |
| 41 | try: |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 42 | if creds.passwd is not None: |
| 43 | ssh.connect(creds.host, |
| 44 | username=creds.user, |
| 45 | password=creds.passwd, |
| 46 | port=creds.port, |
| 47 | allow_agent=False, |
| 48 | look_for_keys=False) |
| 49 | return ssh |
| 50 | |
| 51 | if creds.key_file is not None: |
| 52 | ssh.connect(creds.host, |
| 53 | username=creds.user, |
| 54 | key_filename=creds.key_file, |
| 55 | look_for_keys=False, |
| 56 | port=creds.port) |
| 57 | return ssh |
| 58 | raise ValueError("Wrong credentials {0}".format(creds.__dict__)) |
| 59 | except paramiko.PasswordRequiredException: |
| 60 | raise |
| 61 | except socket.error: |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 62 | if i == retry_count - 1: |
| 63 | raise |
| 64 | time.sleep(timeout) |
| 65 | |
| 66 | |
| 67 | def wait_on_barrier(barrier, latest_start_time): |
| 68 | if barrier is not None: |
| 69 | if latest_start_time is not None: |
| 70 | timeout = latest_start_time - time.time() |
| 71 | else: |
| 72 | timeout = None |
| 73 | |
| 74 | if timeout is not None and timeout > 0: |
| 75 | msg = "Ready and waiting on barrier. " + \ |
| 76 | "Will wait at most {0} seconds" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 77 | logger.debug(msg.format(int(timeout))) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 78 | |
| 79 | if not barrier(timeout): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 80 | logger.debug("Barrier timeouted") |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 81 | else: |
| 82 | logger.debug("Passing barrier, starting test") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 83 | |
| 84 | |
| 85 | @contextlib.contextmanager |
| 86 | def log_error(action, types=(Exception,)): |
| 87 | if not action.startswith("!"): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 88 | logger.debug("Starts : " + action) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 89 | else: |
| 90 | action = action[1:] |
| 91 | |
| 92 | try: |
| 93 | yield |
| 94 | except Exception as exc: |
| 95 | if isinstance(exc, types) and not isinstance(exc, StopIteration): |
| 96 | templ = "Error during {0} stage: {1}" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 97 | logger.debug(templ.format(action, exc.message)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 98 | raise |
| 99 | |
| 100 | |
| 101 | def run_over_ssh(conn, cmd): |
| 102 | "should be replaces by normal implementation, with select" |
| 103 | |
| 104 | stdin, stdout, stderr = conn.exec_command(cmd) |
| 105 | out = stdout.read() |
| 106 | err = stderr.read() |
| 107 | code = stdout.channel.recv_exit_status() |
| 108 | return code, out, err |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def ssize_to_kb(ssize): |
| 112 | try: |
| 113 | smap = dict(k=1, K=1, M=1024, m=1024, G=1024**2, g=1024**2) |
| 114 | for ext, coef in smap.items(): |
| 115 | if ssize.endswith(ext): |
| 116 | return int(ssize[:-1]) * coef |
| 117 | |
| 118 | if int(ssize) % 1024 != 0: |
| 119 | raise ValueError() |
| 120 | |
| 121 | return int(ssize) / 1024 |
| 122 | |
| 123 | except (ValueError, TypeError, AttributeError): |
| 124 | tmpl = "Unknow size format {0!r} (or size not multiples 1024)" |
| 125 | raise ValueError(tmpl.format(ssize)) |