LoAF: Report worker congestion from a saturated task queue

The Long Animation Frame (LoAF) API today reports only individual long
tasks in Web Workers. A worker's event loop can also be saturated by a
flood of consecutive short tasks: no single task is long enough to be
reported on its own, but together they keep the loop busy past the 200ms
congestion threshold. This congestion is invisible to developers even
though it blocks the worker just as effectively as a single long task.

This CL lets the LoAF API detect such congestion in a Web Worker and
report it as a long-animation-frame entry. Congestion is detected by
queuing delay: a task that was already scheduled before the previous
task started running means two tasks were queued at once (backlog depth
>= 2). This is what separates real congestion from a steady async
iteration, which keeps the loop busy without ever building a backlog and
must not be reported.

Once a congested moment opens, consecutive tasks are folded into a
single saturated interval for as long as the thread does not actually go
idle between them. The interval ends when the queue drains (a real idle
gap), and is reported once as a single entry covering the whole
interval.

For a congestion entry, scriptCount counts every top-level script entry
point folded into the interval even when scripts[] is empty (each task
is under the 5ms attribution threshold), and blockingDuration is the
span beyond the 200ms threshold.

The behavior is gated behind the LongAnimationFrameWorker flag, so
default behavior is unchanged. Main-thread congestion is handled in a
separate CL.

Run with --enable-blink-features=LongAnimationFrameWorker.

Design Doc:
https://docs.google.com/document/d/13tERlM0Cd8gKUDC01-lQeW4oNllKs4AKdEGw41bnH4k/edit?usp=sharing

