• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_SLOW_OPERATION_ALARM_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_SLOW_OPERATION_ALARM_H_
18 
19 #include <atomic>
20 #include <memory>
21 #include <string>
22 #include <tuple>
23 
24 #include "absl/base/attributes.h"
25 #include "absl/strings/string_view.h"
26 #include "absl/time/time.h"
27 #include "tensorflow/compiler/xla/types.h"
28 
29 namespace xla {
30 
31 // This RAII object asynchronously prints a warning if it's alive for more than
32 // a certain amount of time.
33 class SlowOperationAlarm {
34  public:
35   // If `counter` is not null, this alarm will throttle itself to logging
36   // once-every-power-of-two occurrences. The counter must outlive this object.
37   SlowOperationAlarm(absl::Duration timeout, std::string msg,
38                      std::atomic<int64>* counter = nullptr);
39   ~SlowOperationAlarm();
40 
41   // Not copyable or movable, because the constructor stores a pointer to `this`
42   // into a global variable.
43   SlowOperationAlarm(const SlowOperationAlarm&) = delete;
44   SlowOperationAlarm(const SlowOperationAlarm&&) = delete;
45   SlowOperationAlarm& operator=(const SlowOperationAlarm&) = delete;
46   SlowOperationAlarm& operator=(const SlowOperationAlarm&&) = delete;
47 
deadline()48   absl::Time deadline() const { return deadline_; }
msg()49   absl::string_view msg() const { return msg_; }
counter()50   std::atomic<int64>* counter() { return counter_; }
51 
52  private:
53   absl::Time deadline_;
54   std::string msg_;
55   // counter_ may be null.  If it's not, this alarm prints something only once
56   // every power of two occurrences.
57   std::atomic<int64>* counter_;
58 };
59 
60 // Returns an object which prints a warning about slow compilation after a
61 // certain amount of time.
62 //
63 // In debug builds, recommends building with -c opt.
64 //
65 // In opt builds, recommends filing a bug.
66 //
67 // This is throttled to once-every-power-of-two occurrences, globally.
68 ABSL_MUST_USE_RESULT std::unique_ptr<SlowOperationAlarm> SlowCompilationAlarm(
69     absl::string_view msg = "");
70 
71 }  // namespace xla
72 
73 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_SLOW_OPERATION_ALARM_H_
74