Update barrier with time measurement
This commit is contained in:
parent
ba0d09b6c4
commit
da6d2f3e6f
67
barrier.py
67
barrier.py
@ -2,49 +2,56 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
|
import struct
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
class Barrier:
|
class Barrier:
|
||||||
def __init__(self, host, port, wait_for):
|
def __init__(self, host, port, waitFor, printer=None):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.wait_for = wait_for
|
self.waitFor = waitFor
|
||||||
|
self.printer = printer
|
||||||
|
self.startTimes = dict()
|
||||||
|
|
||||||
def listen(self):
|
def listen(self):
|
||||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
self.sock.bind((self.host, self.port))
|
self.sock.bind((self.host, self.port))
|
||||||
self.sock.listen()
|
self.sock.listen(128)
|
||||||
|
|
||||||
def waitSingle(self):
|
def wait(self):
|
||||||
connections = []
|
connections = []
|
||||||
addresses = []
|
addresses = []
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
conn, addr = self.sock.accept()
|
conn, addr = self.sock.accept()
|
||||||
connections.append(conn)
|
|
||||||
addresses.append(addr)
|
|
||||||
yield addr
|
|
||||||
|
|
||||||
if len(connections) == self.wait_for:
|
idInBytes = []
|
||||||
|
while len(idInBytes) < 8:
|
||||||
|
data = conn.recv(8 - len(idInBytes))
|
||||||
|
if not data:
|
||||||
|
raise Exception("Could not recv the LogicalPID")
|
||||||
|
idInBytes += data
|
||||||
|
|
||||||
|
pid = struct.unpack('!Q', bytes(idInBytes))[0]
|
||||||
|
connections.append((pid, conn))
|
||||||
|
addresses.append(addr)
|
||||||
|
|
||||||
|
if self.printer:
|
||||||
|
self.printer("Connection from {}, corresponds to PID {}".format(addr, pid))
|
||||||
|
|
||||||
|
if len(connections) == self.waitFor:
|
||||||
break
|
break
|
||||||
|
|
||||||
for conn in connections:
|
for pid, conn in connections:
|
||||||
|
self.startTimes[pid] = int(time.time() * 1000)
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def wait(self):
|
def startTimesFuture(self):
|
||||||
g = self.waitSingle()
|
return self.startTimes
|
||||||
|
|
||||||
conn = []
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
conn.append(next(g))
|
|
||||||
except StopIteration:
|
|
||||||
break
|
|
||||||
|
|
||||||
return conn
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
@ -58,13 +65,14 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--port",
|
"--port",
|
||||||
default=11000,
|
default=10000,
|
||||||
type=int,
|
type=int,
|
||||||
dest="port",
|
dest="port",
|
||||||
help="TCP port where the barrier listens to (default: 11000)",
|
help="TCP port where the barrier listens to (default: 10000)",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
"--processes",
|
"--processes",
|
||||||
required=True,
|
required=True,
|
||||||
type=int,
|
type=int,
|
||||||
@ -74,16 +82,11 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
results = parser.parse_args()
|
results = parser.parse_args()
|
||||||
|
|
||||||
barrier = Barrier(results.host, results.port, results.processes)
|
barrier = Barrier(results.host, results.port, results.processes, print)
|
||||||
barrier.listen()
|
barrier.listen()
|
||||||
print("Barrier listens on {}:{} and waits for {} processes".format(results.host, results.port, results.processes))
|
print("Barrier listens on {}:{} and waits for {} processes".format(results.host, results.port, results.processes))
|
||||||
|
|
||||||
# connectedAddr = barrier.wait()
|
barrier.wait()
|
||||||
connectedAddrGen = barrier.waitSingle()
|
|
||||||
|
|
||||||
while True:
|
for pid, startTs in OrderedDict(sorted(barrier.startTimesFuture().items())).items():
|
||||||
try:
|
print("Process {} started broadcasting at time {} ms from Unix epoch ".format(pid, startTs))
|
||||||
connectedAddr = next(connectedAddrGen)
|
|
||||||
print("Connection from {}".format(connectedAddr))
|
|
||||||
except StopIteration:
|
|
||||||
break
|
|
Loading…
x
Reference in New Issue
Block a user