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 SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_GLOBAL_MEMORY_DUMP_H_
6 #define SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_GLOBAL_MEMORY_DUMP_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 #include "base/component_export.h"
14 #include "base/optional.h"
15 #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
16 
17 namespace memory_instrumentation {
18 
19 // The returned data structure to consumers of the memory_instrumentation
20 // service containing dumps for each process.
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION)21 class COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION)
22     GlobalMemoryDump {
23  public:
24   class COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION)
25       ProcessDump {
26    public:
27     ProcessDump(mojom::ProcessMemoryDumpPtr process_memory_dump);
28     ~ProcessDump();
29 
30     // Returns the metric for the given dump name and metric name. For example,
31     // GetMetric("blink", "size") would return the aggregated sze of the
32     // "blink/" dump.
33     base::Optional<uint64_t> GetMetric(const std::string& dump_name,
34                                        const std::string& metric_name) const;
35 
36     base::ProcessId pid() const { return raw_dump_->pid; }
37     mojom::ProcessType process_type() const { return raw_dump_->process_type; }
38     const base::Optional<std::string>& service_name() const {
39       return raw_dump_->service_name;
40     }
41 
42     const mojom::OSMemDump& os_dump() const { return *raw_dump_->os_dump; }
43 
44    private:
45     mojom::ProcessMemoryDumpPtr raw_dump_;
46 
47     DISALLOW_COPY_AND_ASSIGN(ProcessDump);
48   };
49 
50  public:
51   class COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MEMORY_INSTRUMENTATION)
52       AggregatedMetrics {
53    public:
54     explicit AggregatedMetrics(mojom::AggregatedMetricsPtr aggregated_metrics);
55     ~AggregatedMetrics();
56 
57     int32_t native_library_resident_kb() const {
58       return aggregated_metrics_->native_library_resident_kb;
59     }
60 
61     int32_t native_library_resident_not_ordered_kb() const {
62       return aggregated_metrics_->native_library_resident_not_ordered_kb;
63     }
64 
65     int32_t native_library_not_resident_ordered_kb() const {
66       return aggregated_metrics_->native_library_not_resident_ordered_kb;
67     }
68 
69     static constexpr int32_t kInvalid = -1;
70 
71    private:
72     const mojom::AggregatedMetricsPtr aggregated_metrics_;
73 
74     DISALLOW_COPY_AND_ASSIGN(AggregatedMetrics);
75   };
76 
77   ~GlobalMemoryDump();
78 
79   // Creates an owned instance of this class wrapping the given mojo struct.
80   static std::unique_ptr<GlobalMemoryDump> MoveFrom(
81       mojom::GlobalMemoryDumpPtr ptr);
82 
83   const std::forward_list<ProcessDump>& process_dumps() const {
84     return process_dumps_;
85   }
86 
87   const AggregatedMetrics& aggregated_metrics() { return aggregated_metrics_; }
88 
89  private:
90   GlobalMemoryDump(std::vector<mojom::ProcessMemoryDumpPtr> process_dumps,
91                    mojom::AggregatedMetricsPtr aggregated_metrics);
92 
93   std::forward_list<ProcessDump> process_dumps_;
94   AggregatedMetrics aggregated_metrics_;
95 
96   DISALLOW_COPY_AND_ASSIGN(GlobalMemoryDump);
97 };
98 
99 }  // namespace memory_instrumentation
100 
101 #endif  // SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_GLOBAL_MEMORY_DUMP_H_
102