app.py 4.72 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
# Copyright 2024 Max Planck Institute for Software Systems, and
# National University of Singapore
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import typing as tp
Jakob Görgen's avatar
Jakob Görgen committed
24
import abc
25
import io
Jakob Görgen's avatar
Jakob Görgen committed
26
from simbricks.orchestration.experiment import experiment_environment as expenv
27

28
29
if tp.TYPE_CHECKING:
    from simbricks.orchestration.system.host import base
30

Jakob Görgen's avatar
Jakob Görgen committed
31
class Application(abc.ABC):
32
    def __init__(self, h: 'Host') -> None:
33
34
35
36
37
        self.host = h


# Note AK: Maybe we can factor most of the duplicate calls with the host out
# into a separate module.
38
class BaseLinuxApplication(abc.ABC):
39
    def __init__(self, h: 'LinuxHost') -> None:
40
        self.host = h
41
42
43
        self.start_delay: float | None = None
        self.end_delay: float | None = None
        self.wait = True
44

45
    @abc.abstractmethod
Jakob Görgen's avatar
Jakob Görgen committed
46
    def run_cmds(self, env: expenv.ExpEnv) -> list[str]:
47
        """Commands to run on node."""
48
        pass
49

Jakob Görgen's avatar
Jakob Görgen committed
50
    def cleanup_cmds(self, env: expenv.ExpEnv) -> list[str]:
51
        """Commands to run to cleanup node."""
52
53
54
55
        if self.end_delay is None:
            return []
        else:
            return [f'sleep {self.start_delay}']
56

Jakob Görgen's avatar
Jakob Görgen committed
57
    def config_files(self, env: expenv.ExpEnv) -> dict[str, tp.IO]:
58
59
60
61
62
63
64
65
66
        """
        Additional files to put inside the node, which are mounted under
        `/tmp/guest/`.

        Specified in the following format: `filename_inside_node`:
        `IO_handle_of_file`
        """
        return {}

Jakob Görgen's avatar
Jakob Görgen committed
67
    def prepare_pre_cp(self, env: expenv.ExpEnv) -> list[str]:
68
69
        """Commands to run to prepare node before checkpointing."""
        return [
Jakob Görgen's avatar
Jakob Görgen committed
70
71
72
73
74
            "set -x",
            "export HOME=/root",
            "export LANG=en_US",
            'export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:'
            + '/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"',
75
76
        ]

Jakob Görgen's avatar
Jakob Görgen committed
77
    def prepare_post_cp(self, env: expenv.ExpEnv) -> list[str]:
78
        """Commands to run to prepare node after checkpoint restore."""
79
80
81
82
        if self.end_delay is None:
            return []
        else:
            return [f'sleep {self.end_delay}']
83
84
85
86
87
88
89
90
91

    def strfile(self, s: str) -> io.BytesIO:
        """
        Helper function to convert a string to an IO handle for usage in
        `config_files()`.

        Using this, you can create a file with the string as its content on the
        simulated node.
        """
Jakob Görgen's avatar
Jakob Görgen committed
92
        return io.BytesIO(bytes(s, encoding="UTF-8"))
93
94
95


class PingClient(BaseLinuxApplication):
Hejing Li's avatar
Hejing Li committed
96
97
    def __init__(self, h: 'LinuxHost', server_ip: str = '192.168.64.1') -> None:
        super().__init__(h)
98
99
100
101
102
103
104
        self.server_ip = server_ip

    def run_cmds(self, env: expenv.ExpEnv) -> tp.List[str]:
        return [f'ping {self.server_ip} -c 10']


class Sleep(BaseLinuxApplication):
Hejing Li's avatar
Hejing Li committed
105
106
    def __init__(self, h: 'LinuxHost', delay: float = 10) -> None:
        super().__init__(h)
107
108
109
110
111
112
113
        self.delay = delay

    def run_cmds(self, env: expenv.ExpEnv) -> tp.List[str]:
        return [f'sleep {self.delay}']


class NetperfServer(BaseLinuxApplication):
Hejing Li's avatar
Hejing Li committed
114
115
    def __init__(self, h: 'LinuxHost') -> None:
        super().__init__(h)
116
117
118
119
120
121

    def run_cmds(self, env: expenv.ExpEnv) -> tp.List[str]:
        return ['netserver', 'sleep infinity']


class NetperfClient(BaseLinuxApplication):
Hejing Li's avatar
Hejing Li committed
122
123
    def __init__(self, h: 'LinuxHost', server_ip: str = '192.168.64.1') -> None:
        super().__init__(h)
124
125
126
127
128
129
130
131
132
133
134
135
136
137
        self.server_ip = server_ip
        self.duration_tp = 10
        self.duration_lat = 10

    def run_cmds(self, env: expenv.ExpEnv) -> tp.List[str]:
        return [
            'netserver',
            'sleep 0.5',
            f'netperf -H {self.server_ip} -l {self.duration_tp}',
            (
                f'netperf -H {self.server_ip} -l {self.duration_lat} -t TCP_RR'
                ' -- -o mean_latency,p50_latency,p90_latency,p99_latency'
            )
        ]