Skip to content

Commit

Permalink
add vote instruction count metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Jan 30, 2024
1 parent b1f8a89 commit 7226901
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/vote/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = { workspace = true }

[dependencies]
bincode = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
num-derive = { workspace = true }
num-traits = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions programs/vote/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ extern crate solana_metrics;
#[macro_use]
extern crate solana_frozen_abi_macro;

#[macro_use]
extern crate lazy_static;

pub use solana_sdk::vote::{
authorized_voters, error as vote_error, instruction as vote_instruction,
program::{check_id, id},
Expand Down
69 changes: 68 additions & 1 deletion programs/vote/src/vote_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use {
crate::vote_state,
log::*,
solana_metrics::datapoint_info,
solana_program::vote::{instruction::VoteInstruction, program::id, state::VoteAuthorize},
solana_program_runtime::{
declare_process_instruction, invoke_context::InvokeContext,
Expand All @@ -12,9 +13,13 @@ use {
instruction::InstructionError,
program_utils::limited_deserialize,
pubkey::Pubkey,
timing::AtomicInterval,
transaction_context::{BorrowedAccount, InstructionContext, TransactionContext},
},
std::collections::HashSet,
std::{
collections::HashSet,
sync::atomic::{AtomicU64, Ordering},
},
};

fn process_authorize_with_seed_instruction(
Expand Down Expand Up @@ -49,6 +54,64 @@ fn process_authorize_with_seed_instruction(
)
}

#[derive(Debug, Default)]
struct VoteInstructionMetrics {
vote_count: AtomicU64,
vote_state_update_count: AtomicU64,
compact_vote_state_update_count: AtomicU64,
last_report_time: AtomicInterval,
}

enum VoteInstructionType {
Vote,
VoteStateUpdate,
CompactVoteStateUpdate,
}

impl VoteInstructionMetrics {
fn update_and_report(&self, vote_instruction_type: VoteInstructionType) {
const REPORT_INTERVAL_MS: u64 = 5_000;
match vote_instruction_type {
VoteInstructionType::Vote => {
self.vote_count.fetch_add(1, Ordering::Relaxed);
}
VoteInstructionType::VoteStateUpdate => {
self.vote_state_update_count.fetch_add(1, Ordering::Relaxed);
}
VoteInstructionType::CompactVoteStateUpdate => {
self.compact_vote_state_update_count
.fetch_add(1, Ordering::Relaxed);
}
}

if self.last_report_time.should_update(REPORT_INTERVAL_MS) {
datapoint_info!(
"vote_instructions_processed",
(
"vote_count",
self.vote_count.swap(0, Ordering::Relaxed),
i64
),
(
"vote_state_update_count",
self.vote_state_update_count.swap(0, Ordering::Relaxed),
i64
),
(
"compact_vote_state_update_count",
self.compact_vote_state_update_count
.swap(0, Ordering::Relaxed),
i64
),
);
}
}
}

lazy_static! {
static ref VOTE_INSTRUCTION_METRICS: VoteInstructionMetrics = VoteInstructionMetrics::default();
}

// Citing `runtime/src/block_cost_limit.rs`, vote has statically defined 2100
// units; can consume based on instructions in the future like `bpf_loader` does.
pub const DEFAULT_COMPUTE_UNITS: u64 = 2_100;
Expand All @@ -66,6 +129,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
}

let signers = instruction_context.get_signers(transaction_context)?;

match limited_deserialize(data)? {
VoteInstruction::InitializeAccount(vote_init) => {
let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?;
Expand Down Expand Up @@ -151,6 +215,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
)
}
VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => {
VOTE_INSTRUCTION_METRICS.update_and_report(VoteInstructionType::Vote);
let slot_hashes =
get_sysvar_with_account_check::slot_hashes(invoke_context, instruction_context, 1)?;
let clock =
Expand All @@ -166,6 +231,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
}
VoteInstruction::UpdateVoteState(vote_state_update)
| VoteInstruction::UpdateVoteStateSwitch(vote_state_update, _) => {
VOTE_INSTRUCTION_METRICS.update_and_report(VoteInstructionType::VoteStateUpdate);
let sysvar_cache = invoke_context.get_sysvar_cache();
let slot_hashes = sysvar_cache.get_slot_hashes()?;
let clock = sysvar_cache.get_clock()?;
Expand All @@ -180,6 +246,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
}
VoteInstruction::CompactUpdateVoteState(vote_state_update)
| VoteInstruction::CompactUpdateVoteStateSwitch(vote_state_update, _) => {
VOTE_INSTRUCTION_METRICS.update_and_report(VoteInstructionType::CompactVoteStateUpdate);
let sysvar_cache = invoke_context.get_sysvar_cache();
let slot_hashes = sysvar_cache.get_slot_hashes()?;
let clock = sysvar_cache.get_clock()?;
Expand Down

0 comments on commit 7226901

Please sign in to comment.