File: shared.py

package info (click to toggle)
dask.distributed 2022.12.1%2Bds.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,164 kB
  • sloc: python: 81,938; javascript: 1,549; makefile: 228; sh: 100
file content (572 lines) | stat: -rw-r--r-- 19,405 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
from __future__ import annotations

import asyncio
import weakref
from statistics import mean

import tlz as toolz
from bokeh.core.properties import without_property_validation
from bokeh.layouts import column, row
from bokeh.models import (
    Button,
    ColumnDataSource,
    DataRange1d,
    HoverTool,
    LabelSet,
    NumeralTickFormatter,
    Range1d,
    Select,
    Title,
)
from bokeh.palettes import Spectral9
from bokeh.plotting import figure
from tornado import gen

import dask

from distributed import profile
from distributed.compatibility import WINDOWS
from distributed.dashboard.components import DashboardComponent
from distributed.dashboard.utils import update
from distributed.utils import log_errors

if dask.config.get("distributed.dashboard.export-tool"):
    from distributed.dashboard.export_tool import ExportTool
else:
    ExportTool = None  # type: ignore


profile_interval = dask.config.get("distributed.worker.profile.interval")
profile_interval = dask.utils.parse_timedelta(profile_interval, default="ms")


class Processing(DashboardComponent):
    """Processing and distribution per core

    This shows how many tasks are actively running on each worker and how many
    tasks are enqueued for each worker and how many are in the common pool
    """

    def __init__(self, **kwargs):
        data = self.processing_update({"processing": {}, "nthreads": {}})
        self.source = ColumnDataSource(data)

        x_range = Range1d(-1, 1)
        fig = figure(
            title="Processing and Pending",
            tools="",
            x_range=x_range,
            **kwargs,
        )
        fig.quad(
            source=self.source,
            left=0,
            right="right",
            color=Spectral9[0],
            top="top",
            bottom="bottom",
        )

        fig.xaxis.minor_tick_line_alpha = 0
        fig.yaxis.visible = False
        fig.ygrid.visible = False

        hover = HoverTool()
        fig.add_tools(hover)
        hover = fig.select(HoverTool)
        hover.tooltips = """
        <div>
            <span style="font-size: 14px; font-weight: bold;">Host:</span>&nbsp;
            <span style="font-size: 10px; font-family: Monaco, monospace;">@name</span>
        </div>
        <div>
            <span style="font-size: 14px; font-weight: bold;">Processing:</span>&nbsp;
            <span style="font-size: 10px; font-family: Monaco, monospace;">@processing</span>
        </div>
        """
        hover.point_policy = "follow_mouse"

        self.root = fig

    @without_property_validation
    @log_errors
    def update(self, messages):
        msg = messages["processing"]
        if not msg.get("nthreads"):
            return
        data = self.processing_update(msg)
        x_range = self.root.x_range
        max_right = max(data["right"])
        cores = max(data["nthreads"])
        if x_range.end < max_right:
            x_range.end = max_right + 2
        elif x_range.end > 2 * max_right + cores:  # way out there, walk back
            x_range.end = x_range.end * 0.95 + max_right * 0.05

        update(self.source, data)

    @staticmethod
    @log_errors
    def processing_update(msg):
        names = sorted(msg["processing"])
        names = sorted(names)
        processing = msg["processing"]
        processing = [processing[name] for name in names]
        nthreads = msg["nthreads"]
        nthreads = [nthreads[name] for name in names]
        n = len(names)
        d = {
            "name": list(names),
            "processing": processing,
            "right": list(processing),
            "top": list(range(n, 0, -1)),
            "bottom": list(range(n - 1, -1, -1)),
            "nthreads": nthreads,
        }

        d["alpha"] = [0.7] * n

        return d


class ProfilePlot(DashboardComponent):
    """Time plots of the current resource usage on the cluster

    This is two plots, one for CPU and Memory and another for Network I/O
    """

    def __init__(self, **kwargs):
        state = profile.create()
        data = profile.plot_data(state, profile_interval)
        self.states = data.pop("states")
        self.root, self.source = profile.plot_figure(data, **kwargs)

        @without_property_validation
        @log_errors
        def cb(attr, old, new):
            try:
                ind = new.indices[0]
            except IndexError:
                return
            data = profile.plot_data(self.states[ind], profile_interval)
            del self.states[:]
            self.states.extend(data.pop("states"))
            update(self.source, data)
            self.source.selected = old

        self.source.selected.on_change("indices", cb)

    @without_property_validation
    @log_errors
    def update(self, state):
        self.state = state
        data = profile.plot_data(self.state, profile_interval)
        self.states = data.pop("states")
        update(self.source, data)


