]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: call telemetry.MaybeChild at start of go command
authorMichael Matloob <matloob@golang.org>
Thu, 13 Jun 2024 16:38:20 +0000 (12:38 -0400)
committerMichael Matloob <matloob@golang.org>
Thu, 13 Jun 2024 19:19:47 +0000 (19:19 +0000)
Call the new telemetry.MaybeChild function at the start of the go
command so that the child process logic can be run immediately without
running toolchain selection if this is the child process.

The Start function in the telemetry shim package has been renamed to
OpenCounters to make it clear that that's its only function.

The StartWithUpload function in the telemetry shim package has been
renamed to MaybeParent because that's its actual effective behavior in
cmd/go, the only place it's called: it won't run as the child because
MaybeChild has already been called and would have run as the child if
the program was the telemetry child, and it won't open counters because
telemetry.Start has been called. Checks are added that those functions
are always called before so that the function name and comment are
accurate.

It might make sense to add a true telemetry.MaybeParent function that
doesn't try to start the child or open counters to make things a little
simpler.

Change-Id: Ie81e2418af85cef18ec41f75db66365f6597b8b1
Reviewed-on: https://go-review.googlesource.com/c/go/+/592535
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

23 files changed:
src/cmd/addr2line/main.go
src/cmd/asm/main.go
src/cmd/buildid/buildid.go
src/cmd/cgo/main.go
src/cmd/compile/internal/gc/main.go
src/cmd/covdata/covdata.go
src/cmd/cover/cover.go
src/cmd/distpack/pack.go
src/cmd/doc/main.go
src/cmd/fix/main.go
src/cmd/go/main.go
src/cmd/gofmt/gofmt.go
src/cmd/internal/telemetry/telemetry.go
src/cmd/internal/telemetry/telemetry_bootstrap.go
src/cmd/link/internal/ld/main.go
src/cmd/nm/nm.go
src/cmd/objdump/main.go
src/cmd/pack/pack.go
src/cmd/pprof/pprof.go
src/cmd/preprofile/main.go
src/cmd/test2json/main.go
src/cmd/trace/main.go
src/cmd/vet/main.go

