feat: preference to limit thread count of the LS

Added a new preference (`arduino.language.asyncWorkers`) to control the
number of async workers used by `clangd`.
Users can fine tune the `clangd` thread count to overcome the excessive
CPU usage.

Use 0.1.2 Arduino Tools VSIX in IDE2.

Ref: arduino/arduino-language-server#177
Ref: arduino/vscode-arduino-tools#46

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta 2024-02-06 19:16:20 +01:00 committed by Akos Kitta
parent dda7770105
commit d419a6c6f0
5 changed files with 60 additions and 10 deletions

View File

@ -169,13 +169,13 @@
],
"arduino": {
"arduino-cli": {
"version": "0.35.1"
"version": "0.35.2"
},
"arduino-fwuploader": {
"version": "2.4.1"
},
"arduino-language-server": {
"version": "0.7.5"
"version": "0.7.6"
},
"clangd": {
"version": "14.0.0"

View File

@ -54,6 +54,10 @@ export function isMonitorWidgetDockPanel(
return arg === 'bottom' || arg === 'right';
}
export const defaultAsyncWorkers = 0 as const;
export const minAsyncWorkers = defaultAsyncWorkers;
export const maxAsyncWorkers = 8 as const;
type StrictPreferenceSchemaProperties<T extends object> = {
[p in keyof T]: PreferenceSchemaProperty;
};
@ -79,6 +83,16 @@ const properties: ArduinoPreferenceSchemaProperties = {
),
default: false,
},
'arduino.language.asyncWorkers': {
type: 'number',
description: nls.localize(
'arduino/preferences/language.asyncWorkers',
'Number of async workers used by the Arduino Language Server (clangd). Background index also uses this many workers. The minimum value is 0, and the maximum is 8. When it is 0, the language server uses all available cores. The default value is 0.'
),
minimum: minAsyncWorkers,
maximum: maxAsyncWorkers,
default: defaultAsyncWorkers,
},
'arduino.compile.verbose': {
type: 'boolean',
description: nls.localize(
@ -298,6 +312,7 @@ export const ArduinoConfigSchema: PreferenceSchema = {
export interface ArduinoConfiguration {
'arduino.language.log': boolean;
'arduino.language.realTimeDiagnostics': boolean;
'arduino.language.asyncWorkers': number;
'arduino.compile.verbose': boolean;
'arduino.compile.experimental': boolean;
'arduino.compile.revealRange': ErrorRevealStrategy;

View File

@ -6,19 +6,24 @@ import { inject, injectable } from '@theia/core/shared/inversify';
import { Mutex } from 'async-mutex';
import {
ArduinoDaemon,
assertSanitizedFqbn,
BoardIdentifier,
BoardsService,
ExecutableService,
assertSanitizedFqbn,
isBoardIdentifierChangeEvent,
sanitizeFqbn,
} from '../../common/protocol';
import { CurrentSketch } from '../sketches-service-client-impl';
import {
defaultAsyncWorkers,
maxAsyncWorkers,
minAsyncWorkers,
} from '../arduino-preferences';
import { BoardsDataStore } from '../boards/boards-data-store';
import { BoardsServiceProvider } from '../boards/boards-service-provider';
import { HostedPluginEvents } from '../hosted/hosted-plugin-events';
import { NotificationCenter } from '../notification-center';
import { CurrentSketch } from '../sketches-service-client-impl';
import { SketchContribution, URI } from './contribution';
import { BoardsDataStore } from '../boards/boards-data-store';
interface DaemonAddress {
/**
@ -76,6 +81,10 @@ interface StartLanguageServerParams {
* If `true`, the logging is not forwarded to the _Output_ view via the language client.
*/
readonly silentOutput?: boolean;
/**
* Number of async workers used by `clangd`. Background index also uses this many workers. If `0`, `clangd` uses all available cores. It's `0` by default.
*/
readonly jobs?: number;
}
/**
@ -137,6 +146,7 @@ export class InoLanguage extends SketchContribution {
switch (preferenceName) {
case 'arduino.language.log':
case 'arduino.language.realTimeDiagnostics':
case 'arduino.language.asyncWorkers':
forceRestart();
}
}
@ -168,9 +178,12 @@ export class InoLanguage extends SketchContribution {
}
}),
]);
this.boardsServiceProvider.ready.then(() =>
start(this.boardsServiceProvider.boardsConfig.selectedBoard)
);
Promise.all([
this.boardsServiceProvider.ready,
this.preferences.ready,
]).then(() => {
start(this.boardsServiceProvider.boardsConfig.selectedBoard);
});
}
onStop(): void {
@ -230,11 +243,16 @@ export class InoLanguage extends SketchContribution {
// NOOP
return;
}
this.logger.info(`Starting language server: ${fqbnWithConfig}`);
const log = this.preferences.get('arduino.language.log');
const realTimeDiagnostics = this.preferences.get(
'arduino.language.realTimeDiagnostics'
);
const jobs = this.getAsyncWorkersPreferenceSafe();
this.logger.info(
`Starting language server: ${fqbnWithConfig}${
jobs ? ` (async worker count: ${jobs})` : ''
}`
);
let currentSketchPath: string | undefined = undefined;
if (log) {
const currentSketch = await this.sketchServiceClient.currentSketch();
@ -273,6 +291,7 @@ export class InoLanguage extends SketchContribution {
},
realTimeDiagnostics,
silentOutput: true,
jobs,
}),
]);
} catch (e) {
@ -283,6 +302,21 @@ export class InoLanguage extends SketchContribution {
release();
}
}
// The Theia preference UI validation is bogus.
// To restrict the number of jobs to a valid value.
private getAsyncWorkersPreferenceSafe(): number {
const jobs = this.preferences.get(
'arduino.language.asyncWorkers',
defaultAsyncWorkers
);
if (jobs < minAsyncWorkers) {
return minAsyncWorkers;
}
if (jobs > maxAsyncWorkers) {
return maxAsyncWorkers;
}
return jobs;
}
private async start(
params: StartLanguageServerParams

View File

@ -196,7 +196,7 @@
"theiaPlugins": {
"vscode-builtin-cpp": "https://open-vsx.org/api/vscode/cpp/1.52.1/file/vscode.cpp-1.52.1.vsix",
"vscode-arduino-api": "https://github.com/dankeboy36/vscode-arduino-api/releases/download/0.1.2/vscode-arduino-api-0.1.2.vsix",
"vscode-arduino-tools": "https://downloads.arduino.cc/vscode-arduino-tools/vscode-arduino-tools-0.1.1.vsix",
"vscode-arduino-tools": "https://downloads.arduino.cc/vscode-arduino-tools/vscode-arduino-tools-0.1.2.vsix",
"vscode-builtin-json": "https://open-vsx.org/api/vscode/json/1.46.1/file/vscode.json-1.46.1.vsix",
"vscode-builtin-json-language-features": "https://open-vsx.org/api/vscode/json-language-features/1.46.1/file/vscode.json-language-features-1.46.1.vsix",
"cortex-debug": "https://downloads.arduino.cc/marus25.cortex-debug/marus25.cortex-debug-1.5.1.vsix",

View File

@ -385,6 +385,7 @@
"invalid.editorFontSize": "Invalid editor font size. It must be a positive integer.",
"invalid.sketchbook.location": "Invalid sketchbook location: {0}",
"invalid.theme": "Invalid theme.",
"language.asyncWorkers": "Number of async workers used by the Arduino Language Server (clangd). Background index also uses this many workers. The minimum value is 0, and the maximum is 8. When it is 0, the language server uses all available cores. The default value is 0.",
"language.log": "True if the Arduino Language Server should generate log files into the sketch folder. Otherwise, false. It's false by default.",
"language.realTimeDiagnostics": "If true, the language server provides real-time diagnostics when typing in the editor. It's false by default.",
"manualProxy": "Manual proxy configuration",