-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitoring.py
542 lines (423 loc) · 16.3 KB
/
monitoring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""TensorFlow monitoring APIs."""
import collections
import functools
import time
from tensorflow.core.framework import summary_pb2
from tensorflow.python import pywrap_tfe
from tensorflow.python.client import pywrap_tf_session
from tensorflow.python.framework import c_api_util
from tensorflow.python.util import compat
from tensorflow.python.util.tf_export import tf_export
_MetricMethod = collections.namedtuple('MetricMethod', 'create delete get_cell')
_counter_methods = [
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewCounter0,
delete=pywrap_tfe.TFE_MonitoringDeleteCounter0,
get_cell=pywrap_tfe.TFE_MonitoringGetCellCounter0),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewCounter1,
delete=pywrap_tfe.TFE_MonitoringDeleteCounter1,
get_cell=pywrap_tfe.TFE_MonitoringGetCellCounter1),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewCounter2,
delete=pywrap_tfe.TFE_MonitoringDeleteCounter2,
get_cell=pywrap_tfe.TFE_MonitoringGetCellCounter2),
]
_int_gauge_methods = [
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewIntGauge0,
delete=pywrap_tfe.TFE_MonitoringDeleteIntGauge0,
get_cell=pywrap_tfe.TFE_MonitoringGetCellIntGauge0),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewIntGauge1,
delete=pywrap_tfe.TFE_MonitoringDeleteIntGauge1,
get_cell=pywrap_tfe.TFE_MonitoringGetCellIntGauge1),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewIntGauge2,
delete=pywrap_tfe.TFE_MonitoringDeleteIntGauge2,
get_cell=pywrap_tfe.TFE_MonitoringGetCellIntGauge2),
]
_string_gauge_methods = [
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewStringGauge0,
delete=pywrap_tfe.TFE_MonitoringDeleteStringGauge0,
get_cell=pywrap_tfe.TFE_MonitoringGetCellStringGauge0),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewStringGauge1,
delete=pywrap_tfe.TFE_MonitoringDeleteStringGauge1,
get_cell=pywrap_tfe.TFE_MonitoringGetCellStringGauge1),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewStringGauge2,
delete=pywrap_tfe.TFE_MonitoringDeleteStringGauge2,
get_cell=pywrap_tfe.TFE_MonitoringGetCellStringGauge2),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewStringGauge3,
delete=pywrap_tfe.TFE_MonitoringDeleteStringGauge3,
get_cell=pywrap_tfe.TFE_MonitoringGetCellStringGauge3),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewStringGauge4,
delete=pywrap_tfe.TFE_MonitoringDeleteStringGauge4,
get_cell=pywrap_tfe.TFE_MonitoringGetCellStringGauge4),
]
_bool_gauge_methods = [
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewBoolGauge0,
delete=pywrap_tfe.TFE_MonitoringDeleteBoolGauge0,
get_cell=pywrap_tfe.TFE_MonitoringGetCellBoolGauge0),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewBoolGauge1,
delete=pywrap_tfe.TFE_MonitoringDeleteBoolGauge1,
get_cell=pywrap_tfe.TFE_MonitoringGetCellBoolGauge1),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewBoolGauge2,
delete=pywrap_tfe.TFE_MonitoringDeleteBoolGauge2,
get_cell=pywrap_tfe.TFE_MonitoringGetCellBoolGauge2),
]
_sampler_methods = [
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewSampler0,
delete=pywrap_tfe.TFE_MonitoringDeleteSampler0,
get_cell=pywrap_tfe.TFE_MonitoringGetCellSampler0),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewSampler1,
delete=pywrap_tfe.TFE_MonitoringDeleteSampler1,
get_cell=pywrap_tfe.TFE_MonitoringGetCellSampler1),
_MetricMethod(
create=pywrap_tfe.TFE_MonitoringNewSampler2,
delete=pywrap_tfe.TFE_MonitoringDeleteSampler2,
get_cell=pywrap_tfe.TFE_MonitoringGetCellSampler2),
]
class Metric(object):
"""The base class of metric."""
__slots__ = ["_metric", "_metric_name", "_metric_methods", "_label_length"]
def __init__(self, metric_name, metric_methods, label_length, *args):
"""Creates a new metric.
Args:
metric_name: name of the metric class.
metric_methods: list of swig metric methods.
label_length: length of label args.
*args: the arguments to call create method.
"""
self._metric_name = metric_name
self._metric_methods = metric_methods
self._label_length = label_length
if label_length >= len(self._metric_methods):
raise ValueError('Cannot create {} metric with label >= {}'.format(
self._metric_name, len(self._metric_methods)))
self._metric = self._metric_methods[self._label_length].create(*args)
def __del__(self):
try:
deleter = self._metric_methods[self._label_length].delete
metric = self._metric
except AttributeError:
return
if deleter is not None:
deleter(metric)
def get_cell(self, *labels):
"""Retrieves the cell."""
if len(labels) != self._label_length:
raise ValueError('The {} expects taking {} labels'.format(
self._metric_name, self._label_length))
return self._metric_methods[self._label_length].get_cell(
self._metric, *labels)
class CounterCell(object):
"""CounterCell stores each value of a Counter."""
__slots__ = ["_cell"]
def __init__(self, cell):
"""Creates a new CounterCell.
Args:
cell: A c pointer of TFE_MonitoringCounterCell.
"""
self._cell = cell
def increase_by(self, value):
"""Atomically increments the value.
Args:
value: non-negative value.
"""
pywrap_tfe.TFE_MonitoringCounterCellIncrementBy(self._cell, value)
def value(self):
"""Retrieves the current value."""
return pywrap_tfe.TFE_MonitoringCounterCellValue(self._cell)
class Counter(Metric):
"""A stateful class for updating a cumulative integer metric.
This class encapsulates a set of values (or a single value for a label-less
metric). Each value is identified by a tuple of labels. The class allows the
user to increment each value.
"""
__slots__ = []
def __init__(self, name, description, *labels):
"""Creates a new Counter.
Args:
name: name of the new metric.
description: description of the new metric.
*labels: The label list of the new metric.
"""
super(Counter, self).__init__('Counter', _counter_methods, len(labels),
name, description, *labels)
def get_cell(self, *labels):
"""Retrieves the cell."""
return CounterCell(super(Counter, self).get_cell(*labels))
class IntGaugeCell(object):
"""A single integer value stored in an `IntGauge`."""
__slots__ = ["_cell"]
def __init__(self, cell):
"""Creates a new IntGaugeCell.
Args:
cell: A c pointer of TFE_MonitoringIntGaugeCell.
"""
self._cell = cell
def set(self, value):
"""Atomically set the value.
Args:
value: integer value.
"""
pywrap_tfe.TFE_MonitoringIntGaugeCellSet(self._cell, value)
def value(self):
"""Retrieves the current value."""
return pywrap_tfe.TFE_MonitoringIntGaugeCellValue(self._cell)
class IntGauge(Metric):
"""A stateful class for updating a gauge-like integer metric.
This class encapsulates a set of integer values (or a single value for a
label-less metric). Each value is identified by a tuple of labels. The class
allows the user to set each value.
"""
__slots__ = []
def __init__(self, name, description, *labels):
"""Creates a new IntGauge.
Args:
name: name of the new metric.
description: description of the new metric.
*labels: The label list of the new metric.
"""
super(IntGauge, self).__init__('IntGauge', _int_gauge_methods, len(labels),
name, description, *labels)
def get_cell(self, *labels):
"""Retrieves the cell."""
return IntGaugeCell(super(IntGauge, self).get_cell(*labels))
class StringGaugeCell(object):
"""A single string value stored in an `StringGauge`."""
__slots__ = ["_cell"]
def __init__(self, cell):
"""Creates a new StringGaugeCell.
Args:
cell: A c pointer of TFE_MonitoringStringGaugeCell.
"""
self._cell = cell
def set(self, value):
"""Atomically set the value.
Args:
value: string value.
"""
pywrap_tfe.TFE_MonitoringStringGaugeCellSet(self._cell, value)
def value(self):
"""Retrieves the current value."""
with c_api_util.tf_buffer() as buffer_:
pywrap_tfe.TFE_MonitoringStringGaugeCellValue(self._cell, buffer_)
value = pywrap_tf_session.TF_GetBuffer(buffer_).decode('utf-8')
return value
class StringGauge(Metric):
"""A stateful class for updating a gauge-like string metric.
This class encapsulates a set of string values (or a single value for a
label-less metric). Each value is identified by a tuple of labels. The class
allows the user to set each value.
"""
__slots__ = []
def __init__(self, name, description, *labels):
"""Creates a new StringGauge.
Args:
name: name of the new metric.
description: description of the new metric.
*labels: The label list of the new metric.
"""
super(StringGauge, self).__init__('StringGauge', _string_gauge_methods,
len(labels), name, description, *labels)
def get_cell(self, *labels):
"""Retrieves the cell."""
return StringGaugeCell(super(StringGauge, self).get_cell(*labels))
class BoolGaugeCell(object):
"""A single boolean value stored in an `BoolGauge`."""
__slots__ = ["_cell"]
def __init__(self, cell):
"""Creates a new BoolGaugeCell.
Args:
cell: A c pointer of TFE_MonitoringBoolGaugeCell.
"""
self._cell = cell
def set(self, value):
"""Atomically set the value.
Args:
value: bool value.
"""
pywrap_tfe.TFE_MonitoringBoolGaugeCellSet(self._cell, value)
def value(self):
"""Retrieves the current value."""
return pywrap_tfe.TFE_MonitoringBoolGaugeCellValue(self._cell)
@tf_export("__internal__.monitoring.BoolGauge", v1=[])
class BoolGauge(Metric):
"""A stateful class for updating a gauge-like bool metric.
This class encapsulates a set of boolean values (or a single value for a
label-less metric). Each value is identified by a tuple of labels. The class
allows the user to set each value.
"""
__slots__ = []
def __init__(self, name, description, *labels):
"""Creates a new BoolGauge.
Args:
name: name of the new metric.
description: description of the new metric.
*labels: The label list of the new metric.
"""
super(BoolGauge, self).__init__('BoolGauge', _bool_gauge_methods,
len(labels), name, description, *labels)
def get_cell(self, *labels):
"""Retrieves the cell."""
return BoolGaugeCell(super(BoolGauge, self).get_cell(*labels))
class SamplerCell(object):
"""SamplerCell stores each value of a Sampler."""
__slots__ = ["_cell"]
def __init__(self, cell):
"""Creates a new SamplerCell.
Args:
cell: A c pointer of TFE_MonitoringSamplerCell.
"""
self._cell = cell
def add(self, value):
"""Atomically add a sample.
Args:
value: float value.
"""
pywrap_tfe.TFE_MonitoringSamplerCellAdd(self._cell, value)
def value(self):
"""Retrieves the current distribution of samples.
Returns:
A HistogramProto describing the distribution of samples.
"""
with c_api_util.tf_buffer() as buffer_:
pywrap_tfe.TFE_MonitoringSamplerCellValue(self._cell, buffer_)
proto_data = pywrap_tf_session.TF_GetBuffer(buffer_)
histogram_proto = summary_pb2.HistogramProto()
histogram_proto.ParseFromString(compat.as_bytes(proto_data))
return histogram_proto
class Buckets(object):
"""Bucketing strategies for the samplers."""
__slots__ = ["buckets"]
def __init__(self, buckets):
"""Creates a new Buckets.
Args:
buckets: A c pointer of TFE_MonitoringBuckets.
"""
self.buckets = buckets
def __del__(self):
pywrap_tfe.TFE_MonitoringDeleteBuckets(self.buckets)
class ExponentialBuckets(Buckets):
"""Exponential bucketing strategy.
Sets up buckets of the form:
[-DBL_MAX, ..., scale * growth^i,
scale * growth_factor^(i + 1), ..., DBL_MAX].
"""
__slots__ = []
def __init__(self, scale, growth_factor, bucket_count):
"""Creates a new exponential Buckets.
Args:
scale: float
growth_factor: float
bucket_count: integer
"""
super(ExponentialBuckets, self).__init__(
pywrap_tfe.TFE_MonitoringNewExponentialBuckets(scale, growth_factor,
bucket_count))
class Sampler(Metric):
"""A stateful class for updating a cumulative histogram metric.
This class encapsulates a set of histograms (or a single histogram for a
label-less metric) configured with a list of increasing bucket boundaries.
Each histogram is identified by a tuple of labels. The class allows the
user to add a sample to each histogram value.
"""
__slots__ = []
def __init__(self, name, buckets, description, *labels):
"""Creates a new Sampler.
Args:
name: name of the new metric.
buckets: bucketing strategy of the new metric.
description: description of the new metric.
*labels: The label list of the new metric.
"""
super(Sampler, self).__init__('Sampler', _sampler_methods, len(labels),
name, buckets.buckets, description, *labels)
def get_cell(self, *labels):
"""Retrieves the cell."""
return SamplerCell(super(Sampler, self).get_cell(*labels))
# Keeping track of current MonitoredTimer sections to prevent repetitive
# counting.
MonitoredTimerSections = []
class MonitoredTimer(object):
"""A context manager to measure the walltime and increment a Counter cell."""
__slots__ = [
"cell",
"t",
"monitored_section_name",
"_counting",
"_avoid_repetitive_counting",
]
def __init__(
self, cell, monitored_section_name=None, avoid_repetitive_counting=False
):
"""Creates a new MonitoredTimer.
Args:
cell: the cell associated with the time metric that will be inremented.
monitored_section_name: name of action being monitored here.
avoid_repetitive_counting: when set to True, if already in a monitored
timer section with the same monitored_section_name, skip counting.
"""
self.cell = cell
self.monitored_section_name = monitored_section_name
self._avoid_repetitive_counting = avoid_repetitive_counting
self._counting = True
def __enter__(self):
if (
self._avoid_repetitive_counting
and self.monitored_section_name
and self.monitored_section_name in MonitoredTimerSections
):
self._counting = False
return self
self.t = time.time()
if self.monitored_section_name:
MonitoredTimerSections.append(self.monitored_section_name)
return self
def __exit__(self, exception_type, exception_value, traceback):
del exception_type, exception_value, traceback
if self._counting:
micro_seconds = (time.time() - self.t) * 1000000
self.cell.increase_by(int(micro_seconds))
if self.monitored_section_name:
MonitoredTimerSections.remove(self.monitored_section_name)
def monitored_timer(cell):
"""A function decorator for adding MonitoredTimer support.
Args:
cell: the cell associated with the time metric that will be inremented.
Returns:
A decorator that measure the function runtime and increment the specified
counter cell.
"""
def actual_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with MonitoredTimer(cell):
return func(*args, **kwargs)
return wrapper
return actual_decorator