|
| 1 | +import os |
| 2 | + |
| 3 | +from .collector import ValueCollector |
| 4 | +from .constants import ( |
| 5 | + GC_COUNT_GEN0, |
| 6 | + GC_COUNT_GEN1, |
| 7 | + GC_COUNT_GEN2, |
| 8 | + THREAD_COUNT, |
| 9 | + MEM_RSS, |
| 10 | + CTX_SWITCH_VOLUNTARY, |
| 11 | + CTX_SWITCH_INVOLUNTARY, |
| 12 | + CPU_TIME_SYS, |
| 13 | + CPU_TIME_USER, |
| 14 | + CPU_PERCENT, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class RuntimeMetricCollector(ValueCollector): |
| 19 | + value = [] |
| 20 | + periodic = True |
| 21 | + |
| 22 | + |
| 23 | +class GCRuntimeMetricCollector(RuntimeMetricCollector): |
| 24 | + """ Collector for garbage collection generational counts |
| 25 | +
|
| 26 | + More information at https://docs.python.org/3/library/gc.html |
| 27 | + """ |
| 28 | + required_modules = ['gc'] |
| 29 | + |
| 30 | + def collect_fn(self, keys): |
| 31 | + gc = self.modules.get('gc') |
| 32 | + |
| 33 | + counts = gc.get_count() |
| 34 | + metrics = [ |
| 35 | + (GC_COUNT_GEN0, counts[0]), |
| 36 | + (GC_COUNT_GEN1, counts[1]), |
| 37 | + (GC_COUNT_GEN2, counts[2]), |
| 38 | + ] |
| 39 | + |
| 40 | + return metrics |
| 41 | + |
| 42 | + |
| 43 | +class PSUtilRuntimeMetricCollector(RuntimeMetricCollector): |
| 44 | + """Collector for psutil metrics. |
| 45 | +
|
| 46 | + Performs batched operations via proc.oneshot() to optimize the calls. |
| 47 | + See https://psutil.readthedocs.io/en/latest/#psutil.Process.oneshot |
| 48 | + for more information. |
| 49 | + """ |
| 50 | + required_modules = ['psutil'] |
| 51 | + stored_value = dict( |
| 52 | + CPU_TIME_SYS_TOTAL=0, |
| 53 | + CPU_TIME_USER_TOTAL=0, |
| 54 | + CTX_SWITCH_VOLUNTARY_TOTAL=0, |
| 55 | + CTX_SWITCH_INVOLUNTARY_TOTAL=0, |
| 56 | + ) |
| 57 | + |
| 58 | + def _on_modules_load(self): |
| 59 | + self.proc = self.modules['psutil'].Process(os.getpid()) |
| 60 | + |
| 61 | + def collect_fn(self, keys): |
| 62 | + with self.proc.oneshot(): |
| 63 | + # only return time deltas |
| 64 | + # TODO[tahir]: better abstraction for metrics based on last value |
| 65 | + cpu_time_sys_total = self.proc.cpu_times().system |
| 66 | + cpu_time_user_total = self.proc.cpu_times().user |
| 67 | + cpu_time_sys = cpu_time_sys_total - self.stored_value['CPU_TIME_SYS_TOTAL'] |
| 68 | + cpu_time_user = cpu_time_user_total - self.stored_value['CPU_TIME_USER_TOTAL'] |
| 69 | + |
| 70 | + ctx_switch_voluntary_total = self.proc.num_ctx_switches().voluntary |
| 71 | + ctx_switch_involuntary_total = self.proc.num_ctx_switches().involuntary |
| 72 | + ctx_switch_voluntary = ctx_switch_voluntary_total - self.stored_value['CTX_SWITCH_VOLUNTARY_TOTAL'] |
| 73 | + ctx_switch_involuntary = ctx_switch_involuntary_total - self.stored_value['CTX_SWITCH_INVOLUNTARY_TOTAL'] |
| 74 | + |
| 75 | + self.stored_value = dict( |
| 76 | + CPU_TIME_SYS_TOTAL=cpu_time_sys_total, |
| 77 | + CPU_TIME_USER_TOTAL=cpu_time_user_total, |
| 78 | + CTX_SWITCH_VOLUNTARY_TOTAL=ctx_switch_voluntary_total, |
| 79 | + CTX_SWITCH_INVOLUNTARY_TOTAL=ctx_switch_involuntary_total, |
| 80 | + ) |
| 81 | + |
| 82 | + metrics = [ |
| 83 | + (THREAD_COUNT, self.proc.num_threads()), |
| 84 | + (MEM_RSS, self.proc.memory_info().rss), |
| 85 | + (CTX_SWITCH_VOLUNTARY, ctx_switch_voluntary), |
| 86 | + (CTX_SWITCH_INVOLUNTARY, ctx_switch_involuntary), |
| 87 | + (CPU_TIME_SYS, cpu_time_sys), |
| 88 | + (CPU_TIME_USER, cpu_time_user), |
| 89 | + (CPU_PERCENT, self.proc.cpu_percent()), |
| 90 | + ] |
| 91 | + |
| 92 | + return metrics |
0 commit comments