class ProfileTimePlot(DashboardComponent):
    """Time plots of the current resource usage on the cluster

    This is two plots, one for CPU and Memory and another for Network I/O
    """

    def __init__(self, server, doc=None, **kwargs):
        if doc is not None:
            self.doc = weakref.ref(doc)
            try:
                self.key = doc.session_context.request.arguments.get("key", None)
            except AttributeError:
                self.key = None
            if isinstance(self.key, list):
                self.key = self.key[0]
            if isinstance(self.key, bytes):
                self.key = self.key.decode()
            self.task_names = ["All", self.key] if self.key else ["All"]
        else:
            self.key = None
            self.task_names = ["All"]

        self.server = server
        self.start = None
        self.stop = None
        self.ts = {"count": [], "time": []}
        self.state = profile.create()
        data = profile.plot_data(self.state, profile_interval)
        self.states = data.pop("states")
        self.profile_plot, self.source = profile.plot_figure(data, **kwargs)
        changing = [False]  # avoid repeated changes from within callback

        @without_property_validation
        def cb(attr, old, new):
            if changing[0] or len(new) == 0:
                return
            with log_errors():
                data = profile.plot_data(self.states[new[0]], profile_interval)
                del self.states[:]
                self.states.extend(data.pop("states"))
                changing[0] = True  # don't recursively trigger callback
                update(self.source, data)
                self.source.selected.indices = old
                changing[0] = False

        self.source.selected.on_change("indices", cb)

        self.ts_source = ColumnDataSource({"time": [], "count": []})
        self.ts_plot = figure(
            title="Activity over time",
            height=150,
            x_axis_type="datetime",
            active_drag="xbox_select",
            tools="xpan,xwheel_zoom,xbox_select,reset",
            sizing_mode="stretch_width",
            toolbar_location="above",
        )
        self.ts_plot.line("time", "count", source=self.ts_source)
        self.ts_plot.circle(
            "time", "count", source=self.ts_source, color=None, selection_color="orange"
        )
        self.ts_plot.yaxis.visible = False
        self.ts_plot.grid.visible = False

        @log_errors
        def ts_change(attr, old, new):
            selected = self.ts_source.selected.indices
            if selected:
                start = self.ts_source.data["time"][min(selected)] / 1000
                stop = self.ts_source.data["time"][max(selected)] / 1000
                self.start, self.stop = min(start, stop), max(start, stop)
            else:
                self.start = self.stop = None
            self.trigger_update(update_metadata=False)

        self.ts_source.selected.on_change("indices", ts_change)

        self.reset_button = Button(label="Reset", button_type="success")
        self.reset_button.on_click(lambda: self.update(self.state))

        self.update_button = Button(label="Update", button_type="success")
        self.update_button.on_click(self.trigger_update)

        self.select = Select(value=self.task_names[-1], options=self.task_names)

        def select_cb(attr, old, new):
            if new == "All":
                new = None
            self.key = new
            self.trigger_update(update_metadata=False)

        self.select.on_change("value", select_cb)

        self.root = column(
            row(
                self.select,
                self.reset_button,
                self.update_button,
                sizing_mode="scale_width",
                height=250,
            ),
            self.profile_plot,
            self.ts_plot,
            **kwargs,
        )

        self.subtitle = Title(text=" ", text_font_style="italic")
        self.profile_plot.add_layout(self.subtitle, "above")
        if not dask.config.get("distributed.worker.profile.enabled"):
            self.subtitle.text = "Profiling is disabled."
            self.select.disabled = True
            self.reset_button.disabled = True
            self.update_button.disabled = True

    @without_property_validation
    @log_errors
    def update(self, state, metadata=None):
        self.state = state
        data = profile.plot_data(self.state, profile_interval)
        self.states = data.pop("states")
        update(self.source, data)

        if metadata is not None and metadata["counts"]:
            self.task_names = ["All"] + sorted(metadata["keys"])
            self.select.options = self.task_names
            if self.key:
                ts = metadata["keys"][self.key]
            else:
                ts = metadata["counts"]
            times, counts = zip(*ts)
            self.ts = {"count": counts, "time": [t * 1000 for t in times]}

            self.ts_source.data.update(self.ts)

    @without_property_validation
    def trigger_update(self, update_metadata=True):
        @log_errors
        async def cb():
            prof = await self.server.get_profile(
                key=self.key, start=self.start, stop=self.stop
            )
            if update_metadata:
                metadata = await self.server.get_profile_metadata()
            else:
                metadata = None
            if isinstance(prof, gen.Future):
                prof, metadata = await asyncio.gather(prof, metadata)
            self.doc().add_next_tick_callback(lambda: self.update(prof, metadata))

        self.server.loop.add_callback(cb)


