|
| 1 | +package gpu // import "go.opentelemetry.io/ebpf-profiler/interpreter/gpu" |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + |
| 6 | + log "github.com/sirupsen/logrus" |
| 7 | + "go.opentelemetry.io/ebpf-profiler/interpreter" |
| 8 | + "go.opentelemetry.io/ebpf-profiler/libpf" |
| 9 | + "go.opentelemetry.io/ebpf-profiler/libpf/pfelf" |
| 10 | + "go.opentelemetry.io/ebpf-profiler/remotememory" |
| 11 | +) |
| 12 | + |
| 13 | +type data struct { |
| 14 | + path string |
| 15 | + link interpreter.LinkCloser |
| 16 | + probes []pfelf.USDTProbe |
| 17 | +} |
| 18 | + |
| 19 | +type instance struct { |
| 20 | + path string |
| 21 | + interpreter.InstanceStubs |
| 22 | + link interpreter.LinkCloser |
| 23 | +} |
| 24 | + |
| 25 | +func Loader(ebpf interpreter.EbpfHandler, info *interpreter.LoaderInfo) (interpreter.Data, error) { |
| 26 | + ef, err := info.GetELF() |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + // We use the existence of the .note.stapsdt section to determine if this is a |
| 31 | + // process that has libparcagpucupti.so loaded. Its cheaper and more reliable than loading |
| 32 | + // the symbol table. |
| 33 | + if sec := ef.Section(".note.stapsdt"); sec != nil { |
| 34 | + probes, err := pfelf.ParseUSDTProbes(sec) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + var parcagpuProbes []pfelf.USDTProbe |
| 39 | + for _, probe := range probes { |
| 40 | + if probe.Provider == "parcagpu" { |
| 41 | + parcagpuProbes = append(parcagpuProbes, probe) |
| 42 | + } |
| 43 | + } |
| 44 | + if len(parcagpuProbes) != 3 { |
| 45 | + return nil, nil |
| 46 | + } |
| 47 | + |
| 48 | + // Validate probe arguments match what cuda.ebpf.c expects |
| 49 | + validateProbeArguments(parcagpuProbes, info.FileName()) |
| 50 | + |
| 51 | + return &data{path: info.FileName(), |
| 52 | + probes: parcagpuProbes}, nil |
| 53 | + } |
| 54 | + return nil, nil |
| 55 | +} |
| 56 | + |
| 57 | +// validateProbeArguments checks that the USDT probe arguments match the expectations |
| 58 | +// in cuda.ebpf.c and logs errors if they don't match. |
| 59 | +func validateProbeArguments(probes []pfelf.USDTProbe, path string) { |
| 60 | + var expectedProbes map[string]string |
| 61 | + |
| 62 | + switch runtime.GOARCH { |
| 63 | + case "amd64": |
| 64 | + expectedProbes = map[string]string{ |
| 65 | + "cuda_correlation": "4@-36(%rbp)", |
| 66 | + "kernel_executed": "8@%rax 8@%rdx 8@-40(%rbp) 4@%ecx 8@%rsi", |
| 67 | + "graph_executed": "8@%rax 8@%rdx 8@-64(%rbp) 4@%ecx 4@%esi", |
| 68 | + } |
| 69 | + case "arm64": |
| 70 | + expectedProbes = map[string]string{ |
| 71 | + "cuda_correlation": "4@[sp, 36]", |
| 72 | + "kernel_executed": "8@x1 8@x2 8@[sp, 112] 4@x3 8@x0", |
| 73 | + "graph_executed": "8@x1 8@x2 8@[sp, 88] 4@x3 4@x0", |
| 74 | + } |
| 75 | + default: |
| 76 | + log.Warnf("[cuda] Unknown architecture %s, cannot validate USDT probe arguments for %s", |
| 77 | + runtime.GOARCH, path) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + probeMap := make(map[string]string) |
| 82 | + for _, probe := range probes { |
| 83 | + probeMap[probe.Name] = probe.Arguments |
| 84 | + } |
| 85 | + |
| 86 | + for name, expectedArgs := range expectedProbes { |
| 87 | + actualArgs, ok := probeMap[name] |
| 88 | + if !ok { |
| 89 | + log.Errorf("[cuda] Missing expected USDT probe '%s' in %s", name, path) |
| 90 | + continue |
| 91 | + } |
| 92 | + if actualArgs != expectedArgs { |
| 93 | + log.Errorf("[cuda] USDT probe '%s' in %s has incorrect arguments:\n"+ |
| 94 | + " Expected: %s\n"+ |
| 95 | + " Actual: %s\n"+ |
| 96 | + " This will cause incorrect data collection. cuda.ebpf.c needs to be updated.", |
| 97 | + name, path, expectedArgs, actualArgs) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func (d *data) Attach(ebpf interpreter.EbpfHandler, pid libpf.PID, _ libpf.Address, |
| 103 | + _ remotememory.RemoteMemory) (ii interpreter.Instance, err error) { |
| 104 | + // Maps usdt probe name to ebpf program name. |
| 105 | + // Use the first character of the probe name as a cookie. |
| 106 | + // 'c' -> cuda_correlation |
| 107 | + // 'k' -> cuda_kernel_exec |
| 108 | + // 'g' -> cuda_graph_exec |
| 109 | + cookies := make([]uint64, len(d.probes)) |
| 110 | + progNames := make([]string, len(d.probes)) |
| 111 | + for i, probe := range d.probes { |
| 112 | + cookies[i] = uint64(probe.Name[0]) |
| 113 | + // Map probe names to specific program names for single-shot mode |
| 114 | + switch probe.Name[0] { |
| 115 | + case 'c': |
| 116 | + progNames[i] = "usdt_parcagpu_cuda_correlation" |
| 117 | + case 'k': |
| 118 | + progNames[i] = "usdt_parcagpu_cuda_kernel" |
| 119 | + case 'g': |
| 120 | + progNames[i] = "usdt_parcagpu_cuda_graph" |
| 121 | + } |
| 122 | + } |
| 123 | + lc, err := ebpf.AttachUSDTProbes(pid, d.path, "cuda_probe", d.probes, cookies, progNames, true) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + log.Debugf("[cuda] parcagpu USDT probes attached for %s", d.path) |
| 128 | + d.link = lc |
| 129 | + |
| 130 | + return &instance{link: lc, path: d.path}, nil |
| 131 | +} |
| 132 | + |
| 133 | +// Detach does nothing, we want the probes attached as long as ANY process is using |
| 134 | +// our library. |
| 135 | +func (i *instance) Detach(_ interpreter.EbpfHandler, _ libpf.PID) error { |
| 136 | + if i.link != nil { |
| 137 | + log.Debugf("[cuda] parcagpu USDT probes closed for %s", i.path) |
| 138 | + if err := i.link.Detach(); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func (d *data) Unload(ebpf interpreter.EbpfHandler) { |
| 146 | + if d.link != nil { |
| 147 | + log.Debugf("[cuda] parcagpu USDT probes closed for %s", d.path) |
| 148 | + if err := d.link.Unload(); err != nil { |
| 149 | + log.Errorf("error closing cuda usdt link: %s", err) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments