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

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)

28
    async def prepare(self, env, verbose=False):
29
30
31
        # generate config tars
        for host in self.hosts:
            path = env.cfgtar_path(host)
32
33
            if verbose:
                print('preparing config tar:', path)
34
35
36
37
38
39
            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)]
40
41
            sims.append(exectools.run_cmdlist('prepare_' + self.name, prep_cmds,
                verbose=verbose))
42
43
        await asyncio.wait(sims)

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

51
52
            if verbose:
                print('%s: starting NICS' % self.name)
53
            for nic in self.nics:
54
55
                if verbose:
                    print('start NIC:', nic.run_cmd(env))
56
                sc = exectools.SimpleComponent(nic.full_name(),
57
                        shlex.split(nic.run_cmd(env)), verbose=verbose)
58
59
60
61
62
63
64
                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))

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

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

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

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

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

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

                if host.wait:
                    wait_hosts.append(sc)

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

        finally:
            out.set_end()

            # shut things back down
107
108
            if verbose:
                print('%s: cleaning up' % self.name)
109
110
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
            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):
184
        self.end_time = time.time()
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

    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__)



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