Skip to content

Commit

Permalink
feat(xsnap): meter add/remove on map, set
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc authored and kriskowal committed May 4, 2021
1 parent f649ff7 commit 327062f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/xsnap/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** The version identifier for our meter type.
* TODO Bump this whenever there's a change to metering semantics.
*/
export const METER_TYPE = 'xs-meter-4';
export const METER_TYPE = 'xs-meter-5';

export const ExitCode = {
E_UNKNOWN_ERROR: -1,
Expand Down
17 changes: 14 additions & 3 deletions packages/xsnap/src/xsnap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1400,15 +1400,26 @@ static char* fxReadNetStringError(int code)

static int fxWriteOkay(FILE* outStream, xsUnsignedValue meterIndex, txMachine *the, char* buf, size_t length)
{
char fmt[] = ".{\"compute\":%u,\"allocate\":%u,\"allocateChunksCalls\":%u,\"allocateSlotsCalls\":%u,\"garbageCollectionCount\":%u}\1";
char fmt[] = ("." // OK
"{"
"\"compute\":%u,"
"\"allocate\":%u,"
"\"allocateChunksCalls\":%u,"
"\"allocateSlotsCalls\":%u,"
"\"garbageCollectionCount\":%u,"
"\"mapSetAddCount\":%u,"
"\"mapSetRemoveCount\":%u}"
"\1" // separate meter info from result
);
char numeral64[] = "12345678901234567890"; // big enough for 64bit numeral
char prefix[8 + sizeof fmt + 4 * sizeof numeral64];
char prefix[8 + sizeof fmt + 7 * sizeof numeral64];
// TODO: fxCollect counter
// Prepend the meter usage to the reply.
snprintf(prefix, sizeof(prefix) - 1, fmt,
meterIndex, the->allocatedSpace,
the->allocateChunksCallCount, the->allocateSlotsCallCount,
the->garbageCollectionCount);
the->garbageCollectionCount,
the->mapSetAddCount, the->mapSetRemoveCount);
return fxWriteNetString(outStream, prefix, buf, length);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/xsnap/src/xsnap.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
txUnsigned allocateChunksCallCount; \
txUnsigned allocateSlotsCallCount; \
txUnsigned garbageCollectionCount; \
txUnsigned mapSetAddCount; \
txUnsigned mapSetRemoveCount; \
int promiseJobs; \
void* timerJobs; \
void* waiterCondition; \
Expand Down
11 changes: 9 additions & 2 deletions packages/xsnap/test/test-xsnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,21 @@ test('meter details', async t => {
t.teardown(() => vat.terminate());
const result = await vat.evaluate(`
let m = new Map();
let s1 = new Set();
for (ix = 0; ix < 20000; ix++) {
m.set(ix, 'garbage');
// m.delete(m);
s1.add(ix);
}
for (ix = 0; ix < 20000; ix++) {
m.delete(ix);
s1.delete(ix);
}
`);
const {
meterUsage: { meterType, ...meters },
} = result;
t.log(meters);
t.is(meterType, 'xs-meter-4');
t.is(meterType, 'xs-meter-5');
const { entries, fromEntries } = Object;
t.deepEqual(
{
Expand All @@ -530,6 +535,8 @@ test('meter details', async t => {
allocateChunksCalls: 'number',
allocateSlotsCalls: 'number',
garbageCollectionCount: 'number',
mapSetAddCount: 'number',
mapSetRemoveCount: 'number',
},
fromEntries(entries(meters).map(([p, v]) => [p, typeof v])),
);
Expand Down

0 comments on commit 327062f

Please sign in to comment.