-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(swingset): define 'meterControl' to disable metering
The `meterControl` tool will give liveslots a way to temporarily disable metering, so it can perform GC-sensitive operations without affecting the vat's metering results. Each supervisor provides their own version: all are dummy facades except for XS. refs #3458
- Loading branch information
Showing
11 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// @ts-check | ||
import { assert } from '@agoric/assert'; | ||
|
||
export function makeDummyMeterControl() { | ||
let meteringDisabled = 0; | ||
|
||
function isMeteringDisabled() { | ||
return !!meteringDisabled; | ||
} | ||
|
||
function assertIsMetered(msg) { | ||
assert(!meteringDisabled, msg); | ||
} | ||
|
||
function assertNotMetered(msg) { | ||
assert(!!meteringDisabled, msg); | ||
} | ||
|
||
function runWithoutMetering(thunk) { | ||
meteringDisabled += 1; | ||
try { | ||
return thunk(); | ||
} finally { | ||
meteringDisabled -= 1; | ||
} | ||
} | ||
|
||
async function runWithoutMeteringAsync(thunk) { | ||
meteringDisabled += 1; | ||
try { | ||
return await thunk(); | ||
} finally { | ||
meteringDisabled -= 1; | ||
} | ||
} | ||
|
||
// return a version of f that runs outside metering | ||
function unmetered(f) { | ||
function wrapped(...args) { | ||
return runWithoutMetering(() => f(...args)); | ||
} | ||
return harden(wrapped); | ||
} | ||
|
||
/** @type { MeterControl } */ | ||
const meterControl = { | ||
isMeteringDisabled, | ||
assertIsMetered, | ||
assertNotMetered, | ||
runWithoutMetering, | ||
runWithoutMeteringAsync, | ||
unmetered, | ||
}; | ||
return harden(meterControl); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// eslint-disable-next-line import/order | ||
import { test } from '../tools/prepare-test-env-ava.js'; | ||
|
||
import { makeDummyMeterControl } from '../src/kernel/dummyMeterControl.js'; | ||
|
||
test('dummy meter control', async t => { | ||
const mc = makeDummyMeterControl(); | ||
t.false(mc.isMeteringDisabled()); | ||
t.throws(mc.assertNotMetered); | ||
mc.runWithoutMetering(mc.assertNotMetered); | ||
let x = 0; | ||
mc.runWithoutMetering(() => (x = 1)); | ||
t.is(x, 1); | ||
await mc.runWithoutMeteringAsync(() => (x = 2)); | ||
t.is(x, 2); | ||
function set(y) { | ||
x = y; | ||
return 'yes'; | ||
} | ||
const unmeteredSet = mc.unmetered(set); | ||
t.is(unmeteredSet(3), 'yes'); | ||
t.is(x, 3); | ||
}); |