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 size_t 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 #if defined(__GLIBC__) && __GLIBC_MINOR__ >= 33 44 const auto mem = mallinfo2(); 45 #else 46 const auto mem = mallinfo(); 47 #endif 48 result.total_allocated_bytes = mem.arena; 49 result.in_use_allocated_bytes = mem.uordblks; 50 #endif 51 return result; 52 } 53 AllStatsToStream(std::ostream * stream) const54void MemoryUsage::AllStatsToStream(std::ostream* stream) const { 55 *stream << "max resident set size = " << max_rss_kb / 1024.0 56 << " MB, total malloc-ed size = " 57 << total_allocated_bytes / 1024.0 / 1024.0 58 << " MB, in-use allocated/mmapped size = " 59 << in_use_allocated_bytes / 1024.0 / 1024.0 << " MB"; 60 } 61 62 } // namespace memory 63 } // namespace profiling 64 } // namespace tflite 65