|
| 1 | +package capture |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "yc-agent/internal/capture/executils" |
| 11 | + "yc-agent/internal/logger" |
| 12 | +) |
| 13 | + |
| 14 | +// JFRRecordingName identifies the recording started by StartJFR. |
| 15 | +const JFRRecordingName = "ycJFR" |
| 16 | + |
| 17 | +// JFRFileName is the fixed filename yc-agent uses for the JFR recording it |
| 18 | +// captures in -onlyCapture mode. |
| 19 | +const JFRFileName = "my.jfr" |
| 20 | + |
| 21 | +// JFRCaptureDuration is how long a JFR recording runs for. It's passed as |
| 22 | +// JFR.start's own duration= option, so the JVM auto-stops the recording (and |
| 23 | +// flushes/finalizes the file) on its own - no explicit JFR.stop call needed. |
| 24 | +// The caller (ondemand.FullCapture) waits out this same duration before |
| 25 | +// calling TransmitJFR, in case the rest of the capture finishes sooner, so |
| 26 | +// the file is guaranteed to be finalized by the time it's read. |
| 27 | +const JFRCaptureDuration = 60 * time.Second |
| 28 | + |
| 29 | +// StartJFR starts a JFR recording on the target JVM (pid), writing to |
| 30 | +// filePath. filePath must be an absolute path (or, for a dockerized target, a |
| 31 | +// path inside the container) since the target JVM's own working directory is |
| 32 | +// generally not the same as yc-agent's. The recording auto-stops after |
| 33 | +// JFRCaptureDuration; see TransmitJFR. |
| 34 | +func StartJFR(pid int, javaHome, filePath string) error { |
| 35 | + cmd := fmt.Sprintf("JFR.start name=%s filename=%s duration=%ds", JFRRecordingName, filePath, int(JFRCaptureDuration.Seconds())) |
| 36 | + return runJcmd(pid, javaHome, cmd) |
| 37 | +} |
| 38 | + |
| 39 | +// runJcmd runs a jcmd diagnostic command against pid, trying the JDK's jcmd |
| 40 | +// binary first and falling back to jattach, then tmp jattach (mirrors |
| 41 | +// HDSub.executeJcmd). |
| 42 | +func runJcmd(pid int, javaHome, command string) error { |
| 43 | + out, err := executils.CommandCombinedOutput( |
| 44 | + executils.Command{path.Join(javaHome, "bin/jcmd"), strconv.Itoa(pid), command}, |
| 45 | + executils.SudoHooker{PID: pid}) |
| 46 | + if err == nil { |
| 47 | + logger.Log("jcmd %s: %s", command, out) |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + logger.Log("jcmd failed (%v), falling back to jattach for: %s", err, command) |
| 52 | + |
| 53 | + out, err = executils.CommandCombinedOutput( |
| 54 | + executils.Command{executils.Executable(), "-p", strconv.Itoa(pid), "-jCmdCaptureMode", command}, |
| 55 | + executils.EnvHooker{"pid": strconv.Itoa(pid)}, executils.SudoHooker{PID: pid}) |
| 56 | + if err == nil { |
| 57 | + logger.Log("jattach %s: %s", command, out) |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + logger.Log("jattach failed (%v), falling back to tmp jattach for: %s", err, command) |
| 62 | + |
| 63 | + tempPath, tmpErr := executils.Copy2TempPath() |
| 64 | + if tmpErr != nil { |
| 65 | + return fmt.Errorf("failed to run %q: %w (tmp jattach fallback failed: %v)", command, err, tmpErr) |
| 66 | + } |
| 67 | + |
| 68 | + out, err = executils.CommandCombinedOutput( |
| 69 | + executils.Command{tempPath, "-p", strconv.Itoa(pid), "-jCmdCaptureMode", command}, |
| 70 | + executils.EnvHooker{"pid": strconv.Itoa(pid)}, executils.SudoHooker{PID: pid}) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed to run %q: %w, output: %s", command, err, out) |
| 73 | + } |
| 74 | + logger.Log("tmp jattach %s: %s", command, out) |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// TransmitJFR transmits the JFR recording started by StartJFR (filePath, the |
| 79 | +// same path passed to it) to endpoint with dt=jfr. It assumes the recording |
| 80 | +// has already auto-stopped (StartJFR's duration= option) by the time this |
| 81 | +// runs. If dockerID is set, the recording is copied out of the container to |
| 82 | +// the local, relative JFRFileName first (so it also ends up in the local |
| 83 | +// capture bundle, consistent with how GC logs are handled). |
| 84 | +// |
| 85 | +// The transmission bypasses the -onlyCapture short-circuit in PostData, since |
| 86 | +// the recording needs to reach the yc-receiver regardless of -onlyCapture. |
| 87 | +func TransmitJFR(endpoint, filePath, dockerID string) (msg string, ok bool) { |
| 88 | + localPath := filePath |
| 89 | + if len(dockerID) > 0 { |
| 90 | + localPath = JFRFileName |
| 91 | + if err := DockerCopy(localPath, dockerID+":"+filePath); err != nil { |
| 92 | + return fmt.Sprintf("failed to copy JFR recording out of container: %s", err.Error()), false |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + file, err := os.Open(localPath) |
| 97 | + if err != nil { |
| 98 | + return fmt.Sprintf("failed to open JFR recording %s: %s", localPath, err.Error()), false |
| 99 | + } |
| 100 | + defer file.Close() |
| 101 | + |
| 102 | + return PostDataForce(endpoint, "jfr", file) |
| 103 | +} |
0 commit comments