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 fork support in runtime light #1029

Closed
wants to merge 2 commits into from

Conversation

astrophysik
Copy link
Contributor

@astrophysik astrophysik commented Jun 27, 2024

Fork support in runtime light

This pr add fork and wait functions in runtime light. The main difference from forks in the master is the use of C++20 coroutines

Overiew

The class that stores the current state of the forks is KphpForkContext. It contains fork_scheduler, which is responsible for executing forks. In the schedule function it tries to execute all possible coroutines, if the main fork runs out or there are no forks available for execution the component is blocked.

The fork stored in the light_fork class. It stores a pointer to the first coroutine frame and the last interrupt point. The fork_result type erasue class is used to store the result.

@astrophysik astrophysik self-assigned this Jun 27, 2024
@astrophysik astrophysik added k2 k2 related runtime Feature related to runtime labels Jun 27, 2024
@astrophysik astrophysik marked this pull request as ready for review July 1, 2024 14:58
runtime-light/component/component.cpp Outdated Show resolved Hide resolved
runtime-light/component/component.cpp Outdated Show resolved Hide resolved
runtime-light/core/small-object-storage.h Outdated Show resolved Hide resolved
runtime-light/core/small-object-storage.h Outdated Show resolved Hide resolved
runtime-light/utils/timer.cpp Outdated Show resolved Hide resolved
runtime-light/component/component.cpp Outdated Show resolved Hide resolved
runtime-light/component/component.cpp Outdated Show resolved Hide resolved
runtime-light/streams/streams.h Show resolved Hide resolved
apolyakov
apolyakov previously approved these changes Jul 5, 2024
Copy link
Contributor

@apolyakov apolyakov left a comment

Choose a reason for hiding this comment

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

LGTM

apolyakov
apolyakov previously approved these changes Jul 5, 2024
@astrophysik astrophysik force-pushed the vsadokhov/fork-light-support branch from 58b5083 to 29c4d07 Compare July 5, 2024 12:32
@astrophysik astrophysik requested a review from a team as a code owner July 5, 2024 14:31
@astrophysik astrophysik force-pushed the vsadokhov/fork-light-support branch from 101e17e to 245e44c Compare July 8, 2024 08:21
@astrophysik astrophysik requested review from apolyakov and removed request for a team July 10, 2024 09:55
runtime-light/coroutine/fork-scheduler.h Outdated Show resolved Hide resolved
runtime-light/coroutine/runtime-future.h Outdated Show resolved Hide resolved
runtime-light/coroutine/runtime-future.h Outdated Show resolved Hide resolved
Copy link
Contributor

@apolyakov apolyakov left a comment

Choose a reason for hiding this comment

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

LGTM!

timer_d = arg.timeout_d;
} else if constexpr (std::is_same_v<T, stream_future>) {
timer_d = arg.timeout_d;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's also add static_assert(false, "non-exhaustive visitor!"); here

};

template<typename T>
using internal_optional_type = typename InternalOptionalType<T>::type;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's name it internal_optional_type_t in a manner that standard lib names such traits

@astrophysik astrophysik force-pushed the vsadokhov/fork-light-support branch 2 times, most recently from 0708bb6 to 2071f5e Compare July 17, 2024 10:04

static KphpForkContext &get();

KphpForkContext(memory_resource::unsynchronized_pool_resource &memory_pool)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
KphpForkContext(memory_resource::unsynchronized_pool_resource &memory_pool)
explicit KphpForkContext(memory_resource::unsynchronized_pool_resource &memory_pool)

Comment on lines +159 to +160
running_forks[fork_id] = light_fork(std::move(task));
running_forks[fork_id].resume(fork_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we'll get rid of double lookup?

template<typename T>
using deque = memory_resource::stl::deque<T, memory_resource::unsynchronized_pool_resource>;

fork_scheduler(memory_resource::unsynchronized_pool_resource &memory_pool)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fork_scheduler(memory_resource::unsynchronized_pool_resource &memory_pool)
explicit fork_scheduler(memory_resource::unsynchronized_pool_resource &memory_pool)

and check out other similar places pls

if (func->has_global_vars_inside) {
W << PhpMutableGlobalsAssignCurrent() << NL;
}
W << "vk::final_action action([]{KphpForkContext::get().scheduler.mark_current_fork_as_ready();});" << NL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's recheck that it's working in case when current function is called from other fork. It's a bit strange that we change codegen of function we want to fork. It should be redundant when we use C++-20 coroutines.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to determine that fork is ready outside of its logic, i.e. somewhere in scheduler

Comment on lines +1468 to +1490
if (func->kphp_tracing) {
TracingAutogen::codegen_runtime_func_guard_declaration(W, func);
TracingAutogen::codegen_runtime_func_guard_start(W, func);
}
compile_tracing_profiler(func, W);

for (auto var : func->local_var_ids) {
if (var->type() != VarData::var_local_inplace_t && !var->is_foreach_reference) {
W << VarDeclaration(var);
}
}

if (func->has_variadic_param) {
auto params = func->get_params();
kphp_assert(!params.empty());
auto variadic_arg = std::prev(params.end());
auto name_of_variadic_param = VarName(variadic_arg->as<op_func_param>()->var()->var_id);
W << "if (!" << name_of_variadic_param << ".is_vector())" << BEGIN;
W << "php_warning(\"pass associative array(" << name_of_variadic_param << ") to variadic function: " << FunctionName(func) << "\");" << NL;
W << name_of_variadic_param << " = f$array_values(" << name_of_variadic_param << ");" << NL;
W << END << NL;
}
W << AsSeq{func_root->cmd()} << END << NL;
Copy link
Contributor

Choose a reason for hiding this comment

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

Also let's recheck that it's actually needed in our case

// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

Copy link
Contributor

Choose a reason for hiding this comment

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

let's add midding includes like coroutine, type_traits etc.

Comment on lines +67 to +77
unordered_set<int64_t> ready_forks;
/* map from runtime_future {stream_future, fork_future} to waiting fork*/
unordered_map<runtime_future, int64_t> future_to_blocked_fork;
/* reverse of future_to_blocked_fork */
unordered_map<int64_t, runtime_future> fork_to_awaited_future;
/* timeout timer_d to waiting fork */
unordered_map<uint64_t, int64_t> timer_to_blocked_fork;
/* set of forks that wait to accept incoming stream */
unordered_set<int64_t> wait_incoming_query_forks;

deque<int64_t> forks_ready_to_resume;
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest to add detailed comment about typical fork lifecycle and its transitions between these hash tables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
k2 k2 related runtime Feature related to runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants