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