Skip to content

Commit

Permalink
Display wasm module cache status in cockle-config (#92)
Browse files Browse the repository at this point in the history
* Display wasm module cache status in cockle-config

* Linting
  • Loading branch information
ianthomas23 authored Jan 9, 2025
1 parent d2062b2 commit e67826f
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 24 deletions.
23 changes: 23 additions & 0 deletions src/builtin/cockle_config_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,33 @@ export class CockleConfigCommand extends BuiltinCommand {

stdout.write(`cockle ${COCKLE_VERSION}\n`);
this._writePackageConfig(context);
this._writeModuleConfig(context);

return ExitCode.SUCCESS;
}

private _writeModuleConfig(context: Context) {
const { commandRegistry, stdout, wasmModuleCache } = context;

const allModules = commandRegistry.allModules();

const lines = [['module', 'package', 'cached']];
for (const module of allModules) {
lines.push([module.name, module.packageName, wasmModuleCache.has(module.name) ? 'yes' : '']);
}

let colorMap: Map<number, string> | null = null;
if (stdout.supportsAnsiEscapes()) {
colorMap = new Map();
colorMap.set(1, ansi.styleBrightBlue);
colorMap.set(2, ansi.styleBrightPurple);
}

for (const line of toTable(lines, 1, false, colorMap)) {
stdout.write(line + '\n');
}
}

private _writePackageConfig(context: Context) {
const { commandRegistry, stdout } = context;

Expand Down
16 changes: 14 additions & 2 deletions src/command_registry.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { ICommandRunner } from './commands/command_runner';
import { WasmCommandModule } from './commands/wasm_command_module';
import { WasmCommandPackage } from './commands/wasm_command_package';
import * as AllBuiltinCommands from './builtin';
import { WasmLoader } from './wasm_loader';

export class CommandRegistry {
constructor(wasmLoader: WasmLoader) {
constructor() {
this.registerBuiltinCommands(AllBuiltinCommands);
}

/**
* Return sequence of all wasm modules ordered by module name.
*/
allModules(): WasmCommandModule[] {
const modules: WasmCommandModule[] = [];
for (const pkg of this.wasmPackageMap.values()) {
modules.push(...pkg.modules);
}
modules.sort((a, b) => (a.name < b.name ? -1 : 1));
return modules;
}

get(name: string): ICommandRunner | null {
return this._map.get(name) ?? null;
}
Expand Down
11 changes: 6 additions & 5 deletions src/commands/wasm_command_module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { WasmCommandRunner } from './wasm_command_runner';
import { WasmLoader } from '../wasm_loader';
import { WasmModuleLoader } from '../wasm_module_loader';

/**
* A moduleName.wasm file with its associated moduleName.js wrapper.
* Contains zero or more string command names.
*/
export class WasmCommandModule extends WasmCommandRunner {
constructor(
wasmLoader: WasmLoader,
private readonly name: string,
private readonly commands: string[]
wasmModuleLoader: WasmModuleLoader,
readonly name: string,
private readonly commands: string[],
readonly packageName: string
) {
super(wasmLoader);
super(wasmModuleLoader);
}

moduleName(): string {
Expand Down
8 changes: 4 additions & 4 deletions src/commands/wasm_command_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { ICommandRunner } from './command_runner';
import { Context } from '../context';
import { ExitCode } from '../exit_code';
import { ITermios } from '../termios';
import { WasmLoader } from '../wasm_loader';
import { WasmModuleLoader } from '../wasm_module_loader';

export abstract class WasmCommandRunner implements ICommandRunner {
constructor(readonly wasmLoader: WasmLoader) {}
constructor(readonly wasmModuleLoader: WasmModuleLoader) {}

abstract moduleName(): string;

abstract names(): string[];

async run(cmdName: string, context: Context): Promise<number> {
const { args, bufferedIO, fileSystem, mountpoint, stdin, stdout, stderr } = context;
const { wasmBaseUrl } = this.wasmLoader;
const { wasmBaseUrl } = this.wasmModuleLoader;

const start = Date.now();
const wasmModule = this.wasmLoader.getModule(this.moduleName());
const wasmModule = this.wasmModuleLoader.getModule(this.moduleName());

let _getCharBuffer: number[] = [];

Expand Down
4 changes: 3 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Environment } from './environment';
import { History } from './history';
import { IFileSystem } from './file_system';
import { IInput, IOutput } from './io';
import { WasmModuleCache } from './wasm_module_cache';

/**
* Context used to run commands.
Expand All @@ -23,7 +24,8 @@ export class Context {
readonly stdin: IInput,
readonly stdout: IOutput,
readonly stderr: IOutput,
readonly bufferedIO: WorkerBufferedIO
readonly bufferedIO: WorkerBufferedIO,
readonly wasmModuleCache: WasmModuleCache
) {}

flush(): void {
Expand Down
18 changes: 10 additions & 8 deletions src/shell_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { History } from './history';
import { FileInput, FileOutput, IInput, IOutput, Pipe, TerminalInput, TerminalOutput } from './io';
import { CommandNode, PipeNode, parse } from './parse';
import { longestStartsWith, toColumns } from './utils';
import { WasmLoader } from './wasm_loader';
import { WasmModuleLoader } from './wasm_module_loader';
import { WasmCommandModule } from './commands/wasm_command_module';
import { WasmCommandPackage } from './commands/wasm_command_package';

Expand All @@ -23,8 +23,8 @@ import { WasmCommandPackage } from './commands/wasm_command_package';
export class ShellImpl implements IShellWorker {
constructor(readonly options: IShellImpl.IOptions) {
this._environment = new Environment(options.color);
this._wasmLoader = new WasmLoader(options.wasmBaseUrl);
this._commandRegistry = new CommandRegistry(this._wasmLoader);
this._wasmModuleLoader = new WasmModuleLoader(options.wasmBaseUrl);
this._commandRegistry = new CommandRegistry();
}

get aliases(): Aliases {
Expand Down Expand Up @@ -266,7 +266,7 @@ export class ShellImpl implements IShellWorker {

private async _initFilesystem(): Promise<void> {
const { wasmBaseUrl } = this.options;
const fsModule = this._wasmLoader.getModule('fs');
const fsModule = this._wasmModuleLoader.getModule('fs');
const module = await fsModule({
locateFile: (path: string) => wasmBaseUrl + path
});
Expand Down Expand Up @@ -326,9 +326,10 @@ export class ShellImpl implements IShellWorker {
const commandModules = pkgConfig.modules.map(
(moduleConfig: any) =>
new WasmCommandModule(
this._wasmLoader,
this._wasmModuleLoader,
moduleConfig.name,
moduleConfig.commands ? moduleConfig.commands.split(',') : []
moduleConfig.commands ? moduleConfig.commands.split(',') : [],
pkgConfig.package
)
);
const commandPackage = new WasmCommandPackage(
Expand Down Expand Up @@ -460,7 +461,8 @@ export class ShellImpl implements IShellWorker {
input,
output,
error,
this.options.bufferedIO
this.options.bufferedIO,
this._wasmModuleLoader.cache
);
const exitCode = await runner.run(name, context);

Expand Down Expand Up @@ -562,7 +564,7 @@ export class ShellImpl implements IShellWorker {
private _commandRegistry: CommandRegistry;
private _environment: Environment;
private _history = new History();
private _wasmLoader: WasmLoader;
private _wasmModuleLoader: WasmModuleLoader;

private _fileSystem?: IFileSystem;
private _driveFS?: DriveFS;
Expand Down
15 changes: 15 additions & 0 deletions src/wasm_module_cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class WasmModuleCache {
get(moduleName: string): any {
return this._cache.get(moduleName) ?? undefined;
}

has(moduleName: string): boolean {
return this._cache.has(moduleName);
}

set(moduleName: string, module: any): void {
this._cache.set(moduleName, module);
}

private _cache = new Map<string, any>();
}
10 changes: 6 additions & 4 deletions src/wasm_loader.ts → src/wasm_module_loader.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { WasmModuleCache } from './wasm_module_cache';

/**
* Loader of WASM modules. Once loaded, a module is cached so that it is faster to subsequently.
* Must be run in a WebWorker.
*/
export class WasmLoader {
export class WasmModuleLoader {
constructor(readonly wasmBaseUrl: string) {}

public getModule(name: string): any {
let module = this._cache.get(name);
let module = this.cache.get(name);
if (module === undefined) {
// Maybe should use @jupyterlab/coreutils.URLExt to combine URL components.
const url = this.wasmBaseUrl + name + '.js';
console.log('Importing JS/WASM from ' + url);
importScripts(url);
module = (self as any).Module;
this._cache.set(name, module);
this.cache.set(name, module);
}
return module;
}

private _cache = new Map<string, any>();
cache = new WasmModuleCache();
}

0 comments on commit e67826f

Please sign in to comment.