1 // 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 BASE_TRACE_EVENT_MEMORY_DUMP_PROVIDER_INFO_H_
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_PROVIDER_INFO_H_
7 
8 #include <memory>
9 #include <set>
10 
11 #include "base/base_export.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/trace_event/memory_dump_provider.h"
14 
15 namespace base {
16 
17 class SequencedTaskRunner;
18 
19 namespace trace_event {
20 
21 // Wraps a MemoryDumpProvider (MDP), which is registered via
22 // MemoryDumpManager(MDM)::RegisterDumpProvider(), holding the extra information
23 // required to deal with it (which task runner it should be invoked onto,
24 // whether it has been disabled, etc.)
25 // More importantly, having a refptr to this object guarantees that a MDP that
26 // is not thread-bound (hence which can only be unregistered via
27 // MDM::UnregisterAndDeleteDumpProviderSoon()) will stay alive as long as the
28 // refptr is held.
29 //
30 // Lifetime:
31 // At any time, there is at most one instance of this class for each instance
32 // of a given MemoryDumpProvider, but there might be several scoped_refptr
33 // holding onto each of this. Specifically:
34 // - In nominal conditions, there is a refptr for each registered MDP in the
35 //   MDM's |dump_providers_| list.
36 // - In most cases, the only refptr (in the |dump_providers_| list) is destroyed
37 //   by MDM::UnregisterDumpProvider().
38 // - However, when MDM starts a dump, the list of refptrs is copied into the
39 //   ProcessMemoryDumpAsyncState. That list is pruned as MDP(s) are invoked.
40 // - If UnregisterDumpProvider() is called on a non-thread-bound MDP while a
41 //   dump is in progress, the extar extra of the handle is destroyed in
42 //   MDM::SetupNextMemoryDump() or MDM::InvokeOnMemoryDump(), when the copy
43 //   inside ProcessMemoryDumpAsyncState is erase()-d.
44 // - The PeakDetector can keep extra refptrs when enabled.
45 struct BASE_EXPORT MemoryDumpProviderInfo
46     : public RefCountedThreadSafe<MemoryDumpProviderInfo> {
47  public:
48   // Define a total order based on the |task_runner| affinity, so that MDPs
49   // belonging to the same SequencedTaskRunner are adjacent in the set.
50   struct Comparator {
51     bool operator()(const scoped_refptr<MemoryDumpProviderInfo>& a,
52                     const scoped_refptr<MemoryDumpProviderInfo>& b) const;
53   };
54   using OrderedSet =
55       std::set<scoped_refptr<MemoryDumpProviderInfo>, Comparator>;
56 
57   MemoryDumpProviderInfo(MemoryDumpProvider* dump_provider,
58                          const char* name,
59                          scoped_refptr<SequencedTaskRunner> task_runner,
60                          const MemoryDumpProvider::Options& options,
61                          bool allowed_in_background_mode);
62 
63   // It is safe to access the const fields below from any thread as they are
64   // never mutated.
65 
66   MemoryDumpProvider* const dump_provider;
67 
68   // The |options| arg passed to MDM::RegisterDumpProvider().
69   const MemoryDumpProvider::Options options;
70 
71   // Human readable name, not unique (distinct MDP instances might have the same
72   // name). Used for debugging, testing and whitelisting for BACKGROUND mode.
73   const char* const name;
74 
75   // The task runner on which the MDP::OnMemoryDump call should be posted onto.
76   // Can be nullptr, in which case the MDP will be invoked on a background
77   // thread handled by MDM.
78   const scoped_refptr<SequencedTaskRunner> task_runner;
79 
80   // True if the dump provider is whitelisted for background mode.
81   const bool allowed_in_background_mode;
82 
83   // These fields below, instead, are not thread safe and can be mutated only:
84   // - On the |task_runner|, when not null (i.e. for thread-bound MDPS).
85   // - By the MDM's background thread (or in any other way that guarantees
86   //   sequencing) for non-thread-bound MDPs.
87 
88   // Used to transfer ownership for UnregisterAndDeleteDumpProviderSoon().
89   // nullptr in all other cases.
90   std::unique_ptr<MemoryDumpProvider> owned_dump_provider;
91 
92   // For fail-safe logic (auto-disable failing MDPs).
93   int consecutive_failures;
94 
95   // Flagged either by the auto-disable logic or during unregistration.
96   bool disabled;
97 
98  private:
99   friend class base::RefCountedThreadSafe<MemoryDumpProviderInfo>;
100   ~MemoryDumpProviderInfo();
101 
102   DISALLOW_COPY_AND_ASSIGN(MemoryDumpProviderInfo);
103 };
104 
105 }  // namespace trace_event
106 }  // namespace base
107 
108 #endif  // BASE_TRACE_EVENT_MEMORY_DUMP_PROVIDER_INFO_H_
109