daemon.py 10.4 KB
Newer Older
1 2
from __future__ import print_function

3 4
from containers import ContainerManager
from resources import ResourceManager
5
import json
6
import logging
7
import os
8
import re
9
import sensor
10 11 12
import signal
import zmq
from zmq.eventloop import ioloop, zmqstream
13

14

15 16 17 18 19 20 21 22
application_fsm_table = {'stable': {'i': 's_ask_i', 'd': 's_ask_d'},
                         's_ask_i': {'done': 'stable', 'max': 'max'},
                         's_ask_d': {'done': 'stable', 'min': 'min'},
                         'max': {'d': 'max_ask_d'},
                         'min': {'i': 'min_ask_i'},
                         'max_ask_d': {'done': 'stable', 'min': 'nop'},
                         'min_ask_i': {'done': 'stable', 'max': 'nop'},
                         'nop': {}}
23 24


25
class Application(object):
26 27 28 29 30 31 32 33 34
    def __init__(self, identity):
        self.identity = identity
        self.buf = ''
        self.state = 'stable'

    def append_buffer(self, msg):
        self.buf = self.buf + msg

    def do_transition(self, msg):
35
        transitions = application_fsm_table[self.state]
36 37 38 39 40 41
        if msg in transitions:
            self.state = transitions[msg]
        else:
            pass

    def get_allowed_requests(self):
42
        return application_fsm_table[self.state].keys()
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    def get_messages(self):
        buf = self.buf
        begin = 0
        off = 0
        ret = ''
        while begin < len(buf):
            if buf.startswith('min', begin):
                ret = 'min'
                off = len(ret)
            elif buf.startswith('max', begin):
                ret = 'max'
                off = len(ret)
            elif buf.startswith('done (', begin):
                n = re.split("done \((\d+)\)", buf[begin:])[1]
                ret = 'done'
                off = len('done ()') + len(n)
            else:
                m = re.match("\d+", buf[begin:])
                if m:
                    ret = 'ok'
                    off = m.end()
                else:
                    break
            begin = begin + off
            yield ret
        self.buf = buf[begin:]
        return


class Daemon(object):
    def __init__(self):
75
        self.applications = {}
76
        self.containerpids = {}
77 78
        self.buf = ''
        self.logger = logging.getLogger(__name__)
79
        self.target = 1.0
80

81
    def do_application_receive(self, parts):
82
        self.logger.info("receiving application stream: %r", parts)
83 84 85 86
        identity = parts[0]

        if len(parts[1]) == 0:
            # empty frame, indicate connect/disconnect
87
            if identity in self.applications:
88
                self.logger.info("known client disconnected")
89
                del self.applications[identity]
90 91
            else:
                self.logger.info("new client: " + repr(identity))
92
                self.applications[identity] = Application(identity)
93
        else:
94 95 96
            if identity in self.applications:
                application = self.applications[identity]
                # we need to unpack the stream into application messages
97
                # messages can be: min, max, done (%d), %d
98 99 100
                application.append_buffer(parts[1])
                for m in application.get_messages():
                    application.do_transition(m)
101
                    self.logger.info("application now in state: %s",
102
                                     application.state)
103

104 105
    def do_upstream_receive(self, parts):
        self.logger.info("receiving upstream message: %r", parts)
106
        if len(parts) != 1:
107
            self.logger.error("unexpected msg length, dropping it: %r", parts)
108 109
            return
        msg = json.loads(parts[0])
110 111 112 113 114 115 116 117 118 119 120
        if isinstance(msg, dict):
            command = msg.get('command')
            # TODO: switch to a dispatch dictionary
            if command is None:
                self.logger.error("missing command in message: %r", msg)
                return
            if command == 'setpower':
                self.target = float(msg['limit'])
                self.logger.info("new target measure: %g", self.target)
            elif command == 'run':
                self.logger.info("new container required: %r", msg)
121 122
                pid = self.container_manager.create(msg)
                if pid > 0:
123
                    self.containerpids[pid] = msg['uuid']
124 125 126 127 128 129 130 131 132 133 134 135 136
                    # TODO: obviously we need to send more info than that
                    update = {'type': 'container',
                              'uuid': msg['uuid'],
                              'errno': 0,
                              'pid': pid,
                              }
                    self.upstream_pub.send_json(update)
                else:
                    update = {'type': 'container',
                              'uuid': msg['uuid'],
                              'errno': pid,
                              }
                    self.upstream_pub.send_json(update)
137 138 139 140 141 142 143 144
            elif command == 'list':
                self.logger.info("asked for container list: %r", msg)
                response = self.container_manager.list()
                update = {'type': 'container',
                          'event': 'list',
                          'payload': response,
                          }
                self.upstream_pub.send_json(update)
145 146
            else:
                self.logger.error("invalid command: %r", command)
147

148
    def do_sensor(self):
149
        self.machine_info = self.sensor.do_update()
150 151
        self.logger.info("current state: %r", self.machine_info)
        total_power = self.machine_info['energy']['power']['total']
