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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "inplace_sampler_lib.h"
#include <inttypes.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/ucontext.h>
#include <unistd.h>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <backtrace/Backtrace.h>
#define LOG_TAG "InplaceSampler"
#include <log/log.h>
#include "environment.h"
#include "UnixSocket.h"
#include "utils.h"
#define DEFAULT_SIGNO SIGRTMAX
static constexpr int DEFAULT_SAMPLE_FREQ = 4000;
static constexpr int CHECK_THREADS_INTERVAL_IN_MS = 200;
namespace {
struct ThreadInfo {
std::string name;
};
// SampleManager controls the whole sampling process:
// Read commands from simpleperf
// Set up timers to send signals for each profiled thread regularly.
// Send thread info and map info to simpleperf.
class SampleManager {
public:
SampleManager(std::unique_ptr<UnixSocketConnection> conn) : conn_(std::move(conn)),
tid_(gettid()), signo_(DEFAULT_SIGNO), sample_freq_(DEFAULT_SAMPLE_FREQ),
sample_period_in_ns_(0), dump_callchain_(false), monitor_all_threads_(true) {
}
void Run();
private:
bool HandleMessage(const UnixSocketMessage& msg);
bool ParseStartProfilingMessage(const UnixSocketMessage& msg);
bool SendStartProfilingReplyMessage(bool ok);
bool StartProfiling();
bool InstallSignalHandler();
bool CheckThreads();
bool CheckThreadNameChange(uint64_t timestamp);
bool CheckMapChange(uint64_t timestamp);
void SendThreadMapInfo();
void SendFakeSampleRecord();
std::unique_ptr<UnixSocketConnection> conn_;
int tid_;
int signo_;
uint32_t sample_freq_;
uint32_t sample_period_in_ns_;
bool dump_callchain_;
bool monitor_all_threads_;
std::set<int> monitor_tid_filter_;
std::map<int, ThreadInfo> threads_;
std::map<uint64_t, ThreadMmap> maps_;
std::queue<std::unique_ptr<char[]>> thread_map_info_q_;
IOEventLoop loop_;
};
void SampleManager::Run() {
auto read_callback = [&](const UnixSocketMessage& msg) {
return HandleMessage(msg);
};
auto close_callback = [&]() {
return loop_.ExitLoop();
};
if (!conn_->PrepareForIO(loop_, read_callback, close_callback)) {
return;
}
loop_.RunLoop();
}
bool SampleManager::HandleMessage(const UnixSocketMessage& msg) {
if (msg.type == START_PROFILING) {
if (!ParseStartProfilingMessage(msg)) {
if (!SendStartProfilingReplyMessage(false)) {
return false;
}
return conn_->NoMoreMessage();
}
if (!SendStartProfilingReplyMessage(true)) {
return false;
}
return StartProfiling();
}
if (msg.type == END_PROFILING) {
// Close connection after clearing send buffer.
return conn_->NoMoreMessage();
}
LOG(ERROR) << "Unexpected msg type: " << msg.type;
return false;
}
bool SampleManager::ParseStartProfilingMessage(const UnixSocketMessage& msg) {
char* option = const_cast<char*>(msg.data);
while (option != nullptr && *option != '\0') {
char* next_option = strchr(option, ' ');
if (next_option != nullptr) {
*next_option++ = '\0';
}
char* equal_op = strchr(option, '=');
if (equal_op != nullptr) {
char* key = option;
*equal_op = '\0';
char* value = equal_op + 1;
if (strcmp(key, "freq") == 0) {
sample_freq_ = atoi(value);
} else if (strcmp(key, "signal") == 0) {
signo_ = atoi(value);
} else if (strcmp(key, "tids") == 0) {
monitor_all_threads_ = false;
while (*value != '\0') {
int tid = static_cast<int>(strtol(value, &value, 10));
monitor_tid_filter_.insert(tid);
if (*value == ',') {
++value;
}
}
} else if (strcmp(key, "dump_callchain") == 0) {
dump_callchain_ = (strcmp(value, "1") == 0);
}
}
option = next_option;
}
if (sample_freq_ == 0 || sample_freq_ > 1000000000) {
LOG(ERROR) << "Unexpected sample_freq: " << sample_freq_;
return false;
}
if (sample_freq_ == 1) {
sample_period_in_ns_ = 999999999;
} else {
sample_period_in_ns_ = 1000000000 / sample_freq_;
}
return true;
}
bool SampleManager::SendStartProfilingReplyMessage(bool ok) {
const char* s = ok ? "ok" : "error";
size_t size = sizeof(UnixSocketMessage) + strlen(s) + 1;
std::unique_ptr<char[]> data(new char[size]);
UnixSocketMessage* msg = reinterpret_cast<UnixSocketMessage*>(data.get());
msg->len = size;
msg->type = START_PROFILING_REPLY;
strcpy(msg->data, s);
return conn_->SendMessage(*msg, true);
}
bool SampleManager::StartProfiling() {
if (!InstallSignalHandler()) {
return false;
}
if (!CheckThreads()) {
return false;
}
timeval tv;
tv.tv_sec = CHECK_THREADS_INTERVAL_IN_MS / 1000;
tv.tv_usec = CHECK_THREADS_INTERVAL_IN_MS % 1000 * 1000;
return loop_.AddPeriodicEvent(tv, [&]() {
return CheckThreads();
});
}
bool SampleManager::InstallSignalHandler() {
return true;
}
bool SampleManager::CheckThreads() {
uint64_t timestamp = GetSystemClock();
if (!CheckMapChange(timestamp)) {
return false;
}
if (!CheckThreadNameChange(timestamp)) {
return false;
}
SendThreadMapInfo();
// For testing.
SendFakeSampleRecord();
return true;
}
bool SampleManager::CheckThreadNameChange(uint64_t timestamp) {
std::vector<pid_t> tids = GetThreadsInProcess(getpid());
std::map<pid_t, std::string> current;
for (auto& tid : tids) {
if (tid == tid_) {
// Skip sample thread.
continue;
}
if (monitor_all_threads_ || monitor_tid_filter_.find(tid) != monitor_tid_filter_.end()) {
std::string name;
if (GetThreadName(tid, &name)) {
current[tid] = name;
}
}
}
// Check new threads or threads with new names.
for (auto& pair : current) {
pid_t tid = pair.first;
auto it = threads_.find(tid);
if (it == threads_.end() || it->second.name != pair.second) {
threads_[tid].name = pair.second;
size_t size = sizeof(UnixSocketMessage) + sizeof(uint64_t) + sizeof(uint32_t) +
pair.second.size() + 1;
std::unique_ptr<char[]> data(new char[size]);
UnixSocketMessage* msg = reinterpret_cast<UnixSocketMessage*>(data.get());
msg->len = size;
msg->type = THREAD_INFO;
char* p = msg->data;
MoveToBinaryFormat(timestamp, p);
MoveToBinaryFormat(static_cast<uint32_t>(tid), p);
MoveToBinaryFormat(pair.second.c_str(), pair.second.size() + 1, p);
thread_map_info_q_.push(std::move(data));
}
}
// Check deleted threads.
for (auto it = threads_.begin(); it != threads_.end();) {
int tid = it->first;
if (current.find(tid) == current.end()) {
it = threads_.erase(it);
} else {
++it;
}
}
return true;
}
bool SampleManager::CheckMapChange(uint64_t timestamp) {
std::vector<ThreadMmap> maps;
if (!GetThreadMmapsInProcess(getpid(), &maps)) {
return false;
}
// Check new maps or changed maps.
for (auto& map : maps) {
if (!(map.prot & PROT_EXEC)) {
continue;
}
auto it = maps_.find(map.start_addr);
if (it == maps_.end() || it->second.len != map.len || it->second.pgoff != map.pgoff ||
it->second.name != map.name) {
maps_[map.start_addr] = map;
size_t size = sizeof(UnixSocketMessage) + sizeof(uint64_t) * 4 + map.name.size() + 1;
std::unique_ptr<char[]> data(new char[size]);
UnixSocketMessage* msg = reinterpret_cast<UnixSocketMessage*>(data.get());
msg->len = size;
msg->type = MAP_INFO;
char* p = msg->data;
MoveToBinaryFormat(timestamp, p);
MoveToBinaryFormat(map.start_addr, p);
MoveToBinaryFormat(map.len, p);
MoveToBinaryFormat(map.pgoff, p);
MoveToBinaryFormat(map.name.c_str(), map.name.size() + 1, p);
thread_map_info_q_.push(std::move(data));
}
}
return true;
}
void SampleManager::SendThreadMapInfo() {
while (!thread_map_info_q_.empty()) {
auto& data = thread_map_info_q_.front();
UnixSocketMessage* msg = reinterpret_cast<UnixSocketMessage*>(data.get());
if (!conn_->SendMessage(*msg, false)) {
break;
}
thread_map_info_q_.pop();
}
}
static void FakeFunction() {
}
void SampleManager::SendFakeSampleRecord() {
size_t size = sizeof(UnixSocketMessage) + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 3;
std::unique_ptr<char[]> data(new char[size]);
UnixSocketMessage* msg = reinterpret_cast<UnixSocketMessage*>(data.get());
uint64_t ip = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(&FakeFunction));
msg->len = size;
msg->type = SAMPLE_INFO;
char* p = msg->data;
MoveToBinaryFormat(GetSystemClock(), p);
MoveToBinaryFormat(static_cast<uint32_t>(tid_), p);
MoveToBinaryFormat(1u, p);
MoveToBinaryFormat(1u, p);
MoveToBinaryFormat(ip, p);
conn_->SendMessage(*msg, false);
}
static void* CommunicationThread(void*) {
pthread_setname_np(pthread_self(), "inplace_sampler");
std::string server_path = "inplace_sampler_server_" + std::to_string(getpid());
std::unique_ptr<UnixSocketServer> server = UnixSocketServer::Create(server_path, true);
if (server == nullptr) {
LOG(ERROR) << "failed to create server at path " << server_path;
return nullptr;
}
LOG(INFO) << "Create inplace_sampler_server at " << server_path;
while (true) {
std::unique_ptr<UnixSocketConnection> conn = server->AcceptConnection();
if (conn == nullptr) {
break;
}
SampleManager manager(std::move(conn));
manager.Run();
}
return nullptr;
}
__attribute__((constructor)) void InitSampler() {
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
LOG(ERROR) << "pthread_attr_init failed";
return;
}
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
LOG(ERROR) << "pthread_attr_setdetachstate failed";
return;
}
pthread_t thread;
if (pthread_create(&thread, &attr, CommunicationThread, nullptr) != 0) {
LOG(ERROR) << "pthread_create failed";
return;
}
pthread_attr_destroy(&attr);
}
} // namespace
|