• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
28 
29 #include "perfetto/base/compiler.h"
30 #include "perfetto/base/logging.h"
31 #include "perfetto/ext/base/file_utils.h"
32 #include "perfetto/ext/base/optional.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       unwinding_workers_(MakeUnwindingWorkers(this, kUnwinderThreads)),
213       socket_delegate_(this),
214       weak_factory_(this) {
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_DFATAL_OR_ELOG(
389         "Received duplicated data source instance id: %" PRIu64, id);
390     return;
391   }
392 
393   base::Optional<std::vector<std::string>> normalized_cmdlines =
394       NormalizeCmdlines(heapprofd_config.process_cmdline());
395   if (!normalized_cmdlines.has_value()) {
396     PERFETTO_ELOG("Rejecting data source due to invalid cmdline in config.");
397     return;
398   }
399 
400   // Child mode is only interested in the first data source matching the
401   // already-connected process.
402   if (mode_ == HeapprofdMode::kChild) {
403     if (!ConfigTargetsProcess(heapprofd_config, target_process_,
404                               normalized_cmdlines.value())) {
405       PERFETTO_DLOG("Child mode skipping setup of unrelated data source.");
406       return;
407     }
408 
409     if (!data_sources_.empty()) {
410       PERFETTO_LOG("Child mode skipping concurrent data source.");
411 
412       // Manually write one ProfilePacket about the rejected session.
413       auto buffer_id = static_cast<BufferID>(ds_config.target_buffer());
414       WriteRejectedConcurrentSession(buffer_id, target_process_.pid);
415       return;
416     }
417   }
418 
419   base::Optional<uint64_t> start_cputime_sec;
420   if (heapprofd_config.max_heapprofd_cpu_secs() > 0) {
421     start_cputime_sec = GetCputimeSecForCurrentProcess();
422 
423     if (!start_cputime_sec) {
424       PERFETTO_ELOG("Failed to enforce CPU guardrail. Rejecting config.");
425       return;
426     }
427   }
428 
429   auto buffer_id = static_cast<BufferID>(ds_config.target_buffer());
430   DataSource data_source(endpoint_->CreateTraceWriter(buffer_id));
431   data_source.id = id;
432   auto& cli_config = data_source.client_configuration;
433   if (!HeapprofdConfigToClientConfiguration(heapprofd_config, &cli_config))
434     return;
435   data_source.config = heapprofd_config;
436   data_source.ds_config = ds_config;
437   data_source.normalized_cmdlines = std::move(normalized_cmdlines.value());
438   data_source.stop_timeout_ms = ds_config.stop_timeout_ms()
439                                     ? ds_config.stop_timeout_ms()
440                                     : 5000 /* kDataSourceStopTimeoutMs */;
441   data_source.guardrail_config.cpu_start_secs = start_cputime_sec;
442   data_source.guardrail_config.memory_guardrail_kb =
443       heapprofd_config.max_heapprofd_memory_kb();
444   data_source.guardrail_config.cpu_guardrail_sec =
445       heapprofd_config.max_heapprofd_cpu_secs();
446 
447   InterningOutputTracker::WriteFixedInterningsPacket(
448       data_source.trace_writer.get(),
449       protos::pbzero::TracePacket::SEQ_INCREMENTAL_STATE_CLEARED);
450   data_sources_.emplace(id, std::move(data_source));
451   PERFETTO_DLOG("Set up data source.");
452 
453   if (mode_ == HeapprofdMode::kChild && data_source_callback_)
454     (*data_source_callback_)();
455 }
456 
IsPidProfiled(pid_t pid)457 bool HeapprofdProducer::IsPidProfiled(pid_t pid) {
458   return std::any_of(
459       data_sources_.cbegin(), data_sources_.cend(),
460       [pid](const std::pair<const DataSourceInstanceID, DataSource>& p) {
461         const DataSource& ds = p.second;
462         return ds.process_states.count(pid) > 0;
463       });
464 }
465 
SetStartupProperties(DataSource * data_source)466 void HeapprofdProducer::SetStartupProperties(DataSource* data_source) {
467   const HeapprofdConfig& heapprofd_config = data_source->config;
468   if (heapprofd_config.all())
469     data_source->properties.emplace_back(properties_.SetAll());
470 
471   for (std::string cmdline : data_source->normalized_cmdlines)
472     data_source->properties.emplace_back(
473         properties_.SetProperty(std::move(cmdline)));
474 }
475 
SignalRunningProcesses(DataSource * data_source)476 void HeapprofdProducer::SignalRunningProcesses(DataSource* data_source) {
477   const HeapprofdConfig& heapprofd_config = data_source->config;
478 
479   std::set<pid_t> pids;
480   if (heapprofd_config.all())
481     FindAllProfilablePids(&pids);
482   for (uint64_t pid : heapprofd_config.pid())
483     pids.emplace(static_cast<pid_t>(pid));
484 
485   if (!data_source->normalized_cmdlines.empty())
486     FindPidsForCmdlines(data_source->normalized_cmdlines, &pids);
487 
488   if (heapprofd_config.min_anonymous_memory_kb() > 0)
489     RemoveUnderAnonThreshold(heapprofd_config.min_anonymous_memory_kb(), &pids);
490 
491   for (auto pid_it = pids.cbegin(); pid_it != pids.cend();) {
492     pid_t pid = *pid_it;
493     if (IsPidProfiled(pid)) {
494       PERFETTO_LOG("Rejecting concurrent session for %" PRIdMAX,
495                    static_cast<intmax_t>(pid));
496       data_source->rejected_pids.emplace(pid);
497       pid_it = pids.erase(pid_it);
498       continue;
499     }
500 
501     PERFETTO_DLOG("Sending signal: %d (si_value: %d) to pid: %d",
502                   kProfilingSignal, kHeapprofdSignalValue, pid);
503     union sigval signal_value;
504     signal_value.sival_int = kHeapprofdSignalValue;
505     if (sigqueue(pid, kProfilingSignal, signal_value) != 0) {
506       PERFETTO_DPLOG("sigqueue");
507     }
508     ++pid_it;
509   }
510   data_source->signaled_pids = std::move(pids);
511 }
512 
StartDataSource(DataSourceInstanceID id,const DataSourceConfig &)513 void HeapprofdProducer::StartDataSource(DataSourceInstanceID id,
514                                         const DataSourceConfig&) {
515   PERFETTO_DLOG("Starting data source %" PRIu64, id);
516 
517   auto it = data_sources_.find(id);
518   if (it == data_sources_.end()) {
519     // This is expected in child heapprofd, where we reject uninteresting data
520     // sources in SetupDataSource.
521     if (mode_ == HeapprofdMode::kCentral) {
522       PERFETTO_DFATAL_OR_ELOG(
523           "Received invalid data source instance to start: %" PRIu64, id);
524     }
525     return;
526   }
527 
528   DataSource& data_source = it->second;
529   if (data_source.started) {
530     PERFETTO_DFATAL_OR_ELOG(
531         "Trying to start already started data-source: %" PRIu64, id);
532     return;
533   }
534   const HeapprofdConfig& heapprofd_config = data_source.config;
535 
536   // Central daemon - set system properties for any targets that start later,
537   // and signal already-running targets to start the profiling client.
538   if (mode_ == HeapprofdMode::kCentral) {
539     if (!heapprofd_config.no_startup())
540       SetStartupProperties(&data_source);
541     if (!heapprofd_config.no_running())
542       SignalRunningProcesses(&data_source);
543   }
544 
545   const auto continuous_dump_config = heapprofd_config.continuous_dump_config();
546   uint32_t dump_interval = continuous_dump_config.dump_interval_ms();
547   if (dump_interval) {
548     auto weak_producer = weak_factory_.GetWeakPtr();
549     task_runner_->PostDelayedTask(
550         [weak_producer, id, dump_interval] {
551           if (!weak_producer)
552             return;
553           weak_producer->DoContinuousDump(id, dump_interval);
554         },
555         continuous_dump_config.dump_phase_ms());
556   }
557   data_source.started = true;
558   PERFETTO_DLOG("Started DataSource");
559 }
560 
UnwinderForPID(pid_t pid)561 UnwindingWorker& HeapprofdProducer::UnwinderForPID(pid_t pid) {
562   return unwinding_workers_[static_cast<uint64_t>(pid) % kUnwinderThreads];
563 }
564 
StopDataSource(DataSourceInstanceID id)565 void HeapprofdProducer::StopDataSource(DataSourceInstanceID id) {
566   auto it = data_sources_.find(id);
567   if (it == data_sources_.end()) {
568     endpoint_->NotifyDataSourceStopped(id);
569     if (mode_ == HeapprofdMode::kCentral)
570       PERFETTO_DFATAL_OR_ELOG(
571           "Trying to stop non existing data source: %" PRIu64, id);
572     return;
573   }
574 
575   PERFETTO_LOG("Stopping data source %" PRIu64, id);
576 
577   DataSource& data_source = it->second;
578   data_source.was_stopped = true;
579   ShutdownDataSource(&data_source);
580 }
581 
ShutdownDataSource(DataSource * data_source)582 void HeapprofdProducer::ShutdownDataSource(DataSource* data_source) {
583   data_source->shutting_down = true;
584   // If no processes connected, or all of them have already disconnected
585   // (and have been dumped) and no PIDs have been rejected,
586   // MaybeFinishDataSource can tear down the data source.
587   if (MaybeFinishDataSource(data_source))
588     return;
589 
590   if (!data_source->rejected_pids.empty()) {
591     auto trace_packet = data_source->trace_writer->NewTracePacket();
592     ProfilePacket* profile_packet = trace_packet->set_profile_packet();
593     for (pid_t rejected_pid : data_source->rejected_pids) {
594       ProfilePacket::ProcessHeapSamples* proto =
595           profile_packet->add_process_dumps();
596       proto->set_pid(static_cast<uint64_t>(rejected_pid));
597       proto->set_rejected_concurrent(true);
598     }
599     trace_packet->Finalize();
600     data_source->rejected_pids.clear();
601     if (MaybeFinishDataSource(data_source))
602       return;
603   }
604 
605   for (const auto& pid_and_process_state : data_source->process_states) {
606     pid_t pid = pid_and_process_state.first;
607     UnwinderForPID(pid).PostDisconnectSocket(pid);
608   }
609 
610   auto id = data_source->id;
611   auto weak_producer = weak_factory_.GetWeakPtr();
612   task_runner_->PostDelayedTask(
613       [weak_producer, id] {
614         if (!weak_producer)
615           return;
616         auto ds_it = weak_producer->data_sources_.find(id);
617         if (ds_it != weak_producer->data_sources_.end()) {
618           PERFETTO_ELOG("Final dump timed out.");
619           DataSource& ds = ds_it->second;
620           // Do not dump any stragglers, just trigger the Flush and tear down
621           // the data source.
622           ds.process_states.clear();
623           ds.rejected_pids.clear();
624           PERFETTO_CHECK(weak_producer->MaybeFinishDataSource(&ds));
625         }
626       },
627       data_source->stop_timeout_ms);
628 }
629 
DoContinuousDump(DataSourceInstanceID id,uint32_t dump_interval)630 void HeapprofdProducer::DoContinuousDump(DataSourceInstanceID id,
631                                          uint32_t dump_interval) {
632   auto it = data_sources_.find(id);
633   if (it == data_sources_.end())
634     return;
635   DataSource& data_source = it->second;
636   DumpProcessesInDataSource(&data_source);
637   auto weak_producer = weak_factory_.GetWeakPtr();
638   task_runner_->PostDelayedTask(
639       [weak_producer, id, dump_interval] {
640         if (!weak_producer)
641           return;
642         weak_producer->DoContinuousDump(id, dump_interval);
643       },
644       dump_interval);
645 }
646 
647 // static
SetStats(protos::pbzero::ProfilePacket::ProcessStats * stats,const ProcessState & process_state)648 void HeapprofdProducer::SetStats(
649     protos::pbzero::ProfilePacket::ProcessStats* stats,
650     const ProcessState& process_state) {
651   stats->set_unwinding_errors(process_state.unwinding_errors);
652   stats->set_heap_samples(process_state.heap_samples);
653   stats->set_map_reparses(process_state.map_reparses);
654   stats->set_total_unwinding_time_us(process_state.total_unwinding_time_us);
655   stats->set_client_spinlock_blocked_us(
656       process_state.client_spinlock_blocked_us);
657   auto* unwinding_hist = stats->set_unwinding_time_us();
658   for (const auto& p : process_state.unwinding_time_us.GetData()) {
659     auto* bucket = unwinding_hist->add_buckets();
660     if (p.first == LogHistogram::kMaxBucket)
661       bucket->set_max_bucket(true);
662     else
663       bucket->set_upper_limit(p.first);
664     bucket->set_count(p.second);
665   }
666 }
667 
DumpProcessState(DataSource * data_source,pid_t pid,ProcessState * process_state)668 void HeapprofdProducer::DumpProcessState(DataSource* data_source,
669                                          pid_t pid,
670                                          ProcessState* process_state) {
671   for (auto& heap_id_and_heap_info : process_state->heap_infos) {
672     ProcessState::HeapInfo& heap_info = heap_id_and_heap_info.second;
673 
674     bool from_startup = data_source->signaled_pids.find(pid) ==
675                         data_source->signaled_pids.cend();
676 
677     auto new_heapsamples = [pid, from_startup, process_state, data_source,
678                             &heap_info](
679                                ProfilePacket::ProcessHeapSamples* proto) {
680       proto->set_pid(static_cast<uint64_t>(pid));
681       proto->set_timestamp(heap_info.heap_tracker.dump_timestamp());
682       proto->set_from_startup(from_startup);
683       proto->set_disconnected(process_state->disconnected);
684       proto->set_buffer_overran(process_state->error_state ==
685                                 SharedRingBuffer::kHitTimeout);
686       proto->set_client_error(ErrorStateToProto(process_state->error_state));
687       proto->set_buffer_corrupted(process_state->buffer_corrupted);
688       proto->set_hit_guardrail(data_source->hit_guardrail);
689       if (!heap_info.heap_name.empty())
690         proto->set_heap_name(heap_info.heap_name.c_str());
691       proto->set_sampling_interval_bytes(heap_info.sampling_interval);
692       proto->set_orig_sampling_interval_bytes(heap_info.orig_sampling_interval);
693       auto* stats = proto->set_stats();
694       SetStats(stats, *process_state);
695     };
696 
697     DumpState dump_state(data_source->trace_writer.get(),
698                          std::move(new_heapsamples),
699                          &data_source->intern_state);
700 
701     heap_info.heap_tracker.GetCallstackAllocations(
702         [&dump_state,
703          &data_source](const HeapTracker::CallstackAllocations& alloc) {
704           dump_state.WriteAllocation(alloc, data_source->config.dump_at_max());
705         });
706     dump_state.DumpCallstacks(&callsites_);
707   }
708 }
709 
DumpProcessesInDataSource(DataSource * ds)710 void HeapprofdProducer::DumpProcessesInDataSource(DataSource* ds) {
711   for (std::pair<const pid_t, ProcessState>& pid_and_process_state :
712        ds->process_states) {
713     pid_t pid = pid_and_process_state.first;
714     ProcessState& process_state = pid_and_process_state.second;
715     DumpProcessState(ds, pid, &process_state);
716   }
717 }
718 
DumpAll()719 void HeapprofdProducer::DumpAll() {
720   PERFETTO_LOG("Received signal. Dumping all data sources.");
721   for (auto& id_and_data_source : data_sources_)
722     DumpProcessesInDataSource(&id_and_data_source.second);
723 }
724 
Flush(FlushRequestID flush_id,const DataSourceInstanceID * ids,size_t num_ids)725 void HeapprofdProducer::Flush(FlushRequestID flush_id,
726                               const DataSourceInstanceID* ids,
727                               size_t num_ids) {
728   size_t& flush_in_progress = flushes_in_progress_[flush_id];
729   PERFETTO_DCHECK(flush_in_progress == 0);
730   flush_in_progress = num_ids;
731   for (size_t i = 0; i < num_ids; ++i) {
732     auto it = data_sources_.find(ids[i]);
733     if (it == data_sources_.end()) {
734       PERFETTO_DFATAL_OR_ELOG("Trying to flush unknown data-source %" PRIu64,
735                               ids[i]);
736       flush_in_progress--;
737       continue;
738     }
739     DataSource& data_source = it->second;
740     auto weak_producer = weak_factory_.GetWeakPtr();
741 
742     auto callback = [weak_producer, flush_id] {
743       if (weak_producer)
744         // Reposting because this task runner could be on a different thread
745         // than the IPC task runner.
746         return weak_producer->task_runner_->PostTask([weak_producer, flush_id] {
747           if (weak_producer)
748             return weak_producer->FinishDataSourceFlush(flush_id);
749         });
750     };
751     data_source.trace_writer->Flush(std::move(callback));
752   }
753   if (flush_in_progress == 0) {
754     endpoint_->NotifyFlushComplete(flush_id);
755     flushes_in_progress_.erase(flush_id);
756   }
757 }
758 
FinishDataSourceFlush(FlushRequestID flush_id)759 void HeapprofdProducer::FinishDataSourceFlush(FlushRequestID flush_id) {
760   auto it = flushes_in_progress_.find(flush_id);
761   if (it == flushes_in_progress_.end()) {
762     PERFETTO_DFATAL_OR_ELOG("FinishDataSourceFlush id invalid: %" PRIu64,
763                             flush_id);
764     return;
765   }
766   size_t& flush_in_progress = it->second;
767   if (--flush_in_progress == 0) {
768     endpoint_->NotifyFlushComplete(flush_id);
769     flushes_in_progress_.erase(flush_id);
770   }
771 }
772 
OnDisconnect(base::UnixSocket * self)773 void HeapprofdProducer::SocketDelegate::OnDisconnect(base::UnixSocket* self) {
774   auto it = producer_->pending_processes_.find(self->peer_pid_linux());
775   if (it == producer_->pending_processes_.end()) {
776     PERFETTO_DFATAL_OR_ELOG("Unexpected disconnect.");
777     return;
778   }
779 
780   if (self == it->second.sock.get())
781     producer_->pending_processes_.erase(it);
782 }
783 
OnNewIncomingConnection(base::UnixSocket *,std::unique_ptr<base::UnixSocket> new_connection)784 void HeapprofdProducer::SocketDelegate::OnNewIncomingConnection(
785     base::UnixSocket*,
786     std::unique_ptr<base::UnixSocket> new_connection) {
787   Process peer_process;
788   peer_process.pid = new_connection->peer_pid_linux();
789   if (!GetCmdlineForPID(peer_process.pid, &peer_process.cmdline))
790     PERFETTO_PLOG("Failed to get cmdline for %d", peer_process.pid);
791 
792   producer_->HandleClientConnection(std::move(new_connection), peer_process);
793 }
794 
OnDataAvailable(base::UnixSocket * self)795 void HeapprofdProducer::SocketDelegate::OnDataAvailable(
796     base::UnixSocket* self) {
797   auto it = producer_->pending_processes_.find(self->peer_pid_linux());
798   if (it == producer_->pending_processes_.end()) {
799     PERFETTO_DFATAL_OR_ELOG("Unexpected data.");
800     return;
801   }
802 
803   PendingProcess& pending_process = it->second;
804 
805   base::ScopedFile fds[kHandshakeSize];
806   char buf[1];
807   self->Receive(buf, sizeof(buf), fds, base::ArraySize(fds));
808 
809   static_assert(kHandshakeSize == 2, "change if and else if below.");
810   if (fds[kHandshakeMaps] && fds[kHandshakeMem]) {
811     auto ds_it =
812         producer_->data_sources_.find(pending_process.data_source_instance_id);
813     if (ds_it == producer_->data_sources_.end()) {
814       producer_->pending_processes_.erase(it);
815       return;
816     }
817     DataSource& data_source = ds_it->second;
818 
819     if (data_source.shutting_down) {
820       producer_->pending_processes_.erase(it);
821       PERFETTO_LOG("Got handshake for DS that is shutting down. Rejecting.");
822       return;
823     }
824 
825     std::string maps_file =
826         "/proc/" + std::to_string(self->peer_pid_linux()) + "/maps";
827     if (!IsFile(*fds[kHandshakeMaps], maps_file.c_str())) {
828       producer_->pending_processes_.erase(it);
829       PERFETTO_ELOG("Received invalid maps FD.");
830       return;
831     }
832 
833     std::string mem_file =
834         "/proc/" + std::to_string(self->peer_pid_linux()) + "/mem";
835     if (!IsFile(*fds[kHandshakeMem], mem_file.c_str())) {
836       producer_->pending_processes_.erase(it);
837       PERFETTO_ELOG("Received invalid mem FD.");
838       return;
839     }
840 
841     data_source.process_states.emplace(
842         std::piecewise_construct, std::forward_as_tuple(self->peer_pid_linux()),
843         std::forward_as_tuple(&producer_->callsites_,
844                               data_source.config.dump_at_max()));
845 
846     PERFETTO_DLOG("%d: Received FDs.", self->peer_pid_linux());
847     int raw_fd = pending_process.shmem.fd();
848     // TODO(fmayer): Full buffer could deadlock us here.
849     if (!self->Send(&data_source.client_configuration,
850                     sizeof(data_source.client_configuration), &raw_fd, 1)) {
851       // If Send fails, the socket will have been Shutdown, and the raw socket
852       // closed.
853       producer_->pending_processes_.erase(it);
854       return;
855     }
856 
857     UnwindingWorker::HandoffData handoff_data;
858     handoff_data.data_source_instance_id =
859         pending_process.data_source_instance_id;
860     handoff_data.sock = self->ReleaseSocket();
861     handoff_data.maps_fd = std::move(fds[kHandshakeMaps]);
862     handoff_data.mem_fd = std::move(fds[kHandshakeMem]);
863     handoff_data.shmem = std::move(pending_process.shmem);
864     handoff_data.client_config = data_source.client_configuration;
865     handoff_data.stream_allocations = data_source.config.stream_allocations();
866 
867     producer_->UnwinderForPID(self->peer_pid_linux())
868         .PostHandoffSocket(std::move(handoff_data));
869     producer_->pending_processes_.erase(it);
870   } else if (fds[kHandshakeMaps] || fds[kHandshakeMem]) {
871     PERFETTO_DFATAL_OR_ELOG("%d: Received partial FDs.",
872                             self->peer_pid_linux());
873     producer_->pending_processes_.erase(it);
874   } else {
875     PERFETTO_ELOG("%d: Received no FDs.", self->peer_pid_linux());
876   }
877 }
878 
GetDataSourceForProcess(const Process & proc)879 HeapprofdProducer::DataSource* HeapprofdProducer::GetDataSourceForProcess(
880     const Process& proc) {
881   for (auto& ds_id_and_datasource : data_sources_) {
882     DataSource& ds = ds_id_and_datasource.second;
883     if (ConfigTargetsProcess(ds.config, proc, ds.normalized_cmdlines))
884       return &ds;
885   }
886   return nullptr;
887 }
888 
RecordOtherSourcesAsRejected(DataSource * active_ds,const Process & proc)889 void HeapprofdProducer::RecordOtherSourcesAsRejected(DataSource* active_ds,
890                                                      const Process& proc) {
891   for (auto& ds_id_and_datasource : data_sources_) {
892     DataSource& ds = ds_id_and_datasource.second;
893     if (&ds != active_ds &&
894         ConfigTargetsProcess(ds.config, proc, ds.normalized_cmdlines))
895       ds.rejected_pids.emplace(proc.pid);
896   }
897 }
898 
HandleClientConnection(std::unique_ptr<base::UnixSocket> new_connection,Process process)899 void HeapprofdProducer::HandleClientConnection(
900     std::unique_ptr<base::UnixSocket> new_connection,
901     Process process) {
902   DataSource* data_source = GetDataSourceForProcess(process);
903   if (!data_source) {
904     PERFETTO_LOG("No data source found.");
905     return;
906   }
907   RecordOtherSourcesAsRejected(data_source, process);
908 
909   // In fork mode, right now we check whether the target is not profileable
910   // in the client, because we cannot read packages.list there.
911   if (mode_ == HeapprofdMode::kCentral &&
912       !CanProfile(data_source->ds_config, new_connection->peer_uid_posix(),
913                   data_source->config.target_installed_by())) {
914     PERFETTO_ELOG("%d (%s) is not profileable.", process.pid,
915                   process.cmdline.c_str());
916     return;
917   }
918 
919   uint64_t shmem_size = data_source->config.shmem_size_bytes();
920   if (!shmem_size)
921     shmem_size = kDefaultShmemSize;
922   if (shmem_size > kMaxShmemSize) {
923     PERFETTO_LOG("Specified shared memory size of %" PRIu64
924                  " exceeds maximum size of %" PRIu64 ". Reducing.",
925                  shmem_size, kMaxShmemSize);
926     shmem_size = kMaxShmemSize;
927   }
928 
929   auto shmem = SharedRingBuffer::Create(static_cast<size_t>(shmem_size));
930   if (!shmem || !shmem->is_valid()) {
931     PERFETTO_LOG("Failed to create shared memory.");
932     return;
933   }
934 
935   pid_t peer_pid = new_connection->peer_pid_linux();
936   if (peer_pid != process.pid) {
937     PERFETTO_DFATAL_OR_ELOG("Invalid PID connected.");
938     return;
939   }
940 
941   PendingProcess pending_process;
942   pending_process.sock = std::move(new_connection);
943   pending_process.data_source_instance_id = data_source->id;
944   pending_process.shmem = std::move(*shmem);
945   pending_processes_.emplace(peer_pid, std::move(pending_process));
946 }
947 
PostAllocRecord(UnwindingWorker * worker,std::unique_ptr<AllocRecord> alloc_rec)948 void HeapprofdProducer::PostAllocRecord(
949     UnwindingWorker* worker,
950     std::unique_ptr<AllocRecord> alloc_rec) {
951   // Once we can use C++14, this should be std::moved into the lambda instead.
952   auto* raw_alloc_rec = alloc_rec.release();
953   auto weak_this = weak_factory_.GetWeakPtr();
954   task_runner_->PostTask([weak_this, raw_alloc_rec, worker] {
955     std::unique_ptr<AllocRecord> unique_alloc_ref =
956         std::unique_ptr<AllocRecord>(raw_alloc_rec);
957     if (weak_this) {
958       weak_this->HandleAllocRecord(unique_alloc_ref.get());
959       worker->ReturnAllocRecord(std::move(unique_alloc_ref));
960     }
961   });
962 }
963 
PostFreeRecord(UnwindingWorker *,std::vector<FreeRecord> free_recs)964 void HeapprofdProducer::PostFreeRecord(UnwindingWorker*,
965                                        std::vector<FreeRecord> free_recs) {
966   // Once we can use C++14, this should be std::moved into the lambda instead.
967   std::vector<FreeRecord>* raw_free_recs =
968       new std::vector<FreeRecord>(std::move(free_recs));
969   auto weak_this = weak_factory_.GetWeakPtr();
970   task_runner_->PostTask([weak_this, raw_free_recs] {
971     if (weak_this) {
972       for (FreeRecord& free_rec : *raw_free_recs)
973         weak_this->HandleFreeRecord(std::move(free_rec));
974     }
975     delete raw_free_recs;
976   });
977 }
978 
PostHeapNameRecord(UnwindingWorker *,HeapNameRecord rec)979 void HeapprofdProducer::PostHeapNameRecord(UnwindingWorker*,
980                                            HeapNameRecord rec) {
981   auto weak_this = weak_factory_.GetWeakPtr();
982   task_runner_->PostTask([weak_this, rec] {
983     if (weak_this)
984       weak_this->HandleHeapNameRecord(rec);
985   });
986 }
987 
PostSocketDisconnected(UnwindingWorker *,DataSourceInstanceID ds_id,pid_t pid,SharedRingBuffer::Stats stats)988 void HeapprofdProducer::PostSocketDisconnected(UnwindingWorker*,
989                                                DataSourceInstanceID ds_id,
990                                                pid_t pid,
991                                                SharedRingBuffer::Stats stats) {
992   auto weak_this = weak_factory_.GetWeakPtr();
993   task_runner_->PostTask([weak_this, ds_id, pid, stats] {
994     if (weak_this)
995       weak_this->HandleSocketDisconnected(ds_id, pid, stats);
996   });
997 }
998 
HandleAllocRecord(AllocRecord * alloc_rec)999 void HeapprofdProducer::HandleAllocRecord(AllocRecord* alloc_rec) {
1000   const AllocMetadata& alloc_metadata = alloc_rec->alloc_metadata;
1001   auto it = data_sources_.find(alloc_rec->data_source_instance_id);
1002   if (it == data_sources_.end()) {
1003     PERFETTO_LOG("Invalid data source in alloc record.");
1004     return;
1005   }
1006 
1007   DataSource& ds = it->second;
1008   auto process_state_it = ds.process_states.find(alloc_rec->pid);
1009   if (process_state_it == ds.process_states.end()) {
1010     PERFETTO_LOG("Invalid PID in alloc record.");
1011     return;
1012   }
1013 
1014   if (ds.config.stream_allocations()) {
1015     auto packet = ds.trace_writer->NewTracePacket();
1016     auto* streaming_alloc = packet->set_streaming_allocation();
1017     streaming_alloc->add_address(alloc_metadata.alloc_address);
1018     streaming_alloc->add_size(alloc_metadata.alloc_size);
1019     streaming_alloc->add_sample_size(alloc_metadata.sample_size);
1020     streaming_alloc->add_clock_monotonic_coarse_timestamp(
1021         alloc_metadata.clock_monotonic_coarse_timestamp);
1022     streaming_alloc->add_heap_id(alloc_metadata.heap_id);
1023     streaming_alloc->add_sequence_number(alloc_metadata.sequence_number);
1024     return;
1025   }
1026 
1027   const auto& prefixes = ds.config.skip_symbol_prefix();
1028   if (!prefixes.empty()) {
1029     for (unwindstack::FrameData& frame_data : alloc_rec->frames) {
1030       if (frame_data.map_info == nullptr) {
1031         continue;
1032       }
1033       const std::string& map = frame_data.map_info->name();
1034       if (std::find_if(prefixes.cbegin(), prefixes.cend(),
1035                        [&map](const std::string& prefix) {
1036                          return base::StartsWith(map, prefix);
1037                        }) != prefixes.cend()) {
1038         frame_data.function_name = "FILTERED";
1039       }
1040     }
1041   }
1042 
1043   ProcessState& process_state = process_state_it->second;
1044   HeapTracker& heap_tracker =
1045       process_state.GetHeapTracker(alloc_rec->alloc_metadata.heap_id);
1046 
1047   if (alloc_rec->error)
1048     process_state.unwinding_errors++;
1049   if (alloc_rec->reparsed_map)
1050     process_state.map_reparses++;
1051   process_state.heap_samples++;
1052   process_state.unwinding_time_us.Add(alloc_rec->unwinding_time_us);
1053   process_state.total_unwinding_time_us += alloc_rec->unwinding_time_us;
1054 
1055   // abspc may no longer refer to the same functions, as we had to reparse
1056   // maps. Reset the cache.
1057   if (alloc_rec->reparsed_map)
1058     heap_tracker.ClearFrameCache();
1059 
1060   heap_tracker.RecordMalloc(
1061       alloc_rec->frames, alloc_rec->build_ids, alloc_metadata.alloc_address,
1062       alloc_metadata.sample_size, alloc_metadata.alloc_size,
1063       alloc_metadata.sequence_number,
1064       alloc_metadata.clock_monotonic_coarse_timestamp);
1065 }
1066 
HandleFreeRecord(FreeRecord free_rec)1067 void HeapprofdProducer::HandleFreeRecord(FreeRecord free_rec) {
1068   auto it = data_sources_.find(free_rec.data_source_instance_id);
1069   if (it == data_sources_.end()) {
1070     PERFETTO_LOG("Invalid data source in free record.");
1071     return;
1072   }
1073 
1074   DataSource& ds = it->second;
1075   auto process_state_it = ds.process_states.find(free_rec.pid);
1076   if (process_state_it == ds.process_states.end()) {
1077     PERFETTO_LOG("Invalid PID in free record.");
1078     return;
1079   }
1080 
1081   if (ds.config.stream_allocations()) {
1082     auto packet = ds.trace_writer->NewTracePacket();
1083     auto* streaming_free = packet->set_streaming_free();
1084     streaming_free->add_address(free_rec.entry.addr);
1085     streaming_free->add_heap_id(free_rec.entry.heap_id);
1086     streaming_free->add_sequence_number(free_rec.entry.sequence_number);
1087     return;
1088   }
1089 
1090   ProcessState& process_state = process_state_it->second;
1091 
1092   const FreeEntry& entry = free_rec.entry;
1093   HeapTracker& heap_tracker = process_state.GetHeapTracker(entry.heap_id);
1094   heap_tracker.RecordFree(entry.addr, entry.sequence_number, 0);
1095 }
1096 
HandleHeapNameRecord(HeapNameRecord rec)1097 void HeapprofdProducer::HandleHeapNameRecord(HeapNameRecord rec) {
1098   auto it = data_sources_.find(rec.data_source_instance_id);
1099   if (it == data_sources_.end()) {
1100     PERFETTO_LOG("Invalid data source in free record.");
1101     return;
1102   }
1103 
1104   DataSource& ds = it->second;
1105   auto process_state_it = ds.process_states.find(rec.pid);
1106   if (process_state_it == ds.process_states.end()) {
1107     PERFETTO_LOG("Invalid PID in free record.");
1108     return;
1109   }
1110 
1111   ProcessState& process_state = process_state_it->second;
1112   const HeapName& entry = rec.entry;
1113   if (entry.heap_name[0] != '\0') {
1114     std::string heap_name = entry.heap_name;
1115     if (entry.heap_id == 0) {
1116       PERFETTO_ELOG("Invalid zero heap ID.");
1117       return;
1118     }
1119     ProcessState::HeapInfo& hi = process_state.GetHeapInfo(entry.heap_id);
1120     if (!hi.heap_name.empty() && hi.heap_name != heap_name) {
1121       PERFETTO_ELOG("Overriding heap name %s with %s", hi.heap_name.c_str(),
1122                     heap_name.c_str());
1123     }
1124     hi.heap_name = entry.heap_name;
1125   }
1126   if (entry.sample_interval != 0) {
1127     ProcessState::HeapInfo& hi = process_state.GetHeapInfo(entry.heap_id);
1128     if (!hi.sampling_interval)
1129       hi.orig_sampling_interval = entry.sample_interval;
1130     hi.sampling_interval = entry.sample_interval;
1131   }
1132 }
1133 
TerminateWhenDone()1134 void HeapprofdProducer::TerminateWhenDone() {
1135   if (data_sources_.empty())
1136     TerminateProcess(0);
1137   exit_when_done_ = true;
1138 }
1139 
MaybeFinishDataSource(DataSource * ds)1140 bool HeapprofdProducer::MaybeFinishDataSource(DataSource* ds) {
1141   if (!ds->process_states.empty() || !ds->rejected_pids.empty() ||
1142       !ds->shutting_down) {
1143     return false;
1144   }
1145 
1146   bool was_stopped = ds->was_stopped;
1147   DataSourceInstanceID ds_id = ds->id;
1148   auto weak_producer = weak_factory_.GetWeakPtr();
1149   bool exit_when_done = exit_when_done_;
1150   ds->trace_writer->Flush([weak_producer, exit_when_done, ds_id, was_stopped] {
1151     if (!weak_producer)
1152       return;
1153 
1154     if (was_stopped)
1155       weak_producer->endpoint_->NotifyDataSourceStopped(ds_id);
1156     weak_producer->data_sources_.erase(ds_id);
1157 
1158     if (exit_when_done) {
1159       // Post this as a task to allow NotifyDataSourceStopped to post tasks.
1160       weak_producer->task_runner_->PostTask([weak_producer] {
1161         if (!weak_producer)
1162           return;
1163         weak_producer->TerminateProcess(
1164             /*exit_status=*/0);  // does not return
1165       });
1166     }
1167   });
1168   return true;
1169 }
1170 
HandleSocketDisconnected(DataSourceInstanceID ds_id,pid_t pid,SharedRingBuffer::Stats stats)1171 void HeapprofdProducer::HandleSocketDisconnected(
1172     DataSourceInstanceID ds_id,
1173     pid_t pid,
1174     SharedRingBuffer::Stats stats) {
1175   auto it = data_sources_.find(ds_id);
1176   if (it == data_sources_.end())
1177     return;
1178   DataSource& ds = it->second;
1179 
1180   auto process_state_it = ds.process_states.find(pid);
1181   if (process_state_it == ds.process_states.end()) {
1182     PERFETTO_ELOG("Unexpected disconnect from %d", pid);
1183     return;
1184   }
1185 
1186   PERFETTO_LOG("%d disconnected from heapprofd (ds shutting down: %d).", pid,
1187                ds.shutting_down);
1188 
1189   ProcessState& process_state = process_state_it->second;
1190   process_state.disconnected = !ds.shutting_down;
1191   process_state.error_state = stats.error_state;
1192   process_state.client_spinlock_blocked_us = stats.client_spinlock_blocked_us;
1193   process_state.buffer_corrupted =
1194       stats.num_writes_corrupt > 0 || stats.num_reads_corrupt > 0;
1195 
1196   DumpProcessState(&ds, pid, &process_state);
1197   ds.process_states.erase(pid);
1198   MaybeFinishDataSource(&ds);
1199 }
1200 
CheckDataSourceCpuTask()1201 void HeapprofdProducer::CheckDataSourceCpuTask() {
1202   auto weak_producer = weak_factory_.GetWeakPtr();
1203   task_runner_->PostDelayedTask(
1204       [weak_producer] {
1205         if (!weak_producer)
1206           return;
1207         weak_producer->CheckDataSourceCpuTask();
1208       },
1209       kGuardrailIntervalMs);
1210 
1211   ProfilerCpuGuardrails gr;
1212   for (auto& p : data_sources_) {
1213     DataSource& ds = p.second;
1214     if (gr.IsOverCpuThreshold(ds.guardrail_config)) {
1215       ds.hit_guardrail = true;
1216       PERFETTO_LOG("Data source %" PRIu64 " hit CPU guardrail. Shutting down.",
1217                    ds.id);
1218       ShutdownDataSource(&ds);
1219     }
1220   }
1221 }
1222 
CheckDataSourceMemoryTask()1223 void HeapprofdProducer::CheckDataSourceMemoryTask() {
1224   auto weak_producer = weak_factory_.GetWeakPtr();
1225   task_runner_->PostDelayedTask(
1226       [weak_producer] {
1227         if (!weak_producer)
1228           return;
1229         weak_producer->CheckDataSourceMemoryTask();
1230       },
1231       kGuardrailIntervalMs);
1232   ProfilerMemoryGuardrails gr;
1233   for (auto& p : data_sources_) {
1234     DataSource& ds = p.second;
1235     if (gr.IsOverMemoryThreshold(ds.guardrail_config)) {
1236       ds.hit_guardrail = true;
1237       PERFETTO_LOG("Data source %" PRIu64
1238                    " hit memory guardrail. Shutting down.",
1239                    ds.id);
1240       ShutdownDataSource(&ds);
1241     }
1242   }
1243 }
1244 
1245 }  // namespace profiling
1246 }  // namespace perfetto
1247