queued_request_dispatcher.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef SERVICES_RESOURCE_COORDINATOR_MEMORY_INSTRUMENTATION_QUEUED_REQUEST_DISPATCHER_H_
  5. #define SERVICES_RESOURCE_COORDINATOR_MEMORY_INSTRUMENTATION_QUEUED_REQUEST_DISPATCHER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/containers/flat_map.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/trace_event/memory_dump_request_args.h"
  13. #include "services/resource_coordinator/memory_instrumentation/coordinator_impl.h"
  14. #include "services/resource_coordinator/memory_instrumentation/queued_request.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "third_party/perfetto/include/perfetto/ext/trace_processor/importers/memory_tracker/graph.h"
  17. namespace memory_instrumentation {
  18. class QueuedRequestDispatcher {
  19. public:
  20. using OSMemDumpMap =
  21. base::flat_map<base::ProcessId,
  22. memory_instrumentation::mojom::RawOSMemDumpPtr>;
  23. using RequestGlobalMemoryDumpInternalCallback = base::OnceCallback<
  24. void(bool, uint64_t, memory_instrumentation::mojom::GlobalMemoryDumpPtr)>;
  25. using ChromeCallback = base::RepeatingCallback<void(
  26. base::ProcessId,
  27. bool,
  28. uint64_t,
  29. std::unique_ptr<base::trace_event::ProcessMemoryDump>)>;
  30. using OsCallback =
  31. base::RepeatingCallback<void(base::ProcessId, bool, OSMemDumpMap)>;
  32. using VmRegions =
  33. base::flat_map<base::ProcessId,
  34. std::vector<memory_instrumentation::mojom::VmRegionPtr>>;
  35. struct ClientInfo {
  36. ClientInfo(mojom::ClientProcess* client,
  37. base::ProcessId pid,
  38. mojom::ProcessType process_type,
  39. absl::optional<std::string> service_name);
  40. ClientInfo(ClientInfo&& other);
  41. ~ClientInfo();
  42. const raw_ptr<mojom::ClientProcess> client;
  43. const base::ProcessId pid;
  44. const mojom::ProcessType process_type;
  45. const absl::optional<std::string> service_name;
  46. };
  47. // Sets up the parameters of the queued |request| using |clients| and then
  48. // dispatches the request for a memory dump to each client process.
  49. static void SetUpAndDispatch(QueuedRequest* request,
  50. const std::vector<ClientInfo>& clients,
  51. const ChromeCallback& chrome_callback,
  52. const OsCallback& os_callback);
  53. // Finalizes the queued |request| by collating all responses recieved and
  54. // dispatching to the appropriate callback. Also adds to tracing using
  55. // |tracing_observer| if the |request| requires it.
  56. static void Finalize(QueuedRequest* request,
  57. TracingObserver* tracing_observer,
  58. bool use_proto_writer);
  59. static void SetUpAndDispatchVmRegionRequest(
  60. QueuedVmRegionRequest* request,
  61. const std::vector<ClientInfo>& clients,
  62. const std::vector<base::ProcessId>& desired_pids,
  63. const OsCallback& os_callback);
  64. static VmRegions FinalizeVmRegionRequest(QueuedVmRegionRequest* request);
  65. private:
  66. static bool AddChromeMemoryDumpToTrace(
  67. const base::trace_event::MemoryDumpRequestArgs& args,
  68. base::ProcessId pid,
  69. const base::trace_event::ProcessMemoryDump& raw_chrome_dump,
  70. const perfetto::trace_processor::GlobalNodeGraph& global_graph,
  71. const std::map<base::ProcessId, mojom::ProcessType>& pid_to_process_type,
  72. TracingObserver* tracing_observer,
  73. bool use_proto_writer,
  74. const base::TimeTicks& timestamp);
  75. };
  76. } // namespace memory_instrumentation
  77. #endif // SERVICES_RESOURCE_COORDINATOR_MEMORY_INSTRUMENTATION_QUEUED_REQUEST_DISPATCHER_H_