experiments.py 5.91 KB
Newer Older
1
2
3
4
5
6
import os
import asyncio
import modes.exectools as exectools
import shlex
import time
import json
7
import traceback
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

class Experiment(object):
    name = None
    timeout = None
    checkpoint = False

    def __init__(self, name):
        self.name = name
        self.hosts = []
        self.nics = []
        self.networks = []

    def add_host(self, sim):
        self.hosts.append(sim)

    def add_nic(self, sim):
        self.nics.append(sim)

    def add_network(self, sim):
        self.networks.append(sim)

29
    async def prepare(self, env, verbose=False):
30
31
32
        # generate config tars
        for host in self.hosts:
            path = env.cfgtar_path(host)
33
34
            if verbose:
                print('preparing config tar:', path)
35
36
37
38
39
40
            host.node_config.make_tar(path)

        # prepare all simulators in parallel
        sims = []
        for sim in self.hosts + self.nics + self.networks:
            prep_cmds = [pc for pc in sim.prep_cmds(env)]
41
42
            sims.append(exectools.run_cmdlist('prepare_' + self.name, prep_cmds,
                verbose=verbose))
43
44
        await asyncio.wait(sims)

45
    async def run(self, env, verbose=False):
46
47
48
49
50
51
        running = []
        sockets = []
        out = ExpOutput(self)
        try:
            out.set_start()

52
53
            if verbose:
                print('%s: starting NICS' % self.name)
54
            for nic in self.nics:
55
56
                if verbose:
                    print('start NIC:', nic.run_cmd(env))
57
                sc = exectools.SimpleComponent(nic.full_name(),
58
                        shlex.split(nic.run_cmd(env)), verbose=verbose)
59
60
61
62
63
64
65
                await sc.start()
                running.append((nic, sc))

                sockets.append(env.nic_pci_path(nic))
                sockets.append(env.nic_eth_path(nic))
                sockets.append(env.nic_shm_path(nic))

66
67
68
            if verbose:
                print('%s: waiting for sockets' % self.name)

69
            for s in sockets:
70
                await exectools.await_file(s, verbose=verbose)
71
72
73

            # start networks
            for net in self.networks:
74
75
76
                if verbose:
                    print('start Net:', net.run_cmd(env))

77
                sc = exectools.SimpleComponent(net.full_name(),
78
                        shlex.split(net.run_cmd(env)), verbose=verbose)
79
80
81
82
83
84
                await sc.start()
                running.append((net, sc))

            # start hosts
            wait_hosts = []
            for host in self.hosts:
85
86
87
                if verbose:
                    print('start Host:', host.run_cmd(env))

88
                sc = exectools.SimpleComponent(host.full_name(),
89
                        shlex.split(host.run_cmd(env)), verbose=verbose)
90
91
92
93
94
95
                await sc.start()
                running.append((host,sc))

                if host.wait:
                    wait_hosts.append(sc)

96
97
            if verbose:
                print('%s: waiting for hosts to terminate' % self.name)
98
99
100
101
102
            for sc in wait_hosts:
                await sc.wait()
            # wait for necessary hosts to terminate
        except:
            out.set_failed()
103
            traceback.print_exc()
104
105
106
107
108

        finally:
            out.set_end()

            # shut things back down
109
110
            if verbose:
                print('%s: cleaning up' % self.name)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
            scs = []
            for _,sc in running:
                scs.append(sc.int_term_kill())
            await asyncio.wait(scs)

            for _,sc in running:
                await sc.wait()

            for sock in sockets:
                os.remove(sock)

            for sim,sc in running:
                out.add_sim(sim, sc)
        return out



    def resreq_mem(self):
        mem = 0
        for h in self.hosts:
            mem += h.resreq_mem()
        for n in self.nics:
            mem += n.resreq_mem()
        for n in self.networks:
            mem += n.resreq_mem()
        return mem

    def resreq_cores(self):
        cores = 0
        for h in self.hosts:
            cores += h.resreq_cores()
        for n in self.nics:
            cores += n.resreq_cores()
        for n in self.networks:
            cores += n.resreq_cores()
        return cores

class ExpEnv(object):
    def __init__(self, repo_path, workdir):
        self.repodir = os.path.abspath(repo_path)
        self.workdir = os.path.abspath(workdir)
        self.qemu_img_path = self.repodir + '/qemu/qemu-img'
        self.qemu_path = self.repodir + '/qemu/x86_64-softmmu/qemu-system-x86_64'
        self.qemu_kernel_path = self.repodir + '/images/bzImage'

    def hdcopy_path(self, sim):
        return '%s/hdcopy.%s.%d' % (self.workdir, sim.name, id(sim))

    def hd_path(self, hd_name):
        return '%s/images/output-%s/%s' % (self.repodir, hd_name, hd_name)

    def cfgtar_path(self, sim):
        return '%s/cfg.%s.%d.tar' % (self.workdir, sim.name, id(sim))

    def nic_pci_path(self, sim):
        return '%s/nic.pci.%s.%d' % (self.workdir, sim.name, id(sim))

    def nic_eth_path(self, sim):
        return '%s/nic.eth.%s.%d' % (self.workdir, sim.name, id(sim))

    def nic_shm_path(self, sim):
        return '%s/nic.shm.%s.%d' % (self.workdir, sim.name, id(sim))

class ExpOutput(object):
    def __init__(self, exp):
        self.exp_name = exp.name
        self.start_time = None
        self.end_time = None
        self.sims = {}
        self.success = True

    def set_start(self):
        self.start_time = time.time()

    def set_end(self):
186
        self.end_time = time.time()
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

    def set_failed(self):
        self.success = False

    def add_sim(self, sim, comp):
        obj = {
            'class': sim.__class__.__name__,
            'cmd': comp.cmd_parts,
            'stdout': comp.stdout,
            'stderr': comp.stderr,
        }
        self.sims[sim.full_name()] = obj

    def dumps(self):
        return json.dumps(self.__dict__)



205
206
207
def run_exp_local(exp, env, verbose=False):
    asyncio.run(exp.prepare(env, verbose=verbose))
    return asyncio.run(exp.run(env, verbose=verbose))