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 CPU billing #454

Merged
merged 7 commits into from
Jul 13, 2023
Merged

Add CPU billing #454

merged 7 commits into from
Jul 13, 2023

Conversation

swatanabe
Copy link
Collaborator

By default, accounts have unlimited resources. The billing system is minimal—just enough to ensure that the host functions are sufficient and work correctly.

This also allows boot to complete reliably without timing out, as timeouts are disabled until boot is completed.


## Clocks

UTC, monotonic, and CPU clocks are available to subjective services. The monotonic clock is only guaranteed to be monotonic within a block. The CPU clock measures CPU time spent on the current transaction.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should define or link to something that defines "subjective services"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any existing documentation for subjective services and it certainly doesn't belong here. Maybe under services/concepts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps just in services itself

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be deferred to another PR. Documentation of subjective services is only incidentally related and this PR is already quite large.

@@ -57,7 +61,8 @@ namespace psibase
std::lock_guard<std::mutex> lock{impl->mutex};
if (impl->systemContextCache.empty())
{
auto result = std::make_unique<SystemContext>(SystemContext{impl->db, impl->wasmCache});
auto result = std::make_unique<SystemContext>(
SystemContext{impl->db, impl->wasmCache, {}, impl->watchdogManager});
impl->allSystemContexts.push_back(result.get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sketchy. Couldn't this lead to UB if the unique ptr is destroyed and the linearMemorySpan is called?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, though that never happens. Also, note the comment on the declaration of allSystemContexts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pre-existing code that is not modified by this PR.

std::chrono::nanoseconds getCpuTime();
void setCpuLimit(psibase::AccountNumber account);
};
PSIO_REFLECT(CpuSys, method(getCpuTime), method(setCpuLimit, limit))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be method(setCpuLimit, account)

@@ -293,6 +294,9 @@ namespace SystemService
"transaction references non-existing block");
}

Actor<CpuSys> cpuSys(TransactionSys::service, CpuSys::service);
Actor<AccountSys> accountSys(TransactionSys::service, AccountSys::service);
Copy link
Member

@James-Mart James-Mart Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you not prefer the to<Service>.action syntax? I like how it reads

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to doesn't work here, because it uses state set up by the dispatcher. processTransaction is a direct export that doesn't go through the dispatcher.

@James-Mart James-Mart linked an issue Jul 12, 2023 that may be closed by this pull request
@@ -606,7 +604,7 @@ bool pushTransaction(psibase::SharedState& sharedState,
// for a modified node to skip it during production. This won't hurt
// consensus since replay never uses read-only mode for auth checks.
auto saveTrace = trace;
proofBC.checkFirstAuth(trx, trace, firstAuthWatchdogLimit);
proofBC.checkFirstAuth(trx, trace, std::nullopt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't we default the watchdog limit here to the leeway?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make the leeway option go away entirely and have it managed entirely on chain.

@swatanabe swatanabe merged commit 0d9c79b into main Jul 13, 2023
@swatanabe swatanabe deleted the cpu-billing branch July 13, 2023 20:34
@James-Mart James-Mart added the Node infra Related to the necessary infrastructure provided by all psibase infrastructure providers label Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Node infra Related to the necessary infrastructure provided by all psibase infrastructure providers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Subtask: Track CPU consumption
2 participants