• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
18 
19 namespace tflite {
20 namespace profiling {
21 namespace memory {
22 
TEST(MemoryUsage,AddAndSub)23 TEST(MemoryUsage, AddAndSub) {
24   MemoryUsage mem1, mem2;
25   mem1.max_rss_kb = 5;
26   mem1.total_allocated_bytes = 7000;
27   mem1.in_use_allocated_bytes = 2000;
28 
29   mem2.max_rss_kb = 3;
30   mem2.total_allocated_bytes = 7000;
31   mem2.in_use_allocated_bytes = 4000;
32 
33   const auto add_mem = mem1 + mem2;
34   EXPECT_EQ(8, add_mem.max_rss_kb);
35   EXPECT_EQ(14000, add_mem.total_allocated_bytes);
36   EXPECT_EQ(6000, add_mem.in_use_allocated_bytes);
37 
38   const auto sub_mem = mem1 - mem2;
39   EXPECT_EQ(2, sub_mem.max_rss_kb);
40   EXPECT_EQ(0, sub_mem.total_allocated_bytes);
41   EXPECT_EQ(-2000, sub_mem.in_use_allocated_bytes);
42 }
43 
TEST(MemoryUsage,GetMemoryUsage)44 TEST(MemoryUsage, GetMemoryUsage) {
45   MemoryUsage result;
46   EXPECT_EQ(MemoryUsage::kValueNotSet, result.max_rss_kb);
47   EXPECT_EQ(MemoryUsage::kValueNotSet, result.total_allocated_bytes);
48   EXPECT_EQ(MemoryUsage::kValueNotSet, result.in_use_allocated_bytes);
49 
50 #ifdef __linux__
51   // Just allocate some space in heap so that we could meaningful memory usage
52   // report.
53   std::unique_ptr<int[]> int_array(new int[1204]);
54   for (int i = 0; i < 1024; ++i) int_array[i] = i;
55   result = GetMemoryUsage();
56 
57   // As the getrusage call may fail, we might not be able to get max_rss_kb.
58   EXPECT_NE(MemoryUsage::kValueNotSet, result.total_allocated_bytes);
59 #endif
60 }
61 
TEST(MemoryUsage,IsSupported)62 TEST(MemoryUsage, IsSupported) {
63 #ifdef __linux__
64   EXPECT_TRUE(MemoryUsage::IsSupported());
65 #else
66   EXPECT_FALSE(MemoryUsage::IsSupported());
67 #endif
68 }
69 
70 }  // namespace memory
71 }  // namespace profiling
72 }  // namespace tflite
73