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