提交 bc42fa36 编写于 作者: A Anton Perkov

cleos gets resource limits from chain_plugin and prints them #2661

上级 6e90cafb
...@@ -26,6 +26,12 @@ namespace eosio { namespace chain { namespace resource_limits { ...@@ -26,6 +26,12 @@ namespace eosio { namespace chain { namespace resource_limits {
void validate()const; // throws if the parameters do not satisfy basic sanity checks void validate()const; // throws if the parameters do not satisfy basic sanity checks
}; };
struct account_resource_limit {
int64_t current_per_block;
int64_t max_per_block;
int64_t guaranteed_per_day;
};
class resource_limits_manager { class resource_limits_manager {
public: public:
explicit resource_limits_manager(chainbase::database& db) explicit resource_limits_manager(chainbase::database& db)
...@@ -59,9 +65,15 @@ namespace eosio { namespace chain { namespace resource_limits { ...@@ -59,9 +65,15 @@ namespace eosio { namespace chain { namespace resource_limits {
int64_t get_account_cpu_limit( const account_name& name ) const; int64_t get_account_cpu_limit( const account_name& name ) const;
int64_t get_account_net_limit( const account_name& name ) const; int64_t get_account_net_limit( const account_name& name ) const;
account_resource_limit get_account_cpu_limit_ex( const account_name& name ) const;
account_resource_limit get_account_net_limit_ex( const account_name& name ) const;
int64_t get_account_ram_usage( const account_name& name ) const; int64_t get_account_ram_usage( const account_name& name ) const;
private: private:
chainbase::database& _db; chainbase::database& _db;
}; };
} } } /// eosio::chain } } } /// eosio::chain
FC_REFLECT( eosio::chain::resource_limits::account_resource_limit, (current_per_block)(max_per_block)(guaranteed_per_day) )
...@@ -325,6 +325,7 @@ int64_t resource_limits_manager::get_account_cpu_limit( const account_name& name ...@@ -325,6 +325,7 @@ int64_t resource_limits_manager::get_account_cpu_limit( const account_name& name
uint128_t consumed_ex = (uint128_t)usage.cpu_usage.consumed * (uint128_t)config::rate_limiting_precision; uint128_t consumed_ex = (uint128_t)usage.cpu_usage.consumed * (uint128_t)config::rate_limiting_precision;
uint128_t virtual_capacity_ex = (uint128_t)state.virtual_cpu_limit * (uint128_t)config::rate_limiting_precision; uint128_t virtual_capacity_ex = (uint128_t)state.virtual_cpu_limit * (uint128_t)config::rate_limiting_precision;
uint128_t usable_capacity_ex = (uint128_t)(virtual_capacity_ex * limits.cpu_weight) / (uint128_t)total_cpu_weight; uint128_t usable_capacity_ex = (uint128_t)(virtual_capacity_ex * limits.cpu_weight) / (uint128_t)total_cpu_weight;
if (usable_capacity_ex < consumed_ex) { if (usable_capacity_ex < consumed_ex) {
...@@ -334,6 +335,37 @@ int64_t resource_limits_manager::get_account_cpu_limit( const account_name& name ...@@ -334,6 +335,37 @@ int64_t resource_limits_manager::get_account_cpu_limit( const account_name& name
return (int64_t)((usable_capacity_ex - consumed_ex) / (uint128_t)config::rate_limiting_precision); return (int64_t)((usable_capacity_ex - consumed_ex) / (uint128_t)config::rate_limiting_precision);
} }
account_resource_limit resource_limits_manager::get_account_cpu_limit_ex( const account_name& name ) const {
const auto& cfg = _db.get<resource_limits_config_object>();
const auto& state = _db.get<resource_limits_state_object>();
const auto& usage = _db.get<resource_usage_object, by_owner>(name);
const auto& limits = _db.get<resource_limits_object, by_owner>(boost::make_tuple(false, name));
if (limits.cpu_weight < 0) {
return { -1, -1, -1 };
}
auto total_cpu_weight = state.total_cpu_weight;
if( total_cpu_weight == 0 ) total_cpu_weight = 1;
uint128_t consumed_ex = (uint128_t)usage.cpu_usage.consumed * (uint128_t)config::rate_limiting_precision;
uint128_t virtual_capacity_ex = (uint128_t)state.virtual_cpu_limit * (uint128_t)config::rate_limiting_precision;
uint128_t usable_capacity_ex = (uint128_t)(virtual_capacity_ex * limits.cpu_weight) / (uint128_t)total_cpu_weight;
uint128_t real_capacity_ex = (uint128_t)cfg.cpu_limit_parameters.target * (uint128_t)config::rate_limiting_precision;
uint128_t guaranteed_capacity_ex = (uint128_t)(real_capacity_ex * limits.cpu_weight) / (uint128_t)total_cpu_weight;
uint128_t blocks_per_day = 86400 * 1000 / config::block_interval_ms;
if (usable_capacity_ex < consumed_ex) {
consumed_ex = usable_capacity_ex;
}
return { (int64_t)(std::min(usable_capacity_ex - consumed_ex, real_capacity_ex) / (uint128_t)config::rate_limiting_precision),
(int64_t)(std::min(usable_capacity_ex, real_capacity_ex) / (uint128_t)config::rate_limiting_precision),
(int64_t)(guaranteed_capacity_ex * blocks_per_day / cfg.cpu_limit_parameters.periods / (uint128_t)config::rate_limiting_precision)
};
}
int64_t resource_limits_manager::get_account_net_limit( const account_name& name ) const { int64_t resource_limits_manager::get_account_net_limit( const account_name& name ) const {
const auto& state = _db.get<resource_limits_state_object>(); const auto& state = _db.get<resource_limits_state_object>();
const auto& usage = _db.get<resource_usage_object, by_owner>(name); const auto& usage = _db.get<resource_usage_object, by_owner>(name);
...@@ -348,7 +380,7 @@ int64_t resource_limits_manager::get_account_net_limit( const account_name& name ...@@ -348,7 +380,7 @@ int64_t resource_limits_manager::get_account_net_limit( const account_name& name
auto total_net_weight = state.total_net_weight; auto total_net_weight = state.total_net_weight;
if( total_net_weight == 0 ) total_net_weight = 1; if( total_net_weight == 0 ) total_net_weight = 1;
uint128_t usable_capacity_ex = (uint128_t)(virtual_capacity_ex * limits.net_weight) / (uint128_t)total_net_weight; uint128_t usable_capacity_ex = (uint128_t)(virtual_capacity_ex * limits.net_weight) / (uint128_t)total_net_weight; // max
if (usable_capacity_ex < consumed_ex) { if (usable_capacity_ex < consumed_ex) {
return 0; return 0;
...@@ -358,5 +390,36 @@ int64_t resource_limits_manager::get_account_net_limit( const account_name& name ...@@ -358,5 +390,36 @@ int64_t resource_limits_manager::get_account_net_limit( const account_name& name
} }
account_resource_limit resource_limits_manager::get_account_net_limit_ex( const account_name& name ) const {
const auto& cfg = _db.get<resource_limits_config_object>();
const auto& state = _db.get<resource_limits_state_object>();
const auto& usage = _db.get<resource_usage_object, by_owner>(name);
const auto& limits = _db.get<resource_limits_object, by_owner>(boost::make_tuple(false, name));
if (limits.net_weight < 0) {
return { -1, -1, -1 };
}
auto total_net_weight = state.total_net_weight;
if( total_net_weight == 0 ) total_net_weight = 1;
uint128_t consumed_ex = (uint128_t)usage.net_usage.consumed * (uint128_t)config::rate_limiting_precision;
uint128_t virtual_capacity_ex = (uint128_t)state.virtual_net_limit * (uint128_t)config::rate_limiting_precision;
uint128_t usable_capacity_ex = (uint128_t)(virtual_capacity_ex * limits.net_weight) / (uint128_t)total_net_weight; // max
uint128_t real_capacity_ex = (uint128_t)cfg.net_limit_parameters.target * (uint128_t)config::rate_limiting_precision;
uint128_t guaranteed_capacity_ex = (uint128_t)(real_capacity_ex * limits.net_weight) / (uint128_t)total_net_weight;
uint128_t blocks_per_day = 86400 * 1000 / config::block_interval_ms;
if (usable_capacity_ex < consumed_ex) {
consumed_ex = usable_capacity_ex;
}
return { (int64_t)(std::min(usable_capacity_ex - consumed_ex, real_capacity_ex) / (uint128_t)config::rate_limiting_precision),
(int64_t)(std::min(usable_capacity_ex, real_capacity_ex) / (uint128_t)config::rate_limiting_precision),
(int64_t)(guaranteed_capacity_ex * blocks_per_day / cfg.net_limit_parameters.periods / (uint128_t)config::rate_limiting_precision)
};
}
} } } /// eosio::chain::resource_limits } } } /// eosio::chain::resource_limits
...@@ -538,8 +538,8 @@ read_only::get_account_results read_only::get_account( const get_account_params& ...@@ -538,8 +538,8 @@ read_only::get_account_results read_only::get_account( const get_account_params&
result.last_code_update = a.last_code_update; result.last_code_update = a.last_code_update;
result.created = a.creation_date; result.created = a.creation_date;
result.net_limit = rm.get_account_net_limit( result.account_name ); result.net_limit = rm.get_account_net_limit_ex( result.account_name );
result.cpu_limit = rm.get_account_cpu_limit( result.account_name ); result.cpu_limit = rm.get_account_cpu_limit_ex( result.account_name );
result.ram_usage = rm.get_account_ram_usage( result.account_name ); result.ram_usage = rm.get_account_ram_usage( result.account_name );
const auto& permissions = d.get_index<permission_index,by_owner>(); const auto& permissions = d.get_index<permission_index,by_owner>();
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <eosio/chain/block.hpp> #include <eosio/chain/block.hpp>
#include <eosio/chain/controller.hpp> #include <eosio/chain/controller.hpp>
#include <eosio/chain/contract_table_objects.hpp> #include <eosio/chain/contract_table_objects.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/chain/transaction.hpp> #include <eosio/chain/transaction.hpp>
#include <eosio/chain/abi_serializer.hpp> #include <eosio/chain/abi_serializer.hpp>
...@@ -80,6 +81,7 @@ public: ...@@ -80,6 +81,7 @@ public:
name producer_name; name producer_name;
}; };
using account_resource_limit = chain::resource_limits::account_resource_limit;
struct get_account_results { struct get_account_results {
name account_name; name account_name;
...@@ -91,9 +93,9 @@ public: ...@@ -91,9 +93,9 @@ public:
int64_t net_weight = 0; int64_t net_weight = 0;
int64_t cpu_weight = 0; int64_t cpu_weight = 0;
int64_t net_limit = 0; account_resource_limit net_limit;
int64_t cpu_limit = 0; account_resource_limit cpu_limit;
int64_t ram_usage = 0; int64_t ram_usage;
vector<permission> permissions; vector<permission> permissions;
......
...@@ -722,7 +722,6 @@ struct delegate_bandwidth_subcommand { ...@@ -722,7 +722,6 @@ struct delegate_bandwidth_subcommand {
delegate_bandwidth->add_option("receiver", receiver_str, localized("The account to receive the delegated bandwidth"))->required(); delegate_bandwidth->add_option("receiver", receiver_str, localized("The account to receive the delegated bandwidth"))->required();
delegate_bandwidth->add_option("stake_net_quantity", stake_net_amount, localized("The amount of EOS to stake for network bandwidth"))->required(); delegate_bandwidth->add_option("stake_net_quantity", stake_net_amount, localized("The amount of EOS to stake for network bandwidth"))->required();
delegate_bandwidth->add_option("stake_cpu_quantity", stake_cpu_amount, localized("The amount of EOS to stake for CPU bandwidth"))->required(); delegate_bandwidth->add_option("stake_cpu_quantity", stake_cpu_amount, localized("The amount of EOS to stake for CPU bandwidth"))->required();
delegate_bandwidth->add_option("stake_storage_quantity", stake_storage_amount, localized("The amount of EOS to stake for storage"))->required();
add_standard_transaction_options(delegate_bandwidth); add_standard_transaction_options(delegate_bandwidth);
delegate_bandwidth->set_callback([this] { delegate_bandwidth->set_callback([this] {
...@@ -730,8 +729,7 @@ struct delegate_bandwidth_subcommand { ...@@ -730,8 +729,7 @@ struct delegate_bandwidth_subcommand {
("from", from_str) ("from", from_str)
("receiver", receiver_str) ("receiver", receiver_str)
("stake_net_quantity", stake_net_amount + " EOS") ("stake_net_quantity", stake_net_amount + " EOS")
("stake_cpu_quantity", stake_cpu_amount + " EOS") ("stake_cpu_quantity", stake_cpu_amount + " EOS");
("stake_storage_quantity", stake_storage_amount + " EOS");
wdump((act_payload)); wdump((act_payload));
send_actions({create_action({permission_level{from_str,config::active_name}}, config::system_account_name, N(delegatebw), act_payload)}); send_actions({create_action({permission_level{from_str,config::active_name}}, config::system_account_name, N(delegatebw), act_payload)});
}); });
...@@ -751,7 +749,6 @@ struct undelegate_bandwidth_subcommand { ...@@ -751,7 +749,6 @@ struct undelegate_bandwidth_subcommand {
undelegate_bandwidth->add_option("receiver", receiver_str, localized("The account to undelegate bandwidth from"))->required(); undelegate_bandwidth->add_option("receiver", receiver_str, localized("The account to undelegate bandwidth from"))->required();
undelegate_bandwidth->add_option("unstake_net_quantity", unstake_net_amount, localized("The amount of EOS to undelegate for network bandwidth"))->required(); undelegate_bandwidth->add_option("unstake_net_quantity", unstake_net_amount, localized("The amount of EOS to undelegate for network bandwidth"))->required();
undelegate_bandwidth->add_option("unstake_cpu_quantity", unstake_cpu_amount, localized("The amount of EOS to undelegate for CPU bandwidth"))->required(); undelegate_bandwidth->add_option("unstake_cpu_quantity", unstake_cpu_amount, localized("The amount of EOS to undelegate for CPU bandwidth"))->required();
undelegate_bandwidth->add_option("unstake_storage_bytes", unstake_storage_bytes, localized("The amount of byte storage to undelegate"))->required();
add_standard_transaction_options(undelegate_bandwidth); add_standard_transaction_options(undelegate_bandwidth);
undelegate_bandwidth->set_callback([this] { undelegate_bandwidth->set_callback([this] {
...@@ -759,8 +756,7 @@ struct undelegate_bandwidth_subcommand { ...@@ -759,8 +756,7 @@ struct undelegate_bandwidth_subcommand {
("from", from_str) ("from", from_str)
("receiver", receiver_str) ("receiver", receiver_str)
("unstake_net_quantity", unstake_net_amount + " EOS") ("unstake_net_quantity", unstake_net_amount + " EOS")
("unstake_cpu_quantity", unstake_cpu_amount + " EOS") ("unstake_cpu_quantity", unstake_cpu_amount + " EOS");
("unstake_storage_bytes", unstake_storage_bytes);
send_actions({create_action({permission_level{from_str,config::active_name}}, config::system_account_name, N(undelegatebw), act_payload)}); send_actions({create_action({permission_level{from_str,config::active_name}}, config::system_account_name, N(undelegatebw), act_payload)});
}); });
} }
...@@ -922,7 +918,23 @@ void get_account( const string& accountName ) { ...@@ -922,7 +918,23 @@ void get_account( const string& accountName ) {
} }
std::cout << "memory: " << std::endl std::cout << "memory: " << std::endl
<< ident << "quota: " << res.ram_quota << " bytes used: " << res.ram_usage << " bytes staked: " << "XXX" << " EOS" << std::endl; << ident << "quota: " << res.ram_quota << " bytes used: " << res.ram_usage << " bytes staked: " << "XXX" << " EOS" << std::endl << std::endl;
std::cout << "net bandwidth:" << std::endl
<< "staked: " << "XXX EOS" << " (total stake delegated from account to self)" << std::endl
<< "delegated: " << "XXX EOS" << " (total staked delegated to account from others)" << std::endl
<< "current: ~" << res.net_limit.current_per_block << " bytes/block (assuming current congestion and current usage)" << std::endl
<< "max: ~" << res.net_limit.max_per_block << " bytes/block (assuming current congestion and 0 usage in current window)" << std::endl
<< "guaranteed: " << res.net_limit.guaranteed_per_day << " bytes/block . (assuming 100% congestion and 0 usage in current window)" << std::endl
<< std::endl;
std::cout << "net cpu:" << std::endl
<< "staked: " << "XXX EOS" << " (total stake delegated from account to self)" << std::endl
<< "delegated: " << "XXX EOS" << " (total staked delegated to account from others)" << std::endl
<< "current: ~" << res.cpu_limit.current_per_block/1024 << " kcycle/block (assuming current congestion and current usage)" << std::endl
<< "max: ~" << res.cpu_limit.max_per_block/1024 << " kcycle/block (assuming current congestion and 0 usage in current window)" << std::endl
<< "guaranteed: " << res.cpu_limit.guaranteed_per_day/1024 << " kcycle/block . (assuming 100% congestion and 0 usage in current window)" << std::endl
<< std::endl;
std::cout << fc::json::to_pretty_string(json) << std::endl; std::cout << fc::json::to_pretty_string(json) << std::endl;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册