Skip to content

Commit

Permalink
fix: cleanups and simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 11, 2021
1 parent 8f41058 commit 1fe4eae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/SwingSet/src/kernel/vatManager/types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @typedef { [unknown, ...unknown[]] } Tagged
* @typedef { { workerType: string, allocate: number, compute: number } }
* @typedef { { meterType: string, allocate: number|null, compute: number|null } }
* CrankStats
* @typedef { { reply: Tagged, crankStats: CrankStats } } CrankResults
*/
19 changes: 10 additions & 9 deletions packages/xsnap/src/xsnap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "xsSnapshot.h"
#include "xs.h"

#define SNAPSHOT_SIGNATURE "xsnap 1"
#ifndef XSNAP_VERSION
# error "You must define XSNAP_VERSION in the right Makefile"
#endif
Expand Down Expand Up @@ -176,8 +177,8 @@ static int fxSnapshotWrite(void* stream, void* address, size_t size)
#define xsEndMetering(_THE)
#define xsPatchHostFunction(_FUNCTION,_PATCH)
#define xsMeterHostFunction(_COUNT) (void)(_COUNT)
#define xsBeginCrank(_THE, _LIMIT)
#define xsEndCrank(_THE) 0
#define xsBeginCrank(_THE, _LIMIT)
#define xsEndCrank(_THE) 0
#endif

static xsUnsignedValue gxCrankMeteringLimit = 0;
Expand Down Expand Up @@ -218,8 +219,8 @@ int main(int argc, char* argv[])
xsCreation* creation = &_creation;

txSnapshot snapshot = {
"xsnap-" XSNAP_VERSION,
11,
SNAPSHOT_SIGNATURE,
sizeof(SNAPSHOT_SIGNATURE) - 1,
gxSnapshotCallbacks,
mxSnapshotCallbackCount,
fxSnapshotRead,
Expand Down Expand Up @@ -328,7 +329,7 @@ int main(int argc, char* argv[])
char done = 0;
while (!done) {
// By default, use the infinite meter.
gxCurrentMeter = 0;
gxCurrentMeter = 0;

xsUnsignedValue meterIndex = 0;
char* nsbuf;
Expand Down Expand Up @@ -830,10 +831,10 @@ void fx_Array_prototype_meter(xsMachine* the)

void fxPatchBuiltIns(txMachine* machine)
{
// FIXME: This function is disabled because it caused failures.
// https://github.com/Moddable-OpenSource/moddable/issues/550
// FIXME: This function is disabled because it caused failures.
// https://github.com/Moddable-OpenSource/moddable/issues/550

// TODO: Provide complete metering of builtins and operators.
// TODO: Provide complete metering of builtins and operators.
#if 0
xsBeginHost(machine);
xsVars(2);
Expand Down Expand Up @@ -1332,7 +1333,7 @@ static char* fxReadNetStringError(int code)
static int fxWriteOkay(FILE* outStream, xsUnsignedValue meterIndex, char* buf, size_t length) {
char prefix[32];
// Prepend the meter usage to the reply.
snprintf(prefix, sizeof(prefix) - 1, ".%u;", meterIndex);
snprintf(prefix, sizeof(prefix) - 1, ".%u\1", meterIndex);
return fxWriteNetString(outStream, prefix, buf, length);
}

Expand Down
55 changes: 17 additions & 38 deletions packages/xsnap/src/xsnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import * as node from './node-stream';
// This will need adjustment, but seems to be fine for a start.
const DEFAULT_CRANK_METERING_LIMIT = 1e7;

// The version identifier for our meter type.
// TODO Bump this whenever there's a change to metering semantics.
const METER_TYPE = 'xs-meter-1';

const OK = '.'.charCodeAt(0);
const ERROR = '!'.charCodeAt(0);
const QUERY = '?'.charCodeAt(0);

const SEMI = ';'.charCodeAt(0);
const OK_SEPARATOR = 1;

const importMetaUrl = `file://${__filename}`;

Expand Down Expand Up @@ -115,36 +119,11 @@ export function xsnap(options) {
/** @type {Promise<void>} */
let baton = Promise.resolve();

/** @type {string} */
let xsWorkerType;
async function getWorkerType() {
if (xsWorkerType) {
return xsWorkerType;
}
await new Promise(resolve => {
// Inititialize the worker type before sending messages.
const bufs = [];
const xsnapQueryVersion = spawn(bin, ['-v'], {
stdio: ['ignore', 'pipe', 'inherit'],
});
xsnapQueryVersion.stdout.on('data', chunk => {
bufs.push(chunk);
});
xsnapQueryVersion.on('close', () => {
const str = Buffer.concat(bufs).toString('utf-8');
const firstLine = str.split(/^/m)[0];
xsWorkerType = firstLine.trimEnd();
resolve();
});
});
return xsWorkerType;
}

/**
* @template T
* @typedef {Object} RunResult
* @property {T} reply
* @property {{ workerType: string, allocate: number, compute: number }} crankStats
* @property {{ meterType: string, allocate: number|null, compute: number|null }} crankStats
*/

/**
Expand All @@ -163,23 +142,23 @@ export function xsnap(options) {
xsnapProcess.kill();
throw new Error('xsnap protocol error: received empty message');
} else if (message[0] === OK) {
let compute = NaN;
const semi = message.indexOf(SEMI, 1);
if (semi >= 0) {
// The message is `.938586;reply`.
const computeBuf = message.slice(1, semi);
const computeStr = decoder.decode(computeBuf);
compute = JSON.parse(computeStr);
let compute = null;
const meterSeparator = message.indexOf(OK_SEPARATOR, 1);
if (meterSeparator >= 0) {
// The message is `.meterdata\1reply`.
const meterData = message.slice(1, meterSeparator);
// We parse the meter data as JSON.
// For now it is just a number for the used compute meter.
compute = JSON.parse(decoder.decode(meterData));
}
const workerType = await getWorkerType();
const crankStats = {
workerType,
allocate: NaN,
meterType: METER_TYPE,
allocate: null, // No allocation meter yet.
compute,
};
// console.log('have crankStats', crankStats);
return {
reply: message.subarray(semi < 0 ? 1 : semi + 1),
reply: message.subarray(meterSeparator < 0 ? 1 : meterSeparator + 1),
crankStats,
};
} else if (message[0] === ERROR) {
Expand Down

0 comments on commit 1fe4eae

Please sign in to comment.