Avoid duplicating process handles on the browser UI thread

On Windows, ::DuplicateHandle() is not a cheap user-mode operation.
Handle operations serialize on the per-process handle table lock and,
for process objects, the system-wide process table lock, so duplication
contends with file and process creation machine-wide and is prone to
priority inversion. Third-party security software compounds this by
registering ObRegisterCallbacks() callbacks for PsProcessType covering
OB_OPERATION_HANDLE_DUPLICATE, which run inline on the calling thread.
Browser UI thread stalls of ~1.4s, and in older reports 12-20s, have
been attributed to these calls.

- ChildProcessLauncher::SetProcessPriorityImpl() duplicated the handle
  on every priority change, which for renderers happens on every tab
  foreground/background switch, and is also the code that puts children
  into the background. ChildProcessLauncherHelper now caches its own
  copy on the launcher thread at launch and applies priority changes to
  that, releasing it once the child has exited.
  DumpProcessStack() and, on Android, SetRenderProcessPriority() also
  duplicated the handle just to hand it to the launcher thread. They now
  read that same cached copy, so ChildProcessLauncher no longer
  duplicates on the UI thread at all.

- ModuleEventSinkImpl::OnModuleEvents() duplicated the handle once per
  module load address. Since this is called repeatedly with batches of
  addresses while a child process starts up, it put a large number of
  handle operations on the UI thread. The process is now held by a
  refcounted pointer and shared with the background tasks.

Also add Windows.DuplicateProcess.Duration.{Thread} to measure the cost
of the remaining callers, so that any left on latency-sensitive threads
can be found and removals verified in the field. Threads running a UI
message pump are reported separately, because that is where the
resulting stall is user visible as jank or a hang.

