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_TSL_PLATFORM_MEM_H_ |
17 | #define TENSORFLOW_TSL_PLATFORM_MEM_H_ |
18 | |
19 | // TODO(cwhipkey): remove this when callers use annotations directly. |
20 | #include "tensorflow/tsl/platform/dynamic_annotations.h" |
21 | #include "tensorflow/tsl/platform/platform.h" |
22 | #include "tensorflow/tsl/platform/types.h" |
23 | |
24 | namespace tsl { |
25 | namespace port { |
26 | |
27 | // Aligned allocation/deallocation. `minimum_alignment` must be a power of 2 |
28 | // and a multiple of sizeof(void*). |
29 | void* AlignedMalloc(size_t size, int minimum_alignment); |
30 | void AlignedFree(void* aligned_memory); |
31 | |
32 | void* Malloc(size_t size); |
33 | void* Realloc(void* ptr, size_t size); |
34 | void Free(void* ptr); |
35 | |
36 | // Tries to release num_bytes of free memory back to the operating |
37 | // system for reuse. Use this routine with caution -- to get this |
38 | // memory back may require faulting pages back in by the OS, and |
39 | // that may be slow. |
40 | // |
41 | // Currently, if a malloc implementation does not support this |
42 | // routine, this routine is a no-op. |
43 | void MallocExtension_ReleaseToSystem(std::size_t num_bytes); |
44 | |
45 | // Returns the actual number N of bytes reserved by the malloc for the |
46 | // pointer p. This number may be equal to or greater than the number |
47 | // of bytes requested when p was allocated. |
48 | // |
49 | // This routine is just useful for statistics collection. The |
50 | // client must *not* read or write from the extra bytes that are |
51 | // indicated by this call. |
52 | // |
53 | // Example, suppose the client gets memory by calling |
54 | // p = malloc(10) |
55 | // and GetAllocatedSize(p) may return 16. The client must only use the |
56 | // first 10 bytes p[0..9], and not attempt to read or write p[10..15]. |
57 | // |
58 | // Currently, if a malloc implementation does not support this |
59 | // routine, this routine returns 0. |
60 | std::size_t MallocExtension_GetAllocatedSize(const void* p); |
61 | |
62 | struct MemoryInfo { |
63 | int64_t total = 0; |
64 | int64_t free = 0; |
65 | }; |
66 | |
67 | struct MemoryBandwidthInfo { |
68 | int64_t bw_used = 0; // memory bandwidth used across all CPU (in MBs/second) |
69 | }; |
70 | |
71 | // Retrieves the host memory information. If any of the fields in the returned |
72 | // MemoryInfo structure is INT64_MAX, it means such information is not |
73 | // available. |
74 | MemoryInfo GetMemoryInfo(); |
75 | |
76 | // Retrieves the host memory bandwidth information. If any field in the returned |
77 | // structure is INT64_MAX, it means such information is not available. |
78 | MemoryBandwidthInfo GetMemoryBandwidthInfo(); |
79 | |
80 | // Returns the amount of RAM available in bytes, or INT64_MAX if unknown. |
81 | static inline int64_t AvailableRam() { return GetMemoryInfo().free; } |
82 | |
83 | } // namespace port |
84 | } // namespace tsl |
85 | |
86 | #endif // TENSORFLOW_TSL_PLATFORM_MEM_H_ |
87 | |