-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add CPU billing #454
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6cacf9f
Enable host functions for transaction CPU billing
swatanabe b0b1700
Use clock_gettime instead of getBillableTime. Add tests for posix and…
swatanabe 40d69a3
Add subjective cpu-sys service to manage cpu billing and timeout
swatanabe 6a58275
Remove initial watchdog limit. TransactionSys sets a limit before exe…
swatanabe 2f212e2
Increase test threshold
swatanabe fec74a8
Update summary to link to new page
swatanabe d9755e4
Fix parameter name
swatanabe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,32 @@ | ||
# System Functions | ||
|
||
Some parts of the C++ standard library that interact with the host system are restricted or unavailable in services. | ||
|
||
## I/O | ||
|
||
Only stdout and stderr are available. They both write to a single merged stream which is in included the transaction trace. stdin is not supported. | ||
|
||
Supported APIs: | ||
- `write` | ||
- `printf` family | ||
- `std::cout`, `std::cerr`, `std::clog` | ||
|
||
## 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. | ||
|
||
Supported APIs: | ||
- `clock_gettime` with `CLOCK_REALITME`, `CLOCK_MONOTONIC`, or `CLOCK_PROCESS_CPUTIME_ID` | ||
- `clock` | ||
- `time` | ||
- `std::chrono::system_clock` | ||
- `std::chrono::monotonic_clock` | ||
- `std::chrono::high_resolution_clock` | ||
|
||
## Unavailable Functionality | ||
|
||
- Filesystem | ||
- Nondeterministic random numbers | ||
- Concurrency | ||
- Exceptions | ||
- `setjmp`/`longjmp` |
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
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,68 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <condition_variable> | ||
#include <functional> | ||
#include <mutex> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include <time.h> | ||
|
||
namespace psibase | ||
{ | ||
|
||
struct CpuClock | ||
{ | ||
using rep = std::int64_t; | ||
using period = std::ratio<1, 1000000000>; | ||
using duration = std::chrono::duration<rep, period>; | ||
using time_point = std::chrono::time_point<CpuClock>; | ||
static constexpr bool is_steady = false; | ||
static time_point now(clockid_t clock = CLOCK_THREAD_CPUTIME_ID); | ||
}; | ||
|
||
class Watchdog; | ||
|
||
class WatchdogManager | ||
{ | ||
public: | ||
WatchdogManager(); | ||
~WatchdogManager(); | ||
|
||
private: | ||
friend class Watchdog; | ||
void add(Watchdog* wd); | ||
void remove(Watchdog* wd); | ||
void run(); | ||
std::mutex mutex; | ||
std::condition_variable cond; | ||
std::vector<Watchdog*> children; | ||
bool done = false; | ||
std::thread worker; | ||
}; | ||
|
||
class Watchdog | ||
{ | ||
public: | ||
Watchdog(WatchdogManager& m, std::function<void()> f); | ||
~Watchdog(); | ||
void setLimit(CpuClock::duration dur); | ||
void pause(); | ||
void resume(); | ||
CpuClock::duration elapsed(); | ||
void interrupt(); | ||
|
||
private: | ||
friend class WatchdogManager; | ||
// Called with manager mutex held | ||
CpuClock::duration wait_duration(); | ||
WatchdogManager* manager; | ||
bool paused; | ||
CpuClock::duration elapsedOrStart; | ||
CpuClock::duration limit = CpuClock::duration::max(); | ||
clockid_t cpuclock; | ||
std::function<void()> handler; | ||
}; | ||
|
||
} // namespace psibase |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.