Bug: 40716800, 41439736
Change-Id: I37de4ce39e5e4ee5d140fee2d9e277881d059751
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8341090
Reviewed-by: Greg Thompson <grt@chromium.org>
Commit-Queue: Chris Davis <chrdavis@microsoft.com>
Reviewed-by: Patrick Monette <pmonette@chromium.org>
Reviewed-by: Bo Liu <boliu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1703889}
NOKEYCHECK=True
GitOrigin-RevId: 03788be1271edbb39d96dc72d770c56a83e0178e
2 files changed
tree: bbee988a05d11bdc877c397847169d844388060a
  1. allocator/
  2. android/
  3. apple/
  4. containers/
  5. debug/
  6. files/
  7. fuchsia/
  8. functional/
  9. hash/
  10. i18n/
  11. ios/
  12. json/
  13. logging/
  14. mac/
  15. macros/
  16. memory/
  17. memory_coordinator/
  18. message_loop/
  19. metrics/
  20. nix/
  21. numerics/
  22. posix/
  23. power_monitor/
  24. process/
  25. profiler/
  26. sampling_heap_profiler/
  27. strings/
  28. substring_set_matcher/
  29. synchronization/
  30. system/
  31. task/
  32. test/
  33. third_party/
  34. threading/
  35. time/
  36. timer/
  37. tools/
  38. trace_event/
  39. tracing/
  40. types/
  41. version_info/
  42. win/
  43. at_exit.cc
  44. at_exit.h
  45. at_exit_unittest.cc
  46. atomic_ref_count.h
  47. atomic_sequence_num.h
  48. atomicops.cc
  49. atomicops.h
  50. atomicops_internals_portable.h
  51. atomicops_unittest.cc
  52. auto_reset.h
  53. auto_reset_unittest.cc
  54. barrier_callback.h
  55. barrier_callback_unittest.cc
  56. barrier_closure.cc
  57. barrier_closure.h
  58. barrier_closure_unittest.cc
  59. base64.cc
  60. base64.h
  61. base64_decode_fuzzer.cc
  62. base64_encode_fuzzer.cc
  63. base64_unittest.cc
  64. base64url.cc
  65. base64url.h
  66. base64url_unittest.cc
  67. base_export.h
  68. base_paths.cc
  69. base_paths.h
  70. base_paths_android.cc
  71. base_paths_android.h
  72. base_paths_apple.cc
  73. base_paths_apple.h
  74. base_paths_fuchsia.cc
  75. base_paths_ios.h
  76. base_paths_ios.mm
  77. base_paths_mac.h
  78. base_paths_mac.mm
  79. base_paths_posix.cc
  80. base_paths_posix.h
  81. base_paths_win.cc
  82. base_paths_win.h
  83. base_rust_unittests.rs
  84. base_switches.h
  85. bit_cast.h
  86. bit_cast_unittest.cc
  87. bits.h
  88. bits_unittest.cc
  89. BUILD.gn
  90. build_time.h
  91. build_time_unittest.cc
  92. byte_conversions_perftest.cc
  93. byte_size.cc
  94. byte_size.h
  95. byte_size_nocompile.nc
  96. byte_size_unittest.cc
  97. callback_list.cc
  98. callback_list.h
  99. callback_list_nocompile.nc
  100. callback_list_unittest.cc
  101. cancelable_callback.h
  102. cancelable_callback_unittest.cc
  103. check.cc
  104. check.h
  105. check_deref.h
  106. check_deref_nocompile.nc
  107. check_deref_unittest.cc
  108. check_example.cc
  109. check_is_test.cc
  110. check_is_test.h
  111. check_is_test_unittest.cc
  112. check_nocompile.nc
  113. check_op.cc
  114. check_op.h
  115. check_unittest.cc
  116. check_version_internal.h.in
  117. command_line.cc
  118. command_line.h
  119. command_line.rs
  120. command_line_fuzzer.cc
  121. command_line_rust_shim.cc
  122. command_line_rust_shim.h
  123. command_line_unittest.cc
  124. command_line_unittest.rs
  125. compiler_hardening_test.cc
  126. compiler_specific.h
  127. component_export.h
  128. component_export_unittest.cc
  129. cpu.cc
  130. cpu.h
  131. cpu_unittest.cc
  132. critical_closure.h
  133. critical_closure_internal_ios.mm
  134. dcheck_is_on.h
  135. DEPS
  136. DIR_METADATA
  137. enterprise_util.cc
  138. enterprise_util.h
  139. enterprise_util_mac.mm
  140. enterprise_util_mac_unittest.mm
  141. enterprise_util_win.cc
  142. environment.cc
  143. environment.h
  144. environment_unittest.cc
  145. export_template.h
  146. feature.h
  147. feature.rs
  148. feature_internal.h
  149. feature_list.cc
  150. feature_list.h
  151. feature_list_internal.cc
  152. feature_list_internal.h
  153. feature_list_unittest.cc
  154. feature_nocompile.nc
  155. feature_rust_shim.cc
  156. feature_rust_shim.h
  157. feature_unittest.rs
  158. feature_visitor.h
  159. features.cc
  160. features.h
  161. file_descriptor_posix.cc
  162. file_descriptor_posix.h
  163. file_descriptor_store.cc
  164. file_descriptor_store.h
  165. file_version_info.h
  166. file_version_info_apple.h
  167. file_version_info_apple.mm
  168. file_version_info_win.cc
  169. file_version_info_win.h
  170. file_version_info_win_unittest.cc
  171. format_macros.h
  172. gmock_unittest.cc
  173. gtest_prod_util.h
  174. immediate_crash.h
  175. immediate_crash_unittest.cc
  176. lazy_instance.h
  177. lazy_instance_helpers.cc
  178. lazy_instance_helpers.h
  179. lazy_instance_unittest.cc
  180. libcpp_hardening_test.cc
  181. linux_util.cc
  182. linux_util.h
  183. linux_util_unittest.cc
  184. location.cc
  185. location.h
  186. location_unittest.cc
  187. logging.cc
  188. logging.h
  189. logging_chromeos.cc
  190. logging_nocompile.nc
  191. logging_unittest.cc
  192. logging_win.cc
  193. logging_win.h
  194. moving_window.h
  195. moving_window_unittest.cc
  196. native_library.h
  197. native_library_apple.mm
  198. native_library_fuchsia.cc
  199. native_library_posix.cc
  200. native_library_unittest.cc
  201. native_library_win.cc
  202. no_destructor.h
  203. no_destructor_nocompile.nc
  204. no_destructor_unittest.cc
  205. not_fatal_until.h
  206. notimplemented.h
  207. notreached.h
  208. observer_list.h
  209. observer_list_internal.cc
  210. observer_list_internal.h
  211. observer_list_nocompile.nc
  212. observer_list_perftest.cc
  213. observer_list_threadsafe.cc
  214. observer_list_threadsafe.h
  215. observer_list_threadsafe_unittest.cc
  216. observer_list_types.cc
  217. observer_list_types.h
  218. observer_list_unittest.cc
  219. one_shot_event.cc
  220. one_shot_event.h
  221. one_shot_event_unittest.cc
  222. os_compat_android.cc
  223. os_compat_android.h
  224. OWNERS
  225. parameter_pack.h
  226. parameter_pack_unittest.cc
  227. path_service.cc
  228. path_service.h
  229. path_service_unittest.cc
  230. pending_task.cc
  231. pending_task.h
  232. pickle.cc
  233. pickle.h
  234. pickle_fuzzer.cc
  235. pickle_unittest.cc
  236. PRESUBMIT.py
  237. protobuf_hardening_test.cc
  238. protobuf_hardening_test_support.proto
  239. rand_util.cc
  240. rand_util.h
  241. rand_util_fuchsia.cc
  242. rand_util_perftest.cc
  243. rand_util_posix.cc
  244. rand_util_unittest.cc
  245. rand_util_win.cc
  246. README.md
  247. run_loop.cc
  248. run_loop.h
  249. run_loop.rs
  250. run_loop_nocompile.nc
  251. run_loop_rust_shim.cc
  252. run_loop_rust_shim.h
  253. run_loop_unittest.cc
  254. safe_numerics_nocompile.nc
  255. safe_numerics_unittest.cc
  256. scoped_add_feature_flags.cc
  257. scoped_add_feature_flags.h
  258. scoped_add_feature_flags_unittest.cc
  259. scoped_clear_last_error.h
  260. scoped_clear_last_error_unittest.cc
  261. scoped_clear_last_error_win.cc
  262. scoped_environment_variable_override.cc
  263. scoped_environment_variable_override.h
  264. scoped_generic.h
  265. scoped_generic_unittest.cc
  266. scoped_multi_source_observation.h
  267. scoped_multi_source_observation_unittest.cc
  268. scoped_native_library.cc
  269. scoped_native_library.h
  270. scoped_native_library_unittest.cc
  271. scoped_observation.h
  272. scoped_observation_traits.h
  273. scoped_observation_unittest.cc
  274. SECURITY_OWNERS
  275. security_unittest.cc
  276. sequence_checker.cc
  277. sequence_checker.h
  278. sequence_checker_impl.cc
  279. sequence_checker_impl.h
  280. sequence_checker_nocompile.nc
  281. sequence_checker_unittest.cc
  282. sequence_token.cc
  283. sequence_token.h
  284. sequence_token_unittest.cc
  285. simdutf_shim.cc
  286. simdutf_shim.h
  287. stack_canary_linux.cc
  288. stack_canary_linux.h
  289. stack_canary_linux_unittest.cc
  290. state_transitions.h
  291. state_transitions_unittest.cc
  292. std_clamp_unittest.cc
  293. stl_util.h
  294. stl_util_unittest.cc
  295. supports_user_data.cc
  296. supports_user_data.h
  297. supports_user_data_unittest.cc
  298. sync_socket.cc
  299. sync_socket.h
  300. sync_socket_posix.cc
  301. sync_socket_unittest.cc
  302. sync_socket_win.cc
  303. sys_byteorder.h
  304. sys_byteorder_unittest.cc
  305. syslog_logging.cc
  306. syslog_logging.h
  307. thread_annotations.h
  308. thread_annotations_nocompile.nc
  309. thread_annotations_unittest.cc
  310. token.cc
  311. token.h
  312. token_unittest.cc
  313. tools_sanity_unittest.cc
  314. traits_bag.h
  315. traits_bag_nocompile.nc
  316. traits_bag_unittest.cc
  317. tuple.h
  318. tuple_unittest.cc
  319. unguessable_token.cc
  320. unguessable_token.h
  321. unguessable_token.rs
  322. unguessable_token_unittest.cc
  323. unsafe_buffers_nocompile.nc
  324. unsafe_buffers_unittest.cc
  325. uuid.cc
  326. uuid.h
  327. uuid_unittest.cc
  328. value_iterators.cc
  329. value_iterators.h
  330. value_iterators_unittest.cc
  331. values.cc
  332. values.h
  333. values_nocompile.nc
  334. values_unittest.cc
  335. version.cc
  336. version.h
  337. version_unittest.cc
  338. vlog.cc
  339. vlog.h
  340. vlog_unittest.cc
  341. write_build_date_header.py
