blob: 1d1dd090bc35bf3840ed492fe77926ef59bf7ccb [file] [log] [blame]
brettwbd8214bf2017-06-20 03:47:031// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_
6#define CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_
7
8#include <string>
Erik Chene266c732017-08-12 02:27:199#include <vector>
brettwbd8214bf2017-06-20 03:47:0310
11#include "base/containers/flat_map.h"
erikchen66af0162017-08-02 19:53:1912#include "base/files/file.h"
Albert J. Wong3432f462017-08-02 02:47:2413#include "base/files/platform_file.h"
brettwbd8214bf2017-06-20 03:47:0314#include "base/macros.h"
erikchena4d19192017-08-23 17:33:1615#include "base/memory/weak_ptr.h"
Albert J. Wong59d85acb2017-08-10 00:50:5716#include "base/process/process_handle.h"
erikchen66af0162017-08-02 19:53:1917#include "base/synchronization/lock.h"
erikchen21a2cab2017-08-21 20:50:4418#include "base/values.h"
brettwbd8214bf2017-06-20 03:47:0319#include "build/build_config.h"
Erik Chen5eaed0e2017-08-26 22:16:4920#include "chrome/common/profiling/memlog.mojom.h"
brettwbd8214bf2017-06-20 03:47:0321#include "chrome/profiling/backtrace_storage.h"
Erik Chene266c732017-08-12 02:27:1922#include "services/resource_coordinator/public/interfaces/memory_instrumentation/memory_instrumentation.mojom.h"
brettwbd8214bf2017-06-20 03:47:0323
24namespace base {
Erik Chene266c732017-08-12 02:27:1925
Albert J. Wong3432f462017-08-02 02:47:2426class SequencedTaskRunner;
Erik Chene266c732017-08-12 02:27:1927
28} // namespace base
brettwbd8214bf2017-06-20 03:47:0329
30namespace profiling {
31
32// Manages all connections and logging for each process. Pipes are supplied by
33// the pipe server and this class will connect them to a parser and logger.
Albert J. Wong3432f462017-08-02 02:47:2434//
35// Note |backtrace_storage| must outlive MemlogConnectionManager.
36//
37// This object is constructed on the UI thread, but the rest of the usage
38// (including deletion) is on the IO thread.
39class MemlogConnectionManager {
brettwbd8214bf2017-06-20 03:47:0340 public:
erikchena4d19192017-08-23 17:33:1641 MemlogConnectionManager();
Albert J. Wong3432f462017-08-02 02:47:2442 ~MemlogConnectionManager();
brettwbd8214bf2017-06-20 03:47:0343
erikchen708c31d2017-09-19 00:28:5344 // Parameters to DumpProcess().
45 struct DumpProcessArgs {
46 DumpProcessArgs();
47 ~DumpProcessArgs();
48 DumpProcessArgs(DumpProcessArgs&&);
49 base::ProcessId pid;
50 std::unique_ptr<base::DictionaryValue> metadata;
51 std::vector<memory_instrumentation::mojom::VmRegionPtr> maps;
52 base::File file;
53 mojom::Memlog::DumpProcessCallback callback;
54
55 private:
56 DISALLOW_COPY_AND_ASSIGN(DumpProcessArgs);
57 };
58
59 // Dumps the memory log for the given process into |args.output_file|. This
60 // must be provided the memory map for the given process since that is not
61 // tracked as part of the normal allocation process. When
62 // |hop_to_connection_thread| is |true|, the task is posted onto the
63 // connection thread's task runner, and then back onto the current task
64 // runner. This ensures that all queued tasks on the connection's thread are
65 // flushed before the dump is executed.
66 void DumpProcess(DumpProcessArgs args, bool hop_to_connection_thread);
67
Erik Chenecc10212017-10-02 23:08:4568 void DumpProcessesForTracing(
69 mojom::Memlog::DumpProcessesForTracingCallback callback,
70 memory_instrumentation::mojom::GlobalMemoryDumpPtr dump);
erikchen66af0162017-08-02 19:53:1971
Albert J. Wong59d85acb2017-08-10 00:50:5772 void OnNewConnection(base::ScopedPlatformFile file, base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:0373
74 private:
75 struct Connection;
76
erikchen708c31d2017-09-19 00:28:5377 // This method is posted on the connection's thread, and immediately reposts
78 // DumpProcess back to |task_runner|. This ensures that all queued tasks on
79 // the connection's thread are flushed before the dump is executed.
80 static void HopToConnectionThread(
81 base::WeakPtr<MemlogConnectionManager> manager,
82 DumpProcessArgs args,
83 scoped_refptr<base::SequencedTaskRunner> task_runner);
84
brettwbd8214bf2017-06-20 03:47:0385 // Notification that a connection is complete. Unlike OnNewConnection which
86 // is signaled by the pipe server, this is signaled by the allocation tracker
87 // to ensure that the pipeline for this process has been flushed of all
88 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:5789 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:0390
91 void OnConnectionCompleteThunk(
erikchena4d19192017-08-23 17:33:1692 scoped_refptr<base::SequencedTaskRunner> main_loop,
Albert J. Wong59d85acb2017-08-10 00:50:5793 base::ProcessId process_id);
brettwbd8214bf2017-06-20 03:47:0394
erikchen68532cc2017-08-16 19:38:3695 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:2496
brettwbd8214bf2017-06-20 03:47:0397 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:5798 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:1999 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03100
erikchena4d19192017-08-23 17:33:16101 // Must be last.
102 base::WeakPtrFactory<MemlogConnectionManager> weak_factory_;
103
brettwbd8214bf2017-06-20 03:47:03104 DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager);
105};
106
107} // namespace profiling
108
109#endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_