Skip to content

Commit b4b27e6

Browse files
committed
frontend/latex: show mem and cpu in the logs process stats
1 parent c920228 commit b4b27e6

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

src/packages/frontend/client/project.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,15 @@ export class ProjectClient {
210210
path: "",
211211
command: required,
212212
args: [],
213-
timeout: 30,
214213
max_output: undefined,
215214
bash: false,
216215
aggregate: undefined,
217216
err_on_exit: true,
218217
env: undefined,
219218
post: false, // if true, uses the POST api through nextjs instead of the websocket api.
220219
async_call: undefined, // if given use a callback interface instead of async
220+
timeout: 30,
221+
cb: undefined,
221222
});
222223
}
223224

src/packages/frontend/frame-editors/latex-editor/actions.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -714,18 +714,16 @@ export class Actions extends BaseActions<LatexEditorState> {
714714
const { pid, status } = job;
715715
if (status === "running" && typeof pid === "number") {
716716
try {
717-
// console.log("LatexEditor/actions/kill: killing", pid);
718717
await exec({
719718
project_id: this.project_id,
720719
// negative PID, to kill the entire process group
721720
command: `kill -9 -${pid}`,
722-
// bash: kill + array does not work. IDK why.
721+
// bash:true is necessary. kill + array does not work. IDK why.
723722
bash: true,
724723
err_on_exit: false,
725724
});
726725
} catch (err) {
727726
// likely "No such process", we just ignore it
728-
console.info("LatexEditor/actions/kill:", err);
729727
} finally {
730728
// set this build log to be no longer running
731729
job.status = "completed";
@@ -939,7 +937,6 @@ export class Actions extends BaseActions<LatexEditorState> {
939937
this.parsed_output_log = output.parse = new LatexParser(output.stdout, {
940938
ignoreDuplicates: true,
941939
}).parse();
942-
console.log("set_build_logs after complete:", (output as any)?.status);
943940
this.set_build_logs({ latex: output });
944941
// TODO: knitr complicates multifile a lot, so we do
945942
// not support it yet.
@@ -1356,7 +1353,6 @@ export class Actions extends BaseActions<LatexEditorState> {
13561353
build_logs = build_logs.delete(k);
13571354
}
13581355
}
1359-
console.log("set_job_info.latex", build_logs?.get("latex")?.get("status"));
13601356
this.setState({ build_logs });
13611357
}
13621358

src/packages/frontend/frame-editors/latex-editor/build.tsx

+36-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { CSS, React, Rendered, useRedux } from "@cocalc/frontend/app-framework";
1616
import { Icon, r_join } from "@cocalc/frontend/components";
1717
import Stopwatch from "@cocalc/frontend/editors/stopwatch/stopwatch";
1818
import { webapp_client } from "@cocalc/frontend/webapp-client";
19-
import { path_split, tail } from "@cocalc/util/misc";
19+
import { path_split, tail, unreachable } from "@cocalc/util/misc";
2020
import { COLORS } from "@cocalc/util/theme";
2121
import {
2222
ExecuteCodeOutput,
@@ -99,16 +99,40 @@ export const Build: React.FC<Props> = React.memo((props) => {
9999
});
100100
}
101101

102-
function getMaxMem(stats?: ExecuteCodeOutputAsync["stats"]): string {
103-
if (Array.isArray(stats) && stats.length > 0) {
104-
const max_mem = stats.reduce((cur, val) => {
105-
return val.mem_rss > cur ? val.mem_rss : cur;
106-
}, 0);
107-
// if there is no data (too many processes, etc.) then it is 0.
108-
// That information is misleading and we ignore it
109-
if (max_mem > 0) {
110-
return ` Memory usage: ${max_mem.toFixed(0)} MB.`;
102+
function getResourceUsage(
103+
stats: ExecuteCodeOutputAsync["stats"] | undefined,
104+
type: "peak" | "last",
105+
): string {
106+
if (!Array.isArray(stats) || stats.length === 0) {
107+
return "";
108+
}
109+
110+
switch (type) {
111+
// This is after the job finished. We return the CPU time used and max memory.
112+
case "peak": {
113+
const max_mem = stats.reduce((cur, val) => {
114+
return val.mem_rss > cur ? val.mem_rss : cur;
115+
}, 0);
116+
// if there is no data (too many processes, etc.) then it is 0.
117+
// That information is misleading and we ignore it
118+
if (max_mem > 0) {
119+
return ` Peak memory usage: ${max_mem.toFixed(0)} MB.`;
120+
}
121+
break;
122+
}
123+
124+
// This is while the log updates come in: last known CPU in % and memory usage.
125+
case "last": {
126+
const { mem_rss, cpu_pct } = stats.slice(-1)[0];
127+
if (mem_rss > 0 || cpu_pct > 0) {
128+
return ` Resource usage: ${mem_rss.toFixed(
129+
0,
130+
)} MB memory and ${cpu_pct.toFixed(0)}% CPU.`;
131+
}
132+
break;
111133
}
134+
default:
135+
unreachable(type);
112136
}
113137
return "";
114138
}
@@ -130,7 +154,7 @@ export const Build: React.FC<Props> = React.memo((props) => {
130154
if (typeof elapsed_s === "number" && elapsed_s > 0) {
131155
job_info_str = `Build time: ${elapsed_s.toFixed(1)} seconds.`;
132156
}
133-
job_info_str += getMaxMem(stats);
157+
job_info_str += getResourceUsage(stats, "peak");
134158
}
135159
const title = BUILD_SPECS[stage].label;
136160
// highlights tab, if there is at least one parsed error
@@ -209,7 +233,7 @@ export const Build: React.FC<Props> = React.memo((props) => {
209233
build_logs.forEach((infoI, key) => {
210234
const info: ExecuteCodeOutput = infoI?.toJS();
211235
if (!info || info.type !== "async" || info.status !== "running") return;
212-
const stats_str = getMaxMem(info.stats);
236+
const stats_str = getResourceUsage(info.stats, "last");
213237
const start = info.start;
214238
logTail = tail(info.stdout ?? "" + info.stderr ?? "", 6);
215239
isLongRunning ||=

src/packages/frontend/frame-editors/latex-editor/util.ts

-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export async function gatherJobInfo(
9090
return;
9191
}
9292
if (update.status === "running") {
93-
console.log("gatherJobInfo", update);
9493
set_job_info(update);
9594
} else {
9695
return;

0 commit comments

Comments
 (0)