1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #include <memory>
18 #include <string>
19
20 #include "absl/strings/str_split.h"
21 #include "absl/strings/string_view.h"
22 #include "src/core/config/config_vars.h"
23 #include "src/core/lib/event_engine/forkable.h"
24 #include "src/core/lib/event_engine/posix_engine/ev_epoll1_linux.h"
25 #include "src/core/lib/event_engine/posix_engine/ev_poll_posix.h"
26 #include "src/core/lib/event_engine/posix_engine/event_poller.h"
27 #include "src/core/lib/iomgr/port.h"
28 #include "src/core/util/no_destruct.h"
29
30 namespace grpc_event_engine {
31 namespace experimental {
32
33 #ifdef GRPC_POSIX_SOCKET_TCP
34 namespace {
35 // TODO(yijiem): this object is thread-unsafe, if we are creating pollers in
36 // multiple threads (e.g. multiple event engines) or if we are creating pollers
37 // while we are forking then we will run into issues.
38 grpc_core::NoDestruct<ObjectGroupForkHandler> g_poller_fork_manager;
39
40 class PollerForkCallbackMethods {
41 public:
Prefork()42 static void Prefork() { g_poller_fork_manager->Prefork(); }
PostforkParent()43 static void PostforkParent() { g_poller_fork_manager->PostforkParent(); }
PostforkChild()44 static void PostforkChild() { g_poller_fork_manager->PostforkChild(); }
45 };
46
PollStrategyMatches(absl::string_view strategy,absl::string_view want)47 bool PollStrategyMatches(absl::string_view strategy, absl::string_view want) {
48 return strategy == "all" || strategy == want;
49 }
50 } // namespace
51
MakeDefaultPoller(Scheduler * scheduler)52 std::shared_ptr<PosixEventPoller> MakeDefaultPoller(Scheduler* scheduler) {
53 std::shared_ptr<PosixEventPoller> poller;
54 auto strings =
55 absl::StrSplit(grpc_core::ConfigVars::Get().PollStrategy(), ',');
56 for (auto it = strings.begin(); it != strings.end() && poller == nullptr;
57 it++) {
58 if (PollStrategyMatches(*it, "epoll1")) {
59 poller = MakeEpoll1Poller(scheduler);
60 }
61 if (poller == nullptr && PollStrategyMatches(*it, "poll")) {
62 // If epoll1 fails and if poll strategy matches "poll", use Poll poller
63 poller = MakePollPoller(scheduler, /*use_phony_poll=*/false);
64 } else if (poller == nullptr && PollStrategyMatches(*it, "none")) {
65 // If epoll1 fails and if poll strategy matches "none", use phony poll
66 // poller.
67 poller = MakePollPoller(scheduler, /*use_phony_poll=*/true);
68 }
69 }
70 g_poller_fork_manager->RegisterForkable(
71 poller, PollerForkCallbackMethods::Prefork,
72 PollerForkCallbackMethods::PostforkParent,
73 PollerForkCallbackMethods::PostforkChild);
74 return poller;
75 }
76
77 #else // GRPC_POSIX_SOCKET_TCP
78
79 std::shared_ptr<PosixEventPoller> MakeDefaultPoller(Scheduler* /*scheduler*/) {
80 return nullptr;
81 }
82
83 #endif // GRPC_POSIX_SOCKET_TCP
84
85 } // namespace experimental
86 } // namespace grpc_event_engine
87