Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 5 | #include "content/public/common/profiling_utils.h" |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 6 | |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 7 | #include <limits> |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 8 | #include <memory> |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 9 | #include <string> |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 10 | |
| 11 | #include "base/base_paths.h" |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 12 | #include "base/bind.h" |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 13 | #include "base/clang_profiling_buildflags.h" |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 14 | #include "base/environment.h" |
| 15 | #include "base/files/file.h" |
| 16 | #include "base/files/file_path.h" |
| 17 | #include "base/files/file_util.h" |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 18 | #include "base/guid.h" |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 19 | #include "base/logging.h" |
| 20 | #include "base/path_service.h" |
| 21 | #include "base/rand_util.h" |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 22 | #include "base/run_loop.h" |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 23 | #include "base/strings/strcat.h" |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 24 | #include "base/strings/string_number_conversions.h" |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 25 | #include "base/strings/utf_string_conversions.h" |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 26 | #include "base/synchronization/waitable_event.h" |
| 27 | #include "base/task/task_traits.h" |
| 28 | #include "base/task/thread_pool.h" |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 29 | #include "base/threading/thread_restrictions.h" |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 30 | #include "build/build_config.h" |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 31 | |
| 32 | namespace content { |
| 33 | |
Sebastien Marchand | 388062c | 2021-08-20 21:39:11 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | // Returns the path where the PGO profiles should be saved. |
| 37 | // On Android this is always a static path, on other platforms it's either |
| 38 | // the path specified by the LLVM_PROFILE_FILE environment variable or the |
| 39 | // current path if it's not set. |
| 40 | base::FilePath GetProfileFileDirectory() { |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 41 | base::FilePath path; |
Sebastien Marchand | 388062c | 2021-08-20 21:39:11 | [diff] [blame] | 42 | |
| 43 | // Android differs from the other platforms because it's not possible to |
| 44 | // write in base::DIR_CURRENT and environment variables aren't well supported. |
| 45 | #if defined(OS_ANDROID) |
| 46 | base::PathService::Get(base::DIR_TEMP, &path); |
| 47 | path = path.Append("pgo_profiles/"); |
| 48 | #else // !defined(OS_ANDROID) |
| 49 | std::string prof_template; |
| 50 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 51 | if (env->GetVar("LLVM_PROFILE_FILE", &prof_template)) { |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 52 | #if defined(OS_WIN) |
Jan Wilken Dörrie | 8606989 | 2021-02-23 17:09:13 | [diff] [blame] | 53 | path = base::FilePath(base::UTF8ToWide(prof_template)).DirName(); |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 54 | #else |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 55 | path = base::FilePath(prof_template).DirName(); |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 56 | #endif |
Sebastien Marchand | 388062c | 2021-08-20 21:39:11 | [diff] [blame] | 57 | } |
| 58 | #endif |
| 59 | |
| 60 | if (!path.empty()) { |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 61 | base::CreateDirectory(path); |
| 62 | } else { |
| 63 | base::PathService::Get(base::DIR_CURRENT, &path); |
| 64 | } |
| 65 | |
Sebastien Marchand | 388062c | 2021-08-20 21:39:11 | [diff] [blame] | 66 | return path; |
| 67 | } |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | base::File OpenProfilingFile() { |
| 72 | base::ScopedAllowBlockingForTesting allows_blocking; |
| 73 | base::FilePath path = GetProfileFileDirectory(); |
| 74 | |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 75 | // sajjadm@ and liaoyuke@ experimentally determined that a size 4 pool works |
Sebastien Marchand | 24bb677 | 2020-05-01 18:25:08 | [diff] [blame] | 76 | // well for the coverage builder. |
| 77 | // TODO(https://crbug.com/1059335): Check if this is an appropriate value for |
| 78 | // the PGO builds. |
| 79 | int pool_index = base::RandInt(0, 3); |
| 80 | std::string filename = base::StrCat( |
| 81 | {"child_pool-", base::NumberToString(pool_index), ".profraw"}); |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 82 | #if defined(OS_WIN) |
Jan Wilken Dörrie | 8606989 | 2021-02-23 17:09:13 | [diff] [blame] | 83 | path = path.Append(base::UTF8ToWide(filename)); |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 84 | #else |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 85 | path = path.Append(filename); |
Sajjad Mirza | 68de71d | 2019-09-12 00:21:10 | [diff] [blame] | 86 | #endif |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 87 | uint32_t flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 88 | base::File::FLAG_WRITE; |
| 89 | |
| 90 | base::File file(path, flags); |
| 91 | if (!file.IsValid()) { |
| 92 | LOG(ERROR) << "Opening file: " << path << " failed with " |
| 93 | << file.error_details(); |
| 94 | } |
| 95 | |
| 96 | return file; |
| 97 | } |
Sebastien Marchand | 836d04b3 | 2020-04-28 18:10:03 | [diff] [blame] | 98 | WaitForProcessesToDumpProfilingInfo::WaitForProcessesToDumpProfilingInfo() = |
| 99 | default; |
| 100 | WaitForProcessesToDumpProfilingInfo::~WaitForProcessesToDumpProfilingInfo() = |
| 101 | default; |
| 102 | |
| 103 | void WaitForProcessesToDumpProfilingInfo::WaitForAll() { |
| 104 | base::RunLoop nested_run_loop(base::RunLoop::Type::kNestableTasksAllowed); |
| 105 | |
| 106 | // Some of the waitable events will be signaled on the main thread, use a |
| 107 | // nested run loop to ensure we're not preventing them from signaling. |
| 108 | base::ThreadPool::PostTask( |
| 109 | FROM_HERE, {base::MayBlock(), base::TaskShutdownBehavior::BLOCK_SHUTDOWN}, |
| 110 | base::BindOnce( |
| 111 | &WaitForProcessesToDumpProfilingInfo::WaitForAllOnThreadPool, |
| 112 | base::Unretained(this), nested_run_loop.QuitClosure())); |
| 113 | nested_run_loop.Run(); |
| 114 | } |
| 115 | |
| 116 | void WaitForProcessesToDumpProfilingInfo::WaitForAllOnThreadPool( |
| 117 | base::OnceClosure quit_closure) { |
| 118 | base::ScopedAllowBaseSyncPrimitivesOutsideBlockingScope allow_blocking; |
| 119 | |
| 120 | std::vector<base::WaitableEvent*> events_raw; |
| 121 | events_raw.reserve(events_.size()); |
| 122 | for (const auto& iter : events_) |
| 123 | events_raw.push_back(iter.get()); |
| 124 | |
| 125 | // Wait for all the events to be signaled. |
| 126 | while (events_raw.size()) { |
| 127 | size_t index = |
| 128 | base::WaitableEvent::WaitMany(events_raw.data(), events_raw.size()); |
| 129 | events_raw.erase(events_raw.begin() + index); |
| 130 | } |
| 131 | |
| 132 | std::move(quit_closure).Run(); |
| 133 | } |
| 134 | |
| 135 | base::WaitableEvent* |
| 136 | WaitForProcessesToDumpProfilingInfo::GetNewWaitableEvent() { |
| 137 | events_.push_back(std::make_unique<base::WaitableEvent>()); |
| 138 | return events_.back().get(); |
| 139 | } |
Sajjad Mirza | 94c68bc | 2019-08-15 01:02:35 | [diff] [blame] | 140 | |
| 141 | } // namespace content |