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 #include "tensorflow/lite/profiling/memory_info.h" 16 17 #ifdef __linux__ 18 #include <malloc.h> 19 #include <sys/resource.h> 20 #include <sys/time.h> 21 #endif 22 23 namespace tflite { 24 namespace profiling { 25 namespace memory { 26 27 const int MemoryUsage::kValueNotSet = 0; 28 IsSupported()29bool MemoryUsage::IsSupported() { 30 #ifdef __linux__ 31 return true; 32 #endif 33 return false; 34 } 35 GetMemoryUsage()36MemoryUsage GetMemoryUsage() { 37 MemoryUsage result; 38 #ifdef __linux__ 39 rusage res; 40 if (getrusage(RUSAGE_SELF, &res) == 0) { 41 result.max_rss_kb = res.ru_maxrss; 42 } 43 const auto mem = mallinfo(); 44 result.total_allocated_bytes = mem.arena; 45 result.in_use_allocated_bytes = mem.uordblks; 46 #endif 47 return result; 48 } 49 AllStatsToStream(std::ostream * stream) const50void MemoryUsage::AllStatsToStream(std::ostream* stream) const { 51 *stream << "max resident set size = " << max_rss_kb / 1024.0 52 << " MB, total malloc-ed size = " 53 << total_allocated_bytes / 1024.0 / 1024.0 54 << " MB, in-use allocated/mmapped size = " 55 << in_use_allocated_bytes / 1024.0 / 1024.0 << " MB"; 56 } 57 58 } // namespace memory 59 } // namespace profiling 60 } // namespace tflite 61