Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <memory> |
| 6 | #include <vector> |
| 7 | |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 8 | #include "base/command_line.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
| 10 | #include "base/functional/callback_forward.h" |
| 11 | #include "base/functional/callback_helpers.h" |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 13 | #include "base/path_service.h" |
| 14 | #include "base/rand_util.h" |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 15 | #include "base/task/thread_pool.h" |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 16 | #include "build/build_config.h" |
| 17 | #include "content/public/browser/browser_child_process_host_iterator.h" |
| 18 | #include "content/public/browser/browser_task_traits.h" |
| 19 | #include "content/public/browser/browser_thread.h" |
Will Harris | 6f5f50a | 2021-12-03 17:39:45 | [diff] [blame] | 20 | #include "content/public/browser/child_process_data.h" |
Will Harris | cd57b83 | 2023-01-05 20:03:10 | [diff] [blame] | 21 | #include "content/public/browser/child_process_host.h" |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 22 | #include "content/public/browser/gpu_utils.h" |
| 23 | #include "content/public/browser/render_process_host.h" |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 24 | #include "content/public/common/content_switches.h" |
| 25 | #include "content/public/common/profiling_utils.h" |
| 26 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 27 | #if BUILDFLAG(IS_WIN) |
Will Harris | 6f5f50a | 2021-12-03 17:39:45 | [diff] [blame] | 28 | #include "sandbox/policy/mojom/sandbox.mojom-shared.h" |
| 29 | #endif |
| 30 | |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 31 | namespace content { |
| 32 | |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | // A refcounted class that runs a closure once it's destroyed. |
| 36 | class RefCountedScopedClosureRunner |
| 37 | : public base::RefCounted<RefCountedScopedClosureRunner> { |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 38 | public: |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 39 | RefCountedScopedClosureRunner(base::OnceClosure callback); |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 40 | |
| 41 | private: |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 42 | friend class base::RefCounted<RefCountedScopedClosureRunner>; |
| 43 | ~RefCountedScopedClosureRunner() = default; |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 44 | |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 45 | base::ScopedClosureRunner destruction_callback_; |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 46 | }; |
| 47 | |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 48 | RefCountedScopedClosureRunner::RefCountedScopedClosureRunner( |
| 49 | base::OnceClosure callback) |
| 50 | : destruction_callback_(std::move(callback)) {} |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 51 | |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 52 | } // namespace |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 53 | |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 54 | void AskAllChildrenToDumpProfilingData(base::OnceClosure callback) { |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 55 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 56 | switches::kSingleProcess)) { |
| 57 | return; |
| 58 | } |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 59 | |
| 60 | auto closure_runner = |
| 61 | base::MakeRefCounted<RefCountedScopedClosureRunner>(std::move(callback)); |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 62 | |
| 63 | // Ask all the renderer processes to dump their profiling data. |
| 64 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 65 | !i.IsAtEnd(); i.Advance()) { |
| 66 | DCHECK(!i.GetCurrentValue()->GetProcess().is_current()); |
| 67 | if (!i.GetCurrentValue()->IsInitializedAndNotDead()) |
| 68 | continue; |
| 69 | i.GetCurrentValue()->DumpProfilingData(base::BindOnce( |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 70 | [](scoped_refptr<RefCountedScopedClosureRunner>) {}, closure_runner)); |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Ask all the other child processes to dump their profiling data |
| 74 | for (content::BrowserChildProcessHostIterator browser_child_iter; |
| 75 | !browser_child_iter.Done(); ++browser_child_iter) { |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 76 | #if BUILDFLAG(IS_WIN) |
Will Harris | 6f5f50a | 2021-12-03 17:39:45 | [diff] [blame] | 77 | // On Windows, elevated processes are never passed the profiling data file |
| 78 | // so cannot dump their data. |
Sangbaek Park | 23cea31 | 2024-10-23 21:43:11 | [diff] [blame] | 79 | CHECK(browser_child_iter.GetData().sandbox_type.has_value()); |
Will Harris | 6f5f50a | 2021-12-03 17:39:45 | [diff] [blame] | 80 | if (browser_child_iter.GetData().sandbox_type == |
| 81 | sandbox::mojom::Sandbox::kNoSandboxAndElevatedPrivileges) { |
| 82 | continue; |
| 83 | } |
| 84 | #endif |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 85 | browser_child_iter.GetHost()->DumpProfilingData(base::BindOnce( |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 86 | [](scoped_refptr<RefCountedScopedClosureRunner>) {}, closure_runner)); |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 90 | switches::kInProcessGPU)) { |
| 91 | DumpGpuProfilingData(base::BindOnce( |
Sebastien Marchand | db5895c0 | 2022-02-19 04:49:31 | [diff] [blame] | 92 | [](scoped_refptr<RefCountedScopedClosureRunner>) {}, closure_runner)); |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 93 | } |
Sebastien Marchand | 113505a | 2021-11-22 20:14:47 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace content |