Skip to content

Commit c0ad3b5

Browse files
committed
remote/coordinator: add warnings for slow actions
Signed-off-by: Jan Luebbe <[email protected]>
1 parent fc33503 commit c0ad3b5

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

labgrid/remote/coordinator.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import traceback
66
from enum import Enum
77
from functools import wraps
8+
import time
9+
from contextlib import contextmanager
810

911
import attr
1012
import grpc
@@ -26,6 +28,19 @@
2628
from ..util import atomic_replace, labgrid_version, yaml
2729

2830

31+
@contextmanager
32+
def warn_if_slow(prefix, *, limit=0.1):
33+
monotonic = time.monotonic()
34+
process = time.process_time()
35+
thread = time.thread_time()
36+
yield
37+
monotonic = time.monotonic() - monotonic
38+
process = time.process_time() - process
39+
thread = time.thread_time() - thread
40+
if monotonic > limit:
41+
logging.warning("%s: real %.3f>%.3f, process %.3f, thread %.3f", prefix, monotonic, limit, process, thread)
42+
43+
2944
class Action(Enum):
3045
ADD = 0
3146
DEL = 1
@@ -210,18 +225,21 @@ async def _poll_step(self):
210225
# save changes
211226
try:
212227
if self.save_scheduled:
213-
await self.save()
228+
with warn_if_slow("save changes"):
229+
await self.save()
214230
except Exception: # pylint: disable=broad-except
215231
traceback.print_exc()
216232
# try to re-acquire orphaned resources
217233
try:
218234
async with self.lock:
219-
await self._reacquire_orphaned_resources()
235+
with warn_if_slow("reacquire orphaned resources"):
236+
await self._reacquire_orphaned_resources()
220237
except Exception: # pylint: disable=broad-except
221238
traceback.print_exc()
222239
# update reservations
223240
try:
224-
self.schedule_reservations()
241+
with warn_if_slow("schedule reservations"):
242+
self.schedule_reservations()
225243
except Exception: # pylint: disable=broad-except
226244
traceback.print_exc()
227245

@@ -249,12 +267,14 @@ async def save(self):
249267
logging.debug("Running Save")
250268
self.save_scheduled = False
251269

252-
resources = self._get_resources()
253-
resources = yaml.dump(resources)
254-
resources = resources.encode()
255-
places = self._get_places()
256-
places = yaml.dump(places)
257-
places = places.encode()
270+
with warn_if_slow("dump resources"):
271+
resources = self._get_resources()
272+
resources = yaml.dump(resources)
273+
resources = resources.encode()
274+
with warn_if_slow("dump places"):
275+
places = self._get_places()
276+
places = yaml.dump(places)
277+
places = places.encode()
258278

259279
logging.debug("Awaiting resources")
260280
await self.loop.run_in_executor(None, atomic_replace, "resources.yaml", resources)

0 commit comments

Comments
 (0)