Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grunning: add library for precise on-CPU time measurement #85977

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@
/pkg/util/addr/ @cockroachdb/cli-prs @cockroachdb/obs-inf-prs
/pkg/util/metric/ @cockroachdb/obs-inf-prs
/pkg/util/stop/ @cockroachdb/kv-prs
/pkg/util/grunning/ @cockroachdb/kv-prs
/pkg/util/admission/ @cockroachdb/kv-prs
/pkg/util/tracing @cockroachdb/obs-inf-prs
/pkg/workload/ @cockroachdb/sql-experience-noreview
/pkg/obsservice/ @cockroachdb/obs-inf-prs
Expand Down
4 changes: 4 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ ALL_TESTS = [
"//pkg/util/fuzzystrmatch:fuzzystrmatch_test",
"//pkg/util/goschedstats:goschedstats_test",
"//pkg/util/grpcutil:grpcutil_test",
"//pkg/util/grunning:grunning_test",
"//pkg/util/hlc:hlc_test",
"//pkg/util/httputil:httputil_test",
"//pkg/util/humanizeutil:humanizeutil_test",
Expand Down Expand Up @@ -1866,6 +1867,8 @@ GO_TARGETS = [
"//pkg/util/growstack:growstack",
"//pkg/util/grpcutil:grpcutil",
"//pkg/util/grpcutil:grpcutil_test",
"//pkg/util/grunning:grunning",
"//pkg/util/grunning:grunning_test",
"//pkg/util/hlc:hlc",
"//pkg/util/hlc:hlc_test",
"//pkg/util/httputil:httputil",
Expand Down Expand Up @@ -2821,6 +2824,7 @@ GET_X_DATA_TARGETS = [
"//pkg/util/goschedstats:get_x_data",
"//pkg/util/growstack:get_x_data",
"//pkg/util/grpcutil:get_x_data",
"//pkg/util/grunning:get_x_data",
"//pkg/util/hlc:get_x_data",
"//pkg/util/httputil:get_x_data",
"//pkg/util/humanizeutil:get_x_data",
Expand Down
284 changes: 284 additions & 0 deletions pkg/util/grunning/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "grunning",
srcs = [
"disabled.go",
"enabled.go",
"grunning.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/util/grunning",
visibility = ["//visibility:public"],
)

go_test(
name = "grunning_test",
srcs = [
"disabled_test.go",
"enabled_test.go",
],
deps = select({
"@io_bazel_rules_go//go/platform:aix_ppc64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:android_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:darwin_arm64": [
":grunning",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:dragonfly_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:freebsd_386": [
":grunning",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:freebsd_amd64": [
":grunning",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:freebsd_arm": [
":grunning",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:freebsd_arm64": [
":grunning",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:illumos_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:ios_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:ios_arm64": [
":grunning",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:js_wasm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mips": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mips64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mips64le": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_mipsle": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_ppc64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_ppc64le": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_riscv64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:linux_s390x": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:netbsd_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:openbsd_arm64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:plan9_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:plan9_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:plan9_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:solaris_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:windows_386": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:windows_amd64": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"@io_bazel_rules_go//go/platform:windows_arm": [
":grunning",
"//pkg/testutils/skip",
"//pkg/util/syncutil",
"@com_github_stretchr_testify//require",
],
"//conditions:default": [],
}),
)

get_x_data(name = "get_x_data")
20 changes: 20 additions & 0 deletions pkg/util/grunning/disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

// See grunning.Supported() for an explanation behind this build tag.
//
//go:build (darwin && arm64) || freebsd || !bazel
// +build darwin,arm64 freebsd !bazel

package grunning

func grunningnanos() int64 { return 0 }

func supported() bool { return false }
29 changes: 29 additions & 0 deletions pkg/util/grunning/disabled_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

// See grunning.Supported() for an explanation behind this build tag.
//
//go:build (darwin && arm64) || freebsd || !bazel
// +build darwin,arm64 freebsd !bazel

package grunning_test

import (
"testing"

"github.com/cockroachdb/cockroach/pkg/util/grunning"
"github.com/stretchr/testify/require"
)

func TestDisabled(t *testing.T) {
require.False(t, grunning.Supported())
require.Zero(t, grunning.Time())
require.Zero(t, grunning.Subtract(grunning.Time(), grunning.Time()))
}
28 changes: 28 additions & 0 deletions pkg/util/grunning/enabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

// See grunning.Supported() for an explanation behind this build tag.
//
//go:build !((darwin && arm64) || freebsd || !bazel)
rickystewart marked this conversation as resolved.
Show resolved Hide resolved
// +build !darwin !arm64
// +build !freebsd
// +build bazel

package grunning

import _ "unsafe" // for go:linkname

// grunningnanos returns the running time observed by the current goroutine by
// linking to a private symbol in the (patched) runtime package.
//
//go:linkname grunningnanos runtime.grunningnanos
func grunningnanos() int64

func supported() bool { return true }
Loading