Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Optimize ProcProfile logging time #52829

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
Loading