class ProfileServer(DashboardComponent):
    """Time plots of the current resource usage on the cluster

    This is two plots, one for CPU and Memory and another for Network I/O
    """

    def __init__(self, server, doc=None, **kwargs):
        if doc is not None:
            self.doc = weakref.ref(doc)
        self.server = server
        self.log = self.server.io_loop.profile
        self.start = None
        self.stop = None
        self.ts = {"count": [], "time": []}
        self.state = profile.get_profile(self.log)
        data = profile.plot_data(self.state, profile_interval)
        self.states = data.pop("states")
        self.profile_plot, self.source = profile.plot_figure(data, **kwargs)

        changing = [False]  # avoid repeated changes from within callback

        @without_property_validation
        @log_errors
        def cb(attr, old, new):
            if changing[0] or len(new) == 0:
                return

            data = profile.plot_data(self.states[new[0]], profile_interval)
            del self.states[:]
            self.states.extend(data.pop("states"))
            changing[0] = True  # don't recursively trigger callback
            update(self.source, data)
            self.source.selected.indices = old
            changing[0] = False

        self.source.selected.on_change("indices", cb)

        self.ts_source = ColumnDataSource({"time": [], "count": []})
        self.ts_plot = figure(
            title="Activity over time",
            height=150,
            x_axis_type="datetime",
            active_drag="xbox_select",
            tools="xpan,xwheel_zoom,xbox_select,reset",
            sizing_mode="stretch_width",
            toolbar_location="above",
        )
        self.ts_plot.line("time", "count", source=self.ts_source)
        self.ts_plot.circle(
            "time", "count", source=self.ts_source, color=None, selection_color="orange"
        )
        self.ts_plot.yaxis.visible = False
        self.ts_plot.grid.visible = False

        @log_errors
        def ts_change(attr, old, new):
            selected = self.ts_source.selected.indices
            if selected:
                start = self.ts_source.data["time"][min(selected)] / 1000
                stop = self.ts_source.data["time"][max(selected)] / 1000
                self.start, self.stop = min(start, stop), max(start, stop)
            else:
                self.start = self.stop = None
            self.trigger_update()

        self.ts_source.selected.on_change("indices", ts_change)

        self.reset_button = Button(label="Reset", button_type="success")
        self.reset_button.on_click(lambda: self.update(self.state))

        self.update_button = Button(label="Update", button_type="success")
        self.update_button.on_click(self.trigger_update)

        self.root = column(
            row(self.reset_button, self.update_button, sizing_mode="scale_width"),
            self.profile_plot,
            self.ts_plot,
            **kwargs,
        )

        self.subtitle = Title(text=" ", text_font_style="italic")
        self.profile_plot.add_layout(self.subtitle, "above")
        if not dask.config.get("distributed.worker.profile.enabled"):
            self.subtitle.text = "Profiling is disabled."
            self.reset_button.disabled = True
            self.update_button.disabled = True

    @without_property_validation
    @log_errors
    def update(self, state):
        self.state = state
        data = profile.plot_data(self.state, profile_interval)
        self.states = data.pop("states")
        update(self.source, data)

    @without_property_validation
    def trigger_update(self):
        self.state = profile.get_profile(self.log, start=self.start, stop=self.stop)
        data = profile.plot_data(self.state, profile_interval)
        self.states = data.pop("states")
        update(self.source, data)
        times = [t * 1000 for t, _ in self.log]
        counts = list(toolz.pluck("count", toolz.pluck(1, self.log)))
        self.ts_source.data.update({"time": times, "count": counts})


