• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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_PROFILER_UTILS_BUFFER_POOL_H_
17 #define TENSORFLOW_CORE_PROFILER_UTILS_BUFFER_POOL_H_
18 
19 #include <vector>
20 
21 #include "tensorflow/core/platform/mutex.h"
22 #include "tensorflow/core/platform/thread_annotations.h"
23 
24 namespace tensorflow {
25 namespace profiler {
26 
27 // A lightweight buffer management class for tracking fixed sized buffers that
28 // can be reused. ReusableBuffers only manages buffers that have been
29 // reclaimed (i.e. relinquished by client).
30 // This class is thread-safe.
31 class BufferPool {
32  public:
33   // Allocated buffers will be of a fixed size specified during initialization.
34   explicit BufferPool(size_t buffer_size_in_bytes);
35 
36   ~BufferPool();
37 
38   // Returns a previously reclaimed buffer for use. If there are no buffers
39   // being managed, this allocates and returns 8B aligned buffers of size
40   // `buffer_size_in_bytes_`. The content of returned buffers is undefined.
41   uint8_t* GetOrCreateBuffer();
42 
43   // Reclaims exclusive ownership of a buffer. Clients must pass in a buffer
44   // that was obtained from `GetOrCreateBuffer()`.
45   void ReclaimBuffer(uint8_t* buffer);
46 
47   // Frees all relinquished buffers from memory.
48   void DestroyAllBuffers();
49 
50   // Gets size of a single buffer in bytes.
51   size_t GetBufferSizeInBytes() const;
52 
53  protected:
54   mutex buffers_mutex_;
55   std::vector<uint8_t*> buffers_ TF_GUARDED_BY(buffers_mutex_);
56   size_t buffer_size_in_bytes_;
57 };
58 
59 }  // namespace profiler
60 }  // namespace tensorflow
61 
62 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_BUFFER_POOL_H_
63