xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/slow_operation_alarm.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 <functional>
21 #include <memory>
22 #include <string>
23 #include <tuple>
24 
25 #include "absl/base/attributes.h"
26 #include "absl/strings/string_view.h"
27 #include "absl/time/time.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_t>* counter = nullptr,
39                      absl::string_view context = "");
40   SlowOperationAlarm(absl::Duration timeout,
41                      std::function<std::string()> msg_fn,
42                      std::atomic<int64_t>* counter = nullptr,
43                      absl::string_view context = "");
44   ~SlowOperationAlarm();
45 
46   // Not copyable or movable, because the constructor stores a pointer to `this`
47   // into a global variable.
48   SlowOperationAlarm(const SlowOperationAlarm&) = delete;
49   SlowOperationAlarm(const SlowOperationAlarm&&) = delete;
50   SlowOperationAlarm& operator=(const SlowOperationAlarm&) = delete;
51   SlowOperationAlarm& operator=(const SlowOperationAlarm&&) = delete;
52 
deadline()53   absl::Time deadline() const { return deadline_; }
msg()54   std::string msg() const { return msg_fn_(); }
counter()55   std::atomic<int64_t>* counter() { return counter_; }
cancel()56   void cancel() { UnscheduleAlarm(this); }
57   // Has the alarm fired?  If appropriate, consider cancel()'ing first, to avoid
58   // a race.
fired()59   bool fired() const { return fired_.load(); }
60 
61  private:
62   static void AlarmLoop();
63   static void ScheduleAlarm(SlowOperationAlarm* alarm);
64   static void UnscheduleAlarm(const SlowOperationAlarm* alarm);
65 
66   absl::Time start_;
67   absl::Time deadline_;
68   std::string context_;
69   std::function<std::string()> msg_fn_;
70   std::atomic<bool> fired_{false};
71   // counter_ may be null.  If it's not, this alarm prints something only once
72   // every power of two occurrences.
73   std::atomic<int64_t>* counter_;
74 };
75 
76 // Returns an object which prints a warning about slow compilation after a
77 // certain amount of time. It will also print the total lifetime duration of
78 // the returned object when it goes out of scope.
79 //
80 // In debug builds, recommends building with -c opt.
81 //
82 // In opt builds, recommends filing a bug.
83 //
84 // This is throttled to once-every-power-of-two occurrences, globally.
85 //
86 // `context` is an additional message prepended to the alarm.
87 [[nodiscard]] std::unique_ptr<SlowOperationAlarm> SlowCompilationAlarm(
88     absl::string_view context);
89 
90 }  // namespace xla
91 
92 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_SLOW_OPERATION_ALARM_H_
93