Refactor the build system
[archipelago] / xseg / sys / user / python / send.py
1 #!/usr/bin/env python2.7
2 import sys, os, signal, time
3 from xseg import xseg_api as xseg
4
5 mportno = 0
6 dportno = 2
7 xconf = xseg.xseg_config()
8 cb_null_ptrtype = xseg.CFUNCTYPE(None, xseg.POINTER(xseg.xseg), xseg.uint32_t)
9 nr_requests = 10000
10 concurrent_reqs = 10
11
12 def mapper_loop(ctx):
13     nr_submitted = 0
14     nr_received = 0
15     nr_flying = 0
16     xreq = None
17     recv = None
18
19     t = time.time()
20     while 1:
21         xseg.xseg_prepare_wait(ctx, mportno)
22         if nr_submitted < nr_requests and nr_flying < concurrent_reqs:
23             xreqp = xseg.xseg_get_request(ctx, mportno)
24             try:
25                 xreq = xreqp.contents
26                 xseg.xseg_cancel_wait(ctx, mportno)
27                 if xseg.xseg_prep_request(xreq, 2, 4096) < 0:
28                     xseg.xseg_put_request(ctx, xreq.portno, xreq)
29                     return -1
30                 nr_flying += 1
31                 nr_submitted += 1
32                 xreq.offset = 2
33                 xreq.size = 4096
34                 xreq.op = 1
35
36                 srl = xseg.xseg_submit(ctx, dportno, xreq)
37                 xseg.xseg_signal(ctx, dportno)
38             except Exception as e:
39                 pass
40
41         xreqp = xseg.xseg_receive(ctx, mportno)
42         try:
43             recv = xreqp.contents
44             xseg.xseg_cancel_wait(ctx, mportno)
45             nr_flying -= 1
46             nr_received += 1
47             xseg.xseg_put_request(ctx, recv.portno, recv)
48         except Exception as e:
49             pass
50
51         if recv == None and xreq == None:
52             xseg.xseg_wait_signal(ctx, 1000)
53
54         if nr_received >= nr_requests:
55             break
56
57     print ("Elapsed: %lf\n", time.time() - t)
58     print "submitted %ld, received %ld\n" % (nr_submitted, nr_received)
59
60 def mapper_init():
61     xseg.xseg_initialize()
62     xseg.xseg_parse_spec("segdev:xsegbd:", xconf)
63     ctx=xseg.xseg_join(xconf.type, xconf.name, "posix", xseg.cast(0, cb_null_ptrtype))
64     xseg.xseg_bind_port(ctx, mportno)
65
66     return ctx
67
68 if __name__ == '__main__':
69     nr_requests = int(sys.argv[1])
70     concurrent_reqs = int(sys.argv[2])
71     ctx = mapper_init()
72     mapper_loop(ctx)