Skip to content

Commit

Permalink
fix(compute/build): normalise and bucket heap allocations (#1130)
Browse files Browse the repository at this point in the history
* fix(compute/build): normalise and bucket heap allocations

* ci: bump go version to 1.22.x

* ci: disable staticcheck until it's fixed

* doc(compute/build): typo

Co-authored-by: Cameron Walters (cee-dub) <cameron.walters@gmail.com>

---------

Co-authored-by: Cameron Walters (cee-dub) <cameron.walters@gmail.com>
  • Loading branch information
Integralist and cee-dub committed Feb 14, 2024
1 parent f855798 commit 9f9b913
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/pr_test.yml
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 1.21.x
go-version: 1.22.x
# NOTE: Manage GitHub Actions cache via https://github.com/fastly/cli/actions/caches
# This is useful if you need to clear the cache when a dependency doesn't update correctly.
#
Expand Down Expand Up @@ -76,9 +76,11 @@ jobs:
- name: "Run revive"
run: make revive
shell: bash
- name: "Static analysis check"
run: make staticcheck
shell: bash
# FIXME: Put back staticcheck once it fixes https://github.com/dominikh/go-tools/issues/1496
#
# - name: "Static analysis check"
# run: make staticcheck
# shell: bash
- name: "Security audit"
run: make gosec
shell: bash
Expand All @@ -87,7 +89,7 @@ jobs:
strategy:
matrix:
tinygo-version: [0.27.0]
go-version: [1.21.x]
go-version: [1.22.x]
node-version: [18]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tag_release.yml
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: "Install Go"
uses: actions/setup-go@v4
with:
go-version: '1.21.x'
go-version: '1.22.x'
- name: "Set GOHOSTOS and GOHOSTARCH"
run: echo "GOHOSTOS=$(go env GOHOSTOS)" >> $GITHUB_ENV && echo "GOHOSTARCH=$(go env GOHOSTARCH)" >> $GITHUB_ENV
- name: "Install Rust"
Expand Down
34 changes: 32 additions & 2 deletions pkg/commands/compute/build.go
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"math"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -327,7 +328,7 @@ func (c *BuildCommand) AnnotateWasmBinaryLong(wasmtools string, args []string, l

if metadata.BuildInfo == "enable" {
dc.BuildInfo = DataCollectionBuildInfo{
MemoryHeapAlloc: ms.HeapAlloc,
MemoryHeapAlloc: bucketMB(bytesToMB(ms.HeapAlloc)) + "MB",
}
}
if metadata.MachineInfo == "enable" {
Expand Down Expand Up @@ -823,6 +824,35 @@ func GetNonIgnoredFiles(base string, ignoredFiles map[string]bool) ([]string, er
return files, err
}

// bytesToMB converts the runtime.MemStats.HeapAlloc bytes into megabytes.
func bytesToMB(bytes uint64) uint64 {
return uint64(math.Round(float64(bytes) / (1024 * 1024)))
}

// bucketMB determines a consistent bucket size for heap allocation.
// NOTE: This is to avoid building a package with a fluctuating hashsum.
// e.g. `fastly compute hash-files` should be consistent unless memory increase is significant.
func bucketMB(mb uint64) string {
switch {
case mb < 2:
return "<2"
case mb >= 2 && mb < 5:
return "2-5"
case mb >= 5 && mb < 10:
return "5-10"
case mb >= 10 && mb < 20:
return "10-20"
case mb >= 20 && mb < 30:
return "20-30"
case mb >= 30 && mb < 40:
return "30-40"
case mb >= 40 && mb < 50:
return "40-50"
default:
return ">50"
}
}

// DataCollection represents data annotated onto the Wasm binary.
type DataCollection struct {
BuildInfo DataCollectionBuildInfo `json:"build_info,omitempty"`
Expand All @@ -833,7 +863,7 @@ type DataCollection struct {

// DataCollectionBuildInfo represents build data annotated onto the Wasm binary.
type DataCollectionBuildInfo struct {
MemoryHeapAlloc uint64 `json:"mem_heap_alloc,omitempty"`
MemoryHeapAlloc string `json:"mem_heap_alloc,omitempty"`
}

// DataCollectionMachineInfo represents machine data annotated onto the Wasm binary.
Expand Down

0 comments on commit 9f9b913

Please sign in to comment.