|
5 | 5 | import traceback
|
6 | 6 | from enum import Enum
|
7 | 7 | from functools import wraps
|
| 8 | +import time |
| 9 | +from contextlib import contextmanager |
8 | 10 |
|
9 | 11 | import attr
|
10 | 12 | import grpc
|
|
26 | 28 | from ..util import atomic_replace, labgrid_version, yaml
|
27 | 29 |
|
28 | 30 |
|
| 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 | + |
29 | 44 | class Action(Enum):
|
30 | 45 | ADD = 0
|
31 | 46 | DEL = 1
|
@@ -210,18 +225,21 @@ async def _poll_step(self):
|
210 | 225 | # save changes
|
211 | 226 | try:
|
212 | 227 | if self.save_scheduled:
|
213 |
| - await self.save() |
| 228 | + with warn_if_slow("save changes"): |
| 229 | + await self.save() |
214 | 230 | except Exception: # pylint: disable=broad-except
|
215 | 231 | traceback.print_exc()
|
216 | 232 | # try to re-acquire orphaned resources
|
217 | 233 | try:
|
218 | 234 | async with self.lock:
|
219 |
| - await self._reacquire_orphaned_resources() |
| 235 | + with warn_if_slow("reacquire orphaned resources"): |
| 236 | + await self._reacquire_orphaned_resources() |
220 | 237 | except Exception: # pylint: disable=broad-except
|
221 | 238 | traceback.print_exc()
|
222 | 239 | # update reservations
|
223 | 240 | try:
|
224 |
| - self.schedule_reservations() |
| 241 | + with warn_if_slow("schedule reservations"): |
| 242 | + self.schedule_reservations() |
225 | 243 | except Exception: # pylint: disable=broad-except
|
226 | 244 | traceback.print_exc()
|
227 | 245 |
|
@@ -249,12 +267,14 @@ async def save(self):
|
249 | 267 | logging.debug("Running Save")
|
250 | 268 | self.save_scheduled = False
|
251 | 269 |
|
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() |
258 | 278 |
|
259 | 279 | logging.debug("Awaiting resources")
|
260 | 280 | await self.loop.run_in_executor(None, atomic_replace, "resources.yaml", resources)
|
|
0 commit comments