Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Refactor - Simplify loader utils (backport #29699) (#29757)
Browse files Browse the repository at this point in the history
Refactor - Simplify loader utils (#29699)

* Moves all the common code of tests and benches into loader utils.

* Removes write_sbf_program().

* Removes ignored test: test_program_sbf_test_use_latest_executor2().

(cherry picked from commit ab97d37)

Co-authored-by: Alexander Meißner <AlexanderMeissner@gmx.net>
  • Loading branch information
mergify[bot] and Lichtso authored Jan 19, 2023
1 parent 35ed752 commit 0a3e52b
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 481 deletions.
52 changes: 8 additions & 44 deletions programs/bpf/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,26 @@ use {
bank::Bank,
bank_client::BankClient,
genesis_utils::{create_genesis_config, GenesisConfigInfo},
loader_utils::load_program,
loader_utils::{create_deprecated_program, load_program_from_file},
},
solana_sdk::{
bpf_loader,
client::SyncClient,
entrypoint::SUCCESS,
instruction::{AccountMeta, Instruction},
message::Message,
pubkey::Pubkey,
signature::{Keypair, Signer},
signature::Signer,
},
std::{env, fs::File, io::Read, mem, path::PathBuf, sync::Arc},
std::{mem, sync::Arc},
test::Bencher,
};

/// BPF program file extension
const PLATFORM_FILE_EXTENSION_BPF: &str = "so";
/// Create a BPF program file name
fn create_bpf_path(name: &str) -> PathBuf {
let mut pathbuf = {
let current_exe = env::current_exe().unwrap();
PathBuf::from(current_exe.parent().unwrap().parent().unwrap())
};
pathbuf.push("bpf/");
pathbuf.push(name);
pathbuf.set_extension(PLATFORM_FILE_EXTENSION_BPF);
pathbuf
}

fn load_elf(name: &str) -> Result<Vec<u8>, std::io::Error> {
let path = create_bpf_path(name);
let mut file = File::open(&path).expect(&format!("Unable to open {:?}", path));
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
Ok(elf)
}

fn load_bpf_program(
bank_client: &BankClient,
loader_id: &Pubkey,
payer_keypair: &Keypair,
name: &str,
) -> Pubkey {
let path = create_bpf_path(name);
let mut file = File::open(path).unwrap();
let mut elf = Vec::new();
file.read_to_end(&mut elf).unwrap();
load_program(bank_client, payer_keypair, loader_id, elf)
}

const ARMSTRONG_LIMIT: u64 = 500;
const ARMSTRONG_EXPECTED: u64 = 5;

#[bench]
fn bench_program_create_executable(bencher: &mut Bencher) {
let elf = load_elf("bench_alu").unwrap();
let elf = load_program_from_file("bench_alu");

bencher.iter(|| {
let _ = Executable::<BpfError, ThisInstructionMeter>::from_elf(
Expand All @@ -98,7 +62,7 @@ fn bench_program_alu(bencher: &mut Bencher) {
.write_u64::<LittleEndian>(ARMSTRONG_LIMIT)
.unwrap();
inner_iter.write_u64::<LittleEndian>(0).unwrap();
let elf = load_elf("bench_alu").unwrap();
let elf = load_program_from_file("bench_alu");
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, |invoke_context| {
invoke_context
Expand Down Expand Up @@ -191,7 +155,7 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {
let bank_client = BankClient::new_shared(&bank);

let invoke_program_id =
load_bpf_program(&bank_client, &bpf_loader::id(), &mint_keypair, "noop");
create_deprecated_program(&bank_client, &bpf_loader::id(), &mint_keypair, "noop");

let mint_pubkey = mint_keypair.pubkey();
let account_metas = vec![AccountMeta::new(mint_pubkey, true)];
Expand All @@ -214,7 +178,7 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {

#[bench]
fn bench_create_vm(bencher: &mut Bencher) {
let elf = load_elf("noop").unwrap();
let elf = load_program_from_file("noop");
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, |invoke_context| {
const BUDGET: u64 = 200_000;
Expand Down Expand Up @@ -262,7 +226,7 @@ fn bench_create_vm(bencher: &mut Bencher) {

#[bench]
fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
let elf = load_elf("tuner").unwrap();
let elf = load_program_from_file("tuner");
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, |invoke_context| {
const BUDGET: u64 = 200_000;
Expand Down
Loading

0 comments on commit 0a3e52b

Please sign in to comment.