Skip to content

Commit

Permalink
Make maxVatsOnline customizable to optimize resource usage #9574 (#10001
Browse files Browse the repository at this point in the history
)

closes: #9574
refs: #9946

## Description
This change set builds on top of PR #9987 to plumb maxVatsOnline into JS side of SwingSet.

### Security Considerations
A new locally-customizable configuration item is being passed to SwingSet from golang via initAction message.

### Scaling Considerations
New config variable is read from a local config file, parsed, and included in the initAction message. However, the variable is not part of consensus and passed along to the SwingSet only once. 

### Documentation Considerations
new variable `maxVatsOnline` is added to `[swingset]` section of `config/app.toml`

### Testing Considerations
Tested manually by watching total number of vats running as measured by looking at ps aux | grep -c 'xsnap-worker v'  while running make scenario2-run-chain for different values of maxVatsOnline in the config.

### Upgrade Considerations
None, since the default value is the same as previously hardcoded one.
  • Loading branch information
mergify[bot] committed Aug 31, 2024
2 parents 074c43c + d3376fa commit 4492e10
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion golang/cosmos/x/swingset/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,26 @@ const DefaultConfigTemplate = `
# If relative, it is interpreted against the application home directory
# (e.g., ~/.agoric).
slogfile = "{{ .Swingset.SlogFile }}"
# The maximum number of vats that the SwingSet kernel will bring online. A lower number
# requires less memory but may have a negative performance impact if vats need to
# be frequently paged out to remain under this limit.
max_vats_online = {{ .Swingset.MaxVatsOnline }}
`

// SwingsetConfig defines configuration for the SwingSet VM.
// TODO: Consider extensions from docs/env.md.
type SwingsetConfig struct {
// SlogFile is the absolute path at which a SwingSet log "slog" file should be written.
SlogFile string `mapstructure:"slogfile" json:"slogfile,omitempty"`
// MaxVatsOnline is the maximum number of vats that the SwingSet kernel will have online
// at any given time.
MaxVatsOnline int `mapstructure:"max_vats_online" json:"maxVatsOnline,omitempty"`
}

var DefaultSwingsetConfig = SwingsetConfig{
SlogFile: "",
SlogFile: "",
MaxVatsOnline: 50,
}

func SwingsetConfigFromViper(resolvedConfig servertypes.AppOptions) (*SwingsetConfig, error) {
Expand Down
26 changes: 26 additions & 0 deletions packages/cosmic-swingset/src/chain-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ const toNumber = specimen => {
return number;
};

/**
* The swingset config object parsed and resolved by cosmos in
* `golang/cosmos/x/swingset/config.go`. The shape should be kept in sync
* with `SwingsetConfig` defined there.
*
* @typedef {object} CosmosSwingsetConfig
* @property {string} [slogfile]
* @property {number} [maxVatsOnline]
*/

/**
* A boot message consists of cosmosInitAction fields that are subject to
* consensus. See cosmosInitAction in {@link ../../../golang/cosmos/app/app.go}.
Expand All @@ -90,6 +100,19 @@ const makeBootMsg = initAction => {
};
};

/**
* Extract local Swingset-specific configuration which is
* not part of the consensus.
*
* @param {CosmosSwingsetConfig} [resolvedConfig]
*/
const makeSwingsetConfig = resolvedConfig => {
const { maxVatsOnline } = resolvedConfig || {};
return {
maxVatsOnline,
};
};

/**
* @template {unknown} [T=unknown]
* @param {(req: string) => string} call
Expand Down Expand Up @@ -283,6 +306,8 @@ export default async function main(progname, args, { env, homedir, agcc }) {
// eslint-disable-next-line dot-notation
if (slogfile) env['SLOGFILE'] = slogfile;

const swingsetConfig = makeSwingsetConfig(initAction.resolvedConfig);

const sendToChainStorage = msg => chainSend(portNums.storage, msg);
// this object is used to store the mailbox state.
const fromBridgeMailbox = data => {
Expand Down Expand Up @@ -515,6 +540,7 @@ export default async function main(progname, args, { env, homedir, agcc }) {
swingStoreTraceFile,
keepSnapshots,
afterCommitCallback,
swingsetConfig,
});

const { blockingSend, shutdown } = s;
Expand Down
7 changes: 7 additions & 0 deletions packages/cosmic-swingset/src/launch-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export async function buildSwingset(
verbose,
profileVats,
debugVats,
warehousePolicy,
},
) {
const debugPrefix = debugName === undefined ? '' : `${debugName}:`;
Expand Down Expand Up @@ -226,6 +227,7 @@ export async function buildSwingset(
verbose,
profileVats,
debugVats,
warehousePolicy,
},
);

Expand Down Expand Up @@ -330,6 +332,7 @@ export async function launch({
swingStoreExportCallback,
keepSnapshots,
afterCommitCallback = async () => ({}),
swingsetConfig,
}) {
console.info('Launching SwingSet kernel');

Expand Down Expand Up @@ -394,6 +397,9 @@ export async function launch({
});

console.debug(`buildSwingset`);
const warehousePolicy = {
maxVatsOnline: swingsetConfig.maxVatsOnline,
};
const {
coreProposals: bootstrapCoreProposals,
controller,
Expand All @@ -411,6 +417,7 @@ export async function launch({
debugName,
slogCallbacks,
slogSender,
warehousePolicy,
},
);

Expand Down

0 comments on commit 4492e10

Please sign in to comment.