• 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_FRAMEWORK_TRACKING_ALLOCATOR_H_
17 #define TENSORFLOW_CORE_FRAMEWORK_TRACKING_ALLOCATOR_H_
18 
19 #include <unordered_map>
20 #include "tensorflow/core/framework/allocator.h"
21 #include "tensorflow/core/lib/core/refcount.h"
22 #include "tensorflow/core/lib/gtl/inlined_vector.h"
23 #include "tensorflow/core/platform/mutex.h"
24 #include "tensorflow/core/platform/thread_annotations.h"
25 #include "tensorflow/core/platform/types.h"
26 
27 namespace tensorflow {
28 
29 // TrackingAllocator is a wrapper for an Allocator. It keeps a running
30 // count of the number of bytes allocated through the wrapper. It is
31 // used by the Executor to "charge" allocations to particular Op
32 // executions. Each Op gets a separate TrackingAllocator wrapper
33 // around the underlying allocator.
34 //
35 // The implementation assumes the invariant that all calls to
36 // AllocateRaw by an Op (or work items spawned by the Op) will occur
37 // before the Op's Compute method returns. Thus the high watermark is
38 // established once Compute returns.
39 //
40 // DeallocateRaw can be called long after the Op has finished,
41 // e.g. when an output tensor is deallocated, and the wrapper cannot
42 // be deleted until the last of these calls has occurred.  The
43 // TrackingAllocator keeps track of outstanding calls using a
44 // reference count, and deletes itself once the last call has been
45 // received and the high watermark has been retrieved.
46 struct AllocRecord {
AllocRecordAllocRecord47   AllocRecord(int64 a_btyes, int64 a_micros)
48       : alloc_bytes(a_btyes), alloc_micros(a_micros) {}
AllocRecordAllocRecord49   AllocRecord() : AllocRecord(0, 0) {}
50 
51   int64 alloc_bytes;
52   int64 alloc_micros;
53 };
54 
55 class TrackingAllocator : public Allocator {
56  public:
57   explicit TrackingAllocator(Allocator* allocator, bool track_ids);
Name()58   string Name() override { return allocator_->Name(); }
AllocateRaw(size_t alignment,size_t num_bytes)59   void* AllocateRaw(size_t alignment, size_t num_bytes) override {
60     return AllocateRaw(alignment, num_bytes, AllocationAttributes());
61   }
62   void* AllocateRaw(size_t alignment, size_t num_bytes,
63                     const AllocationAttributes& allocation_attr) override;
64   void DeallocateRaw(void* ptr) override;
65   bool TracksAllocationSizes() override;
66   size_t RequestedSize(const void* ptr) override;
67   size_t AllocatedSize(const void* ptr) override;
68   int64 AllocationId(const void* ptr) override;
69   absl::optional<AllocatorStats> GetStats() override;
70   void ClearStats() override;
71 
72   // If the underlying allocator tracks allocation sizes, this returns
73   // a tuple where the first value is the total number of bytes
74   // allocated through this wrapper, the second value is the high
75   // watermark of bytes allocated through this wrapper and the third value is
76   // the allocated bytes through this wrapper that are still alive. If the
77   // underlying allocator does not track allocation sizes the first
78   // value is the total number of bytes requested through this wrapper
79   // and the second and the third are 0.
80   //
81   std::tuple<size_t, size_t, size_t> GetSizes();
82   // After GetRecordsAndUnRef is called, the only further calls allowed
83   // on this wrapper are calls to DeallocateRaw with pointers that
84   // were allocated by this wrapper and have not yet been
85   // deallocated. After this call completes and all allocated pointers
86   // have been deallocated the wrapper will delete itself.
87   gtl::InlinedVector<AllocRecord, 4> GetRecordsAndUnRef();
88   // Returns a copy of allocation records collected so far.
89   gtl::InlinedVector<AllocRecord, 4> GetCurrentRecords();
90 
91  protected:
~TrackingAllocator()92   ~TrackingAllocator() override {}
93 
94  private:
95   bool UnRef() EXCLUSIVE_LOCKS_REQUIRED(mu_);
96 
97   Allocator* allocator_;  // not owned.
98   mutex mu_;
99   // the number of calls to AllocateRaw that have not yet been matched
100   // by a corresponding call to DeAllocateRaw, plus 1 if the Executor
101   // has not yet read out the high watermark.
102   int ref_ GUARDED_BY(mu_);
103   // the current number of outstanding bytes that have been allocated
104   // by this wrapper, or 0 if the underlying allocator does not track
105   // allocation sizes.
106   size_t allocated_ GUARDED_BY(mu_);
107   // the maximum number of outstanding bytes that have been allocated
108   // by this wrapper, or 0 if the underlying allocator does not track
109   // allocation sizes.
110   size_t high_watermark_ GUARDED_BY(mu_);
111   // the total number of bytes that have been allocated by this
112   // wrapper if the underlying allocator tracks allocation sizes,
113   // otherwise the total number of bytes that have been requested by
114   // this allocator.
115   size_t total_bytes_ GUARDED_BY(mu_);
116 
117   gtl::InlinedVector<AllocRecord, 4> allocations_ GUARDED_BY(mu_);
118 
119   // Track allocations locally if requested in the constructor and the
120   // underlying allocator doesn't already do it for us.
121   const bool track_sizes_locally_;
122   struct Chunk {
123     size_t requested_size;
124     size_t allocated_size;
125     int64 allocation_id;
126   };
127   std::unordered_map<const void*, Chunk> in_use_ GUARDED_BY(mu_);
128   int64 next_allocation_id_ GUARDED_BY(mu_);
129 };
130 
131 }  // end namespace tensorflow
132 
133 #endif  // TENSORFLOW_CORE_FRAMEWORK_TRACKING_ALLOCATOR_H_
134