...

Source file src/internal/trace/testdata/testprog/annotations.go

Documentation: internal/trace/testdata/testprog

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Tests user tasks, regions, and logging.
     6  
     7  //go:build ignore
     8  
     9  package main
    10  
    11  import (
    12  	"context"
    13  	"log"
    14  	"os"
    15  	"runtime/trace"
    16  	"sync"
    17  )
    18  
    19  func main() {
    20  	bgctx, cancel := context.WithCancel(context.Background())
    21  	defer cancel()
    22  
    23  	// Create a pre-existing region. This won't end up in the trace.
    24  	preExistingRegion := trace.StartRegion(bgctx, "pre-existing region")
    25  
    26  	// Start tracing.
    27  	if err := trace.Start(os.Stdout); err != nil {
    28  		log.Fatalf("failed to start tracing: %v", err)
    29  	}
    30  
    31  	// Beginning of traced execution.
    32  	var wg sync.WaitGroup
    33  	ctx, task := trace.NewTask(bgctx, "task0") // EvUserTaskCreate("task0")
    34  	trace.StartRegion(ctx, "task0 region")
    35  
    36  	wg.Add(1)
    37  	go func() {
    38  		defer wg.Done()
    39  		defer task.End() // EvUserTaskEnd("task0")
    40  
    41  		trace.StartRegion(ctx, "unended region")
    42  
    43  		trace.WithRegion(ctx, "region0", func() {
    44  			// EvUserRegionBegin("region0", start)
    45  			trace.WithRegion(ctx, "region1", func() {
    46  				trace.Log(ctx, "key0", "0123456789abcdef") // EvUserLog("task0", "key0", "0....f")
    47  			})
    48  			// EvUserRegionEnd("region0", end)
    49  		})
    50  	}()
    51  	wg.Wait()
    52  
    53  	preExistingRegion.End()
    54  	postExistingRegion := trace.StartRegion(bgctx, "post-existing region")
    55  
    56  	// End of traced execution.
    57  	trace.Stop()
    58  
    59  	postExistingRegion.End()
    60  }
    61  

View as plain text