Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vote instruction count metrics #35015

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
80 changes: 77 additions & 3 deletions 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,69 @@ 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,
total: AtomicU64,
last_report_time: AtomicInterval,
}

enum VoteInstructionType {
Vote,
VoteStateUpdate,
CompactVoteStateUpdate,
Other,
}

impl VoteInstructionMetrics {
fn update_and_report(&self, vote_instruction_type: VoteInstructionType) {
const REPORT_INTERVAL_MS: u64 = 5_000;
self.total.fetch_add(1, Ordering::Relaxed);
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
),
("total", self.total.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 @@ -59,14 +127,15 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
let data = instruction_context.get_instruction_data();

trace!("process_instruction: {:?}", data);
let mut vote_instr_type = VoteInstructionType::Other;

let mut me = instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
if *me.get_owner() != id() {
return Err(InstructionError::InvalidAccountOwner);
}

let signers = instruction_context.get_signers(transaction_context)?;
match limited_deserialize(data)? {
let process_result = match limited_deserialize(data)? {
VoteInstruction::InitializeAccount(vote_init) => {
let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?;
if !rent.is_exempt(me.get_lamports(), me.get_data().len()) {
Expand Down Expand Up @@ -151,6 +220,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
)
}
VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => {
vote_instr_type = VoteInstructionType::Vote;
let slot_hashes =
get_sysvar_with_account_check::slot_hashes(invoke_context, instruction_context, 1)?;
let clock =
Expand All @@ -166,6 +236,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
}
VoteInstruction::UpdateVoteState(vote_state_update)
| VoteInstruction::UpdateVoteStateSwitch(vote_state_update, _) => {
vote_instr_type = 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 +251,7 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
}
VoteInstruction::CompactUpdateVoteState(vote_state_update)
| VoteInstruction::CompactUpdateVoteStateSwitch(vote_state_update, _) => {
vote_instr_type = 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 Expand Up @@ -230,7 +302,9 @@ declare_process_instruction!(Entrypoint, DEFAULT_COMPUTE_UNITS, |invoke_context|
&invoke_context.feature_set,
)
}
}
};
VOTE_INSTRUCTION_METRICS.update_and_report(vote_instr_type);
process_result
});

#[cfg(test)]
Expand Down
Loading