index e77785f156d04140b841aa4b2be1ff0fba5e7ad6..b1ec4e02785d7338a387455c1a6836e5545461c4 100644 (file)
@@ -46,7 +46,7 @@ func usage() {
 func main() {
        log.SetFlags(0)
        log.SetPrefix("addr2line: ")
-       telemetry.Start()
+       telemetry.OpenCounters()
 
        // pprof expects this behavior when checking for addr2line
        if len(os.Args) > 1 && os.Args[1] == "--help" {
index 82a2fa80e082c80cecc01aaeabe0176fd27793f9..ca4e25d047c4a9706960991f6ba12dfc67002881 100644 (file)
@@ -26,7 +26,7 @@ import (
 func main() {
        log.SetFlags(0)
        log.SetPrefix("asm: ")
-       telemetry.Start()
+       telemetry.OpenCounters()
 
        buildcfg.Check()
        GOARCH := buildcfg.GOARCH
index 7abc37283fa6ed1e7eb06923a123328c9256b204..a008122a0aa743380071b2c23fc25a1f5cf4708a 100644 (file)
@@ -26,7 +26,7 @@ var wflag = flag.Bool("w", false, "write build ID")
 func main() {
        log.SetPrefix("buildid: ")
        log.SetFlags(0)
-       telemetry.Start()
+       telemetry.OpenCounters()
        flag.Usage = usage
        flag.Parse()
        telemetry.Inc("buildid/invocations")
index c258985fee286d6d2e9add36ef5835121046e918..bf879be814fe997798204edb10b2e01f3e9b907b 100644 (file)
@@ -258,7 +258,7 @@ var goarch, goos, gomips, gomips64 string
 var gccBaseCmd []string
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
        objabi.AddVersionFlag() // -V
        objabi.Flagparse(usage)
        telemetry.Inc("cgo/invocations")
index 41f5e43ec6594bc178abae42288602a426afd200..3887d4156d6e3f4988c3888c3b6261e42cf226c5 100644 (file)
@@ -59,7 +59,7 @@ func handlePanic() {
 // code, and finally writes the compiled package definition to disk.
 func Main(archInit func(*ssagen.ArchInfo)) {
        base.Timer.Start("fe", "init")
-       telemetry.Start()
+       telemetry.OpenCounters()
        telemetry.Inc("compile/invocations")
 
        defer handlePanic()
index b280203f0cf5d3274f163ddd42cd35a9a1049e95..48d7b9ed08915a786b15bd5d648a5cb2055edf8b 100644 (file)
@@ -109,7 +109,7 @@ const (
 )
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
 
        // First argument should be mode/subcommand.
        if len(os.Args) < 2 {
index 912f7cafb50247c196052cac54237f456969d8ea..47eebaadd3a1ce4e3dc4c0f147edc8a6c5b37817 100644 (file)
@@ -87,7 +87,7 @@ const (
 )
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
 
        objabi.AddVersionFlag()
        flag.Usage = usage
index 0faab5c0b8f1fdfbe22ae279a2b7f72af146f52e..9ad33ee589cc8b45e20a96beaf03de4a6d50d7d2 100644 (file)
@@ -69,7 +69,7 @@ var (
 func main() {
        log.SetPrefix("distpack: ")
        log.SetFlags(0)
-       telemetry.Start()
+       telemetry.OpenCounters()
        flag.Usage = usage
        flag.Parse()
        telemetry.Inc("distpack/invocations")
index d02bf65c40ff97f2ab5e4926257909f3e42ab694..4dbddcb79fcd217c65ccd1ab2253ae88a50998c2 100644 (file)
@@ -87,7 +87,7 @@ func usage() {
 func main() {
        log.SetFlags(0)
        log.SetPrefix("doc: ")
-       telemetry.Start()
+       telemetry.OpenCounters()
        dirsInit()
        err := do(os.Stdout, flag.CommandLine, os.Args[1:])
        if err != nil {
index b0aabae889bc11001a6c5992abe8e4b8a8a33401..d915ece4ceb839e1570fac527e220ab26c8c95ed 100644 (file)
@@ -65,7 +65,7 @@ func usage() {
 }
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
        flag.Usage = usage
        flag.Parse()
        telemetry.Inc("fix/invocations")
index 9d140de21575e133d18bb4fe6146977465f6e110..eb33df1ad4c9296534eb893486195fefcea2bb51 100644 (file)
@@ -95,11 +95,12 @@ var counterErrorsGOPATHEntryRelative = telemetry.NewCounter("go/errors:gopath-en
 
 func main() {
        log.SetFlags(0)
-       telemetry.Start() // Open the telemetry counter file so counters can be written to it.
+       telemetry.MaybeChild()   // Run in child mode if this is the telemetry sidecar child process.
+       telemetry.OpenCounters() // Open the telemetry counter file so counters can be written to it.
        handleChdirFlag()
        toolchain.Select()
 
-       telemetry.StartWithUpload() // Run the upload process. Opening the counter file is idempotent.
+       telemetry.MaybeParent() // Run the upload process. Opening the counter file is idempotent.
        flag.Usage = base.Usage
        flag.Parse()
        telemetry.Inc("go/invocations")
index 03f7bef89c3eb8b7ff4bedee13e4fad512dc941d..d6721f9327316d33aac605feea93451c235669db 100644 (file)
@@ -374,7 +374,7 @@ func main() {
 }
 
 func gofmtMain(s *sequencer) {
-       telemetry.Start()
+       telemetry.OpenCounters()
        flag.Usage = usage
        flag.Parse()
        telemetry.Inc("gofmt/invocations")
index f11d80d19f95fc1c706899a80fd8478e8bb86289..b0c864a1a95ec6a4b0de08f0fb5a39005aa97f57 100644 (file)
@@ -19,25 +19,43 @@ import (
        "golang.org/x/telemetry/counter"
 )
 
-// Start opens the counter files for writing if telemetry is supported
+var openCountersCalled, maybeChildCalled bool
+
+// OpenCounters opens the counter files for writing if telemetry is supported
 // on the current platform (and does nothing otherwise).
-func Start() {
+func OpenCounters() {
+       openCountersCalled = true
        telemetry.Start(telemetry.Config{
                TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"),
        })
 }
 
-// StartWithUpload opens the counter files for writing if telemetry
-// is supported on the current platform and also enables a once a day
-// check to see if the weekly reports are ready to be uploaded.
-// It should only be called by cmd/go
-func StartWithUpload() {
+// MaybeParent does a once a day check to see if the weekly reports are
+// ready to be processed or uploaded, and if so, starts the telemetry child to
+// do so. It should only be called by cmd/go, and only after OpenCounters and MaybeChild
+// have already been called.
+func MaybeParent() {
+       if !openCountersCalled || !maybeChildCalled {
+               panic("MaybeParent must be called after OpenCounters and MaybeChild")
+       }
        telemetry.Start(telemetry.Config{
                Upload:       true,
                TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"),
        })
 }
 
+// MaybeChild executes the telemetry child logic if the calling program is
+// the telemetry child process, and does nothing otherwise. It is meant to be
+// called as the first thing in a program that uses telemetry.OpenCounters but cannot
+// call telemetry.OpenCounters immediately when it starts.
+func MaybeChild() {
+       maybeChildCalled = true
+       telemetry.MaybeChild(telemetry.Config{
+               Upload:       true,
+               TelemetryDir: os.Getenv("TEST_TELEMETRY_DIR"),
+       })
+}
+
 // Inc increments the counter with the given name.
 func Inc(name string) {
        counter.Inc(name)
index 1740bdb7019d1190b115b46ab2f467fc88b74677..05c0ee1c56e9c94faa6a7dffc5c9714386f716d9 100644 (file)
@@ -12,8 +12,9 @@ type dummyCounter struct{}
 
 func (dc dummyCounter) Inc() {}
 
-func Start()                                                              {}
-func StartWithUpload()                                                    {}
+func OpenCounters()                                                       {}
+func MaybeParent()                                                        {}
+func MaybeChild()                                                         {}
 func Inc(name string)                                                     {}
 func NewCounter(name string) dummyCounter                                 { return dummyCounter{} }
 func NewStackCounter(name string, depth int) dummyCounter                 { return dummyCounter{} }
index 9f50ad183acbca01ed8cdd2e15cdb69623b79614..3183e1a13ed86606e4f42f1a6ff36bc7fa97b711 100644 (file)
@@ -157,7 +157,7 @@ func (t *ternaryFlag) IsBoolFlag() bool { return true } // parse like a boolean
 func Main(arch *sys.Arch, theArch Arch) {
        log.SetPrefix("link: ")
        log.SetFlags(0)
-       telemetry.Start()
+       telemetry.OpenCounters()
        telemetry.Inc("link/invocations")
 
        thearch = theArch
index 62cf155362751bd8730e397262db673cdc82620e..e0d98d5f6c67d015e331e9df9ce830675abeb46a 100644 (file)
@@ -68,7 +68,7 @@ func (nflag) String() string {
 
 func main() {
        log.SetFlags(0)
-       telemetry.Start()
+       telemetry.OpenCounters()
        flag.Usage = usage
        flag.Parse()
        telemetry.Inc("nm/invocations")
index bd1762636d0499acf79e5f2453d2c0ef7aec8e51..7554b5500c1216604e6402c05200b6074aa5fa9a 100644 (file)
@@ -58,7 +58,7 @@ func usage() {
 func main() {
        log.SetFlags(0)
        log.SetPrefix("objdump: ")
-       telemetry.Start()
+       telemetry.OpenCounters()
 
        flag.Usage = usage
        flag.Parse()
index 6d7eaf7e5bc36a06e2dec98481f07f69fc1cacef..28f217ace1b57ee0006a1299077619f45a8c4546 100644 (file)
@@ -31,7 +31,7 @@ func usage() {
 func main() {
        log.SetFlags(0)
        log.SetPrefix("pack: ")
-       telemetry.Start()
+       telemetry.OpenCounters()
        // need "pack op archive" at least.
        if len(os.Args) < 3 {
                log.Print("not enough arguments")
index 69d3201cdb9396232aaea9d365ecb1d59ee78902..722b745287eec1ffe1560ef3acd93c4fd61f5e31 100644 (file)
@@ -32,7 +32,7 @@ import (
 )
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
        telemetry.Inc("pprof/invocations")
        options := &driver.Options{
                Fetch: new(fetcher),
index 78063c1463ba3179e5d8e1cf2cce44d1b5d17876..1260eed10471ee8044c2420ce42f3730650eb2c7 100644 (file)
@@ -73,7 +73,7 @@ func main() {
 
        log.SetFlags(0)
        log.SetPrefix("preprofile: ")
-       telemetry.Start()
+       telemetry.OpenCounters()
 
        flag.Usage = usage
        flag.Parse()
index 36e7cf90b560c48c03a090fff549d34d19dba5da..844ee5aa6c6cc13c47600c9f758e37e06f9627bc 100644 (file)
@@ -116,7 +116,7 @@ func ignoreSignals() {
 }
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
 
        flag.Usage = usage
        flag.Parse()
index 16721ef8427becf58414fe253493a1bd8020a504..e48048b9f257caa3cc39ff48850be9ab55192efc 100644 (file)
@@ -64,7 +64,7 @@ var (
 )
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
        flag.Usage = func() {
                fmt.Fprint(os.Stderr, usageMessage)
                os.Exit(2)
index eff82dcc71079d5b149a97024a1ec5f76900d354..84821d43fcb149a12fe25a6d86b5814e0973606c 100644 (file)
@@ -47,7 +47,7 @@ import (
 )
 
 func main() {
-       telemetry.Start()
+       telemetry.OpenCounters()
        objabi.AddVersionFlag()
 
        telemetry.Inc("vet/invocations")