Skip to content

Commit

Permalink
Add stateClonedCount metric
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Jul 11, 2022
1 parent 7bf6764 commit 9e96a9c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export class StateContextCache {
if (!item) {
return null;
}

this.metrics?.hits.inc();
// clonedCount + 1 as there's a .clone() below
this.metrics?.stateClonedCount.observe(item.clonedCount + 1);

return item.clone();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ export class CheckpointStateCache {
this.metrics?.lookups.inc();
const cpKey = toCheckpointKey(cp);
const item = this.cache.get(cpKey);
if (item) {
this.metrics?.hits.inc();
if (cpKey === this.preComputedCheckpoint) {
this.preComputedCheckpointHits = (this.preComputedCheckpointHits ?? 0) + 1;
}

if (!item) {
return null;
}

this.metrics?.hits.inc();
// clonedCount + 1 as there's a .clone() below
this.metrics?.stateClonedCount.observe(item.clonedCount + 1);

if (cpKey === this.preComputedCheckpoint) {
this.preComputedCheckpointHits = (this.preComputedCheckpointHits ?? 0) + 1;
}
return item ? item.clone() : null;

return item.clone();
}

add(cp: phase0.Checkpoint, item: CachedBeaconStateAllForks): void {
Expand Down
10 changes: 10 additions & 0 deletions packages/beacon-node/src/metrics/metrics/lodestar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,11 @@ export function createLodestarMetrics(
name: "lodestar_state_cache_seconds_since_last_read",
help: "Avg min max of all state cache items seconds since last reads",
}),
stateClonedCount: register.histogram({
name: "lodestar_state_cache_state_cloned_clount",
help: "Histogram of cloned count per state every time state.clone() is called",
buckets: [1, 2, 5, 10, 50, 250],
}),
},

cpStateCache: {
Expand Down Expand Up @@ -852,6 +857,11 @@ export function createLodestarMetrics(
name: "lodestar_cp_state_epoch_seconds_since_last_read",
help: "Avg min max of all state cache items seconds since last reads",
}),
stateClonedCount: register.histogram({
name: "lodestar_cp_state_cache_state_cloned_clount",
help: "Histogram of cloned count per state every time state.clone() is called",
buckets: [1, 2, 5, 10, 50, 250],
}),
},

seenCache: {
Expand Down
8 changes: 8 additions & 0 deletions packages/state-transition/src/cache/stateCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {BeaconStatePhase0, BeaconStateAltair, BeaconStateBellatrix, BeaconStateA
export type BeaconStateCache = {
config: IBeaconConfig;
epochCtx: EpochContext;
/** Count of clones created from this BeaconStateCache instance. readonly to prevent accidental usage downstream */
readonly clonedCount: number;
};

/**
Expand Down Expand Up @@ -119,6 +121,7 @@ export function createCachedBeaconState<T extends BeaconStateAllForks>(
return getCachedBeaconState(state, {
config: immutableData.config,
epochCtx: EpochContext.createFromState(state, immutableData, opts),
clonedCount: 0,
});
}

Expand All @@ -139,9 +142,14 @@ export function getCachedBeaconState<T extends BeaconStateAllForks>(

function clone(this: T & BeaconStateCache): T & BeaconStateCache {
const viewDUCloned = viewDUClone();

// Override `readonly` attribute in single place where `.clonedCount` is incremented
(this as {clonedCount: number}).clonedCount++;

return getCachedBeaconState(viewDUCloned, {
config: this.config,
epochCtx: this.epochCtx.clone(),
clonedCount: 0,
}) as T & BeaconStateCache;
}

Expand Down

0 comments on commit 9e96a9c

Please sign in to comment.