-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
libexpr: Add EvalProfiler and use it for FunctionCallTrace
#13219
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,48 @@ | ||
| #include "nix/expr/eval-profiler.hh" | ||
| #include "nix/expr/nixexpr.hh" | ||
|
|
||
| namespace nix { | ||
|
|
||
| void EvalProfiler::preFunctionCallHook( | ||
| const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos) | ||
| { | ||
| } | ||
|
|
||
| void EvalProfiler::postFunctionCallHook( | ||
| const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos) | ||
| { | ||
| } | ||
|
|
||
| void MultiEvalProfiler::preFunctionCallHook( | ||
| const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos) | ||
| { | ||
| for (auto & profiler : profilers) { | ||
| if (profiler->getNeededHooks().test(Hook::preFunctionCall)) | ||
| profiler->preFunctionCallHook(state, v, args, pos); | ||
| } | ||
| } | ||
|
|
||
| void MultiEvalProfiler::postFunctionCallHook( | ||
| const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos) | ||
| { | ||
| for (auto & profiler : profilers) { | ||
| if (profiler->getNeededHooks().test(Hook::postFunctionCall)) | ||
| profiler->postFunctionCallHook(state, v, args, pos); | ||
| } | ||
| } | ||
|
|
||
| EvalProfiler::Hooks MultiEvalProfiler::getNeededHooksImpl() const | ||
| { | ||
| Hooks hooks; | ||
| for (auto & p : profilers) | ||
| hooks |= p->getNeededHooks(); | ||
| return hooks; | ||
| } | ||
|
|
||
| void MultiEvalProfiler::addProfiler(ref<EvalProfiler> profiler) | ||
| { | ||
| profilers.push_back(profiler); | ||
| invalidateNeededHooks(); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,113 @@ | ||
| #pragma once | ||
| /** | ||
| * @file | ||
| * | ||
| * Evaluation profiler interface definitions and builtin implementations. | ||
| */ | ||
|
|
||
| #include "nix/util/ref.hh" | ||
|
|
||
| #include <vector> | ||
| #include <span> | ||
| #include <bitset> | ||
| #include <optional> | ||
|
|
||
| namespace nix { | ||
|
|
||
| class EvalState; | ||
| class PosIdx; | ||
| struct Value; | ||
|
|
||
| class EvalProfiler | ||
| { | ||
| public: | ||
| enum Hook { | ||
| preFunctionCall, | ||
| postFunctionCall, | ||
| }; | ||
|
|
||
| static constexpr std::size_t numHooks = Hook::postFunctionCall + 1; | ||
| using Hooks = std::bitset<numHooks>; | ||
|
|
||
| private: | ||
| std::optional<Hooks> neededHooks; | ||
|
|
||
| protected: | ||
| /** Invalidate the cached neededHooks. */ | ||
| void invalidateNeededHooks() | ||
| { | ||
| neededHooks = std::nullopt; | ||
| } | ||
|
|
||
| /** | ||
| * Get which hooks need to be called. | ||
| * | ||
| * This is the actual implementation which has to be defined by subclasses. | ||
| * Public API goes through the needsHooks, which is a | ||
| * non-virtual interface (NVI) which caches the return value. | ||
| */ | ||
| virtual Hooks getNeededHooksImpl() const | ||
| { | ||
| return Hooks{}; | ||
| } | ||
|
|
||
| public: | ||
| /** | ||
| * Hook called in the EvalState::callFunction preamble. | ||
| * Gets called only if (getNeededHooks().test(Hook::preFunctionCall)) is true. | ||
| * | ||
| * @param state Evaluator state. | ||
| * @param v Function being invoked. | ||
| * @param args Function arguments. | ||
| * @param pos Function position. | ||
| */ | ||
| virtual void | ||
| preFunctionCallHook(const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos); | ||
|
|
||
| /** | ||
| * Hook called on EvalState::callFunction exit. | ||
| * Gets called only if (getNeededHooks().test(Hook::postFunctionCall)) is true. | ||
| * | ||
| * @param state Evaluator state. | ||
| * @param v Function being invoked. | ||
| * @param args Function arguments. | ||
| * @param pos Function position. | ||
| */ | ||
| virtual void | ||
| postFunctionCallHook(const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos); | ||
|
|
||
| virtual ~EvalProfiler() = default; | ||
|
|
||
| /** | ||
| * Get which hooks need to be invoked for this EvalProfiler instance. | ||
| */ | ||
| Hooks getNeededHooks() | ||
| { | ||
| if (neededHooks.has_value()) | ||
| return *neededHooks; | ||
| return *(neededHooks = getNeededHooksImpl()); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Profiler that invokes multiple profilers at once. | ||
| */ | ||
| class MultiEvalProfiler : public EvalProfiler | ||
| { | ||
| std::vector<ref<EvalProfiler>> profilers; | ||
|
|
||
| [[gnu::noinline]] Hooks getNeededHooksImpl() const override; | ||
|
|
||
| public: | ||
| MultiEvalProfiler() = default; | ||
|
|
||
| /** Register a profiler instance. */ | ||
| void addProfiler(ref<EvalProfiler> profiler); | ||
|
|
||
| [[gnu::noinline]] void | ||
| preFunctionCallHook(const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos) override; | ||
| [[gnu::noinline]] void | ||
| postFunctionCallHook(const EvalState & state, const Value & v, std::span<Value *> args, const PosIdx pos) override; | ||
| }; | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Since #13211
operator[]is much cheaper to call, sincePosTablenow caches the line information.