Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 17 | #include <android-base/file.h> |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 18 | #include <android-base/macros.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
| 22 | #include <iostream> |
Ken Chen | b1d4888 | 2021-10-25 21:11:22 +0800 | [diff] [blame] | 23 | #include "bpf/BpfMap.h" |
| 24 | #include "bpf/BpfUtils.h" |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 25 | #include "include/libbpf_android.h" |
| 26 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 27 | using ::testing::TestWithParam;; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace bpf { |
| 31 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 32 | class BpfLoadTest : public TestWithParam<std::string> { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 33 | protected: |
| 34 | BpfLoadTest() {} |
Maciej Żenczykowski | cd5c302 | 2020-01-26 20:17:58 -0800 | [diff] [blame] | 35 | int mProgFd; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 36 | std::string mTpProgPath; |
| 37 | std::string mTpMapPath;; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 38 | |
| 39 | void SetUp() { |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 40 | auto progName = android::base::Basename(GetParam()); |
| 41 | progName = progName.substr(0, progName.find_last_of('.')); |
| 42 | mTpProgPath = "/sys/fs/bpf/prog_" + progName + "_tracepoint_sched_sched_switch"; |
| 43 | unlink(mTpProgPath.c_str()); |
| 44 | |
| 45 | mTpMapPath = "/sys/fs/bpf/map_" + progName + "_cpu_pid_map"; |
| 46 | unlink(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 47 | |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 48 | bool critical = true; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 49 | EXPECT_EQ(android::bpf::loadProg(GetParam().c_str(), &critical), 0); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 50 | EXPECT_EQ(false, critical); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 51 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 52 | mProgFd = bpf_obj_get(mTpProgPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 53 | EXPECT_GT(mProgFd, 0); |
| 54 | |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 55 | int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch"); |
| 56 | EXPECT_NE(ret, 0); |
| 57 | } |
| 58 | |
| 59 | void TearDown() { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 60 | close(mProgFd); |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 61 | unlink(mTpProgPath.c_str()); |
| 62 | unlink(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void checkMapNonZero() { |
| 66 | // The test program installs a tracepoint on sched:sched_switch |
| 67 | // and expects the kernel to populate a PID corresponding to CPU |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 68 | android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 69 | |
| 70 | // Wait for program to run a little |
| 71 | sleep(1); |
| 72 | |
| 73 | int non_zero = 0; |
| 74 | const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val, |
| 75 | BpfMap<uint32_t, uint32_t>& map) { |
| 76 | if (val && !non_zero) { |
| 77 | non_zero = 1; |
| 78 | } |
| 79 | |
| 80 | UNUSED(key); |
| 81 | UNUSED(map); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 82 | return base::Result<void>(); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
Bernie Innocenti | 43d2538 | 2020-02-10 07:25:16 +0900 | [diff] [blame] | 85 | EXPECT_RESULT_OK(m.iterateWithValue(iterFunc)); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 86 | EXPECT_EQ(non_zero, 1); |
| 87 | } |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 88 | |
| 89 | void checkMapBtf() { |
| 90 | // Earlier kernels lack BPF_BTF_LOAD support |
| 91 | if (!isAtLeastKernelVersion(4, 19, 0)) GTEST_SKIP() << "pre-4.19 kernel does not support BTF"; |
| 92 | |
| 93 | const bool haveBtf = GetParam().find("btf") != std::string::npos; |
| 94 | |
| 95 | std::string str; |
| 96 | EXPECT_EQ(android::base::ReadFileToString(mTpMapPath, &str), haveBtf); |
| 97 | if (haveBtf) EXPECT_FALSE(str.empty()); |
| 98 | } |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 101 | INSTANTIATE_TEST_SUITE_P(BpfLoadTests, BpfLoadTest, |
| 102 | ::testing::Values("/system/etc/bpf/bpf_load_tp_prog.o")); |
| 103 | |
| 104 | TEST_P(BpfLoadTest, bpfCheckMap) { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 105 | checkMapNonZero(); |
| 106 | } |
| 107 | |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 108 | TEST_P(BpfLoadTest, bpfCheckBtf) { |
| 109 | checkMapBtf(); |
| 110 | } |
| 111 | |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 112 | } // namespace bpf |
| 113 | } // namespace android |