forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sdk, programs/bpf_loader: add sol_remaining_compute_units syscall (so…
…lana-labs#31640) bpf_loader: add sol_remaining_compute_units syscall Co-authored-by: jonch <9093549+jon-chuang@users.noreply.github.com>
- Loading branch information
1 parent
c40e88a
commit 525e59f
Showing
20 changed files
with
266 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
programs/sbf/c/src/remaining_compute_units/remaining_compute_units.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @brief sol_remaining_compute_units Syscall test | ||
*/ | ||
#include <solana_sdk.h> | ||
#include <stdio.h> | ||
|
||
extern uint64_t entrypoint(const uint8_t *input) { | ||
char buffer[200]; | ||
|
||
int i = 0; | ||
for (; i < 100000; ++i) { | ||
if (i % 500 == 0) { | ||
uint64_t remaining = sol_remaining_compute_units(); | ||
snprintf(buffer, 200, "remaining compute units: %d", (int)remaining); | ||
sol_log(buffer); | ||
if (remaining < 25000) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "solana-sbf-rust-remaining-compute-units" | ||
documentation = "https://docs.rs/solana-sbf-rust-remaining-compute-units" | ||
version = { workspace = true } | ||
description = { workspace = true } | ||
authors = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[features] | ||
no-entrypoint = [] | ||
test-bpf = [] | ||
dummy-for-ci-check = ["test-bpf"] | ||
|
||
[dependencies] | ||
solana-program = { workspace = true } | ||
|
||
[dev-dependencies] | ||
solana-program-runtime = { workspace = true } | ||
solana-program-test = { workspace = true } | ||
solana-sdk = { workspace = true } | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//! @brief Example Rust-based BPF program that exercises the sol_remaining_compute_units syscall | ||
extern crate solana_program; | ||
use solana_program::{ | ||
account_info::AccountInfo, compute_units::sol_remaining_compute_units, | ||
entrypoint::ProgramResult, msg, pubkey::Pubkey, | ||
}; | ||
solana_program::entrypoint!(process_instruction); | ||
pub fn process_instruction( | ||
_program_id: &Pubkey, | ||
_accounts: &[AccountInfo], | ||
_instruction_data: &[u8], | ||
) -> ProgramResult { | ||
let mut i = 0u32; | ||
for _ in 0..100_000 { | ||
if i % 500 == 0 { | ||
let remaining = sol_remaining_compute_units(); | ||
msg!("remaining compute units: {:?}", remaining); | ||
if remaining < 25_000 { | ||
break; | ||
} | ||
} | ||
i = i.saturating_add(1); | ||
} | ||
|
||
msg!("i: {:?}", i); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#![cfg(feature = "test-bpf")] | ||
|
||
use { | ||
solana_program_test::*, | ||
solana_sbf_rust_remaining_compute_units::process_instruction, | ||
solana_sdk::{ | ||
instruction::Instruction, pubkey::Pubkey, signature::Signer, transaction::Transaction, | ||
}, | ||
}; | ||
|
||
#[tokio::test] | ||
async fn test_remaining_compute_units() { | ||
let program_id = Pubkey::new_unique(); | ||
let program_test = ProgramTest::new( | ||
"solana_sbf_rust_remaining_compute_units", | ||
program_id, | ||
processor!(process_instruction), | ||
); | ||
let (mut banks_client, payer, recent_blockhash) = program_test.start().await; | ||
|
||
let mut transaction = Transaction::new_with_payer( | ||
&[Instruction::new_with_bincode(program_id, &(), vec![])], | ||
Some(&payer.pubkey()), | ||
); | ||
transaction.sign(&[&payer], recent_blockhash); | ||
banks_client.process_transaction(transaction).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// Return the remaining compute units the program may consume | ||
#[inline] | ||
pub fn sol_remaining_compute_units() -> u64 { | ||
#[cfg(target_os = "solana")] | ||
unsafe { | ||
crate::syscalls::sol_remaining_compute_units() | ||
} | ||
|
||
#[cfg(not(target_os = "solana"))] | ||
{ | ||
crate::program_stubs::sol_remaining_compute_units() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
/** | ||
* @brief Solana logging utilities | ||
*/ | ||
|
||
#include <sol/types.h> | ||
#include <sol/string.h> | ||
#include <sol/entrypoint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* Prints a string to stdout | ||
*/ | ||
/* DO NOT MODIFY THIS GENERATED FILE. INSTEAD CHANGE sdk/sbf/c/inc/sol/inc/compute_units.inc AND RUN `cargo run --bin gen-headers` */ | ||
#ifndef SOL_SBFV2 | ||
uint64_t sol_remaining_compute_units(); | ||
#else | ||
typedef uint64_t(*sol_remaining_compute_units_pointer_type)(); | ||
static uint64_t sol_remaining_compute_units() { | ||
sol_remaining_compute_units_pointer_type sol_remaining_compute_units_pointer = (sol_remaining_compute_units_pointer_type) 3991886574; | ||
return sol_remaining_compute_units_pointer(); | ||
} | ||
#endif | ||
|
||
#ifdef SOL_TEST | ||
/** | ||
* Stub functions when building tests | ||
*/ | ||
|
||
uint64_t sol_remaining_compute_units() { | ||
return UINT64_MAX; | ||
} | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
/**@}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
/** | ||
* @brief Solana logging utilities | ||
*/ | ||
|
||
#include <sol/types.h> | ||
#include <sol/string.h> | ||
#include <sol/entrypoint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* Prints a string to stdout | ||
*/ | ||
@SYSCALL uint64_t sol_remaining_compute_units(); | ||
|
||
#ifdef SOL_TEST | ||
/** | ||
* Stub functions when building tests | ||
*/ | ||
|
||
uint64_t sol_remaining_compute_units() { | ||
return UINT64_MAX; | ||
} | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
/**@}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.