• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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_CORE_COMMON_RUNTIME_PROCESS_STATE_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_PROCESS_STATE_H_
18 
19 #include <functional>
20 #include <map>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "tensorflow/core/framework/allocator.h"
25 #include "tensorflow/core/framework/allocator_registry.h"
26 #include "tensorflow/core/platform/mutex.h"
27 #include "tensorflow/core/platform/thread_annotations.h"
28 #include "tensorflow/core/platform/types.h"
29 #include "tensorflow/core/protobuf/config.pb.h"
30 
31 namespace tensorflow {
32 
33 class Allocator;
34 class PoolAllocator;
35 
36 // Singleton that manages per-process state, e.g. allocation of
37 // shared resources.
38 class ProcessState : public ProcessStateInterface {
39  public:
40   static ProcessState* singleton();
41 
42   // Descriptor for memory allocation attributes, used by optional
43   // runtime correctness analysis logic.
44   struct MemDesc {
45     enum MemLoc { CPU, GPU };
46     MemLoc loc;
47     int dev_index;
48     bool gpu_registered;
49     bool nic_registered;
MemDescMemDesc50     MemDesc()
51         : loc(CPU),
52           dev_index(0),
53           gpu_registered(false),
54           nic_registered(false) {}
55     string DebugString();
56   };
57 
58   // If NUMA Allocators are desired, call this before calling any
59   // Allocator accessor.
EnableNUMA()60   void EnableNUMA() { numa_enabled_ = true; }
61 
62   // Returns what we know about the memory at ptr.
63   // If we know nothing, it's called CPU 0 with no other attributes.
64   MemDesc PtrType(const void* ptr);
65 
66   // Returns the one CPUAllocator used for the given numa_node.
67   // Treats numa_node == kNUMANoAffinity as numa_node == 0.
68   Allocator* GetCPUAllocator(int numa_node);
69 
70   // Registers alloc visitor for the CPU allocator(s).
71   // REQUIRES: must be called before GetCPUAllocator.
72   void AddCPUAllocVisitor(SubAllocator::Visitor v);
73 
74   // Registers free visitor for the CPU allocator(s).
75   // REQUIRES: must be called before GetCPUAllocator.
76   void AddCPUFreeVisitor(SubAllocator::Visitor v);
77 
78   typedef std::unordered_map<const void*, MemDesc> MDMap;
79 
80  protected:
81   ProcessState();
82   friend class GPUProcessState;
83 
84   // If these flags need to be runtime configurable consider adding
85   // them to ConfigProto.
86   static const bool FLAGS_brain_mem_reg_gpu_dma = true;
87   static const bool FLAGS_brain_gpu_record_mem_types = false;
88 
89   // Helper method for unit tests to reset the ProcessState singleton by
90   // cleaning up everything. Never use in production.
91   void TestOnlyReset();
92 
93   static ProcessState* instance_;
94   bool numa_enabled_;
95 
96   mutex mu_;
97 
98   // Indexed by numa_node.  If we want numa-specific allocators AND a
99   // non-specific allocator, maybe should index by numa_node+1.
100   std::vector<Allocator*> cpu_allocators_ GUARDED_BY(mu_);
101   std::vector<SubAllocator::Visitor> cpu_alloc_visitors_ GUARDED_BY(mu_);
102   std::vector<SubAllocator::Visitor> cpu_free_visitors_ GUARDED_BY(mu_);
103 
104   // Optional RecordingAllocators that wrap the corresponding
105   // Allocators for runtime attribute use analysis.
106   MDMap mem_desc_map_;
107   std::vector<Allocator*> cpu_al_ GUARDED_BY(mu_);
108 };
109 
110 namespace internal {
111 class RecordingAllocator : public Allocator {
112  public:
RecordingAllocator(ProcessState::MDMap * mm,Allocator * a,ProcessState::MemDesc md,mutex * mu)113   RecordingAllocator(ProcessState::MDMap* mm, Allocator* a,
114                      ProcessState::MemDesc md, mutex* mu)
115       : mm_(mm), a_(a), md_(md), mu_(mu) {}
116 
Name()117   string Name() override { return a_->Name(); }
AllocateRaw(size_t alignment,size_t num_bytes)118   void* AllocateRaw(size_t alignment, size_t num_bytes) override {
119     void* p = a_->AllocateRaw(alignment, num_bytes);
120     mutex_lock l(*mu_);
121     (*mm_)[p] = md_;
122     return p;
123   }
DeallocateRaw(void * p)124   void DeallocateRaw(void* p) override {
125     mutex_lock l(*mu_);
126     auto iter = mm_->find(p);
127     mm_->erase(iter);
128     a_->DeallocateRaw(p);
129   }
TracksAllocationSizes()130   bool TracksAllocationSizes() override { return a_->TracksAllocationSizes(); }
RequestedSize(const void * p)131   size_t RequestedSize(const void* p) override { return a_->RequestedSize(p); }
AllocatedSize(const void * p)132   size_t AllocatedSize(const void* p) override { return a_->AllocatedSize(p); }
GetStats()133   absl::optional<AllocatorStats> GetStats() override { return a_->GetStats(); }
ClearStats()134   void ClearStats() override { a_->ClearStats(); }
135   ProcessState::MDMap* mm_;  // not owned
136   Allocator* a_;             // not owned
137   ProcessState::MemDesc md_;
138   mutex* mu_;
139 };
140 }  // namespace internal
141 }  // namespace tensorflow
142 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_PROCESS_STATE_H_
143