Test: third_party/blink/tools/run_web_tests.py -t release_x64 --additional-driver-flag=--enable-blink-features=LongAnimationFrameWorker external/wpt/long-animation-frame/loaf-congested-moment-worker.html
Bug: 534893134
Change-Id: I69beb4a72021b2c4ac094c4ae5d14deabe796739
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8132581
Reviewed-by: Noam Rosenthal <nrosenthal@google.com>
Reviewed-by: Scott Haseley <shaseley@chromium.org>
Commit-Queue: Joone Hur <joonehur@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1672187}
NOKEYCHECK=True
GitOrigin-RevId: 1d77c4bca902204d46136da93b7665ea017f3fa6
3 files changed
tree: bd5ca0c304f0e8be0d939a63174f4f76bcde44ca
  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. big_endian.h
  86. big_endian_perftest.cc
  87. bit_cast.h
  88. bit_cast_unittest.cc
  89. bits.h
  90. bits_unittest.cc
  91. BUILD.gn
  92. build_time.h
  93. build_time_unittest.cc
  94. byte_size.cc
  95. byte_size.h
  96. byte_size_nocompile.nc
  97. byte_size_unittest.cc
  98. callback_list.cc
  99. callback_list.h
  100. callback_list_nocompile.nc
  101. callback_list_unittest.cc
  102. cancelable_callback.h
  103. cancelable_callback_unittest.cc
  104. check.cc
  105. check.h
  106. check_deref.h
  107. check_deref_nocompile.nc
  108. check_deref_unittest.cc
  109. check_example.cc
  110. check_is_test.cc
  111. check_is_test.h
  112. check_is_test_unittest.cc
  113. check_nocompile.nc
  114. check_op.cc
  115. check_op.h
  116. check_unittest.cc
  117. check_version_internal.h.in
  118. command_line.cc
  119. command_line.h
  120. command_line.rs
  121. command_line_fuzzer.cc
  122. command_line_rust_shim.cc
  123. command_line_rust_shim.h
  124. command_line_unittest.cc
  125. command_line_unittest.rs
  126. compiler_hardening_test.cc
  127. compiler_specific.h
  128. component_export.h
  129. component_export_unittest.cc
  130. cpu.cc
  131. cpu.h
  132. cpu_unittest.cc
  133. critical_closure.h
  134. critical_closure_internal_ios.mm
  135. dcheck_is_on.h
  136. DEPS
  137. DIR_METADATA
  138. enterprise_util.cc
  139. enterprise_util.h
  140. enterprise_util_mac.mm
  141. enterprise_util_mac_unittest.mm
  142. enterprise_util_win.cc
  143. environment.cc
  144. environment.h
  145. environment_unittest.cc
  146. export_template.h
  147. feature.h
  148. feature.rs
  149. feature_internal.h
  150. feature_list.cc
  151. feature_list.h
  152. feature_list_internal.cc
  153. feature_list_internal.h
  154. feature_list_unittest.cc
  155. feature_nocompile.nc
  156. feature_unittest.rs
  157. feature_visitor.h
  158. features.cc
  159. features.h
  160. file_descriptor_posix.cc
  161. file_descriptor_posix.h
  162. file_descriptor_store.cc
  163. file_descriptor_store.h
  164. file_version_info.h
  165. file_version_info_apple.h
  166. file_version_info_apple.mm
  167. file_version_info_win.cc
  168. file_version_info_win.h
  169. file_version_info_win_unittest.cc
  170. format_macros.h
  171. gmock_unittest.cc
  172. gtest_prod_util.h
  173. immediate_crash.h
  174. immediate_crash_unittest.cc
  175. lazy_instance.h
  176. lazy_instance_helpers.cc
  177. lazy_instance_helpers.h
  178. lazy_instance_unittest.cc
  179. libcpp_hardening_test.cc
  180. linux_util.cc
  181. linux_util.h
  182. linux_util_unittest.cc
  183. location.cc
  184. location.h
  185. location_unittest.cc
  186. logging.cc
  187. logging.h
  188. logging_chromeos.cc
  189. logging_nocompile.nc
  190. logging_unittest.cc
  191. logging_win.cc
  192. logging_win.h
  193. moving_window.h
  194. moving_window_unittest.cc
  195. native_library.h
  196. native_library_apple.mm
  197. native_library_fuchsia.cc
  198. native_library_posix.cc
  199. native_library_unittest.cc
  200. native_library_win.cc
  201. no_destructor.h
  202. no_destructor_nocompile.nc
  203. no_destructor_unittest.cc
  204. not_fatal_until.h
  205. notimplemented.h
  206. notreached.h
  207. observer_list.h
  208. observer_list_internal.cc
  209. observer_list_internal.h
  210. observer_list_nocompile.nc
  211. observer_list_perftest.cc
  212. observer_list_threadsafe.cc
  213. observer_list_threadsafe.h
  214. observer_list_threadsafe_unittest.cc
  215. observer_list_types.cc
  216. observer_list_types.h
  217. observer_list_unittest.cc
  218. one_shot_event.cc
  219. one_shot_event.h
  220. one_shot_event_unittest.cc
  221. os_compat_android.cc
  222. os_compat_android.h
  223. OWNERS
  224. parameter_pack.h
  225. parameter_pack_unittest.cc
  226. path_service.cc
  227. path_service.h
  228. path_service_unittest.cc
  229. pending_task.cc
  230. pending_task.h
  231. pickle.cc
  232. pickle.h
  233. pickle_fuzzer.cc
  234. pickle_unittest.cc
  235. PRESUBMIT.py
  236. protobuf_hardening_test.cc
  237. protobuf_hardening_test_support.proto
  238. rand_util.cc
  239. rand_util.h
  240. rand_util_fuchsia.cc
  241. rand_util_perftest.cc
  242. rand_util_posix.cc
  243. rand_util_unittest.cc
  244. rand_util_win.cc
  245. README.md
  246. run_loop.cc
  247. run_loop.h
  248. run_loop.rs
  249. run_loop_nocompile.nc
  250. run_loop_rust_shim.cc
  251. run_loop_rust_shim.h
  252. run_loop_unittest.cc
  253. safe_numerics_nocompile.nc
  254. safe_numerics_unittest.cc
  255. scoped_add_feature_flags.cc
  256. scoped_add_feature_flags.h
  257. scoped_add_feature_flags_unittest.cc
  258. scoped_clear_last_error.h
  259. scoped_clear_last_error_unittest.cc
  260. scoped_clear_last_error_win.cc
  261. scoped_environment_variable_override.cc
  262. scoped_environment_variable_override.h
  263. scoped_generic.h
  264. scoped_generic_unittest.cc
  265. scoped_multi_source_observation.h
  266. scoped_multi_source_observation_unittest.cc
  267. scoped_native_library.cc
  268. scoped_native_library.h
  269. scoped_native_library_unittest.cc
  270. scoped_observation.h
  271. scoped_observation_traits.h
  272. scoped_observation_unittest.cc
  273. SECURITY_OWNERS
  274. security_unittest.cc
  275. sequence_checker.cc
  276. sequence_checker.h
  277. sequence_checker_impl.cc
  278. sequence_checker_impl.h
  279. sequence_checker_nocompile.nc
  280. sequence_checker_unittest.cc
  281. sequence_token.cc
  282. sequence_token.h
  283. sequence_token_unittest.cc
  284. simdutf_shim.cc
  285. simdutf_shim.h
  286. stack_canary_linux.cc
  287. stack_canary_linux.h
  288. stack_canary_linux_unittest.cc
  289. state_transitions.h
  290. state_transitions_unittest.cc
  291. std_clamp_unittest.cc
  292. stl_util.h
  293. stl_util_unittest.cc
  294. supports_user_data.cc
  295. supports_user_data.h
  296. supports_user_data_unittest.cc
  297. sync_socket.cc
  298. sync_socket.h
  299. sync_socket_posix.cc
  300. sync_socket_unittest.cc
  301. sync_socket_win.cc
  302. sys_byteorder.h
  303. sys_byteorder_unittest.cc
  304. syslog_logging.cc
  305. syslog_logging.h
  306. thread_annotations.h
  307. thread_annotations_nocompile.nc
  308. thread_annotations_unittest.cc
  309. token.cc
  310. token.h
  311. token_unittest.cc
  312. tools_sanity_unittest.cc
  313. traits_bag.h
  314. traits_bag_nocompile.nc
  315. traits_bag_unittest.cc
  316. tuple.h
  317. tuple_unittest.cc
  318. unguessable_token.cc
  319. unguessable_token.h
  320. unguessable_token.rs
  321. unguessable_token_unittest.cc
  322. unsafe_buffers_nocompile.nc
  323. unsafe_buffers_unittest.cc
  324. uuid.cc
  325. uuid.h
  326. uuid_unittest.cc
  327. value_iterators.cc
  328. value_iterators.h
  329. value_iterators_unittest.cc
  330. values.cc
  331. values.h
  332. values_nocompile.nc
  333. values_unittest.cc
  334. version.cc
  335. version.h
  336. version_unittest.cc
  337. vlog.cc
  338. vlog.h
  339. vlog_unittest.cc
  340. 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.