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 "src/profiling/memory/wire_protocol.h"
18
19 #include <sys/socket.h>
20 #include <sys/types.h>
21
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/scoped_file.h"
24 #include "perfetto/ext/base/unix_socket.h"
25 #include "test/gtest_and_gmock.h"
26
27 namespace perfetto {
28 namespace profiling {
29
30 bool operator==(const AllocMetadata& one, const AllocMetadata& other);
operator ==(const AllocMetadata & one,const AllocMetadata & other)31 bool operator==(const AllocMetadata& one, const AllocMetadata& other) {
32 return std::tie(one.sequence_number, one.alloc_size, one.sample_size,
33 one.alloc_address, one.stack_pointer,
34 one.clock_monotonic_coarse_timestamp, one.heap_id,
35 one.arch) == std::tie(other.sequence_number, other.alloc_size,
36 other.sample_size, other.alloc_address,
37 other.stack_pointer,
38 other.clock_monotonic_coarse_timestamp,
39 other.heap_id, other.arch) &&
40 memcmp(one.register_data, other.register_data, kMaxRegisterDataSize) ==
41 0;
42 }
43
44 bool operator==(const FreeEntry& one, const FreeEntry& other);
operator ==(const FreeEntry & one,const FreeEntry & other)45 bool operator==(const FreeEntry& one, const FreeEntry& other) {
46 return (std::tie(one.sequence_number, one.addr, one.heap_id) ==
47 std::tie(other.sequence_number, other.addr, other.heap_id));
48 }
49
50 namespace {
51
CopyFD(int fd)52 base::ScopedFile CopyFD(int fd) {
53 int sv[2];
54 PERFETTO_CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
55 base::UnixSocketRaw send_sock(base::ScopedFile(sv[0]),
56 base::SockFamily::kUnix,
57 base::SockType::kStream);
58 base::UnixSocketRaw recv_sock(base::ScopedFile(sv[1]),
59 base::SockFamily::kUnix,
60 base::SockType::kStream);
61 char msg[] = "a";
62 PERFETTO_CHECK(send_sock.Send(msg, sizeof(msg), &fd, 1));
63 base::ScopedFile res;
64 recv_sock.Receive(msg, sizeof(msg), &res, 1);
65 return res;
66 }
67
68 constexpr auto kShmemSize = 1048576;
69
TEST(WireProtocolTest,AllocMessage)70 TEST(WireProtocolTest, AllocMessage) {
71 char payload[] = {0x77, 0x77, 0x77, 0x00};
72 WireMessage msg = {};
73 msg.record_type = RecordType::Malloc;
74 AllocMetadata metadata = {};
75 metadata.sequence_number = 0xA1A2A3A4A5A6A7A8;
76 metadata.alloc_size = 0xB1B2B3B4B5B6B7B8;
77 metadata.alloc_address = 0xC1C2C3C4C5C6C7C8;
78 metadata.stack_pointer = 0xD1D2D3D4D5D6D7D8;
79 metadata.arch = unwindstack::ARCH_X86;
80 for (size_t i = 0; i < kMaxRegisterDataSize; ++i)
81 metadata.register_data[i] = 0x66;
82 msg.alloc_header = &metadata;
83 msg.payload = payload;
84 msg.payload_size = sizeof(payload);
85
86 auto shmem_client = SharedRingBuffer::Create(kShmemSize);
87 ASSERT_TRUE(shmem_client);
88 ASSERT_TRUE(shmem_client->is_valid());
89 auto shmem_server = SharedRingBuffer::Attach(CopyFD(shmem_client->fd()));
90
91 ASSERT_GE(SendWireMessage(&shmem_client.value(), msg), 0);
92
93 auto buf = shmem_server->BeginRead();
94 ASSERT_TRUE(buf);
95 WireMessage recv_msg;
96 ASSERT_TRUE(ReceiveWireMessage(reinterpret_cast<char*>(buf.data), buf.size,
97 &recv_msg));
98 shmem_server->EndRead(std::move(buf));
99
100 ASSERT_EQ(recv_msg.record_type, msg.record_type);
101 ASSERT_EQ(*recv_msg.alloc_header, *msg.alloc_header);
102 ASSERT_EQ(recv_msg.payload_size, msg.payload_size);
103 ASSERT_STREQ(recv_msg.payload, msg.payload);
104 }
105
TEST(WireProtocolTest,FreeMessage)106 TEST(WireProtocolTest, FreeMessage) {
107 WireMessage msg = {};
108 msg.record_type = RecordType::Free;
109 FreeEntry entry = {};
110 entry.sequence_number = 0x111111111111111;
111 entry.addr = 0x222222222222222;
112 msg.free_header = &entry;
113
114 auto shmem_client = SharedRingBuffer::Create(kShmemSize);
115 ASSERT_TRUE(shmem_client);
116 ASSERT_TRUE(shmem_client->is_valid());
117 auto shmem_server = SharedRingBuffer::Attach(CopyFD(shmem_client->fd()));
118
119 ASSERT_GE(SendWireMessage(&shmem_client.value(), msg), 0);
120
121 auto buf = shmem_server->BeginRead();
122 ASSERT_TRUE(buf);
123 WireMessage recv_msg;
124 ASSERT_TRUE(ReceiveWireMessage(reinterpret_cast<char*>(buf.data), buf.size,
125 &recv_msg));
126 shmem_server->EndRead(std::move(buf));
127
128 ASSERT_EQ(recv_msg.record_type, msg.record_type);
129 ASSERT_EQ(*recv_msg.free_header, *msg.free_header);
130 ASSERT_EQ(recv_msg.payload_size, msg.payload_size);
131 }
132
TEST(GetHeapSamplingInterval,Default)133 TEST(GetHeapSamplingInterval, Default) {
134 ClientConfiguration cli_config{};
135 cli_config.all_heaps = true;
136 cli_config.num_heaps = 0;
137 cli_config.default_interval = 4096u;
138 EXPECT_EQ(GetHeapSamplingInterval(cli_config, "something"), 4096u);
139 }
140
TEST(GetHeapSamplingInterval,Selected)141 TEST(GetHeapSamplingInterval, Selected) {
142 ClientConfiguration cli_config{};
143 cli_config.all_heaps = false;
144 cli_config.num_heaps = 1;
145 cli_config.default_interval = 1;
146 memcpy(cli_config.heaps[0].name, "something", sizeof("something"));
147 cli_config.heaps[0].interval = 4096u;
148 EXPECT_EQ(GetHeapSamplingInterval(cli_config, "something"), 4096u);
149 EXPECT_EQ(GetHeapSamplingInterval(cli_config, "else"), 0u);
150 }
151
TEST(GetHeapSamplingInterval,SelectedAndDefault)152 TEST(GetHeapSamplingInterval, SelectedAndDefault) {
153 ClientConfiguration cli_config{};
154 cli_config.all_heaps = true;
155 cli_config.num_heaps = 1;
156 cli_config.default_interval = 1;
157 memcpy(cli_config.heaps[0].name, "something", sizeof("something"));
158 cli_config.heaps[0].interval = 4096u;
159 EXPECT_EQ(GetHeapSamplingInterval(cli_config, "something"), 4096u);
160 EXPECT_EQ(GetHeapSamplingInterval(cli_config, "else"), 1u);
161 }
162
TEST(GetHeapSamplingInterval,DisabledAndDefault)163 TEST(GetHeapSamplingInterval, DisabledAndDefault) {
164 ClientConfiguration cli_config{};
165 cli_config.all_heaps = true;
166 cli_config.num_heaps = 1;
167 cli_config.default_interval = 1;
168 memcpy(cli_config.heaps[0].name, "something", sizeof("something"));
169 cli_config.heaps[0].interval = 0u;
170 EXPECT_EQ(GetHeapSamplingInterval(cli_config, "something"), 0u);
171 EXPECT_EQ(GetHeapSamplingInterval(cli_config, "else"), 1u);
172 }
173
174 } // namespace
175 } // namespace profiling
176 } // namespace perfetto
177