Skip to content

Commit

Permalink
feat: preference to limit thread count of the LS
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Akos Kitta authored and kittaakos committed Feb 7, 2024
1 parent dda7770 commit d419a6c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
4 changes: 2 additions & 2 deletions arduino-ide-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions arduino-ide-extension/src/browser/arduino-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 41 additions & 7 deletions arduino-ide-extension/src/browser/contributions/ino-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -137,6 +146,7 @@ export class InoLanguage extends SketchContribution {
switch (preferenceName) {
case 'arduino.language.log':
case 'arduino.language.realTimeDiagnostics':
case 'arduino.language.asyncWorkers':
forceRestart();
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -273,6 +291,7 @@ export class InoLanguage extends SketchContribution {
},
realTimeDiagnostics,
silentOutput: true,
jobs,
}),
]);
} catch (e) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion electron-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d419a6c

Please sign in to comment.