Skip to content

Commit f5f5b39

Browse files
authored
Merge pull request #2928 from github/d10c/trim-cache-command
Add "CodeQL: Trim Cache" command that calls `evaluation/trimCache`
2 parents 5e8de88 + b097804 commit f5f5b39

8 files changed

Lines changed: 75 additions & 0 deletions

File tree

extensions/ql-vscode/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Increase the required version of VS Code to 1.82.0. [#2877](https://github.com/github/vscode-codeql/pull/2877)
1212
- Fix a bug where the query server was restarted twice after configuration changes. [#2884](https://github.com/github/vscode-codeql/pull/2884).
1313
- Add support for the `telemetry.telemetryLevel` setting. For more information, see the [telemetry documentation](https://codeql.github.com/docs/codeql-for-visual-studio-code/about-telemetry-in-codeql-for-visual-studio-code). [#2824](https://github.com/github/vscode-codeql/pull/2824).
14+
- Add a "CodeQL: Trim Cache" command that clears the evaluation cache of a database except for predicates annotated with the `cached` keyword. Its purpose is to get accurate performance measurements when tuning the final stage of a query, like a data-flow configuration. This is equivalent to the `codeql database cleanup --mode=normal` CLI command. In contrast, the existing "CodeQL: Clear Cache" command clears the entire cache. CodeQL CLI v2.15.1 or later is required. [#2928](https://github.com/github/vscode-codeql/pull/2928)
1415
- Fix syntax highlighting directly after import statements with instantiation arguments. [#2792](https://github.com/github/vscode-codeql/pull/2792)
1516
- The `debug.saveBeforeStart` setting is now respected when running variant analyses. [#2950](https://github.com/github/vscode-codeql/pull/2950)
1617
- The 'open database' button of the model editor was renamed to 'open source'. Also, it's now only available if the source archive is available as a workspace folder. [#2945](https://github.com/github/vscode-codeql/pull/2945)

extensions/ql-vscode/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,10 @@
712712
"command": "codeQL.clearCache",
713713
"title": "CodeQL: Clear Cache"
714714
},
715+
{
716+
"command": "codeQL.trimCache",
717+
"title": "CodeQL: Trim Cache"
718+
},
715719
{
716720
"command": "codeQL.installPackDependencies",
717721
"title": "CodeQL: Install Pack Dependencies"
@@ -1806,6 +1810,10 @@
18061810
{
18071811
"command": "codeQL.gotoQLContextEditor",
18081812
"when": "false"
1813+
},
1814+
{
1815+
"command": "codeQL.trimCache",
1816+
"when": "codeql.supportsTrimCache"
18091817
}
18101818
],
18111819
"editor/context": [

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,13 @@ export class CodeQLCliServer implements Disposable {
14921492
CliVersionConstraint.CLI_VERSION_WITH_QUICK_EVAL_COUNT,
14931493
) >= 0,
14941494
);
1495+
await this.app.commands.execute(
1496+
"setContext",
1497+
"codeql.supportsTrimCache",
1498+
newVersion.compare(
1499+
CliVersionConstraint.CLI_VERSION_WITH_TRIM_CACHE,
1500+
) >= 0,
1501+
);
14951502
} catch (e) {
14961503
this._versionChangedListeners.forEach((listener) =>
14971504
listener(undefined),
@@ -1755,6 +1762,12 @@ export class CliVersionConstraint {
17551762
"2.14.0",
17561763
);
17571764

1765+
/**
1766+
* CLI version where the query server supports the `evaluation/trimCache` method
1767+
* with `codeql database cleanup --mode=trim` semantics.
1768+
*/
1769+
public static CLI_VERSION_WITH_TRIM_CACHE = new SemVer("2.15.1");
1770+
17581771
constructor(private readonly cli: CodeQLCliServer) {
17591772
/**/
17601773
}

extensions/ql-vscode/src/common/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export type LocalDatabasesCommands = {
207207
"codeQL.chooseDatabaseGithub": () => Promise<void>;
208208
"codeQL.upgradeCurrentDatabase": () => Promise<void>;
209209
"codeQL.clearCache": () => Promise<void>;
210+
"codeQL.trimCache": () => Promise<void>;
210211

211212
// Explorer context menu
212213
"codeQL.setCurrentDatabase": (uri: Uri) => Promise<void>;

extensions/ql-vscode/src/databases/local-databases-ui.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ export class DatabaseUI extends DisposableObject {
252252
"codeQL.upgradeCurrentDatabase":
253253
this.handleUpgradeCurrentDatabase.bind(this),
254254
"codeQL.clearCache": this.handleClearCache.bind(this),
255+
"codeQL.trimCache": this.handleTrimCache.bind(this),
255256
"codeQLDatabases.chooseDatabaseFolder":
256257
this.handleChooseDatabaseFolder.bind(this),
257258
"codeQLDatabases.chooseDatabaseArchive":
@@ -703,6 +704,25 @@ export class DatabaseUI extends DisposableObject {
703704
);
704705
}
705706

707+
private async handleTrimCache(): Promise<void> {
708+
return withProgress(
709+
async (_progress, token) => {
710+
if (
711+
this.queryServer !== undefined &&
712+
this.databaseManager.currentDatabaseItem !== undefined
713+
) {
714+
await this.queryServer.trimCacheInDatabase(
715+
this.databaseManager.currentDatabaseItem,
716+
token,
717+
);
718+
}
719+
},
720+
{
721+
title: "Trimming cache",
722+
},
723+
);
724+
}
725+
706726
private async handleGetCurrentDatabase(): Promise<string | undefined> {
707727
const dbItem = await this.getDatabaseItemInternal(undefined);
708728
return dbItem?.databaseUri.fsPath;

extensions/ql-vscode/src/query-server/legacy/legacy-query-runner.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,23 @@ export class LegacyQueryRunner extends QueryRunner {
5353
) {
5454
this.qs.onDidStartQueryServer(callBack);
5555
}
56+
5657
async clearCacheInDatabase(
5758
dbItem: DatabaseItem,
5859
token: CancellationToken,
5960
): Promise<void> {
6061
await clearCacheInDatabase(this.qs, dbItem, token);
6162
}
6263

64+
async trimCacheInDatabase(
65+
dbItem: DatabaseItem,
66+
token: CancellationToken,
67+
): Promise<void> {
68+
// For the sake of conforming to the QueryRunner interface, delegate to clearCacheInDatabase
69+
// just for the effect of getting a legacy query server deprecation error response.
70+
await clearCacheInDatabase(this.qs, dbItem, token);
71+
}
72+
6373
public async compileAndRunQueryAgainstDatabaseCore(
6474
dbPath: string,
6575
query: CoreQueryTarget,

extensions/ql-vscode/src/query-server/new-query-runner.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
clearPackCache,
1111
deregisterDatabases,
1212
registerDatabases,
13+
trimCache,
14+
TrimCacheParams,
1315
upgradeDatabase,
1416
} from "./new-messages";
1517
import { CoreQueryResults, CoreQueryTarget, QueryRunner } from "./query-runner";
@@ -70,6 +72,21 @@ export class NewQueryRunner extends QueryRunner {
7072
await this.qs.sendRequest(clearCache, params, token);
7173
}
7274

75+
async trimCacheInDatabase(
76+
dbItem: DatabaseItem,
77+
token: CancellationToken,
78+
): Promise<void> {
79+
if (dbItem.contents === undefined) {
80+
throw new Error("Can't trim the cache in an invalid database.");
81+
}
82+
83+
const db = dbItem.databaseUri.fsPath;
84+
const params: TrimCacheParams = {
85+
db,
86+
};
87+
await this.qs.sendRequest(trimCache, params, token);
88+
}
89+
7390
public async compileAndRunQueryAgainstDatabaseCore(
7491
dbPath: string,
7592
query: CoreQueryTarget,

extensions/ql-vscode/src/query-server/query-runner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ export abstract class QueryRunner {
6767
token: CancellationToken,
6868
): Promise<void>;
6969

70+
abstract trimCacheInDatabase(
71+
dbItem: DatabaseItem,
72+
token: CancellationToken,
73+
): Promise<void>;
74+
7075
/**
7176
* Overridden in subclasses to evaluate the query via the query server and return the results.
7277
*/

0 commit comments

Comments
 (0)