PK"9Mq^DDdask_memusage.py"""Low-impact, task-level memory profiling for Dask. API usage: from dask_memoryusage import install install(scheduler, "/tmp/memusage.csv") CLI usage: dask-scheduler --preload dask_memusage --memusage.csv /tmp/memusage.csv """ import os import csv from time import sleep from threading import Lock, Thread from collections import defaultdict from psutil import Process import click from distributed.diagnostics.plugin import SchedulerPlugin from distributed.client import Client from distributed.scheduler import Scheduler __all__ = ["install"] __version__ = "1.0" def _process_memory(): """Return process memory usage, in MB. We include memory used by subprocesses. """ proc = Process(os.getpid()) return sum([ p.memory_info().rss / (1024 * 1024) for p in [proc] + list(proc.children(recursive=True)) ]) class _WorkerMemory(object): """Track memory usage by each worker.""" def __init__(self, scheduler_address): self._scheduler_address = scheduler_address self._lock = Lock() self._worker_memory = defaultdict(list) def start(self): """Start the thread.""" t = Thread(target=self._fetch_memory, name="WorkerMemory") t.setDaemon(True) t.start() def _add_memory(self, worker_address, mem): """Record memory timepoint for a worker.""" self._worker_memory[worker_address].append(mem) def _fetch_memory(self): """Retrieve worker memory every 10ms.""" client = Client(self._scheduler_address, timeout=30) while True: worker_to_mem = client.run(_process_memory) with self._lock: for worker, mem in worker_to_mem.items(): self._add_memory(worker, mem) sleep(0.01) def memory_for_task(self, worker_address): """The worker finished its previous task. Return its memory usage and then reset it. """ with self._lock: result = self._worker_memory[worker_address] if not result: result = [0] del self._worker_memory[worker_address] return result class MemoryUsagePlugin(SchedulerPlugin): """Record max and min memory usage for a task. Assumptions: * One task per process: each process has a single thread running a single task at a time. Limitations: * Statistical profiling at 10ms resolution. """ def __init__(self, scheduler, csv_path): SchedulerPlugin.__init__(self) f = open(os.path.join(csv_path), "w", buffering=1) self._csv = csv.writer(f) self._csv.writerow(["task_key", "min_memory_mb", "max_memory_mb"]) self._worker_memory = _WorkerMemory(scheduler.address) self._worker_memory.start() def transition(self, key, start, finish, *args, **kwargs): """Called by the Scheduler every time a task changes status.""" # If the task finished, record its memory usage: if start == "processing" and finish in ("memory", "erred"): worker_address = kwargs["worker"] memory_usage = self._worker_memory.memory_for_task(worker_address) max_memory_usage = max(memory_usage) min_memory_usage = min(memory_usage) self._csv.writerow([key, min_memory_usage, max_memory_usage]) def install(scheduler: Scheduler, csv_path: str): """Register the memory usage profiler with a distributed Scheduler. :param scheduler: The Distributed Scheduler to register with. :param csv_path: The filesystem path where the CSV file will be written. """ plugin = MemoryUsagePlugin(scheduler, csv_path) scheduler.add_plugin(plugin) @click.command() @click.option("--memusage-csv", default="memusage.csv") def dask_setup(scheduler, memusage_csv): install(scheduler, memusage_csv) PK+y9MV77#dask_memusage-1.0.dist-info/LICENSEMIT License Copyright (c) 2018 Itamar Turner-Trauring 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. PK!HSmPO!dask_memusage-1.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UrPK!Ho $dask_memusage-1.0.dist-info/METADATAV[o6~8K$-,َ8mAr){LhX̋FRv_CJvmDޠ9< 'p-W8BU;^ v3kNx;I>B e*kB ]X5 ,NrnifTWxNP;RpݯFyVMfזܵ *.?nm=^ .y!QJ7tĮeVT2M.}2T'%en֚]JXYSXT뢦aBrxePs*-+;Pj+ K P:);IaI7Gq] n! *p\ R1&$NΓ"/#ܞ#WOA #DY5 2'E/S)ۆeB]aD17bVq]OTBH}z||܊=,q!ꃚwH-Qp:D*q">h|4:Yoxz6{`|S01:UjAł{LxUMdae z8 G0d";C~5)J!|h멪ˆ'X/r%0h,"fo®4jQ0GX[4;$z%2&cw[ Z$Ȇ ҒHdB%*"5M.MVŻMƋ<4s1 .MsLKY;{ wrwii3{)kr;zUuѧDr)HO"I*y-h}0|jkr>vJBwa{[L@[lMJ*H!vv-YN c `t ^u6ȷ̲ҙ\S1&XQ\#fX2`YPod9Û?e^ rBw{v+dYP]Й8{y s/bqNJ^S4D5T1at y?0C$>ɀva6|t%kt.SIvH$;)|xCPK"9Mq^DDdask_memusage.pyPK+y9MV77#rdask_memusage-1.0.dist-info/LICENSEPK!HSmPO!dask_memusage-1.0.dist-info/WHEELPK!Ho $ydask_memusage-1.0.dist-info/METADATAPK!He#"dask_memusage-1.0.dist-info/RECORDPK