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 | |
| 17 | #include <android-base/macros.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
| 21 | #include <iostream> |
| 22 | #include "include/bpf/BpfMap.h" |
| 23 | #include "include/bpf/BpfUtils.h" |
| 24 | #include "include/libbpf_android.h" |
| 25 | |
| 26 | using ::testing::Test; |
| 27 | |
| 28 | constexpr const char tp_prog_path[] = |
| 29 | "/sys/fs/bpf/prog_bpf_load_tp_prog_tracepoint_sched_sched_switch"; |
Steven Moreland | cdeb920 | 2020-01-14 11:36:43 -0800 | [diff] [blame] | 30 | constexpr const char tp_map_path[] = "/sys/fs/bpf/map_bpf_load_tp_prog_cpu_pid_map"; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace bpf { |
| 34 | |
| 35 | class BpfLoadTest : public testing::Test { |
| 36 | protected: |
| 37 | BpfLoadTest() {} |
Maciej Żenczykowski | cd5c302 | 2020-01-26 20:17:58 -0800 | [diff] [blame] | 38 | int mProgFd; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 39 | |
| 40 | void SetUp() { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 41 | unlink(tp_prog_path); |
| 42 | unlink(tp_map_path); |
| 43 | |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 44 | bool critical = true; |
| 45 | EXPECT_EQ(android::bpf::loadProg("/system/etc/bpf/bpf_load_tp_prog.o", &critical), 0); |
| 46 | EXPECT_EQ(false, critical); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 47 | |
| 48 | mProgFd = bpf_obj_get(tp_prog_path); |
| 49 | EXPECT_GT(mProgFd, 0); |
| 50 | |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 51 | int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch"); |
| 52 | EXPECT_NE(ret, 0); |
| 53 | } |
| 54 | |
| 55 | void TearDown() { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 56 | close(mProgFd); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 57 | unlink(tp_prog_path); |
| 58 | unlink(tp_map_path); |
| 59 | } |
| 60 | |
| 61 | void checkMapNonZero() { |
| 62 | // The test program installs a tracepoint on sched:sched_switch |
| 63 | // and expects the kernel to populate a PID corresponding to CPU |
Maciej Żenczykowski | cd5c302 | 2020-01-26 20:17:58 -0800 | [diff] [blame] | 64 | android::bpf::BpfMap<uint32_t, uint32_t> m(tp_map_path); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 65 | |
| 66 | // Wait for program to run a little |
| 67 | sleep(1); |
| 68 | |
| 69 | int non_zero = 0; |
| 70 | const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val, |
| 71 | BpfMap<uint32_t, uint32_t>& map) { |
| 72 | if (val && !non_zero) { |
| 73 | non_zero = 1; |
| 74 | } |
| 75 | |
| 76 | UNUSED(key); |
| 77 | UNUSED(map); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 78 | return base::Result<void>(); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Bernie Innocenti | 43d2538 | 2020-02-10 07:25:16 +0900 | [diff] [blame] | 81 | EXPECT_RESULT_OK(m.iterateWithValue(iterFunc)); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 82 | EXPECT_EQ(non_zero, 1); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | TEST_F(BpfLoadTest, bpfCheckMap) { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 87 | checkMapNonZero(); |
| 88 | } |
| 89 | |
| 90 | } // namespace bpf |
| 91 | } // namespace android |