-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathprocessWatcher.ts
More file actions
133 lines (113 loc) · 4.01 KB
/
processWatcher.ts
File metadata and controls
133 lines (113 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as cp from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { promisify } from "util";
import * as vscode from "vscode";
import { sendInfo } from "vscode-extension-telemetry-wrapper";
import { LSDaemon } from "./daemon";
const execFile = promisify(cp.execFile);
interface IJdtlsMetadata {
pid?: string;
jreHome?: string;
workspace?: string;
}
export class ProcessWatcher {
private workspace?: string;
private pid?: string;
private jreHome?: string;
private lastHeartbeat?: string;
private context: vscode.ExtensionContext
constructor(private daemon: LSDaemon) {
this.context = daemon.context;
}
public async start(): Promise<boolean> {
const javaExt = vscode.extensions.getExtension("redhat.java");
if (!javaExt) {
return false;
}
// get embedded JRE Home
let jreHome: string | undefined;
try {
const jreFolder = path.join(javaExt.extensionPath, "jre");
const jreDistros = await fs.promises.readdir(jreFolder);
if (jreDistros.length > 0) {
jreHome = path.join(jreFolder, jreDistros[0]);
}
} catch (error) {
// do nothing when jre is not embedded, to avoid spamming logs
}
if (!jreHome) {
return false;
}
this.jreHome = jreHome;
const jdtlsmeta = await getPidAndWS(jreHome, this.context);
this.pid = jdtlsmeta.pid;
this.workspace = jdtlsmeta.workspace;
return (this.pid !== undefined && this.workspace !== undefined);
}
public monitor() {
const id = setInterval(() => {
this.upTime().then(seconds => {
this.lastHeartbeat = seconds;
}).catch(_e => {
clearInterval(id);
this.onDidJdtlsCrash(this.lastHeartbeat);
});
}, 5000);
// TBD: e.g. constantly monitor heap size and uptime
}
public async upTime(): Promise<string | undefined> {
if (!this.jreHome || !this.pid) {
throw new Error("unsupported");
}
const execRes = await execFile(path.join(this.jreHome, "bin", "jcmd"), [this.pid, "VM.uptime"]);
const r = /\d+\.\d+ s/;
return execRes.stdout.match(r)?.toString();
}
public async heapSize(): Promise<string> {
if (!this.jreHome || !this.pid) {
throw new Error("unsupported");
}
const execRes = await execFile(path.join(this.jreHome, "bin", "jcmd"), [this.pid, "GC.heap_info"]);
const ryoung = /PSYoungGen\s+total \d+K, used \d+K/;
const y = execRes.stdout.match(ryoung)?.toString();
const rold = /ParOldGen\s+total \d+K, used \d+K/;
const o = execRes.stdout.match(rold)?.toString();
return [y, o].join(os.EOL);
}
private onDidJdtlsCrash(lastHeartbeat?: string) {
sendInfo("", {
name: "jdtls-last-heartbeat",
message: lastHeartbeat!
});
this.daemon.logWatcher.sendErrorAndStackOnCrash();
}
}
function parseJdtlsJps(jdtlsJpsLine: string): IJdtlsMetadata {
const spaceIdx = jdtlsJpsLine.indexOf(" ");
const pid = jdtlsJpsLine.slice(0, spaceIdx);
const cmd = jdtlsJpsLine.slice(spaceIdx + 1);
const res = cmd.match(/-XX:HeapDumpPath=(.*(redhat.java|vscodesws_[0-9a-f]{5}))/);
let workspace;
if (res && res[1]) {
workspace = res[1];
}
return {
pid,
workspace
};
}
async function getPidAndWS(jreHome: string, context:vscode.ExtensionContext) {
const jpsExecRes = await execFile(path.join(jreHome, "bin", "jps"), ["-v"]);
const jdtlsLines = jpsExecRes.stdout.split(os.EOL).filter(line => line.includes("org.eclipse.jdt.ls.core"));
let jdtlsJpsLine;
if (context.storageUri) {
jdtlsJpsLine = jdtlsLines.find(line => line.includes(path.dirname(context.storageUri!.fsPath)));
} else {
jdtlsJpsLine = jdtlsLines.find(line => line.includes("vscodesws_"));
}
return jdtlsJpsLine ? parseJdtlsJps(jdtlsJpsLine) : {};
}