runtime: better approximate total cost of scavenging
runtime: better approximate total cost of scavenging

Currently, the scavenger is paced according to how long it takes to
scavenge one runtime page's worth of memory. However, this pacing
doesn't take into account the additional cost of actually using a
scavenged page. This operation, "sysUsed," is a counterpart to the
scavenging operation "sysUnused." On most systems this operation is a
no-op, but on some systems like Darwin and Windows we actually make a
syscall. Even on systems where it's a no-op, the cost is implicit: a
more expensive page fault when re-using the page.

On Darwin in particular the cost of "sysUnused" is fairly close to the
cost of "sysUsed", which skews the pacing to be too fast. A lot of
soon-to-be-allocated memory ends up scavenged, resulting in many more
expensive "sysUsed" operations, ultimately slowing down the application.

The way to fix this problem is to include the future cost of "sysUsed"
on a page in the scavenging cost. However, measuring the "sysUsed" cost
directly (like we do with "sysUnused") on most systems is infeasible
because we would have to measure the cost of the first access.

Instead, this change applies a multiplicative constant to the measured
scavenging time which is based on a per-system ratio of "sysUnused" to
"sysUsed" costs in the worst case (on systems where it's a no-op, we
measure the cost of the first access). This ultimately slows down the
scavenger to a more reasonable pace, limiting its impact on performance
but still retaining the memory footprint improvements from the previous
release.

Fixes #36507.

Change-Id: I050659cd8cdfa5a32f5cc0b56622716ea0fa5407
Reviewed-on: https://go-review.googlesource.com/c/go/+/214517
Run-TryBot: Michael Knyszek <4473450dc5c88f06e1fb787f303d7805ec2aee65@google.com>
TryBot-Result: Gobot Gobot <66cb808b70d30c07676d5e946fee83fd561249e5@golang.org>
Reviewed-by: Austin Clements <7ea35d812706d9213868749011af1ed4fa2f6aa0@google.com>

nawawi/go,CAFxX/go,CAFxX/go,cloudflare/go,nawawi/go,cloudflare/go,akutz/go,akutz/go,CAFxX/go,CAFxX/go,nawawi/go,akutz/go,cloudflare/go,akutz/go,akutz/go,CAFxX/go,golang/go,akutz/go,nawawi/go,nawawi/go,golang/go,cloudflare/go,cloudflare/go,cloudflare/go,golang/go,golang/go,nawawi/go,golang/go,nawawi/go,golang/go,CAFxX/go,cloudflare/go,CAFxX/go,akutz/go,CAFxX/go,golang/go,cloudflare/go,nawawi/go,akutz/go,golang/go