|
| 1 | +#include <catch2/catch_test_macros.hpp> |
| 2 | +#include <perfcpp/counter/config.hpp> |
| 3 | +#include <perfcpp/sample/config.hpp> |
| 4 | + |
| 5 | +TEST_CASE("Config defaults", "[Config]") |
| 6 | +{ |
| 7 | + const auto config = perf::Config{}; |
| 8 | + |
| 9 | + SECTION("boolean defaults") |
| 10 | + { |
| 11 | + REQUIRE_FALSE(config.is_include_child_threads()); |
| 12 | + REQUIRE(config.is_include_kernel()); |
| 13 | + REQUIRE(config.is_include_user()); |
| 14 | + REQUIRE(config.is_include_hypervisor()); |
| 15 | + REQUIRE(config.is_include_idle()); |
| 16 | + REQUIRE(config.is_include_guest()); |
| 17 | + REQUIRE(config.is_include_host()); |
| 18 | + REQUIRE_FALSE(config.is_pinned()); |
| 19 | + REQUIRE_FALSE(config.is_debug()); |
| 20 | + } |
| 21 | + |
| 22 | + SECTION("cpu core defaults to any") |
| 23 | + { |
| 24 | + REQUIRE(config.cpu_core() == perf::CpuCore::Any); |
| 25 | + REQUIRE(config.cpu_core().is_any()); |
| 26 | + } |
| 27 | + |
| 28 | + SECTION("process defaults to calling") |
| 29 | + { |
| 30 | + REQUIRE(config.process() == perf::Process::Calling); |
| 31 | + REQUIRE(config.process().is_calling()); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +TEST_CASE("Config setter roundtrips", "[Config]") |
| 36 | +{ |
| 37 | + auto config = perf::Config{}; |
| 38 | + |
| 39 | + SECTION("include_child_threads") |
| 40 | + { |
| 41 | + config.include_child_threads(true); |
| 42 | + REQUIRE(config.is_include_child_threads()); |
| 43 | + config.include_child_threads(false); |
| 44 | + REQUIRE_FALSE(config.is_include_child_threads()); |
| 45 | + } |
| 46 | + |
| 47 | + SECTION("include_kernel") |
| 48 | + { |
| 49 | + config.include_kernel(false); |
| 50 | + REQUIRE_FALSE(config.is_include_kernel()); |
| 51 | + } |
| 52 | + |
| 53 | + SECTION("include_user") |
| 54 | + { |
| 55 | + config.include_user(false); |
| 56 | + REQUIRE_FALSE(config.is_include_user()); |
| 57 | + } |
| 58 | + |
| 59 | + SECTION("include_hypervisor") |
| 60 | + { |
| 61 | + config.include_hypervisor(false); |
| 62 | + REQUIRE_FALSE(config.is_include_hypervisor()); |
| 63 | + } |
| 64 | + |
| 65 | + SECTION("include_idle") |
| 66 | + { |
| 67 | + config.include_idle(false); |
| 68 | + REQUIRE_FALSE(config.is_include_idle()); |
| 69 | + } |
| 70 | + |
| 71 | + SECTION("include_guest") |
| 72 | + { |
| 73 | + config.include_guest(false); |
| 74 | + REQUIRE_FALSE(config.is_include_guest()); |
| 75 | + } |
| 76 | + |
| 77 | + SECTION("include_host") |
| 78 | + { |
| 79 | + config.include_host(false); |
| 80 | + REQUIRE_FALSE(config.is_include_host()); |
| 81 | + } |
| 82 | + |
| 83 | + SECTION("pinned") |
| 84 | + { |
| 85 | + config.pinned(true); |
| 86 | + REQUIRE(config.is_pinned()); |
| 87 | + } |
| 88 | + |
| 89 | + SECTION("debug") |
| 90 | + { |
| 91 | + config.debug(true); |
| 92 | + REQUIRE(config.is_debug()); |
| 93 | + } |
| 94 | + |
| 95 | + SECTION("num_physical_counters") |
| 96 | + { |
| 97 | + config.num_physical_counters(2U); |
| 98 | + REQUIRE(config.num_physical_counters() == 2U); |
| 99 | + } |
| 100 | + |
| 101 | + SECTION("num_events_per_physical_counter") |
| 102 | + { |
| 103 | + config.num_events_per_physical_counter(8U); |
| 104 | + REQUIRE(config.num_events_per_physical_counter() == 8U); |
| 105 | + } |
| 106 | + |
| 107 | + SECTION("cpu_core from CpuCore") |
| 108 | + { |
| 109 | + config.cpu_core(perf::CpuCore{ std::uint16_t{ 5U } }); |
| 110 | + REQUIRE(config.cpu_core() == std::uint16_t{ 5U }); |
| 111 | + REQUIRE_FALSE(config.cpu_core().is_any()); |
| 112 | + } |
| 113 | + |
| 114 | + SECTION("cpu_core from integer") |
| 115 | + { |
| 116 | + config.cpu_core(3U); |
| 117 | + REQUIRE(config.cpu_core() == std::uint16_t{ 3U }); |
| 118 | + } |
| 119 | + |
| 120 | + SECTION("cpu_core revert to any") |
| 121 | + { |
| 122 | + config.cpu_core(5U); |
| 123 | + config.cpu_core(perf::CpuCore::Any); |
| 124 | + REQUIRE(config.cpu_core().is_any()); |
| 125 | + } |
| 126 | + |
| 127 | + SECTION("process from Process") |
| 128 | + { |
| 129 | + config.process(perf::Process{ 1337 }); |
| 130 | + REQUIRE(config.process() == perf::Process{ 1337 }); |
| 131 | + REQUIRE_FALSE(config.process().is_calling()); |
| 132 | + REQUIRE_FALSE(config.process().is_any()); |
| 133 | + } |
| 134 | + |
| 135 | + SECTION("process from pid") |
| 136 | + { |
| 137 | + config.process(static_cast<pid_t>(42)); |
| 138 | + REQUIRE(config.process() == 42); |
| 139 | + } |
| 140 | + |
| 141 | + SECTION("process revert to any") |
| 142 | + { |
| 143 | + config.process(perf::Process{ 1337 }); |
| 144 | + config.process(perf::Process::Any); |
| 145 | + REQUIRE(config.process().is_any()); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +TEST_CASE("Config with explicit counter limits", "[Config]") |
| 150 | +{ |
| 151 | + const auto config = perf::Config{ 2U, 1U }; |
| 152 | + |
| 153 | + REQUIRE(config.num_physical_counters() == 2U); |
| 154 | + REQUIRE(config.num_events_per_physical_counter() == 1U); |
| 155 | +} |
| 156 | + |
| 157 | +TEST_CASE("Process and CpuCore", "[Config]") |
| 158 | +{ |
| 159 | + SECTION("Process statics are distinct") |
| 160 | + { |
| 161 | + REQUIRE_FALSE(perf::Process::Any == perf::Process::Calling); |
| 162 | + REQUIRE(perf::Process::Any.is_any()); |
| 163 | + REQUIRE_FALSE(perf::Process::Any.is_calling()); |
| 164 | + REQUIRE(perf::Process::Calling.is_calling()); |
| 165 | + REQUIRE_FALSE(perf::Process::Calling.is_any()); |
| 166 | + } |
| 167 | + |
| 168 | + SECTION("Process from pid") |
| 169 | + { |
| 170 | + const auto process = perf::Process{ 1234 }; |
| 171 | + REQUIRE(process == 1234); |
| 172 | + REQUIRE_FALSE(process.is_any()); |
| 173 | + REQUIRE_FALSE(process.is_calling()); |
| 174 | + } |
| 175 | + |
| 176 | + SECTION("CpuCore any") |
| 177 | + { |
| 178 | + REQUIRE(perf::CpuCore::Any.is_any()); |
| 179 | + } |
| 180 | + |
| 181 | + SECTION("CpuCore from id") |
| 182 | + { |
| 183 | + const auto core = perf::CpuCore{ std::uint16_t{ 7U } }; |
| 184 | + REQUIRE(core == std::uint16_t{ 7U }); |
| 185 | + REQUIRE_FALSE(core.is_any()); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +TEST_CASE("SampleConfig defaults", "[SampleConfig]") |
| 190 | +{ |
| 191 | + const auto config = perf::SampleConfig{}; |
| 192 | + |
| 193 | + SECTION("inherits Config defaults") |
| 194 | + { |
| 195 | + REQUIRE(config.is_include_kernel()); |
| 196 | + REQUIRE(config.is_include_user()); |
| 197 | + REQUIRE_FALSE(config.is_debug()); |
| 198 | + } |
| 199 | + |
| 200 | + SECTION("buffer pages") |
| 201 | + { |
| 202 | + REQUIRE(config.buffer_pages() == 4097U); |
| 203 | + } |
| 204 | + |
| 205 | + SECTION("precision") |
| 206 | + { |
| 207 | + REQUIRE(config.precise_ip() == perf::Precision::MustHaveConstantSkid); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +TEST_CASE("SampleConfig setter roundtrips", "[SampleConfig]") |
| 212 | +{ |
| 213 | + auto config = perf::SampleConfig{}; |
| 214 | + |
| 215 | + SECTION("period") |
| 216 | + { |
| 217 | + config.period(50000U); |
| 218 | + REQUIRE(std::holds_alternative<perf::Period>(config.period_or_frequency())); |
| 219 | + REQUIRE(std::get<perf::Period>(config.period_or_frequency()).get() == 50000U); |
| 220 | + } |
| 221 | + |
| 222 | + SECTION("frequency") |
| 223 | + { |
| 224 | + config.frequency(1000U); |
| 225 | + REQUIRE(std::holds_alternative<perf::Frequency>(config.period_or_frequency())); |
| 226 | + REQUIRE(std::get<perf::Frequency>(config.period_or_frequency()).get() == 1000U); |
| 227 | + } |
| 228 | + |
| 229 | + SECTION("period and frequency are mutually exclusive") |
| 230 | + { |
| 231 | + config.period(50000U); |
| 232 | + config.frequency(1000U); |
| 233 | + REQUIRE(std::holds_alternative<perf::Frequency>(config.period_or_frequency())); |
| 234 | + } |
| 235 | + |
| 236 | + SECTION("precision") |
| 237 | + { |
| 238 | + config.precision(perf::Precision::RequestZeroSkid); |
| 239 | + REQUIRE(config.precise_ip() == perf::Precision::RequestZeroSkid); |
| 240 | + } |
| 241 | + |
| 242 | + SECTION("buffer_pages") |
| 243 | + { |
| 244 | + config.buffer_pages(8193U); |
| 245 | + REQUIRE(config.buffer_pages() == 8193U); |
| 246 | + } |
| 247 | +} |
0 commit comments