README.md

What is this

Contains a written down set of principles and other information on //base. Please add to it!

About //base:

Chromium is a very mature project. Most things that are generally useful are already here and things not here aren't generally useful.

The bar for adding stuff to base is that it must have demonstrated wide applicability. Prefer to add things closer to where they're used (i.e. “not base”), and pull into base only when needed. In a project our size, sometimes even duplication is OK and inevitable.

Adding a new logging macro DPVELOG_NE is not more clear than just writing the stuff you want to log in a regular logging statement, even if it makes your calling code longer. Just add it to your own code.

If the code in question does not need to be used inside base, but will have multiple consumers across the codebase, consider placing it in a new directory under components/ instead.

base is written for the Chromium project and is not intended to be used outside it. Using base outside of src.git is explicitly not supported, and base makes no guarantees about API (or even ABI) stability (like all other code in Chromium). New code that depends on base/ must be in src.git. Code that's not in src.git but pulled in through DEPS (for example, v8) cannot use base.

Qualifications for being in //base OWNERS

  • interest and ability to learn low level/high detail/complex c++ stuff
  • inclination to always ask why and understand everything (including external interactions like win32) rather than just hoping the author did it right
  • mentorship/experience
  • demonstrated good judgement (esp with regards to public APIs) over a length of time

Owners are added when a contributor has shown the above qualifications and when they express interest. There isn't an upper bound on the number of OWNERS.

