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/heapprofd_producer.h"
18
19 #include <signal.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include <algorithm>
25 #include <cinttypes>
26 #include <functional>
27 #include <optional>
28 #include <string>
29
30 #include "perfetto/base/compiler.h"
31 #include "perfetto/base/logging.h"
32 #include "perfetto/ext/base/file_utils.h"
33 #include "perfetto/ext/base/string_splitter.h"
34 #include "perfetto/ext/base/string_utils.h"
35 #include "perfetto/ext/base/thread_task_runner.h"
36 #include "perfetto/ext/base/watchdog_posix.h"
37 #include "perfetto/ext/tracing/core/basic_types.h"
38 #include "perfetto/ext/tracing/core/trace_writer.h"
39 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
40 #include "perfetto/tracing/core/data_source_config.h"
41 #include "perfetto/tracing/core/data_source_descriptor.h"
42 #include "perfetto/tracing/core/forward_decls.h"
43 #include "protos/perfetto/trace/profiling/profile_packet.pbzero.h"
44 #include "src/profiling/common/producer_support.h"
45 #include "src/profiling/common/profiler_guardrails.h"
46 #include "src/profiling/memory/shared_ring_buffer.h"
47 #include "src/profiling/memory/unwound_messages.h"
48 #include "src/profiling/memory/wire_protocol.h"
49
50 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
51 #include <sys/system_properties.h>
52 #endif
53
54 namespace perfetto {
55 namespace profiling {
56 namespace {
57 using ::perfetto::protos::pbzero::ProfilePacket;
58
59 constexpr char kHeapprofdDataSource[] = "android.heapprofd";
60 constexpr size_t kUnwinderThreads = 5;
61
62 constexpr uint32_t kInitialConnectionBackoffMs = 100;
63 constexpr uint32_t kMaxConnectionBackoffMs = 30 * 1000;
64 constexpr uint32_t kGuardrailIntervalMs = 30 * 1000;
65
66 constexpr uint64_t kDefaultShmemSize = 8 * 1048576; // ~8 MB
67 constexpr uint64_t kMaxShmemSize = 500 * 1048576; // ~500 MB
68
69 // Constants specified by bionic, hardcoded here for simplicity.
70 constexpr int kProfilingSignal = __SIGRTMIN + 4;
71 constexpr int kHeapprofdSignalValue = 0;
72
MakeUnwindingWorkers(HeapprofdProducer * delegate,size_t n)73 std::vector<UnwindingWorker> MakeUnwindingWorkers(HeapprofdProducer* delegate,
74 size_t n) {
75 std::vector<UnwindingWorker> ret;
76 for (size_t i = 0; i < n; ++i) {
77 ret.emplace_back(delegate,
78 base::ThreadTaskRunner::CreateAndStart("heapprofdunwind"));
79 }
80 return ret;
81 }
82
ConfigTargetsProcess(const HeapprofdConfig & cfg,const Process & proc,const std::vector<std::string> & normalized_cmdlines)83 bool ConfigTargetsProcess(const HeapprofdConfig& cfg,
84 const Process& proc,
85 const std::vector<std::string>& normalized_cmdlines) {
86 if (cfg.all())
87 return true;
88
89 const auto& pids = cfg.pid();
90 if (std::find(pids.cbegin(), pids.cend(), static_cast<uint64_t>(proc.pid)) !=
91 pids.cend()) {
92 return true;
93 }
94
95 if (std::find(normalized_cmdlines.cbegin(), normalized_cmdlines.cend(),
96 proc.cmdline) != normalized_cmdlines.cend()) {
97 return true;
98 }
99 return false;
100 }
101
IsFile(int fd,const char * fn)102 bool IsFile(int fd, const char* fn) {
103 struct stat fdstat;
104 struct stat fnstat;
105 if (fstat(fd, &fdstat) == -1) {
106 PERFETTO_PLOG("fstat");
107 return false;
108 }
109 if (lstat(fn, &fnstat) == -1) {
110 PERFETTO_PLOG("lstat");
111 return false;
112 }
113 return fdstat.st_ino == fnstat.st_ino;
114 }
115
116 protos::pbzero::ProfilePacket::ProcessHeapSamples::ClientError
ErrorStateToProto(SharedRingBuffer::ErrorState state)117 ErrorStateToProto(SharedRingBuffer::ErrorState state) {
118 switch (state) {
119 case (SharedRingBuffer::kNoError):
120 return protos::pbzero::ProfilePacket::ProcessHeapSamples::
121 CLIENT_ERROR_NONE;
122 case (SharedRingBuffer::kHitTimeout):
123 return protos::pbzero::ProfilePacket::ProcessHeapSamples::
124 CLIENT_ERROR_HIT_TIMEOUT;
125 case (SharedRingBuffer::kInvalidStackBounds):
126 return protos::pbzero::ProfilePacket::ProcessHeapSamples::
127 CLIENT_ERROR_INVALID_STACK_BOUNDS;
128 }
129 }
130
131 } // namespace
132
HeapprofdConfigToClientConfiguration(const HeapprofdConfig & heapprofd_config,ClientConfiguration * cli_config)133 bool HeapprofdConfigToClientConfiguration(
134 const HeapprofdConfig& heapprofd_config,
135 ClientConfiguration* cli_config) {
136 cli_config->default_interval = heapprofd_config.sampling_interval_bytes();
137 cli_config->block_client = heapprofd_config.block_client();
138 cli_config->disable_fork_teardown = heapprofd_config.disable_fork_teardown();
139 cli_config->disable_vfork_detection =
140 heapprofd_config.disable_vfork_detection();
141 cli_config->block_client_timeout_us =
142 heapprofd_config.block_client_timeout_us();
143 cli_config->all_heaps = heapprofd_config.all_heaps();
144 cli_config->adaptive_sampling_shmem_threshold =
145 heapprofd_config.adaptive_sampling_shmem_threshold();
146 cli_config->adaptive_sampling_max_sampling_interval_bytes =
147 heapprofd_config.adaptive_sampling_max_sampling_interval_bytes();
148 size_t n = 0;
149 const std::vector<std::string>& exclude_heaps =
150 heapprofd_config.exclude_heaps();
151 // heaps[i] and heaps_interval[i] represent that the heap named in heaps[i]
152 // should be sampled with sampling interval of heap_interval[i].
153 std::vector<std::string> heaps = heapprofd_config.heaps();
154 std::vector<uint64_t> heap_intervals =
155 heapprofd_config.heap_sampling_intervals();
156 if (heaps.empty() && !cli_config->all_heaps) {
157 heaps.push_back("libc.malloc");
158 }
159
160 if (heap_intervals.empty()) {
161 heap_intervals.assign(heaps.size(),
162 heapprofd_config.sampling_interval_bytes());
163 }
164 if (heap_intervals.size() != heaps.size()) {
165 PERFETTO_ELOG("heap_sampling_intervals and heaps length mismatch.");
166 return false;
167 }
168 if (std::find(heap_intervals.begin(), heap_intervals.end(), 0u) !=
169 heap_intervals.end()) {
170 PERFETTO_ELOG("zero sampling interval.");
171 return false;
172 }
173 if (!exclude_heaps.empty()) {
174 // For disabled heaps, we add explicit entries but with sampling interval
175 // 0. The consumer of the sampling intervals in ClientConfiguration,
176 // GetSamplingInterval in wire_protocol.h, uses 0 to signal a heap is
177 // disabled, either because it isn't enabled (all_heaps is not set, and the
178 // heap isn't named), or because we explicitely set it here.
179 heaps.insert(heaps.end(), exclude_heaps.cbegin(), exclude_heaps.cend());
180 heap_intervals.insert(heap_intervals.end(), exclude_heaps.size(), 0u);
181 }
182 if (heaps.size() > base::ArraySize(cli_config->heaps)) {
183 heaps.resize(base::ArraySize(cli_config->heaps));
184 PERFETTO_ELOG("Too many heaps requested. Truncating.");
185 }
186 for (size_t i = 0; i < heaps.size(); ++i) {
187 const std::string& heap = heaps[i];
188 const uint64_t interval = heap_intervals[i];
189 // -1 for the \0 byte.
190 if (heap.size() > HEAPPROFD_HEAP_NAME_SZ - 1) {
191 PERFETTO_ELOG("Invalid heap name %s (larger than %d)", heap.c_str(),
192 HEAPPROFD_HEAP_NAME_SZ - 1);
193 continue;
194 }
195 base::StringCopy(&cli_config->heaps[n].name[0], heap.c_str(),
196 sizeof(cli_config->heaps[n].name));
197 cli_config->heaps[n].interval = interval;
198 n++;
199 }
200 cli_config->num_heaps = n;
201 return true;
202 }
203
204 // We create kUnwinderThreads unwinding threads. Bookkeeping is done on the main
205 // thread.
HeapprofdProducer(HeapprofdMode mode,base::TaskRunner * task_runner,bool exit_when_done)206 HeapprofdProducer::HeapprofdProducer(HeapprofdMode mode,
207 base::TaskRunner* task_runner,
208 bool exit_when_done)
209 : task_runner_(task_runner),
210 mode_(mode),
211 exit_when_done_(exit_when_done),
212 socket_delegate_(this),
213 weak_factory_(this),
214 unwinding_workers_(MakeUnwindingWorkers(this, kUnwinderThreads)) {
215 CheckDataSourceCpuTask();
216 CheckDataSourceMemoryTask();
217 }
218
219 HeapprofdProducer::~HeapprofdProducer() = default;
220
SetTargetProcess(pid_t target_pid,std::string target_cmdline)221 void HeapprofdProducer::SetTargetProcess(pid_t target_pid,
222 std::string target_cmdline) {
223 target_process_.pid = target_pid;
224 target_process_.cmdline = target_cmdline;
225 }
226
SetDataSourceCallback(std::function<void ()> fn)227 void HeapprofdProducer::SetDataSourceCallback(std::function<void()> fn) {
228 data_source_callback_ = fn;
229 }
230
AdoptSocket(base::ScopedFile fd)231 void HeapprofdProducer::AdoptSocket(base::ScopedFile fd) {
232 PERFETTO_DCHECK(mode_ == HeapprofdMode::kChild);
233 auto socket = base::UnixSocket::AdoptConnected(
234 std::move(fd), &socket_delegate_, task_runner_, base::SockFamily::kUnix,
235 base::SockType::kStream);
236
237 HandleClientConnection(std::move(socket), target_process_);
238 }
239
OnConnect()240 void HeapprofdProducer::OnConnect() {
241 PERFETTO_DCHECK(state_ == kConnecting);
242 state_ = kConnected;
243 ResetConnectionBackoff();
244 PERFETTO_LOG("Connected to the service, mode [%s].",
245 mode_ == HeapprofdMode::kCentral ? "central" : "child");
246
247 DataSourceDescriptor desc;
248 desc.set_name(kHeapprofdDataSource);
249 desc.set_will_notify_on_stop(true);
250 endpoint_->RegisterDataSource(desc);
251 }
252
OnDisconnect()253 void HeapprofdProducer::OnDisconnect() {
254 PERFETTO_DCHECK(state_ == kConnected || state_ == kConnecting);
255 PERFETTO_LOG("Disconnected from tracing service");
256
257 // Do not attempt to reconnect if we're a process-private process, just quit.
258 if (exit_when_done_) {
259 TerminateProcess(/*exit_status=*/1); // does not return
260 }
261
262 // Central mode - attempt to reconnect.
263 auto weak_producer = weak_factory_.GetWeakPtr();
264 if (state_ == kConnected)
265 return task_runner_->PostTask([weak_producer] {
266 if (!weak_producer)
267 return;
268 weak_producer->Restart();
269 });
270
271 state_ = kNotConnected;
272 IncreaseConnectionBackoff();
273 task_runner_->PostDelayedTask(
274 [weak_producer] {
275 if (!weak_producer)
276 return;
277 weak_producer->ConnectService();
278 },
279 connection_backoff_ms_);
280 }
281
ConnectWithRetries(const char * socket_name)282 void HeapprofdProducer::ConnectWithRetries(const char* socket_name) {
283 PERFETTO_DCHECK(state_ == kNotStarted);
284 state_ = kNotConnected;
285
286 ResetConnectionBackoff();
287 producer_sock_name_ = socket_name;
288 ConnectService();
289 }
290
ConnectService()291 void HeapprofdProducer::ConnectService() {
292 SetProducerEndpoint(ProducerIPCClient::Connect(
293 producer_sock_name_, this, "android.heapprofd", task_runner_));
294 }
295
SetProducerEndpoint(std::unique_ptr<TracingService::ProducerEndpoint> endpoint)296 void HeapprofdProducer::SetProducerEndpoint(
297 std::unique_ptr<TracingService::ProducerEndpoint> endpoint) {
298 PERFETTO_DCHECK(state_ == kNotConnected || state_ == kNotStarted);
299 state_ = kConnecting;
300 endpoint_ = std::move(endpoint);
301 }
302
IncreaseConnectionBackoff()303 void HeapprofdProducer::IncreaseConnectionBackoff() {
304 connection_backoff_ms_ *= 2;
305 if (connection_backoff_ms_ > kMaxConnectionBackoffMs)
306 connection_backoff_ms_ = kMaxConnectionBackoffMs;
307 }
308
ResetConnectionBackoff()309 void HeapprofdProducer::ResetConnectionBackoff() {
310 connection_backoff_ms_ = kInitialConnectionBackoffMs;
311 }
312
Restart()313 void HeapprofdProducer::Restart() {
314 // We lost the connection with the tracing service. At this point we need
315 // to reset all the data sources. Trying to handle that manually is going to
316 // be error prone. What we do here is simply destroy the instance and
317 // recreate it again.
318
319 // Oneshot producer should not attempt restarts.
320 if (exit_when_done_)
321 PERFETTO_FATAL("Attempting to restart a one shot producer.");
322
323 HeapprofdMode mode = mode_;
324 base::TaskRunner* task_runner = task_runner_;
325 const char* socket_name = producer_sock_name_;
326 const bool exit_when_done = exit_when_done_;
327
328 // Invoke destructor and then the constructor again.
329 this->~HeapprofdProducer();
330 new (this) HeapprofdProducer(mode, task_runner, exit_when_done);
331
332 ConnectWithRetries(socket_name);
333 }
334
335 // TODO(rsavitski): would be cleaner to shut down the event loop instead
336 // (letting main exit). One test-friendly approach is to supply a shutdown
337 // callback in the constructor.
TerminateProcess(int exit_status)338 __attribute__((noreturn)) void HeapprofdProducer::TerminateProcess(
339 int exit_status) {
340 PERFETTO_CHECK(mode_ == HeapprofdMode::kChild);
341 PERFETTO_LOG("Shutting down child heapprofd (status %d).", exit_status);
342 exit(exit_status);
343 }
344
OnTracingSetup()345 void HeapprofdProducer::OnTracingSetup() {}
346
WriteRejectedConcurrentSession(BufferID buffer_id,pid_t pid)347 void HeapprofdProducer::WriteRejectedConcurrentSession(BufferID buffer_id,
348 pid_t pid) {
349 auto trace_writer = endpoint_->CreateTraceWriter(buffer_id);
350 auto trace_packet = trace_writer->NewTracePacket();
351 trace_packet->set_timestamp(
352 static_cast<uint64_t>(base::GetBootTimeNs().count()));
353 auto profile_packet = trace_packet->set_profile_packet();
354 auto process_dump = profile_packet->add_process_dumps();
355 process_dump->set_pid(static_cast<uint64_t>(pid));
356 process_dump->set_rejected_concurrent(true);
357 trace_packet->Finalize();
358 trace_writer->Flush();
359 }
360
SetupDataSource(DataSourceInstanceID id,const DataSourceConfig & ds_config)361 void HeapprofdProducer::SetupDataSource(DataSourceInstanceID id,
362 const DataSourceConfig& ds_config) {
363 if (ds_config.session_initiator() ==
364 DataSourceConfig::SESSION_INITIATOR_TRUSTED_SYSTEM) {
365 PERFETTO_LOG("Setting up datasource: statsd initiator.");
366 } else {
367 PERFETTO_LOG("Setting up datasource: non-statsd initiator.");
368 }
369 if (mode_ == HeapprofdMode::kChild && ds_config.enable_extra_guardrails()) {
370 PERFETTO_ELOG("enable_extra_guardrails is not supported on user.");
371 return;
372 }
373
374 HeapprofdConfig heapprofd_config;
375 heapprofd_config.ParseFromString(ds_config.heapprofd_config_raw());
376
377 if (heapprofd_config.all() && !heapprofd_config.pid().empty())
378 PERFETTO_ELOG("No point setting all and pid");
379 if (heapprofd_config.all() && !heapprofd_config.process_cmdline().empty())
380 PERFETTO_ELOG("No point setting all and process_cmdline");
381
382 if (ds_config.name() != kHeapprofdDataSource) {
383 PERFETTO_DLOG("Invalid data source name.");
384 return;
385 }
386
387 if (data_sources_.find(id) != data_sources_.end()) {
388 PERFETTO_ELOG("Received duplicated data source instance id: %" PRIu64, id);
389 return;
390 }
391
392 std::optional<std::vector<std::string>> normalized_cmdlines =
393 NormalizeCmdlines(heapprofd_config.process_cmdline());
394 if (!normalized_cmdlines.has_value()) {
395 PERFETTO_ELOG("Rejecting data source due to invalid cmdline in config.");
396 return;
397 }
398
399 // Child mode is only interested in the first data source matching the
400 // already-connected process.
401 if (mode_ == HeapprofdMode::kChild) {
402 if (!ConfigTargetsProcess(heapprofd_config, target_process_,
403 normalized_cmdlines.value())) {
404 PERFETTO_DLOG("Child mode skipping setup of unrelated data source.");
405 return;
406 }
407
408 if (!data_sources_.empty()) {
409 PERFETTO_LOG("Child mode skipping concurrent data source.");
410
411 // Manually write one ProfilePacket about the rejected session.
412 auto buffer_id = static_cast<BufferID>(ds_config.target_buffer());
413 WriteRejectedConcurrentSession(buffer_id, target_process_.pid);
414 return;
415 }
416 }
417
418 std::optional<uint64_t> start_cputime_sec;
419 if (heapprofd_config.max_heapprofd_cpu_secs() > 0) {
420 start_cputime_sec = GetCputimeSecForCurrentProcess();
421
422 if (!start_cputime_sec) {
423 PERFETTO_ELOG("Failed to enforce CPU guardrail. Rejecting config.");
424 return;
425 }
426 }
427
428 auto buffer_id = static_cast<BufferID>(ds_config.target_buffer());
429 DataSource data_source(endpoint_->CreateTraceWriter(buffer_id));
430 data_source.id = id;
431 auto& cli_config = data_source.client_configuration;
432 if (!HeapprofdConfigToClientConfiguration(heapprofd_config, &cli_config))
433 return;
434 data_source.config = heapprofd_config;
435 data_source.ds_config = ds_config;
436 data_source.normalized_cmdlines = std::move(normalized_cmdlines.value());
437 data_source.stop_timeout_ms = ds_config.stop_timeout_ms()
438 ? ds_config.stop_timeout_ms()
439 : 5000 /* kDataSourceStopTimeoutMs */;
440 data_source.guardrail_config.cpu_start_secs = start_cputime_sec;
441 data_source.guardrail_config.memory_guardrail_kb =
442 heapprofd_config.max_heapprofd_memory_kb();
443 data_source.guardrail_config.cpu_guardrail_sec =
444 heapprofd_config.max_heapprofd_cpu_secs();
445
446 InterningOutputTracker::WriteFixedInterningsPacket(
447 data_source.trace_writer.get(),
448 protos::pbzero::TracePacket::SEQ_INCREMENTAL_STATE_CLEARED);
449 data_sources_.emplace(id, std::move(data_source));
450 PERFETTO_DLOG("Set up data source.");
451
452 if (mode_ == HeapprofdMode::kChild && data_source_callback_)
453 (*data_source_callback_)();
454 }
455
IsPidProfiled(pid_t pid)456 bool HeapprofdProducer::IsPidProfiled(pid_t pid) {
457 return std::any_of(
458 data_sources_.cbegin(), data_sources_.cend(),
459 [pid](const std::pair<const DataSourceInstanceID, DataSource>& p) {
460 const DataSource& ds = p.second;
461 return ds.process_states.count(pid) > 0;
462 });
463 }
464
SetStartupProperties(DataSource * data_source)465 void HeapprofdProducer::SetStartupProperties(DataSource* data_source) {
466 const HeapprofdConfig& heapprofd_config = data_source->config;
467 if (heapprofd_config.all())
468 data_source->properties.emplace_back(properties_.SetAll());
469
470 for (std::string cmdline : data_source->normalized_cmdlines)
471 data_source->properties.emplace_back(
472 properties_.SetProperty(std::move(cmdline)));
473 }
474
SignalRunningProcesses(DataSource * data_source)475 void HeapprofdProducer::SignalRunningProcesses(DataSource* data_source) {
476 const HeapprofdConfig& heapprofd_config = data_source->config;
477
478 std::set<pid_t> pids;
479 if (heapprofd_config.all())
480 FindAllProfilablePids(&pids);
481 for (uint64_t pid : heapprofd_config.pid())
482 pids.emplace(static_cast<pid_t>(pid));
483
484 if (!data_source->normalized_cmdlines.empty())
485 FindPidsForCmdlines(data_source->normalized_cmdlines, &pids);
486
487 if (heapprofd_config.min_anonymous_memory_kb() > 0)
488 RemoveUnderAnonThreshold(heapprofd_config.min_anonymous_memory_kb(), &pids);
489
490 for (auto pid_it = pids.cbegin(); pid_it != pids.cend();) {
491 pid_t pid = *pid_it;
492 if (IsPidProfiled(pid)) {
493 PERFETTO_LOG("Rejecting concurrent session for %" PRIdMAX,
494 static_cast<intmax_t>(pid));
495 data_source->rejected_pids.emplace(pid);
496 pid_it = pids.erase(pid_it);
497 continue;
498 }
499
500 PERFETTO_DLOG("Sending signal: %d (si_value: %d) to pid: %d",
501 kProfilingSignal, kHeapprofdSignalValue, pid);
502 union sigval signal_value;
503 signal_value.sival_int = kHeapprofdSignalValue;
504 if (sigqueue(pid, kProfilingSignal, signal_value) != 0) {
505 PERFETTO_DPLOG("sigqueue");
506 }
507 ++pid_it;
508 }
509 data_source->signaled_pids = std::move(pids);
510 }
511
StartDataSource(DataSourceInstanceID id,const DataSourceConfig &)512 void HeapprofdProducer::StartDataSource(DataSourceInstanceID id,
513 const DataSourceConfig&) {
514 PERFETTO_DLOG("Starting data source %" PRIu64, id);
515
516 auto it = data_sources_.find(id);
517 if (it == data_sources_.end()) {
518 // This is expected in child heapprofd, where we reject uninteresting data
519 // sources in SetupDataSource.
520 if (mode_ == HeapprofdMode::kCentral) {
521 PERFETTO_ELOG("Received invalid data source instance to start: %" PRIu64,
522 id);
523 }
524 return;
525 }
526
527 DataSource& data_source = it->second;
528 if (data_source.started) {
529 PERFETTO_ELOG("Trying to start already started data-source: %" PRIu64, id);
530 return;
531 }
532 const HeapprofdConfig& heapprofd_config = data_source.config;
533
534 // Central daemon - set system properties for any targets that start later,
535 // and signal already-running targets to start the profiling client.
536 if (mode_ == HeapprofdMode::kCentral) {
537 if (!heapprofd_config.no_startup())
538 SetStartupProperties(&data_source);
539 if (!heapprofd_config.no_running())
540 SignalRunningProcesses(&data_source);
541 }
542
543 const auto continuous_dump_config = heapprofd_config.continuous_dump_config();
544 uint32_t dump_interval = continuous_dump_config.dump_interval_ms();
545 if (dump_interval) {
546 data_source.dump_interval_ms = dump_interval;
547 auto weak_producer = weak_factory_.GetWeakPtr();
548 task_runner_->PostDelayedTask(
549 [weak_producer, id] {
550 if (!weak_producer)
551 return;
552 weak_producer->DoDrainAndContinuousDump(id);
553 },
554 continuous_dump_config.dump_phase_ms());
555 }
556 data_source.started = true;
557 PERFETTO_DLOG("Started DataSource");
558 }
559
UnwinderForPID(pid_t pid)560 UnwindingWorker& HeapprofdProducer::UnwinderForPID(pid_t pid) {
561 return unwinding_workers_[static_cast<uint64_t>(pid) % kUnwinderThreads];
562 }
563
StopDataSource(DataSourceInstanceID id)564 void HeapprofdProducer::StopDataSource(DataSourceInstanceID id) {
565 auto it = data_sources_.find(id);
566 if (it == data_sources_.end()) {
567 endpoint_->NotifyDataSourceStopped(id);
568 if (mode_ == HeapprofdMode::kCentral)
569 PERFETTO_ELOG("Trying to stop non existing data source: %" PRIu64, id);
570 return;
571 }
572
573 PERFETTO_LOG("Stopping data source %" PRIu64, id);
574
575 DataSource& data_source = it->second;
576 data_source.was_stopped = true;
577 ShutdownDataSource(&data_source);
578 }
579
ShutdownDataSource(DataSource * data_source)580 void HeapprofdProducer::ShutdownDataSource(DataSource* data_source) {
581 data_source->shutting_down = true;
582 // If no processes connected, or all of them have already disconnected
583 // (and have been dumped) and no PIDs have been rejected,
584 // MaybeFinishDataSource can tear down the data source.
585 if (MaybeFinishDataSource(data_source))
586 return;
587
588 if (!data_source->rejected_pids.empty()) {
589 auto trace_packet = data_source->trace_writer->NewTracePacket();
590 ProfilePacket* profile_packet = trace_packet->set_profile_packet();
591 for (pid_t rejected_pid : data_source->rejected_pids) {
592 ProfilePacket::ProcessHeapSamples* proto =
593 profile_packet->add_process_dumps();
594 proto->set_pid(static_cast<uint64_t>(rejected_pid));
595 proto->set_rejected_concurrent(true);
596 }
597 trace_packet->Finalize();
598 data_source->rejected_pids.clear();
599 if (MaybeFinishDataSource(data_source))
600 return;
601 }
602
603 for (const auto& pid_and_process_state : data_source->process_states) {
604 pid_t pid = pid_and_process_state.first;
605 UnwinderForPID(pid).PostDisconnectSocket(pid);
606 }
607
608 auto id = data_source->id;
609 auto weak_producer = weak_factory_.GetWeakPtr();
610 task_runner_->PostDelayedTask(
611 [weak_producer, id] {
612 if (!weak_producer)
613 return;
614 auto ds_it = weak_producer->data_sources_.find(id);
615 if (ds_it != weak_producer->data_sources_.end()) {
616 PERFETTO_ELOG("Final dump timed out.");
617 DataSource& ds = ds_it->second;
618
619 for (const auto& pid_and_process_state : ds.process_states) {
620 pid_t pid = pid_and_process_state.first;
621 weak_producer->UnwinderForPID(pid).PostPurgeProcess(pid);
622 }
623 // Do not dump any stragglers, just trigger the Flush and tear down
624 // the data source.
625 ds.process_states.clear();
626 ds.rejected_pids.clear();
627 PERFETTO_CHECK(weak_producer->MaybeFinishDataSource(&ds));
628 }
629 },
630 data_source->stop_timeout_ms);
631 }
632
DoDrainAndContinuousDump(DataSourceInstanceID id)633 void HeapprofdProducer::DoDrainAndContinuousDump(DataSourceInstanceID id) {
634 auto it = data_sources_.find(id);
635 if (it == data_sources_.end())
636 return;
637 DataSource& data_source = it->second;
638 PERFETTO_DCHECK(data_source.pending_free_drains == 0);
639
640 for (auto& [pid, process_state] : data_source.process_states) {
641 UnwinderForPID(pid).PostDrainFree(data_source.id, pid);
642 data_source.pending_free_drains++;
643 }
644
645 // In case there are no pending free drains, dump immediately.
646 DoContinuousDump(&data_source);
647 }
648
DoContinuousDump(DataSource * ds)649 void HeapprofdProducer::DoContinuousDump(DataSource* ds) {
650 if (ds->pending_free_drains != 0) {
651 return;
652 }
653
654 DumpProcessesInDataSource(ds);
655 auto id = ds->id;
656 auto weak_producer = weak_factory_.GetWeakPtr();
657 task_runner_->PostDelayedTask(
658 [weak_producer, id] {
659 if (!weak_producer)
660 return;
661 weak_producer->DoDrainAndContinuousDump(id);
662 },
663 ds->dump_interval_ms);
664 }
665
PostDrainDone(UnwindingWorker *,DataSourceInstanceID ds_id)666 void HeapprofdProducer::PostDrainDone(UnwindingWorker*,
667 DataSourceInstanceID ds_id) {
668 auto weak_this = weak_factory_.GetWeakPtr();
669 task_runner_->PostTask([weak_this, ds_id] {
670 if (weak_this)
671 weak_this->DrainDone(ds_id);
672 });
673 }
674
DrainDone(DataSourceInstanceID ds_id)675 void HeapprofdProducer::DrainDone(DataSourceInstanceID ds_id) {
676 auto it = data_sources_.find(ds_id);
677 if (it == data_sources_.end()) {
678 return;
679 }
680 DataSource& data_source = it->second;
681 data_source.pending_free_drains--;
682 DoContinuousDump(&data_source);
683 }
684
685 // static
SetStats(protos::pbzero::ProfilePacket::ProcessStats * stats,const ProcessState & process_state)686 void HeapprofdProducer::SetStats(
687 protos::pbzero::ProfilePacket::ProcessStats* stats,
688 const ProcessState& process_state) {
689 stats->set_unwinding_errors(process_state.unwinding_errors);
690 stats->set_heap_samples(process_state.heap_samples);
691 stats->set_map_reparses(process_state.map_reparses);
692 stats->set_total_unwinding_time_us(process_state.total_unwinding_time_us);
693 stats->set_client_spinlock_blocked_us(
694 process_state.client_spinlock_blocked_us);
695 auto* unwinding_hist = stats->set_unwinding_time_us();
696 for (const auto& p : process_state.unwinding_time_us.GetData()) {
697 auto* bucket = unwinding_hist->add_buckets();
698 if (p.first == LogHistogram::kMaxBucket)
699 bucket->set_max_bucket(true);
700 else
701 bucket->set_upper_limit(p.first);
702 bucket->set_count(p.second);
703 }
704 }
705
DumpProcessState(DataSource * data_source,pid_t pid,ProcessState * process_state)706 void HeapprofdProducer::DumpProcessState(DataSource* data_source,
707 pid_t pid,
708 ProcessState* process_state) {
709 for (auto& heap_id_and_heap_info : process_state->heap_infos) {
710 ProcessState::HeapInfo& heap_info = heap_id_and_heap_info.second;
711
712 bool from_startup = data_source->signaled_pids.find(pid) ==
713 data_source->signaled_pids.cend();
714
715 auto new_heapsamples = [pid, from_startup, process_state, data_source,
716 &heap_info](
717 ProfilePacket::ProcessHeapSamples* proto) {
718 proto->set_pid(static_cast<uint64_t>(pid));
719 proto->set_timestamp(heap_info.heap_tracker.dump_timestamp());
720 proto->set_from_startup(from_startup);
721 proto->set_disconnected(process_state->disconnected);
722 proto->set_buffer_overran(process_state->error_state ==
723 SharedRingBuffer::kHitTimeout);
724 proto->set_client_error(ErrorStateToProto(process_state->error_state));
725 proto->set_buffer_corrupted(process_state->buffer_corrupted);
726 proto->set_hit_guardrail(data_source->hit_guardrail);
727 if (!heap_info.heap_name.empty())
728 proto->set_heap_name(heap_info.heap_name.c_str());
729 proto->set_sampling_interval_bytes(heap_info.sampling_interval);
730 proto->set_orig_sampling_interval_bytes(heap_info.orig_sampling_interval);
731 auto* stats = proto->set_stats();
732 SetStats(stats, *process_state);
733 };
734
735 DumpState dump_state(data_source->trace_writer.get(),
736 std::move(new_heapsamples),
737 &data_source->intern_state);
738
739 heap_info.heap_tracker.GetCallstackAllocations(
740 [&dump_state,
741 &data_source](const HeapTracker::CallstackAllocations& alloc) {
742 dump_state.WriteAllocation(alloc, data_source->config.dump_at_max());
743 });
744 dump_state.DumpCallstacks(&callsites_);
745 }
746 }
747
DumpProcessesInDataSource(DataSource * ds)748 void HeapprofdProducer::DumpProcessesInDataSource(DataSource* ds) {
749 for (std::pair<const pid_t, ProcessState>& pid_and_process_state :
750 ds->process_states) {
751 pid_t pid = pid_and_process_state.first;
752 ProcessState& process_state = pid_and_process_state.second;
753 DumpProcessState(ds, pid, &process_state);
754 }
755 }
756
DumpAll()757 void HeapprofdProducer::DumpAll() {
758 PERFETTO_LOG("Received signal. Dumping all data sources.");
759 for (auto& id_and_data_source : data_sources_)
760 DumpProcessesInDataSource(&id_and_data_source.second);
761 }
762
Flush(FlushRequestID flush_id,const DataSourceInstanceID * ids,size_t num_ids,FlushFlags)763 void HeapprofdProducer::Flush(FlushRequestID flush_id,
764 const DataSourceInstanceID* ids,
765 size_t num_ids,
766 FlushFlags) {
767 size_t& flush_in_progress = flushes_in_progress_[flush_id];
768 PERFETTO_DCHECK(flush_in_progress == 0);
769 flush_in_progress = num_ids;
770 for (size_t i = 0; i < num_ids; ++i) {
771 auto it = data_sources_.find(ids[i]);
772 if (it == data_sources_.end()) {
773 PERFETTO_ELOG("Trying to flush unknown data-source %" PRIu64, ids[i]);
774 flush_in_progress--;
775 continue;
776 }
777 DataSource& data_source = it->second;
778 auto weak_producer = weak_factory_.GetWeakPtr();
779
780 auto callback = [weak_producer, flush_id] {
781 if (weak_producer)
782 // Reposting because this task runner could be on a different thread
783 // than the IPC task runner.
784 return weak_producer->task_runner_->PostTask([weak_producer, flush_id] {
785 if (weak_producer)
786 return weak_producer->FinishDataSourceFlush(flush_id);
787 });
788 };
789 data_source.trace_writer->Flush(std::move(callback));
790 }
791 if (flush_in_progress == 0) {
792 endpoint_->NotifyFlushComplete(flush_id);
793 flushes_in_progress_.erase(flush_id);
794 }
795 }
796
FinishDataSourceFlush(FlushRequestID flush_id)797 void HeapprofdProducer::FinishDataSourceFlush(FlushRequestID flush_id) {
798 auto it = flushes_in_progress_.find(flush_id);
799 if (it == flushes_in_progress_.end()) {
800 PERFETTO_ELOG("FinishDataSourceFlush id invalid: %" PRIu64, flush_id);
801 return;
802 }
803 size_t& flush_in_progress = it->second;
804 if (--flush_in_progress == 0) {
805 endpoint_->NotifyFlushComplete(flush_id);
806 flushes_in_progress_.erase(flush_id);
807 }
808 }
809
OnDisconnect(base::UnixSocket * self)810 void HeapprofdProducer::SocketDelegate::OnDisconnect(base::UnixSocket* self) {
811 auto it = producer_->pending_processes_.find(self->peer_pid_linux());
812 if (it == producer_->pending_processes_.end()) {
813 PERFETTO_ELOG("Unexpected disconnect.");
814 return;
815 }
816
817 if (self == it->second.sock.get())
818 producer_->pending_processes_.erase(it);
819 }
820
OnNewIncomingConnection(base::UnixSocket *,std::unique_ptr<base::UnixSocket> new_connection)821 void HeapprofdProducer::SocketDelegate::OnNewIncomingConnection(
822 base::UnixSocket*,
823 std::unique_ptr<base::UnixSocket> new_connection) {
824 Process peer_process;
825 peer_process.pid = new_connection->peer_pid_linux();
826 if (!GetCmdlineForPID(peer_process.pid, &peer_process.cmdline))
827 PERFETTO_PLOG("Failed to get cmdline for %d", peer_process.pid);
828
829 producer_->HandleClientConnection(std::move(new_connection), peer_process);
830 }
831
OnDataAvailable(base::UnixSocket * self)832 void HeapprofdProducer::SocketDelegate::OnDataAvailable(
833 base::UnixSocket* self) {
834 auto it = producer_->pending_processes_.find(self->peer_pid_linux());
835 if (it == producer_->pending_processes_.end()) {
836 PERFETTO_ELOG("Unexpected data.");
837 return;
838 }
839
840 PendingProcess& pending_process = it->second;
841
842 base::ScopedFile fds[kHandshakeSize];
843 char buf[1];
844 self->Receive(buf, sizeof(buf), fds, base::ArraySize(fds));
845
846 static_assert(kHandshakeSize == 2, "change if and else if below.");
847 if (fds[kHandshakeMaps] && fds[kHandshakeMem]) {
848 auto ds_it =
849 producer_->data_sources_.find(pending_process.data_source_instance_id);
850 if (ds_it == producer_->data_sources_.end()) {
851 producer_->pending_processes_.erase(it);
852 return;
853 }
854 DataSource& data_source = ds_it->second;
855
856 if (data_source.shutting_down) {
857 producer_->pending_processes_.erase(it);
858 PERFETTO_LOG("Got handshake for DS that is shutting down. Rejecting.");
859 return;
860 }
861
862 std::string maps_file =
863 "/proc/" + std::to_string(self->peer_pid_linux()) + "/maps";
864 if (!IsFile(*fds[kHandshakeMaps], maps_file.c_str())) {
865 producer_->pending_processes_.erase(it);
866 PERFETTO_ELOG("Received invalid maps FD.");
867 return;
868 }
869
870 std::string mem_file =
871 "/proc/" + std::to_string(self->peer_pid_linux()) + "/mem";
872 if (!IsFile(*fds[kHandshakeMem], mem_file.c_str())) {
873 producer_->pending_processes_.erase(it);
874 PERFETTO_ELOG("Received invalid mem FD.");
875 return;
876 }
877
878 data_source.process_states.emplace(
879 std::piecewise_construct, std::forward_as_tuple(self->peer_pid_linux()),
880 std::forward_as_tuple(&producer_->callsites_,
881 data_source.config.dump_at_max()));
882
883 PERFETTO_DLOG("%d: Received FDs.", self->peer_pid_linux());
884 int raw_fd = pending_process.shmem.fd();
885 // TODO(fmayer): Full buffer could deadlock us here.
886 if (!self->Send(&data_source.client_configuration,
887 sizeof(data_source.client_configuration), &raw_fd, 1)) {
888 // If Send fails, the socket will have been Shutdown, and the raw socket
889 // closed.
890 producer_->pending_processes_.erase(it);
891 return;
892 }
893
894 UnwindingWorker::HandoffData handoff_data;
895 handoff_data.data_source_instance_id =
896 pending_process.data_source_instance_id;
897 handoff_data.sock = self->ReleaseSocket();
898 handoff_data.maps_fd = std::move(fds[kHandshakeMaps]);
899 handoff_data.mem_fd = std::move(fds[kHandshakeMem]);
900 handoff_data.shmem = std::move(pending_process.shmem);
901 handoff_data.client_config = data_source.client_configuration;
902 handoff_data.stream_allocations = data_source.config.stream_allocations();
903
904 producer_->UnwinderForPID(self->peer_pid_linux())
905 .PostHandoffSocket(std::move(handoff_data));
906 producer_->pending_processes_.erase(it);
907 } else if (fds[kHandshakeMaps] || fds[kHandshakeMem]) {
908 PERFETTO_ELOG("%d: Received partial FDs.", self->peer_pid_linux());
909 producer_->pending_processes_.erase(it);
910 } else {
911 PERFETTO_ELOG("%d: Received no FDs.", self->peer_pid_linux());
912 }
913 }
914
GetDataSourceForProcess(const Process & proc)915 HeapprofdProducer::DataSource* HeapprofdProducer::GetDataSourceForProcess(
916 const Process& proc) {
917 for (auto& ds_id_and_datasource : data_sources_) {
918 DataSource& ds = ds_id_and_datasource.second;
919 if (ConfigTargetsProcess(ds.config, proc, ds.normalized_cmdlines))
920 return &ds;
921 }
922 return nullptr;
923 }
924
RecordOtherSourcesAsRejected(DataSource * active_ds,const Process & proc)925 void HeapprofdProducer::RecordOtherSourcesAsRejected(DataSource* active_ds,
926 const Process& proc) {
927 for (auto& ds_id_and_datasource : data_sources_) {
928 DataSource& ds = ds_id_and_datasource.second;
929 if (&ds != active_ds &&
930 ConfigTargetsProcess(ds.config, proc, ds.normalized_cmdlines))
931 ds.rejected_pids.emplace(proc.pid);
932 }
933 }
934
HandleClientConnection(std::unique_ptr<base::UnixSocket> new_connection,Process process)935 void HeapprofdProducer::HandleClientConnection(
936 std::unique_ptr<base::UnixSocket> new_connection,
937 Process process) {
938 DataSource* data_source = GetDataSourceForProcess(process);
939 if (!data_source) {
940 PERFETTO_LOG("No data source found.");
941 return;
942 }
943 RecordOtherSourcesAsRejected(data_source, process);
944
945 // In fork mode, right now we check whether the target is not profileable
946 // in the client, because we cannot read packages.list there.
947 if (mode_ == HeapprofdMode::kCentral &&
948 !CanProfile(data_source->ds_config, new_connection->peer_uid_posix(),
949 data_source->config.target_installed_by())) {
950 PERFETTO_ELOG("%d (%s) is not profileable.", process.pid,
951 process.cmdline.c_str());
952 return;
953 }
954
955 uint64_t shmem_size = data_source->config.shmem_size_bytes();
956 if (!shmem_size)
957 shmem_size = kDefaultShmemSize;
958 if (shmem_size > kMaxShmemSize) {
959 PERFETTO_LOG("Specified shared memory size of %" PRIu64
960 " exceeds maximum size of %" PRIu64 ". Reducing.",
961 shmem_size, kMaxShmemSize);
962 shmem_size = kMaxShmemSize;
963 }
964
965 auto shmem = SharedRingBuffer::Create(static_cast<size_t>(shmem_size));
966 if (!shmem || !shmem->is_valid()) {
967 PERFETTO_LOG("Failed to create shared memory.");
968 return;
969 }
970
971 pid_t peer_pid = new_connection->peer_pid_linux();
972 if (peer_pid != process.pid) {
973 PERFETTO_ELOG("Invalid PID connected.");
974 return;
975 }
976
977 PendingProcess pending_process;
978 pending_process.sock = std::move(new_connection);
979 pending_process.data_source_instance_id = data_source->id;
980 pending_process.shmem = std::move(*shmem);
981 pending_processes_.emplace(peer_pid, std::move(pending_process));
982 }
983
PostAllocRecord(UnwindingWorker * worker,std::unique_ptr<AllocRecord> alloc_rec)984 void HeapprofdProducer::PostAllocRecord(
985 UnwindingWorker* worker,
986 std::unique_ptr<AllocRecord> alloc_rec) {
987 // Once we can use C++14, this should be std::moved into the lambda instead.
988 auto* raw_alloc_rec = alloc_rec.release();
989 auto weak_this = weak_factory_.GetWeakPtr();
990 task_runner_->PostTask([weak_this, raw_alloc_rec, worker] {
991 std::unique_ptr<AllocRecord> unique_alloc_ref =
992 std::unique_ptr<AllocRecord>(raw_alloc_rec);
993 if (weak_this) {
994 weak_this->HandleAllocRecord(unique_alloc_ref.get());
995 worker->ReturnAllocRecord(std::move(unique_alloc_ref));
996 }
997 });
998 }
999
PostFreeRecord(UnwindingWorker *,std::vector<FreeRecord> free_recs)1000 void HeapprofdProducer::PostFreeRecord(UnwindingWorker*,
1001 std::vector<FreeRecord> free_recs) {
1002 // Once we can use C++14, this should be std::moved into the lambda instead.
1003 std::vector<FreeRecord>* raw_free_recs =
1004 new std::vector<FreeRecord>(std::move(free_recs));
1005 auto weak_this = weak_factory_.GetWeakPtr();
1006 task_runner_->PostTask([weak_this, raw_free_recs] {
1007 if (weak_this) {
1008 for (FreeRecord& free_rec : *raw_free_recs)
1009 weak_this->HandleFreeRecord(std::move(free_rec));
1010 }
1011 delete raw_free_recs;
1012 });
1013 }
1014
PostHeapNameRecord(UnwindingWorker *,HeapNameRecord rec)1015 void HeapprofdProducer::PostHeapNameRecord(UnwindingWorker*,
1016 HeapNameRecord rec) {
1017 auto weak_this = weak_factory_.GetWeakPtr();
1018 task_runner_->PostTask([weak_this, rec] {
1019 if (weak_this)
1020 weak_this->HandleHeapNameRecord(rec);
1021 });
1022 }
1023
PostSocketDisconnected(UnwindingWorker *,DataSourceInstanceID ds_id,pid_t pid,SharedRingBuffer::Stats stats)1024 void HeapprofdProducer::PostSocketDisconnected(UnwindingWorker*,
1025 DataSourceInstanceID ds_id,
1026 pid_t pid,
1027 SharedRingBuffer::Stats stats) {
1028 auto weak_this = weak_factory_.GetWeakPtr();
1029 task_runner_->PostTask([weak_this, ds_id, pid, stats] {
1030 if (weak_this)
1031 weak_this->HandleSocketDisconnected(ds_id, pid, stats);
1032 });
1033 }
1034
HandleAllocRecord(AllocRecord * alloc_rec)1035 void HeapprofdProducer::HandleAllocRecord(AllocRecord* alloc_rec) {
1036 const AllocMetadata& alloc_metadata = alloc_rec->alloc_metadata;
1037 auto it = data_sources_.find(alloc_rec->data_source_instance_id);
1038 if (it == data_sources_.end()) {
1039 PERFETTO_LOG("Invalid data source in alloc record.");
1040 return;
1041 }
1042
1043 DataSource& ds = it->second;
1044 auto process_state_it = ds.process_states.find(alloc_rec->pid);
1045 if (process_state_it == ds.process_states.end()) {
1046 PERFETTO_LOG("Invalid PID in alloc record.");
1047 return;
1048 }
1049
1050 if (ds.config.stream_allocations()) {
1051 auto packet = ds.trace_writer->NewTracePacket();
1052 auto* streaming_alloc = packet->set_streaming_allocation();
1053 streaming_alloc->add_address(alloc_metadata.alloc_address);
1054 streaming_alloc->add_size(alloc_metadata.alloc_size);
1055 streaming_alloc->add_sample_size(alloc_metadata.sample_size);
1056 streaming_alloc->add_clock_monotonic_coarse_timestamp(
1057 alloc_metadata.clock_monotonic_coarse_timestamp);
1058 streaming_alloc->add_heap_id(alloc_metadata.heap_id);
1059 streaming_alloc->add_sequence_number(alloc_metadata.sequence_number);
1060 return;
1061 }
1062
1063 const auto& prefixes = ds.config.skip_symbol_prefix();
1064 if (!prefixes.empty()) {
1065 for (unwindstack::FrameData& frame_data : alloc_rec->frames) {
1066 if (frame_data.map_info == nullptr) {
1067 continue;
1068 }
1069 const std::string& map = frame_data.map_info->name();
1070 if (std::find_if(prefixes.cbegin(), prefixes.cend(),
1071 [&map](const std::string& prefix) {
1072 return base::StartsWith(map, prefix);
1073 }) != prefixes.cend()) {
1074 frame_data.function_name = "FILTERED";
1075 }
1076 }
1077 }
1078
1079 ProcessState& process_state = process_state_it->second;
1080 HeapTracker& heap_tracker =
1081 process_state.GetHeapTracker(alloc_rec->alloc_metadata.heap_id);
1082
1083 if (alloc_rec->error)
1084 process_state.unwinding_errors++;
1085 if (alloc_rec->reparsed_map)
1086 process_state.map_reparses++;
1087 process_state.heap_samples++;
1088 process_state.unwinding_time_us.Add(alloc_rec->unwinding_time_us);
1089 process_state.total_unwinding_time_us += alloc_rec->unwinding_time_us;
1090
1091 // abspc may no longer refer to the same functions, as we had to reparse
1092 // maps. Reset the cache.
1093 if (alloc_rec->reparsed_map)
1094 heap_tracker.ClearFrameCache();
1095
1096 heap_tracker.RecordMalloc(
1097 alloc_rec->frames, alloc_rec->build_ids, alloc_metadata.alloc_address,
1098 alloc_metadata.sample_size, alloc_metadata.alloc_size,
1099 alloc_metadata.sequence_number,
1100 alloc_metadata.clock_monotonic_coarse_timestamp);
1101 }
1102
HandleFreeRecord(FreeRecord free_rec)1103 void HeapprofdProducer::HandleFreeRecord(FreeRecord free_rec) {
1104 auto it = data_sources_.find(free_rec.data_source_instance_id);
1105 if (it == data_sources_.end()) {
1106 PERFETTO_LOG("Invalid data source in free record.");
1107 return;
1108 }
1109
1110 DataSource& ds = it->second;
1111 auto process_state_it = ds.process_states.find(free_rec.pid);
1112 if (process_state_it == ds.process_states.end()) {
1113 PERFETTO_LOG("Invalid PID in free record.");
1114 return;
1115 }
1116
1117 if (ds.config.stream_allocations()) {
1118 auto packet = ds.trace_writer->NewTracePacket();
1119 auto* streaming_free = packet->set_streaming_free();
1120 streaming_free->add_address(free_rec.entry.addr);
1121 streaming_free->add_heap_id(free_rec.entry.heap_id);
1122 streaming_free->add_sequence_number(free_rec.entry.sequence_number);
1123 return;
1124 }
1125
1126 ProcessState& process_state = process_state_it->second;
1127
1128 const FreeEntry& entry = free_rec.entry;
1129 HeapTracker& heap_tracker = process_state.GetHeapTracker(entry.heap_id);
1130 heap_tracker.RecordFree(entry.addr, entry.sequence_number, 0);
1131 }
1132
HandleHeapNameRecord(HeapNameRecord rec)1133 void HeapprofdProducer::HandleHeapNameRecord(HeapNameRecord rec) {
1134 auto it = data_sources_.find(rec.data_source_instance_id);
1135 if (it == data_sources_.end()) {
1136 PERFETTO_LOG("Invalid data source in free record.");
1137 return;
1138 }
1139
1140 DataSource& ds = it->second;
1141 auto process_state_it = ds.process_states.find(rec.pid);
1142 if (process_state_it == ds.process_states.end()) {
1143 PERFETTO_LOG("Invalid PID in free record.");
1144 return;
1145 }
1146
1147 ProcessState& process_state = process_state_it->second;
1148 const HeapName& entry = rec.entry;
1149 if (entry.heap_name[0] != '\0') {
1150 std::string heap_name = entry.heap_name;
1151 if (entry.heap_id == 0) {
1152 PERFETTO_ELOG("Invalid zero heap ID.");
1153 return;
1154 }
1155 ProcessState::HeapInfo& hi = process_state.GetHeapInfo(entry.heap_id);
1156 if (!hi.heap_name.empty() && hi.heap_name != heap_name) {
1157 PERFETTO_ELOG("Overriding heap name %s with %s", hi.heap_name.c_str(),
1158 heap_name.c_str());
1159 }
1160 hi.heap_name = entry.heap_name;
1161 }
1162 if (entry.sample_interval != 0) {
1163 ProcessState::HeapInfo& hi = process_state.GetHeapInfo(entry.heap_id);
1164 if (!hi.sampling_interval)
1165 hi.orig_sampling_interval = entry.sample_interval;
1166 hi.sampling_interval = entry.sample_interval;
1167 }
1168 }
1169
TerminateWhenDone()1170 void HeapprofdProducer::TerminateWhenDone() {
1171 if (data_sources_.empty())
1172 TerminateProcess(0);
1173 exit_when_done_ = true;
1174 }
1175
MaybeFinishDataSource(DataSource * ds)1176 bool HeapprofdProducer::MaybeFinishDataSource(DataSource* ds) {
1177 if (!ds->process_states.empty() || !ds->rejected_pids.empty() ||
1178 !ds->shutting_down) {
1179 return false;
1180 }
1181
1182 bool was_stopped = ds->was_stopped;
1183 DataSourceInstanceID ds_id = ds->id;
1184 auto weak_producer = weak_factory_.GetWeakPtr();
1185 bool exit_when_done = exit_when_done_;
1186 ds->trace_writer->Flush([weak_producer, exit_when_done, ds_id, was_stopped] {
1187 if (!weak_producer)
1188 return;
1189
1190 if (was_stopped)
1191 weak_producer->endpoint_->NotifyDataSourceStopped(ds_id);
1192 weak_producer->data_sources_.erase(ds_id);
1193
1194 if (exit_when_done) {
1195 // Post this as a task to allow NotifyDataSourceStopped to post tasks.
1196 weak_producer->task_runner_->PostTask([weak_producer] {
1197 if (!weak_producer)
1198 return;
1199 weak_producer->TerminateProcess(
1200 /*exit_status=*/0); // does not return
1201 });
1202 }
1203 });
1204 return true;
1205 }
1206
HandleSocketDisconnected(DataSourceInstanceID ds_id,pid_t pid,SharedRingBuffer::Stats stats)1207 void HeapprofdProducer::HandleSocketDisconnected(
1208 DataSourceInstanceID ds_id,
1209 pid_t pid,
1210 SharedRingBuffer::Stats stats) {
1211 auto it = data_sources_.find(ds_id);
1212 if (it == data_sources_.end())
1213 return;
1214 DataSource& ds = it->second;
1215
1216 auto process_state_it = ds.process_states.find(pid);
1217 if (process_state_it == ds.process_states.end()) {
1218 PERFETTO_ELOG("Unexpected disconnect from %d", pid);
1219 return;
1220 }
1221
1222 PERFETTO_LOG("%d disconnected from heapprofd (ds shutting down: %d).", pid,
1223 ds.shutting_down);
1224
1225 ProcessState& process_state = process_state_it->second;
1226 process_state.disconnected = !ds.shutting_down;
1227 process_state.error_state = stats.error_state;
1228 process_state.client_spinlock_blocked_us = stats.client_spinlock_blocked_us;
1229 process_state.buffer_corrupted =
1230 stats.num_writes_corrupt > 0 || stats.num_reads_corrupt > 0;
1231
1232 DumpProcessState(&ds, pid, &process_state);
1233 ds.process_states.erase(pid);
1234 MaybeFinishDataSource(&ds);
1235 }
1236
CheckDataSourceCpuTask()1237 void HeapprofdProducer::CheckDataSourceCpuTask() {
1238 auto weak_producer = weak_factory_.GetWeakPtr();
1239 task_runner_->PostDelayedTask(
1240 [weak_producer] {
1241 if (!weak_producer)
1242 return;
1243 weak_producer->CheckDataSourceCpuTask();
1244 },
1245 kGuardrailIntervalMs);
1246
1247 ProfilerCpuGuardrails gr;
1248 for (auto& p : data_sources_) {
1249 DataSource& ds = p.second;
1250 if (gr.IsOverCpuThreshold(ds.guardrail_config)) {
1251 ds.hit_guardrail = true;
1252 PERFETTO_LOG("Data source %" PRIu64 " hit CPU guardrail. Shutting down.",
1253 ds.id);
1254 ShutdownDataSource(&ds);
1255 }
1256 }
1257 }
1258
CheckDataSourceMemoryTask()1259 void HeapprofdProducer::CheckDataSourceMemoryTask() {
1260 auto weak_producer = weak_factory_.GetWeakPtr();
1261 task_runner_->PostDelayedTask(
1262 [weak_producer] {
1263 if (!weak_producer)
1264 return;
1265 weak_producer->CheckDataSourceMemoryTask();
1266 },
1267 kGuardrailIntervalMs);
1268 ProfilerMemoryGuardrails gr;
1269 for (auto& p : data_sources_) {
1270 DataSource& ds = p.second;
1271 if (gr.IsOverMemoryThreshold(ds.guardrail_config)) {
1272 ds.hit_guardrail = true;
1273 PERFETTO_LOG("Data source %" PRIu64
1274 " hit memory guardrail. Shutting down.",
1275 ds.id);
1276 ShutdownDataSource(&ds);
1277 }
1278 }
1279 }
1280
1281 } // namespace profiling
1282 } // namespace perfetto
1283