Skip to content

Commit

Permalink
v1.17: Fix - Uses fetch_add() to accumulate usage counters in `Load…
Browse files Browse the repository at this point in the history
…edPrograms` (backport of #34319) (#34348)

Fix - Uses `fetch_add()` to accumulate usage counters in `LoadedPrograms` (#34319)

Uses fetch_add() to accumulate usage counters.

(cherry picked from commit f70d548)

Co-authored-by: Alexander Meißner <AlexanderMeissner@gmx.net>
  • Loading branch information
mergify[bot] and Lichtso committed Dec 12, 2023
1 parent 222ca9b commit 0b6a172
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,13 +600,11 @@ impl<FG: ForkGraph> LoadedPrograms<FG> {
if matches!(existing.program, LoadedProgramType::Unloaded(_)) {
// The unloaded program is getting reloaded
// Copy over the usage counter to the new entry
let mut usage_count = existing.tx_usage_counter.load(Ordering::Relaxed);
saturating_add_assign!(
usage_count,
entry.tx_usage_counter.load(Ordering::Relaxed)
entry.tx_usage_counter.fetch_add(
existing.tx_usage_counter.load(Ordering::Relaxed),
Ordering::Relaxed,
);
entry.tx_usage_counter.store(usage_count, Ordering::Relaxed);
entry.ix_usage_counter.store(
entry.ix_usage_counter.fetch_add(
existing.ix_usage_counter.load(Ordering::Relaxed),
Ordering::Relaxed,
);
Expand Down Expand Up @@ -786,7 +784,7 @@ impl<FG: ForkGraph> LoadedPrograms<FG> {
let mut unloaded = Vec::new();
let current_slot = working_slot.current_slot();
let found = keys
.filter_map(|(key, (match_criteria, count))| {
.filter_map(|(key, (match_criteria, usage_count))| {
if let Some(second_level) = self.entries.get(&key) {
for entry in second_level.iter().rev() {
let is_ancestor = if let Some(fork_graph) = &self.fork_graph {
Expand All @@ -810,41 +808,41 @@ impl<FG: ForkGraph> LoadedPrograms<FG> {
{
if current_slot >= entry.effective_slot {
if !Self::is_entry_usable(entry, current_slot, &match_criteria) {
missing.push((key, count));
missing.push((key, usage_count));
return None;
}

if !Self::matches_environment(entry, environments) {
missing.push((key, count));
missing.push((key, usage_count));
return None;
}

if let LoadedProgramType::Unloaded(_environment) = &entry.program {
unloaded.push((key, count));
unloaded.push((key, usage_count));
return None;
}

let mut usage_count =
entry.tx_usage_counter.load(Ordering::Relaxed);
saturating_add_assign!(usage_count, count);
entry.tx_usage_counter.store(usage_count, Ordering::Relaxed);
entry
.tx_usage_counter
.fetch_add(usage_count, Ordering::Relaxed);
return Some((key, entry.clone()));
} else if entry.is_implicit_delay_visibility_tombstone(current_slot) {
// Found a program entry on the current fork, but it's not effective
// yet. It indicates that the program has delayed visibility. Return
// the tombstone to reflect that.
return Some((
key,
Arc::new(LoadedProgram::new_tombstone(
entry.deployment_slot,
LoadedProgramType::DelayVisibility,
)),
let entry_to_return = Arc::new(LoadedProgram::new_tombstone(
entry.deployment_slot,
LoadedProgramType::DelayVisibility,
));
entry_to_return
.tx_usage_counter
.fetch_add(usage_count, Ordering::Relaxed);
return Some((key, entry_to_return));
}
}
}
}
missing.push((key, count));
missing.push((key, usage_count));
None
})
.collect::<HashMap<Pubkey, Arc<LoadedProgram>>>();
Expand Down

0 comments on commit 0b6a172

Please sign in to comment.