blob: c3f287a4234daa149ab29bf5e79191f9ab001485 [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"
Albert J. Wong59d85acb2017-08-10 00:50:5715#include "base/process/process_handle.h"
erikchen66af0162017-08-02 19:53:1916#include "base/synchronization/lock.h"
brettwbd8214bf2017-06-20 03:47:0317#include "build/build_config.h"
18#include "chrome/profiling/backtrace_storage.h"
Erik Chene266c732017-08-12 02:27:1919#include "services/resource_coordinator/public/interfaces/memory_instrumentation/memory_instrumentation.mojom.h"
brettwbd8214bf2017-06-20 03:47:0320
21namespace base {
Erik Chene266c732017-08-12 02:27:1922
Albert J. Wong3432f462017-08-02 02:47:2423class SequencedTaskRunner;
brettwbd8214bf2017-06-20 03:47:0324class SingleThreadTaskRunner;
Erik Chene266c732017-08-12 02:27:1925
26} // namespace base
brettwbd8214bf2017-06-20 03:47:0327
28namespace profiling {
29
30// Manages all connections and logging for each process. Pipes are supplied by
31// the pipe server and this class will connect them to a parser and logger.
Albert J. Wong3432f462017-08-02 02:47:2432//
33// Note |backtrace_storage| must outlive MemlogConnectionManager.
34//
35// This object is constructed on the UI thread, but the rest of the usage
36// (including deletion) is on the IO thread.
37class MemlogConnectionManager {
brettwbd8214bf2017-06-20 03:47:0338 public:
erikchen68532cc2017-08-16 19:38:3639 explicit MemlogConnectionManager(
40 scoped_refptr<base::SequencedTaskRunner> io_runner);
Albert J. Wong3432f462017-08-02 02:47:2441 ~MemlogConnectionManager();
brettwbd8214bf2017-06-20 03:47:0342
Erik Chene266c732017-08-12 02:27:1943 // Dumps the memory log for the given process into |output_file|. This must
44 // be provided the memory map for the given process since that is not tracked
45 // as part of the normal allocation process.
46 void DumpProcess(
47 base::ProcessId pid,
48 const std::vector<memory_instrumentation::mojom::VmRegionPtr>& maps,
49 base::File output_file);
erikchen66af0162017-08-02 19:53:1950
Albert J. Wong59d85acb2017-08-10 00:50:5751 void OnNewConnection(base::ScopedPlatformFile file, base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:0352
53 private:
54 struct Connection;
55
brettwbd8214bf2017-06-20 03:47:0356 // Notification that a connection is complete. Unlike OnNewConnection which
57 // is signaled by the pipe server, this is signaled by the allocation tracker
58 // to ensure that the pipeline for this process has been flushed of all
59 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:5760 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:0361
62 void OnConnectionCompleteThunk(
63 scoped_refptr<base::SingleThreadTaskRunner> main_loop,
Albert J. Wong59d85acb2017-08-10 00:50:5764 base::ProcessId process_id);
brettwbd8214bf2017-06-20 03:47:0365
Albert J. Wong3432f462017-08-02 02:47:2466 scoped_refptr<base::SequencedTaskRunner> io_runner_;
erikchen68532cc2017-08-16 19:38:3667 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:2468
brettwbd8214bf2017-06-20 03:47:0369 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:5770 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:1971 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:0372
73 DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager);
74};
75
76} // namespace profiling
77
78#endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_