152 153 154 155 156 157
        msg = {'type': 'power',
               'total': total_power,
               'limit': self.target
               }
        self.upstream_pub.send_json(msg)
        self.logger.info("sending sensor message: %r", msg)
158 159

    def do_control(self):
160
        total_power = self.machine_info['energy']['power']['total']
161

162
        for identity, application in self.applications.iteritems():
163
            if total_power < self.target:
164
                if 'i' in application.get_allowed_requests():
165
                    self.downstream.send_multipart([identity, 'i'])
166
                    application.do_transition('i')
167
            elif total_power > self.target:
168
                if 'd' in application.get_allowed_requests():
169
                    self.downstream.send_multipart([identity, 'd'])
170
                    application.do_transition('d')
171 172
            else:
                pass
173
            self.logger.info("application now in state: %s", application.state)
174 175

    def do_signal(self, signum, frame):
176 177 178 179 180 181 182 183 184 185 186
        if signum == signal.SIGINT:
            ioloop.IOLoop.current().add_callback_from_signal(self.do_shutdown)
        elif signum == signal.SIGCHLD:
            ioloop.IOLoop.current().add_callback_from_signal(self.do_children)
        else:
            self.logger.error("wrong signal: %d", signum)

    def do_children(self):
        # find out if children have terminated
        while True:
            try:
187 188
                pid, status, rusage = os.wait3(os.WNOHANG)
                if pid == 0 and status == 0:
189 190 191 192
                    break
            except OSError:
                break

193 194 195 196 197 198 199 200 201 202 203 204 205
            self.logger.info("child update %d: %r", pid, status)
            # check if its a pid we care about
            if pid in self.containerpids:
                # check if this is an exit
                if os.WIFEXITED(status):
                    uuid = self.containerpids[pid]
                    self.container_manager.delete(uuid)
                    msg = {'type': 'container',
                           'event': 'exit',
                           'status': status,
                           'uuid': None,
                           }
                    self.upstream_pub.send_json(msg)
206
            else:
207
                self.logger.debug("child update ignored")
208
                pass
209 210

    def do_shutdown(self):
211
        self.sensor.stop()
212 213 214
        ioloop.IOLoop.current().stop()

    def main(self):
215
        # Bind port for downstream clients
216
        bind_port = 1234
217
        # Bind address for downstream clients
218
        bind_address = '*'
219 220 221 222
        # PUB port for upstream clients
        upstream_pub_port = 2345
        # SUB port for upstream clients
        upstream_sub_port = 3456
223

224
        # setup application listening socket
225
        context = zmq.Context()
226 227 228 229 230 231 232 233 234 235 236
        downstream_socket = context.socket(zmq.STREAM)
        upstream_pub_socket = context.socket(zmq.PUB)
        upstream_sub_socket = context.socket(zmq.SUB)

        downstream_bind_param = "tcp://%s:%d" % (bind_address, bind_port)
        upstream_pub_param = "tcp://%s:%d" % (bind_address, upstream_pub_port)
        upstream_sub_param = "tcp://localhost:%d" % (upstream_sub_port)

        downstream_socket.bind(downstream_bind_param)
        upstream_pub_socket.bind(upstream_pub_param)
        upstream_sub_socket.connect(upstream_sub_param)
237
        upstream_sub_filter = ""
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        upstream_sub_socket.setsockopt(zmq.SUBSCRIBE, upstream_sub_filter)

        self.logger.info("downstream socket bound to: %s",
                         downstream_bind_param)
        self.logger.info("upstream pub socket bound to: %s",
                         upstream_pub_param)
        self.logger.info("upstream sub socket connected to: %s",
                         upstream_sub_param)

        # register socket triggers
        self.downstream = zmqstream.ZMQStream(downstream_socket)
        self.downstream.on_recv(self.do_application_receive)
        self.upstream_sub = zmqstream.ZMQStream(upstream_sub_socket)
        self.upstream_sub.on_recv(self.do_upstream_receive)
        # create a stream to let ioloop deal with blocking calls on HWM
        self.upstream_pub = zmqstream.ZMQStream(upstream_pub_socket)
254

255 256 257
        # create resource and container manager
        self.resource_manager = ResourceManager()
        self.container_manager = ContainerManager(self.resource_manager)
258

259 260
        # create sensor manager and make first measurement
        self.sensor = sensor.SensorManager()
261
        self.sensor.start()
262
        self.machine_info = self.sensor.do_update()
263 264 265 266

        # setup periodic sensor updates
        self.sensor_cb = ioloop.PeriodicCallback(self.do_sensor, 1000)
        self.sensor_cb.start()
267 268 269 270 271 272

        self.control = ioloop.PeriodicCallback(self.do_control, 1000)
        self.control.start()

        # take care of signals
        signal.signal(signal.SIGINT, self.do_signal)
273
        signal.signal(signal.SIGCHLD, self.do_signal)
274 275 276 277 278 279 280 281 282

        ioloop.IOLoop.current().start()


def runner():
    ioloop.install()
    logging.basicConfig(level=logging.DEBUG)
    daemon = Daemon()
    daemon.main()