class SystemMonitor(DashboardComponent):
    def __init__(self, worker, height=150, last_count=None, **kwargs):
        self.worker = worker

        names = worker.monitor.quantities
        self.last_count = 0
        if last_count is not None:
            names = worker.monitor.range_query(start=last_count)
            self.last_count = last_count
        self.source = ColumnDataSource({name: [] for name in names})
        self.label_source = ColumnDataSource(
            {
                "x": [5] * 3,
                "y": [70, 55, 40],
                "cpu": ["max: 45%", "min: 45%", "mean: 45%"],
                "memory": ["max: 133.5MiB", "min: 23.6MiB", "mean: 115.4MiB"],
            }
        )
        update(self.source, self.get_data())

        x_range = DataRange1d(follow="end", follow_interval=20000, range_padding=0)

        tools = "reset,xpan,xwheel_zoom"

        self.cpu = figure(
            title="CPU",
            x_axis_type="datetime",
            height=height,
            tools=tools,
            toolbar_location="above",
            x_range=x_range,
            **kwargs,
        )
        self.cpu.line(source=self.source, x="time", y="cpu")
        self.cpu.yaxis.axis_label = "Percentage"
        self.cpu.add_layout(
            LabelSet(
                x="x",
                y="y",
                x_units="screen",
                y_units="screen",
                text="cpu",
                text_font_size="1em",
                source=self.label_source,
            )
        )
        self.mem = figure(
            title="Memory",
            x_axis_type="datetime",
            height=height,
            tools=tools,
            toolbar_location="above",
            x_range=x_range,
            **kwargs,
        )
        self.mem.line(source=self.source, x="time", y="memory")
        self.mem.yaxis.axis_label = "Bytes"
        self.mem.add_layout(
            LabelSet(
                x="x",
                y="y",
                x_units="screen",
                y_units="screen",
                text="memory",
                text_font_size="1em",
                source=self.label_source,
            )
        )
        self.bandwidth = figure(
            title="Bandwidth",
            x_axis_type="datetime",
            height=height,
            x_range=x_range,
            tools=tools,
            toolbar_location="above",
            **kwargs,
        )
        self.bandwidth.line(
            source=self.source,
            x="time",
            y="host_net_io.read_bps",
            color="red",
            legend_label="read",
        )
        self.bandwidth.line(
            source=self.source,
            x="time",
            y="host_net_io.write_bps",
            color="blue",
            legend_label="write",
        )
        self.bandwidth.yaxis.axis_label = "Bytes / second"

        # self.cpu.yaxis[0].formatter = NumeralTickFormatter(format='0%')
        self.bandwidth.yaxis[0].formatter = NumeralTickFormatter(format="0.0b")
        self.mem.yaxis[0].formatter = NumeralTickFormatter(format="0.0b")

        plots = [self.cpu, self.mem, self.bandwidth]

        if not WINDOWS:
            self.num_fds = figure(
                title="Number of File Descriptors",
                x_axis_type="datetime",
                height=height,
                x_range=x_range,
                tools=tools,
                toolbar_location="above",
                **kwargs,
            )

            self.num_fds.line(source=self.source, x="time", y="num_fds")
            plots.append(self.num_fds)

        if "sizing_mode" in kwargs:
            kw = {"sizing_mode": kwargs["sizing_mode"]}
        else:
            kw = {}

        if not WINDOWS:
            self.num_fds.y_range.start = 0
        self.mem.y_range.start = 0
        self.cpu.y_range.start = 0
        self.bandwidth.y_range.start = 0

        self.root = column(*plots, **kw)
        self.worker.monitor.update()

    def get_data(self):
        d = self.worker.monitor.range_query(start=self.last_count)
        d["time"] = [x * 1000 for x in d["time"]]
        self.last_count = self.worker.monitor.count
        return d

    @without_property_validation
    @log_errors
    def update(self):
        self.source.stream(self.get_data(), 1000)
        self.label_source.data["cpu"] = [
            "{}: {:.1f}%".format(f.__name__, f(self.source.data["cpu"]))
            for f in [min, max, mean]
        ]
        self.label_source.data["memory"] = [
            "{}: {}".format(
                f.__name__, dask.utils.format_bytes(f(self.source.data["memory"]))
            )
            for f in [min, max, mean]
        ]