Skip to content

Commit

Permalink
[Enhancement] Optimize ProcProfile logging time (#52829)
Browse files Browse the repository at this point in the history
Signed-off-by: gengjun-git <[email protected]>
  • Loading branch information
gengjun-git authored Nov 13, 2024
1 parent fe38253 commit 1d43043
Showing 1 changed file with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public class ProcProfileCollector extends FrontendDaemon {
private static final Logger LOG = LogManager.getLogger(ProcProfileCollector.class);
private static final String CPU_FILE_NAME_PREFIX = "cpu-profile-";
private static final String MEM_FILE_NAME_PREFIX = "mem-profile-";
private static final long LOG_INTERVAL = 3600 * 1000L;

private final SimpleDateFormat profileTimeFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
private final String profileLogDir;

private long lastCollectTime = -1;
private long lastLogTime = -1;

public ProcProfileCollector() {
super("ProcProfileCollector");
Expand Down Expand Up @@ -87,7 +89,7 @@ private void collectMemProfile() {
try {
compressFile(fileName);
} catch (IOException e) {
LOG.warn("compress file {} failed", fileName, e);
checkAndLog(() -> LOG.warn("compress file {} failed, reason: {}", fileName, e.getMessage()));
}
}

Expand All @@ -102,7 +104,7 @@ private void collectCPUProfile() {
try {
compressFile(fileName);
} catch (IOException e) {
LOG.warn("compress file {} failed", fileName, e);
checkAndLog(() -> LOG.warn("compress file {} failed, reason: {}", fileName, e.getMessage()));
}
}

Expand All @@ -112,13 +114,13 @@ private void collectProfile(String... command) {
Process process = processBuilder.start();
process.waitFor();
if (process.exitValue() != 0) {
LOG.info("collect profile failed, stdout: {}, stderr: {}",
checkAndLog(() -> LOG.warn("collect profile failed, stdout: {}, stderr: {}",
getMsgFromInputStream(process.getInputStream()),
getMsgFromInputStream(process.getErrorStream()));
getMsgFromInputStream(process.getErrorStream())));
stopProfile();
}
} catch (IOException | InterruptedException e) {
LOG.warn("collect profile failed", e);
checkAndLog(() -> LOG.warn("collect profile failed, reason: {}", e.getMessage()));
}
}

Expand All @@ -133,11 +135,11 @@ private void stopProfile() {
process.destroyForcibly();
}
} catch (IOException | InterruptedException e) {
LOG.warn("stop profile failed", e);
checkAndLog(() -> LOG.warn("stop profile failed, reason: {}", e.getMessage()));
}
}

private String getMsgFromInputStream(InputStream inputStream) throws IOException {
private String getMsgFromInputStream(InputStream inputStream) {
if (inputStream == null) {
return "";
}
Expand All @@ -148,6 +150,9 @@ private String getMsgFromInputStream(InputStream inputStream) throws IOException
sb.append(line).append("\n");
}
return sb.toString();
} catch (IOException e) {
checkAndLog(() -> LOG.warn("get message from input stream failed, reason: {}", e.getMessage()));
return "";
}
}

Expand Down Expand Up @@ -227,4 +232,11 @@ private String getPid() {
.getName()
.split("@")[0];
}

private void checkAndLog(Runnable runnable) {
if (System.currentTimeMillis() - lastLogTime > LOG_INTERVAL) {
runnable.run();
lastLogTime = System.currentTimeMillis();
}
}
}

0 comments on commit 1d43043

Please sign in to comment.