File: required_resources.cpp

package info (click to toggle)
bpftrace 0.24.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,496 kB
  • sloc: cpp: 60,982; ansic: 10,952; python: 953; yacc: 665; sh: 536; lex: 295; makefile: 22
file content (166 lines) | stat: -rw-r--r-- 4,508 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <sstream>

#include "format_string.h"
#include "required_resources.h"
#include "struct.h"
#include "types.h"
#include "gtest/gtest.h"

namespace bpftrace::test {

// ========================================================================
// It's a bit overkill to completely test every field in `RequiredResources`
// so for these tests we opt to get coverage on every type we serialize.
// ========================================================================

TEST(required_resources, round_trip_simple)
{
  std::ostringstream serialized(std::ios::binary);
  {
    RequiredResources r;
    r.probe_ids.emplace_back("itsastring");
    r.save_state(serialized);
  }

  std::istringstream input(serialized.str());
  {
    RequiredResources r;
    r.load_state(input);
    ASSERT_EQ(r.probe_ids.size(), 1UL);
    EXPECT_EQ(r.probe_ids[0], "itsastring");
  }
}

TEST(required_resources, round_trip_field_sized_type)
{
  std::ostringstream serialized(std::ios::binary);
  {
    RequiredResources r;
    r.system_args.emplace_back(FormatString("field0"),
                               std::vector{ Field{
                                   .name = "myfield",
                                   .type = CreateInt32(),
                                   .offset = 123,
                                   .bitfield = Bitfield(1, 2, 0xFF),
                               } });
    r.save_state(serialized);
  }

  std::istringstream input(serialized.str());
  {
    RequiredResources r;
    r.load_state(input);

    ASSERT_EQ(r.system_args.size(), 1UL);
    EXPECT_EQ(std::get<0>(r.system_args[0]).str(), "field0");

    auto &fields = std::get<1>(r.system_args[0]);
    ASSERT_EQ(fields.size(), 1UL);
    auto &field = fields[0];
    EXPECT_EQ(field.name, "myfield");
    EXPECT_TRUE(field.type.IsIntTy());
    EXPECT_EQ(field.type.GetSize(), 4UL);
    EXPECT_EQ(field.offset, 123);
    // clang-tidy does not recognize ASSERT_*() terminates testcase
    // NOLINTBEGIN(bugprone-unchecked-optional-access)
    ASSERT_TRUE(field.bitfield.has_value());
    EXPECT_EQ(field.bitfield->read_bytes, 1UL);
    EXPECT_EQ(field.bitfield->access_rshift, 2UL);
    EXPECT_EQ(field.bitfield->mask, 0xFFUL);
    // NOLINTEND(bugprone-unchecked-optional-access)
  }
}

TEST(required_resources, round_trip_map_info)
{
  std::ostringstream serialized(std::ios::binary);
  {
    MapInfo info{
      .key_type = CreateNone(),
      .value_type = CreateInet(3),
      .detail =
          LinearHistogramArgs{
              .min = 99,
              .max = 123,
              .step = 33,
          },
    };
    info.key_type = CreateInt32();
    RequiredResources r;
    r.maps_info.insert({ "mymap", info });
    r.save_state(serialized);
  }

  std::istringstream input(serialized.str());
  {
    RequiredResources r;
    r.load_state(input);

    ASSERT_EQ(r.maps_info.count("mymap"), 1UL);
    const auto &map_info = r.maps_info["mymap"];

    EXPECT_TRUE(map_info.value_type.IsInetTy());
    EXPECT_EQ(map_info.value_type.GetSize(), 3UL);

    EXPECT_TRUE(map_info.key_type.IsIntegerTy());
    EXPECT_EQ(map_info.key_type.GetSize(), 4);

    const auto &lhist_args = std::get<LinearHistogramArgs>(map_info.detail);
    EXPECT_EQ(lhist_args.min, 99);
    EXPECT_EQ(lhist_args.max, 123);
    EXPECT_EQ(lhist_args.step, 33);
  }
}

TEST(required_resources, round_trip_probes)
{
  std::ostringstream serialized(std::ios::binary);
  {
    RequiredResources r;

    Probe p;
    p.type = ProbeType::hardware;
    p.path = "mypath";
    p.index = 3;
    r.special_probes["test"] = std::move(p);

    r.save_state(serialized);
  }

  std::istringstream input(serialized.str());
  {
    RequiredResources r;
    r.load_state(input);

    ASSERT_EQ(r.special_probes.size(), 1UL);
    auto &probe = r.special_probes["test"];
    EXPECT_EQ(probe.type, ProbeType::hardware);
    EXPECT_EQ(probe.path, "mypath");
    EXPECT_EQ(probe.index, 3);
  }
}

TEST(required_resources, round_trip_multiple_members)
{
  std::ostringstream serialized(std::ios::binary);
  {
    RequiredResources r;
    r.join_args.emplace_back("joinarg0");
    r.time_args.emplace_back("timearg0");
    r.save_state(serialized);
  }

  std::istringstream input(serialized.str());
  {
    RequiredResources r;
    r.load_state(input);

    ASSERT_EQ(r.join_args.size(), 1UL);
    EXPECT_EQ(r.join_args[0], "joinarg0");
    ASSERT_EQ(r.time_args.size(), 1UL);
    EXPECT_EQ(r.time_args[0], "timearg0");
  }
}

} // namespace bpftrace::test