1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
24namespace tsl {
25namespace port {
26
27// Aligned allocation/deallocation. `minimum_alignment` must be a power of 2
28// and a multiple of sizeof(void*).
29void* AlignedMalloc(size_t size, int minimum_alignment);
30void AlignedFree(void* aligned_memory);
31
32void* Malloc(size_t size);
33void* Realloc(void* ptr, size_t size);
34void 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.
43void 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.
60std::size_t MallocExtension_GetAllocatedSize(const void* p);
61
62struct MemoryInfo {
63 int64_t total = 0;
64 int64_t free = 0;
65};
66
67struct 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.
74MemoryInfo 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.
78MemoryBandwidthInfo GetMemoryBandwidthInfo();
79
80// Returns the amount of RAM available in bytes, or INT64_MAX if unknown.
81static inline int64_t AvailableRam() { return GetMemoryInfo().free; }
82
83} // namespace port
84} // namespace tsl
85
86#endif // TENSORFLOW_TSL_PLATFORM_MEM_H_
87