|
5 | 5 | "bytes" |
6 | 6 | "context" |
7 | 7 | "fmt" |
8 | | - "os" |
9 | 8 | "os/exec" |
10 | 9 | "strconv" |
11 | 10 | "strings" |
@@ -80,49 +79,111 @@ func (p *GoPprofProfiler) Invoke(job *job.ProfilingJob) (error, time.Duration) { |
80 | 79 |
|
81 | 80 | return err, time.Since(start) |
82 | 81 | } |
83 | | - |
84 | | -func (m *goPprofManager) fetchProfileFromPID(job *job.ProfilingJob) error { |
85 | | - port, err := findListeningPortForPID(job.PID) |
| 82 | +func (p *goPprofManager) heapProfile(job *job.ProfilingJob, port string, fileName string) error { |
| 83 | + var out bytes.Buffer |
| 84 | + var stderr bytes.Buffer |
| 85 | + targetURL := fmt.Sprintf( |
| 86 | + "http://127.0.0.1:%s/debug/pprof/%s?seconds=%d", |
| 87 | + port, "heap", int(job.Interval.Seconds()), |
| 88 | + ) |
| 89 | + // for local testing |
| 90 | + // cmd := exec.Command( |
| 91 | + // "curl", targetURL, "-o", fileName, |
| 92 | + // ) |
| 93 | + cmd := exec.Command( |
| 94 | + "nsenter", "-t", job.PID, "-n", "wget", "-qO", fileName, targetURL, |
| 95 | + ) |
| 96 | + cmd.Stdout = &out |
| 97 | + cmd.Stderr = &stderr |
| 98 | + err := cmd.Run() |
86 | 99 | if err != nil { |
87 | | - log.ErrorLogLn(fmt.Sprintf("failed to find listening port for PID %s: %s", job.PID, err)) |
88 | | - port = "8080" |
89 | | - log.DebugLogLn(fmt.Sprintf("using default port %s", port)) |
| 100 | + log.ErrorLogLn(out.String()) |
| 101 | + return errors.Wrapf(err, "failed to nsenter+wget %q error %s", targetURL, stderr.String()) |
90 | 102 | } |
91 | | - |
92 | | - profileType := "profile" |
93 | | - if job.OutputType == api.HeapDump { |
94 | | - profileType = "heap" |
95 | | - } |
96 | | - |
97 | | - // Build the real HTTP URL |
| 103 | + return nil |
| 104 | +} |
| 105 | +func (p *goPprofManager) cpuProfile(job *job.ProfilingJob, port string, fileName string) error { |
| 106 | + var out bytes.Buffer |
| 107 | + var stderr bytes.Buffer |
98 | 108 | targetURL := fmt.Sprintf( |
99 | 109 | "http://127.0.0.1:%s/debug/pprof/%s?seconds=%d", |
100 | | - port, profileType, int(job.Interval.Seconds()), |
| 110 | + port, "profile", int(job.Interval.Seconds()), |
101 | 111 | ) |
102 | | - |
103 | | - // Shell out to nsenter + wget |
| 112 | + // for local testing |
| 113 | + // cmd := exec.Command( |
| 114 | + // "curl", targetURL, "-o", fileName, |
| 115 | + // ) |
104 | 116 | cmd := exec.Command( |
105 | | - "nsenter", "-t", job.PID, "-n", |
106 | | - "wget", "-qO", "-", targetURL, |
| 117 | + "nsenter", "-t", job.PID, "-n", "wget", "-qO", fileName, targetURL, |
107 | 118 | ) |
108 | | - |
109 | | - // Capture stdout of the command directly into a file |
110 | | - rawFile := common.GetResultFile(common.TmpDir(), job.Tool, api.Pprof, job.PID, job.Iteration) |
111 | | - out, err := os.Create(rawFile) |
| 119 | + cmd.Stdout = &out |
| 120 | + cmd.Stderr = &stderr |
| 121 | + err := cmd.Run() |
112 | 122 | if err != nil { |
113 | | - return errors.Wrap(err, "could not create profile file") |
| 123 | + log.ErrorLogLn(out.String()) |
| 124 | + return errors.Wrapf(err, "failed to nsenter+wget %q error %s", targetURL, stderr.String()) |
114 | 125 | } |
115 | | - defer out.Close() |
| 126 | + return nil |
| 127 | +} |
| 128 | +func (p *goPprofManager) convertPprofToRaw(pprofFilePath string) (string, error) { |
| 129 | + // Convert the pprof output to raw format using go tool pprof |
| 130 | + var out bytes.Buffer |
| 131 | + var stderr bytes.Buffer |
| 132 | + cmd := exec.Command("go", "tool", "pprof", "--raw", pprofFilePath) |
| 133 | + cmd.Stdout = &out |
| 134 | + cmd.Stderr = &stderr |
| 135 | + err := cmd.Run() |
| 136 | + if err != nil { |
| 137 | + log.ErrorLogLn(out.String()) |
| 138 | + return "", errors.Wrapf(err, "failed to convert pprof output %q to raw format error %s", pprofFilePath, stderr.String()) |
| 139 | + } |
| 140 | + return out.String(), nil |
116 | 141 |
|
117 | | - cmd.Stdout = out |
118 | | - cmd.Stderr = os.Stderr // so you’ll see any wget errors in your logs |
| 142 | +} |
119 | 143 |
|
120 | | - if err := cmd.Run(); err != nil { |
121 | | - return errors.Wrapf(err, "failed to nsenter+wget %q", targetURL) |
| 144 | +func (m *goPprofManager) fetchProfileFromPID(job *job.ProfilingJob) error { |
| 145 | + port, err := findListeningPortForPID(job.PID) |
| 146 | + if err != nil { |
| 147 | + log.ErrorLogLn(fmt.Sprintf("failed to find listening port for PID %s: %s", job.PID, err)) |
| 148 | + port = "8080" |
| 149 | + log.DebugLogLn(fmt.Sprintf("using default port %s", port)) |
| 150 | + } |
| 151 | + rawFilePath := common.GetResultFile(common.TmpDir(), job.Tool, job.OutputType, job.PID, job.Iteration) |
| 152 | + if job.OutputType == api.HeapDump { |
| 153 | + err = m.heapProfile(job, port, rawFilePath) |
| 154 | + if err != nil { |
| 155 | + return errors.Wrapf(err, "failed to fetch heap profile for PID %s", job.PID) |
| 156 | + } |
| 157 | + } else if job.OutputType == api.Pprof { |
| 158 | + err = m.cpuProfile(job, port, rawFilePath) |
| 159 | + if err != nil { |
| 160 | + return errors.Wrapf(err, "failed to fetch CPU profile for PID %s", job.PID) |
| 161 | + } |
| 162 | + } else if job.OutputType == api.Raw { |
| 163 | + profileFilePath := common.GetResultFile(common.TmpDir(), job.Tool, "cpu", job.PID, job.Iteration) |
| 164 | + err = m.cpuProfile(job, port, profileFilePath) |
| 165 | + if err != nil { |
| 166 | + return errors.Wrapf(err, "failed to create CPU profile for PID %s", job.PID) |
| 167 | + } |
| 168 | + profileRaw, err := m.convertPprofToRaw(profileFilePath) |
| 169 | + if err != nil { |
| 170 | + return errors.Wrapf(err, "failed to convert CPU profile for PID %s", job.PID) |
| 171 | + } |
| 172 | + heapFilePath := common.GetResultFile(common.TmpDir(), job.Tool, "heap", job.PID, job.Iteration) |
| 173 | + err = m.heapProfile(job, port, heapFilePath) |
| 174 | + if err != nil { |
| 175 | + return errors.Wrapf(err, "failed to fetch heap profile for PID %s", job.PID) |
| 176 | + } |
| 177 | + heapRaw, err := m.convertPprofToRaw(heapFilePath) |
| 178 | + if err != nil { |
| 179 | + return errors.Wrapf(err, "failed to convert heap profile for PID %s", job.PID) |
| 180 | + } |
| 181 | + file.Write(rawFilePath, fmt.Sprintf("heap dump\n %s \n cpu dump\n %s", heapRaw, profileRaw)) |
| 182 | + } else { |
| 183 | + return errors.New("unsupported output type for Go pprof profiler") |
122 | 184 | } |
123 | | - |
124 | 185 | // Finally, publish the file as before |
125 | | - return m.publisher.Do(job.Compressor, rawFile, job.OutputType) |
| 186 | + return m.publisher.Do(job.Compressor, rawFilePath, job.OutputType) |
126 | 187 | } |
127 | 188 |
|
128 | 189 | func findListeningPortForPID(pid string) (string, error) { |
|
0 commit comments