Design and naming

  • Be sure to use the base namespace.
  • STL-like constructs should adhere as closely to STL as possible. Functions and behaviors not present in STL should only be added when they are related to the specific data structure implemented by the container.
  • For STL-like constructs our policy is that they should use STL-like naming even when it may conflict with the style guide. So functions and class names should be lower case with underscores. Non-STL-like classes and functions should use Google naming.

Performance testing

Since the primitives provided by //base are used very widely, it is important to ensure they scale to the necessary workloads and perform well under all supported platforms. The base_perftests target is a suite of synthetic microbenchmarks that measure performance in various scenarios:

  • BasicPostTaskPerfTest: Exercises MessageLoopTaskRunner's multi-threaded queue in isolation.
  • ConditionVariablePerfTest: Measures thread switching cost of condition variables.
  • IntegratedPostTaskPerfTest: Exercises the full MessageLoop/RunLoop machinery.
  • JSONPerfTest: Tests JSONWriter and JSONReader performance.
  • MessageLoopPerfTest: Measures the speed of task posting in various configurations.
  • ObserverListPerfTest: Exercises adding, removing and signalling observers.
  • PartitionLockPerfTest: Tests the implementation of Lock used in PartitionAlloc
  • PthreadEventPerfTest: Establishes the baseline thread switching cost using pthreads.
  • RandUtilPerfTest: Measures the time it takes to generate random numbers.
  • ScheduleWorkTest: Measures the overhead of MessagePump::ScheduleWork.
  • SequenceManagerPerfTest: Benchmarks SequenceManager scheduling with various underlying task runners.
  • TaskObserverPerfTest: Measures the incremental cost of adding task observers.
  • TaskPerfTest: Checks the cost of posting tasks between threads.
  • ThreadLocalStoragePerfTest: Exercises different mechanisms for accessing data associated with the current thread (C++ thread_local, the implementation in //base, the POSIX/WinAPI directly)
  • WaitableEvent{Thread,}PerfTest: Measures waitable events in single and multithreaded scenarios.

Regressions in these benchmarks can generally by caused by 1) operating system changes, 2) compiler version or flag changes or 3) changes in //base code itself.

Rust code

Rust code in base should be organized into very small crates, split up by function. Merging crates is sometimes unavoidable (due to dependency cycles or the orphaning rule).

Rust files should live near the equivalent C++ files (if any), and use the same naming scheme (for example, run_loop.rs, not run_loop_rust.rs).

When adding FFI shims, prefer separate _shim.h files rather than adding code to existing C++ files. This helps avoid circular dependencies with the //base target. It also avoids adding code to commonly-used headers, which can increase compile size by a lot.

Crates which you expect to be widely used should be added to the public_deps of the //base:base_rust target, so that developers can simply depend on //base:base_rust the same way they do with //base.