|
1 |
| -import os.path |
| 1 | +import os |
2 | 2 | import math
|
3 | 3 | import textwrap
|
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from test import support |
4 | 7 |
|
5 | 8 |
|
6 | 9 | def format_duration(seconds):
|
@@ -54,3 +57,94 @@ def printlist(x, width=70, indent=4, file=None):
|
54 | 57 | print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width,
|
55 | 58 | initial_indent=blanks, subsequent_indent=blanks),
|
56 | 59 | file=file)
|
| 60 | + |
| 61 | +BUFSIZE = 8192 |
| 62 | +LOAD_FACTOR_1 = 0.9200444146293232478931553241 |
| 63 | +SAMPLING_INTERVAL = 5 |
| 64 | +COUNTER_NAME = r'\System\Processor Queue Length' |
| 65 | + |
| 66 | +""" |
| 67 | +This class asynchronously interacts with the `typeperf` command to read |
| 68 | +the system load on Windows. Mulitprocessing and threads can't be used |
| 69 | +here because they interfere with the test suite's cases for those |
| 70 | +modules. |
| 71 | +""" |
| 72 | +class WindowsLoadTracker(): |
| 73 | + def __init__(self): |
| 74 | + self.load = 0.0 |
| 75 | + self.start() |
| 76 | + |
| 77 | + def start(self): |
| 78 | + import _winapi |
| 79 | + import msvcrt |
| 80 | + import uuid |
| 81 | + |
| 82 | + # Create a named pipe which allows for asynchronous IO in Windows |
| 83 | + pipe_name = r'\\.\pipe\typeperf_output_' + str(uuid.uuid4()) |
| 84 | + |
| 85 | + open_mode = _winapi.PIPE_ACCESS_INBOUND |
| 86 | + open_mode |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE |
| 87 | + open_mode |= _winapi.FILE_FLAG_OVERLAPPED |
| 88 | + |
| 89 | + # This is the read end of the pipe, where we will be grabbing output |
| 90 | + self.pipe = _winapi.CreateNamedPipe( |
| 91 | + pipe_name, open_mode, _winapi.PIPE_WAIT, |
| 92 | + 1, BUFSIZE, BUFSIZE, _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL |
| 93 | + ) |
| 94 | + # The write end of the pipe which is passed to the created process |
| 95 | + pipe_write_end = _winapi.CreateFile( |
| 96 | + pipe_name, _winapi.GENERIC_WRITE, 0, _winapi.NULL, |
| 97 | + _winapi.OPEN_EXISTING, 0x80000000, _winapi.NULL |
| 98 | + ) |
| 99 | + # Open up the handle as a python file object so we can pass it to |
| 100 | + # subprocess |
| 101 | + command_stdout = msvcrt.open_osfhandle(pipe_write_end, 0) |
| 102 | + |
| 103 | + # Connect to the read end of the pipe in overlap/async mode |
| 104 | + overlap = _winapi.ConnectNamedPipe(self.pipe, overlapped=True) |
| 105 | + overlap.GetOverlappedResult(True) |
| 106 | + |
| 107 | + # Spawn off the load monitor |
| 108 | + command = ['typeperf', COUNTER_NAME, '-si', str(SAMPLING_INTERVAL)] |
| 109 | + self.p = subprocess.Popen(command, stdout=command_stdout, cwd=support.SAVEDCWD) |
| 110 | + |
| 111 | + # Close our copy of the write end of the pipe |
| 112 | + os.close(command_stdout) |
| 113 | + |
| 114 | + def read_output(self): |
| 115 | + import _winapi |
| 116 | + |
| 117 | + overlapped, _ = _winapi.ReadFile(self.pipe, BUFSIZE, True) |
| 118 | + bytes_read, res = overlapped.GetOverlappedResult(False) |
| 119 | + if res != 0: |
| 120 | + return |
| 121 | + |
| 122 | + return overlapped.getbuffer().decode() |
| 123 | + |
| 124 | + def getloadavg(self): |
| 125 | + typeperf_output = self.read_output() |
| 126 | + # Nothing to update, just return the current load |
| 127 | + if not typeperf_output: |
| 128 | + return self.load |
| 129 | + |
| 130 | + # Process the backlog of load values |
| 131 | + for line in typeperf_output.splitlines(): |
| 132 | + # typeperf outputs in a CSV format like this: |
| 133 | + # "07/19/2018 01:32:26.605","3.000000" |
| 134 | + toks = line.split(',') |
| 135 | + # Ignore blank lines and the initial header |
| 136 | + if line.strip() == '' or (COUNTER_NAME in line) or len(toks) != 2: |
| 137 | + continue |
| 138 | + |
| 139 | + load = float(toks[1].replace('"', '')) |
| 140 | + # We use an exponentially weighted moving average, imitating the |
| 141 | + # load calculation on Unix systems. |
| 142 | + # https://en.wikipedia.org/wiki/Load_(computing)#Unix-style_load_calculation |
| 143 | + new_load = self.load * LOAD_FACTOR_1 + load * (1.0 - LOAD_FACTOR_1) |
| 144 | + self.load = new_load |
| 145 | + |
| 146 | + return self.load |
| 147 | + |
| 148 | + def __del__(self): |
| 149 | + self.p.kill() |
| 150 | + self.p.wait() |
0 commit comments