File size: 1,820 Bytes
13c2bf6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | // Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Tests user tasks, regions, and logging.
//go:build ignore
package main
import (
"context"
"fmt"
"log"
"os"
"runtime/trace"
"time"
)
func main() {
baseCtx := context.Background()
// Create a task that starts and ends entirely outside of the trace.
ctx0, t0 := trace.NewTask(baseCtx, "parent")
// Create a task that starts before the trace and ends during the trace.
ctx1, t1 := trace.NewTask(ctx0, "type1")
// Start tracing.
if err := trace.Start(os.Stdout); err != nil {
log.Fatalf("failed to start tracing: %v", err)
}
t1.End()
// Create a task that starts during the trace and ends after.
ctx2, t2 := trace.NewTask(ctx0, "type2")
// Create a task that starts and ends during the trace.
ctx3, t3 := trace.NewTask(baseCtx, "type3")
// Generate some events.
for i := 0; i < 2; i++ {
do(baseCtx, 4)
do(ctx0, 2)
do(ctx1, 3)
do(ctx2, 6)
do(ctx3, 5)
}
// Finish up tasks according to their lifetime relative to the trace.
t3.End()
trace.Stop()
t2.End()
t0.End()
}
func do(ctx context.Context, k int) {
trace.Log(ctx, "log", "before do")
var t *trace.Task
ctx, t = trace.NewTask(ctx, "do")
defer t.End()
trace.Log(ctx, "log2", "do")
// Create a region and spawn more tasks and more workers.
trace.WithRegion(ctx, "fanout", func() {
for i := 0; i < k; i++ {
go func(i int) {
trace.WithRegion(ctx, fmt.Sprintf("region%d", i), func() {
trace.Logf(ctx, "log", "fanout region%d", i)
if i == 2 {
do(ctx, 0)
return
}
})
}(i)
}
})
// Sleep to let things happen, but also increase the chance that we
// advance a generation.
time.Sleep(10 * time.Millisecond)
}
|