iter_metrics.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import collections
  2. from typing import List
  3. from ray.util.annotations import Deprecated
  4. from ray.util.timer import _Timer
  5. @Deprecated
  6. class MetricsContext:
  7. """Metrics context object for a local iterator.
  8. This object is accessible by all operators of a local iterator. It can be
  9. used to store and retrieve global execution metrics for the iterator.
  10. It can be accessed by calling LocalIterator.get_metrics(), which is only
  11. allowable inside iterator functions.
  12. Attributes:
  13. counters: dict storing increasing metrics.
  14. timers: dict storing latency timers.
  15. info: dict storing misc metric values.
  16. current_actor: reference to the actor handle that
  17. produced the current iterator output. This is automatically set
  18. for gather_async().
  19. """
  20. def __init__(self):
  21. self.counters = collections.defaultdict(int)
  22. self.timers = collections.defaultdict(_Timer)
  23. self.info = {}
  24. self.current_actor = None
  25. def save(self):
  26. """Return a serializable copy of this context."""
  27. return {
  28. "counters": dict(self.counters),
  29. "info": dict(self.info),
  30. "timers": None, # TODO(ekl) consider persisting timers too
  31. }
  32. def restore(self, values):
  33. """Restores state given the output of save()."""
  34. self.counters.clear()
  35. self.counters.update(values["counters"])
  36. self.timers.clear()
  37. self.info = values["info"]
  38. @Deprecated
  39. class SharedMetrics:
  40. """Holds an indirect reference to a (shared) metrics context.
  41. This is used by LocalIterator.union() to point the metrics contexts of
  42. entirely separate iterator chains to the same underlying context."""
  43. def __init__(
  44. self, metrics: MetricsContext = None, parents: List["SharedMetrics"] = None
  45. ):
  46. self.metrics = metrics or MetricsContext()
  47. self.parents = parents or []
  48. self.set(self.metrics)
  49. def set(self, metrics):
  50. """Recursively set self and parents to point to the same metrics."""
  51. self.metrics = metrics
  52. for parent in self.parents:
  53. parent.set(metrics)
  54. def get(self):
  55. return self.metrics