Repository: go
Issue: runtime/trace: Bad HeapGoal/NextGC Metric
Labels: compiler/runtime

### What version of Go are you using (`go version`)?

<pre>
$ go version go1.21.3 darwin/arm64
</pre>

### Does this issue reproduce with the latest release?

Yes, including tip.

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE='auto'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/USER/Library/Caches/go-build'
GOENV='/Users/USER/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS='-count=1'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/USER/go/pkg/mod'
GONOPROXY='github.com/Org'
GONOSUMDB='github.com/Org,go.orgbuild.io'
GOOS='darwin'
GOPATH='/Users/USER/go'
GOPRIVATE='github.com/Org'
GOPROXY='binaries.orgbuild.io,proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.21.3/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.21.3/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='c++'
CGO_ENABLED='1'
GOMOD=''
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/0t/nwrdnfnd2mjdgr0z_p_f6xww0000gq/T/go-build3503366670=/tmp/go-build -gno-record-gcc-switches -fno-common'
</pre></details>

### What did you do?

Capture a trace of running the `encoding/json` tests and then viewing it in go tool trace:

```
$ go test -trace json.trace encoding/json
```

![CleanShot 2023-11-01 at 09 07 26@2x](https://github.com/golang/go/assets/15000/d0db7168-e753-4390-85ee-f6c7e36553fb)

(I did this while implementing heap metrics for [CL 538515](https://go-review.googlesource.com/c/go/+/538515). At first I thought it's a bug in my code, but then I realized the problem exists in the old tracer as well 😅.)

### What did you expect to see?

A reasonable NextGC metric value.

### What did you see instead?

An unreasonable NextGC metric value. Maybe caused by a `uint64` underflow?

As far as I can tell, the problem appears to be in the data, not the trace viewer.

```
$ go tool trace -d ./json.trace | grep HeapGoal
2023/11/01 09:10:58 Parsing trace...
2704 HeapGoal p=0 g=1 off=142 mem=4194304
3983536 HeapGoal p=3 g=36 off=133427 mem=5988680
7451664 HeapGoal p=0 g=35 off=4877 mem=10640088
12998705 HeapGoal p=8 g=19 off=144611 mem=18734952
22220882 HeapGoal p=5 g=19 off=141308 mem=32462632
41322629 HeapGoal p=2 g=36 off=117962 mem=49052584
73809753 HeapGoal p=1 g=36 off=95052 mem=38877592
100867596 HeapGoal p=0 g=34 off=49758 mem=17658824
103327101 HeapGoal p=1 g=80 off=95363 mem=49754376
106035005 HeapGoal p=1 g=34 off=95542 mem=55731352
112637966 HeapGoal p=1 g=34 off=95774 mem=67923528
122922303 HeapGoal p=0 g=50 off=57951 mem=91832744
127586144 HeapGoal p=1 g=146 off=97325 mem=8946670875736242639
128176272 HeapGoal p=1 g=146 off=97379 mem=91832744
135046177 HeapGoal p=6 g=15 off=143059 mem=19712536
144138898 HeapGoal p=7 g=14 off=144046 mem=35340824
165010277 HeapGoal p=1 g=14 off=104248 mem=53870584
185668071 HeapGoal p=0 g=121 off=80009 mem=8946670875735194404
186297063 HeapGoal p=0 g=121 off=80048 mem=53870584
199934729 HeapGoal p=4 g=34 off=138916 mem=33694248
214716523 HeapGoal p=1 g=50 off=115986 mem=50463048
233048733 HeapGoal p=3 g=18 off=136782 mem=22212712
250342656 HeapGoal p=3 g=19 off=136882 mem=34606152
260790321 HeapGoal p=2 g=35 off=132457 mem=35196680
265980994 HeapGoal p=2 g=14 off=132538 mem=62106312
```

cc @mknyszek @prattmic