1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_PROFILING_MEMORY_INFO_H_ 16 #define TENSORFLOW_LITE_PROFILING_MEMORY_INFO_H_ 17 18 #include <cstdint> 19 #include <sstream> 20 21 namespace tflite { 22 namespace profiling { 23 namespace memory { 24 25 struct MemoryUsage { 26 static const int kValueNotSet; 27 28 // Indicates whether obtaining memory usage is supported on the platform, thus 29 // indicating whether the values defined in this struct make sense or not. 30 static bool IsSupported(); 31 MemoryUsageMemoryUsage32 MemoryUsage() 33 : max_rss_kb(kValueNotSet), 34 total_allocated_bytes(kValueNotSet), 35 in_use_allocated_bytes(kValueNotSet) {} 36 37 // The maximum memory size (in kilobytes) occupied by an OS process that is 38 // held in main memory (RAM). Such memory usage information is generally 39 // referred as resident set size (rss). This is an alias to rusage::ru_maxrss. 40 int64_t max_rss_kb; 41 42 // Total non-mmapped space allocated from system in bytes. This is an alias to 43 // mallinfo::arena. 44 int total_allocated_bytes; 45 46 // Total allocated (including mmapped) bytes that's in use (i.e. excluding 47 // those are freed). This is an alias to mallinfo::uordblks. 48 int in_use_allocated_bytes; 49 50 MemoryUsage operator+(MemoryUsage const& obj) const { 51 MemoryUsage res; 52 res.max_rss_kb = max_rss_kb + obj.max_rss_kb; 53 res.total_allocated_bytes = 54 total_allocated_bytes + obj.total_allocated_bytes; 55 res.in_use_allocated_bytes = 56 in_use_allocated_bytes + obj.in_use_allocated_bytes; 57 return res; 58 } 59 60 MemoryUsage operator-(MemoryUsage const& obj) const { 61 MemoryUsage res; 62 res.max_rss_kb = max_rss_kb - obj.max_rss_kb; 63 res.total_allocated_bytes = 64 total_allocated_bytes - obj.total_allocated_bytes; 65 res.in_use_allocated_bytes = 66 in_use_allocated_bytes - obj.in_use_allocated_bytes; 67 return res; 68 } 69 70 void AllStatsToStream(std::ostream* stream) const; 71 72 friend std::ostream& operator<<(std::ostream& stream, 73 const MemoryUsage& obj) { 74 obj.AllStatsToStream(&stream); 75 return stream; 76 } 77 }; 78 79 // Return the memory usage from the system. 80 // Note: this currently only works on Linux-based systems. Support on other 81 // systems will be added later. 82 MemoryUsage GetMemoryUsage(); 83 84 } // namespace memory 85 } // namespace profiling 86 } // namespace tflite 87 88 #endif // TENSORFLOW_LITE_PROFILING_MEMORY_INFO_H_ 89