-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathprocessWatcher.ts
More file actions
207 lines (177 loc) · 6.76 KB
/
Copy pathprocessWatcher.ts
File metadata and controls
207 lines (177 loc) · 6.76 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// 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";
import { activatingTimestamp } from "../extension";
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;
}
// First check java.jdt.ls.java.home setting
let jreHome: string | undefined;
let configJavaHome = vscode.workspace.getConfiguration().get<string>('java.jdt.ls.java.home');
// If user has explicitly configured a Java home, use that
if (configJavaHome) {
if (await this.isValidJpsPath(configJavaHome)) {
jreHome = configJavaHome;
} else {
// Log warning but continue with fallback
console.warn(`Configured Java home ${configJavaHome} is not valid or not accessible. Checking other options.`);
}
}
// If not found, check for default runtime in java.configuration.runtimes
if (!jreHome) {
const runtimes = vscode.workspace.getConfiguration().get<any[]>('java.configuration.runtimes');
if (Array.isArray(runtimes) && runtimes.length > 0) {
// First look for one marked as default
const defaultRuntime = runtimes.find(r => r.default === true);
if (defaultRuntime && defaultRuntime.path) {
if (await this.isValidJpsPath(defaultRuntime.path)) {
jreHome = defaultRuntime.path;
}
}
// If no default is set or default is invalid, try the first one
if (!jreHome && runtimes[0].path) {
if (await this.isValidJpsPath(runtimes[0].path)) {
jreHome = runtimes[0].path;
}
}
}
}
// If no valid JDK is found in settings, fall back to embedded JRE
if (!jreHome) {
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 => {
if (!this.lastHeartbeat && seconds) {
this.sendClientInitializeTime(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 async isValidJpsPath(jdkPath: string): Promise<boolean> {
try {
// Check if path exists
await fs.promises.access(jdkPath, fs.constants.R_OK);
// Check if the jps tool exists in the bin directory
const jpsPath = path.join(jdkPath, "bin", "jps" + (os.platform() === 'win32' ? '.exe' : ''));
await fs.promises.access(jpsPath, fs.constants.X_OK);
return true;
} catch (err) {
return false;
}
}
private onDidJdtlsCrash(lastHeartbeat?: string) {
sendInfo("", {
name: "jdtls-last-heartbeat",
message: lastHeartbeat!
});
this.daemon.logWatcher.sendErrorAndStackOnCrash();
}
/**
* Send the time the client takes to initialize. This is the time between the
* activation and the JVM process is launched.
*/
private sendClientInitializeTime(seconds: string) {
const upTime = seconds.match(/\d+\.\d+/)?.toString();
if (upTime) {
let interval = Math.round(
performance.now() - activatingTimestamp - parseFloat(upTime) * 1000
);
sendInfo("", {
name: "client-initialize-time",
message: interval.toString()
});
}
}
}
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) : {};
}