xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/xla_debug_info_manager.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_XLA_DEBUG_INFO_MANAGER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_XLA_DEBUG_INFO_MANAGER_H_
18 
19 #include <string>
20 #include <utility>
21 
22 #include "absl/container/flat_hash_map.h"
23 #include "tensorflow/compiler/xla/service/hlo.pb.h"
24 #include "tensorflow/compiler/xla/service/hlo_module.h"
25 #include "tensorflow/core/lib/core/status.h"
26 
27 namespace xla {
28 
29 using ModuleIdentifier = int;
30 
31 // XlaDebugInfoManager tracks all XLA programs (Executables) throughout their
32 // lifetime. Because the tracing period can start during an Executable's
33 // execution, we need to track Executables even when tracing is off.
34 // This class is thread-safe.
35 class XlaDebugInfoManager {
36  public:
Get()37   static XlaDebugInfoManager* Get() {
38     static XlaDebugInfoManager* singleton = new XlaDebugInfoManager();
39     return singleton;
40   }
41 
42   // Registers an active module to XlaDebugInfoManager.
43   // The module_id is expected to be unique per process.
44   void RegisterModule(
45       ModuleIdentifier module_id, std::shared_ptr<const HloModule> hlo_module,
46       std::shared_ptr<const BufferAssignmentProto> buffer_assignment);
47 
48   // Unregisters an active module.
49   void UnregisterModule(ModuleIdentifier module_id);
50 
51   // Start tracing, began to collecting debug information for all the running
52   // modules during the tracing period.
53   void StartTracing();
54 
55   // Stops tracing.
56   // If module_debug_info is not null, returns debug information for all the
57   // modules that were alive since StartTracing().
58   void StopTracing(
59       std::vector<std::unique_ptr<HloProto>>* module_debug_info = nullptr);
60 
61   friend class XlaDebugInfoManagerTestPeer;
62 
63  private:
64   XlaDebugInfoManager() = default;
65 
66   struct XlaModuleEntry {
67     std::shared_ptr<const HloModule> hlo_module;
68     std::shared_ptr<const BufferAssignmentProto> buffer_assignment;
69     bool active = false;
70   };
71 
72   mutable absl::Mutex mutex_;
73   bool tracing_active_ ABSL_GUARDED_BY(mutex_) = false;
74   // Active modules are those still tracked by us. There could be much more
75   // active modules than running modules, we will try to reduce the trace size
76   // by only transfer those modules that were running during tracing period.
77   absl::flat_hash_map<ModuleIdentifier, XlaModuleEntry> modules_
78       ABSL_GUARDED_BY(mutex_);
79 };
80 
81 }  // namespace xla
82 
83 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_XLA_DEBUG_INFO_MANAGER_H_
84