1 /* Copyright 2022 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 #include "tensorflow/compiler/xla/service/rendezvous.h"
17
18 #include "absl/synchronization/mutex.h"
19 #include "absl/time/time.h"
20 #include "tensorflow/core/platform/logging.h"
21
22 namespace xla {
23
AwaitAndLogIfStuck(absl::Mutex & mutex,const absl::Condition & condition,absl::Duration warn_stuck_timeout,absl::Duration terminate_timeout)24 void AwaitAndLogIfStuck(absl::Mutex& mutex, const absl::Condition& condition,
25 absl::Duration warn_stuck_timeout,
26 absl::Duration terminate_timeout) {
27 if (mutex.AwaitWithTimeout(condition, warn_stuck_timeout)) {
28 return;
29 }
30
31 LOG(ERROR) << "This thread has been waiting for "
32 << absl::ToInt64Seconds(warn_stuck_timeout)
33 << " seconds and may be stuck:";
34
35 if (mutex.AwaitWithTimeout(condition, terminate_timeout)) {
36 LOG(ERROR) << "Thread is unstuck! Warning above was a false-positive. "
37 "Perhaps the timeout is too short.";
38 return;
39 }
40
41 LOG(ERROR)
42 << "Termination timeout of " << absl::ToInt64Seconds(terminate_timeout)
43 << " seconds exceeded. Exiting to ensure a consistent program state.";
44 std::exit(42);
45 }
46
47 } // namespace xla
48