aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/common/profiler_guardrails_unittest.cc
blob: ca9d1b23293defcfe7fcc6aeba4792eccc506c54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "src/profiling/common/profiler_guardrails.h"

#include <unistd.h>

#include <cinttypes>
#include <map>

#include "perfetto/ext/base/file_utils.h"
#include "perfetto/ext/base/optional.h"
#include "perfetto/ext/base/temp_file.h"
#include "test/gtest_and_gmock.h"

namespace perfetto {
namespace profiling {
namespace {

TEST(ProfilerCpuGuardrailsTest, Exceeded) {
  const auto clk = static_cast<unsigned long>(sysconf(_SC_CLK_TCK));
  base::TempFile f = base::TempFile::CreateUnlinked();
  constexpr const char stat[] =
      "2965981 (zsh) S 2965977 2965981 2965981 34822 2966607 4194304 6632 6697 "
      "0 0 1000000 6000000 4 1 20 0 1 0 227163466 15839232 2311 "
      "18446744073709551615 "
      "94823961161728 94823961762781 140722993535472 0 0 0 2 3686400 134295555 "
      "0 0 0 17 2 0 0 0 0 0 94823961905904 94823961935208 94823993954304 "
      "140722993543678 140722993543691 140722993543691 140722993545195 0";
  base::WriteAll(f.fd(), stat, sizeof(stat));
  ASSERT_NE(lseek(f.fd(), 0, SEEK_SET), -1);
  ProfilerCpuGuardrails gr(f.ReleaseFD());

  GuardrailConfig gc;
  gc.cpu_guardrail_sec = 5000000 / clk;
  gc.cpu_start_secs = 1000000 / clk;
  EXPECT_TRUE(gr.IsOverCpuThreshold(gc));
}

TEST(ProfilerCpuGuardrailsTest, NotExceeded) {
  const auto clk = static_cast<unsigned long>(sysconf(_SC_CLK_TCK));
  base::TempFile f = base::TempFile::CreateUnlinked();
  constexpr const char stat[] =
      "2965981 (zsh) S 2965977 2965981 2965981 34822 2966607 4194304 6632 6697 "
      "0 0 1000000 6000000 4 1 20 0 1 0 227163466 15839232 2311 "
      "18446744073709551615 "
      "94823961161728 94823961762781 140722993535472 0 0 0 2 3686400 134295555 "
      "0 0 0 17 2 0 0 0 0 0 94823961905904 94823961935208 94823993954304 "
      "140722993543678 140722993543691 140722993543691 140722993545195 0";
  base::WriteAll(f.fd(), stat, sizeof(stat));
  ASSERT_NE(lseek(f.fd(), 0, SEEK_SET), -1);
  ProfilerCpuGuardrails gr(f.ReleaseFD());

  GuardrailConfig gc;
  gc.cpu_guardrail_sec = 7000000 / clk;
  gc.cpu_start_secs = 1000000 / clk;
  EXPECT_FALSE(gr.IsOverCpuThreshold(gc));
}

TEST(ProfilerMemoryGuardrailsTest, Exceeded) {
  base::TempFile f = base::TempFile::CreateUnlinked();
  constexpr const char status[] =
      "VmPeak:\t    5432 kB\n"
      "VmSize:\t    5432 kB\n"
      "VmLck:\t       0 kB\n"
      "VmPin:\t       0 kB\n"
      "VmHWM:\t     584 kB\n"
      "VmRSS:\t     80 kB\n"
      "RssAnon:\t      68 kB\n"
      "RssFile:\t     516 kB\n"
      "RssShmem:\t       0 kB\n"
      "VmData:\t     316 kB\n"
      "VmStk:\t     132 kB\n"
      "VmExe:\t      20 kB\n"
      "VmLib:\t    1460 kB\n"
      "VmPTE:\t      44 kB\n"
      "VmSwap:\t       10 kB\n";

  base::WriteAll(f.fd(), status, sizeof(status));
  ASSERT_NE(lseek(f.fd(), 0, SEEK_SET), -1);
  ProfilerMemoryGuardrails gr(f.ReleaseFD());

  GuardrailConfig gc;
  gc.memory_guardrail_kb = 77;
  EXPECT_TRUE(gr.IsOverMemoryThreshold(gc));
}

TEST(ProfilerMemoryGuardrailsTest, NotExceeded) {
  base::TempFile f = base::TempFile::CreateUnlinked();
  constexpr const char status[] =
      "VmPeak:\t    5432 kB\n"
      "VmSize:\t    5432 kB\n"
      "VmLck:\t       0 kB\n"
      "VmPin:\t       0 kB\n"
      "VmHWM:\t     584 kB\n"
      "VmRSS:\t     80 kB\n"
      "RssAnon:\t      68 kB\n"
      "RssFile:\t     516 kB\n"
      "RssShmem:\t       0 kB\n"
      "VmData:\t     316 kB\n"
      "VmStk:\t     132 kB\n"
      "VmExe:\t      20 kB\n"
      "VmLib:\t    1460 kB\n"
      "VmPTE:\t      44 kB\n"
      "VmSwap:\t       10 kB\n";

  base::WriteAll(f.fd(), status, sizeof(status));
  ASSERT_NE(lseek(f.fd(), 0, SEEK_SET), -1);
  ProfilerMemoryGuardrails gr(f.ReleaseFD());
  GuardrailConfig gc;
  gc.memory_guardrail_kb = 100;
  EXPECT_FALSE(gr.IsOverMemoryThreshold(gc));
}

}  // namespace
}  // namespace profiling
}  // namespace perfetto