|
14 | 14 | #include "trpc/runtime/fiber_runtime.h" |
15 | 15 |
|
16 | 16 | #include "trpc/common/config/trpc_config.h" |
| 17 | +#include "trpc/runtime/runtime_state.h" |
17 | 18 | #include "trpc/runtime/threadmodel/fiber/fiber_thread_model.h" |
18 | 19 | #include "trpc/runtime/threadmodel/thread_model_manager.h" |
19 | 20 | #include "trpc/util/likely.h" |
|
23 | 24 |
|
24 | 25 | namespace trpc::fiber { |
25 | 26 |
|
| 27 | +// fiber_threadmodel state manager |
| 28 | +RuntimeState fiber_runtime_state{RuntimeState::kUnknown}; |
| 29 | + |
26 | 30 | // fiber_threadmodel has not owned threadmodel's ownership, it managered by ThreadModelManager |
27 | 31 | static FiberThreadModel* fiber_threadmodel = nullptr; |
28 | 32 |
|
@@ -75,56 +79,67 @@ std::ptrdiff_t NearestSchedulingGroupIndex() { |
75 | 79 | } // namespace detail |
76 | 80 |
|
77 | 81 | void StartRuntime() { |
78 | | - trpc::fiber::FiberThreadModel::Options options; |
79 | | - options.group_id = ThreadModelManager::GetInstance()->GenGroupId(); |
80 | | - options.group_name = ""; |
81 | | - options.scheduling_name = "v1"; |
82 | | - |
83 | | - const GlobalConfig& global_config = TrpcConfig::GetInstance()->GetGlobalConfig(); |
84 | | - |
85 | | - // only support one fiber threadmodel instance |
86 | | - if (global_config.threadmodel_config.fiber_model.size() == 1) { |
87 | | - const auto& conf = global_config.threadmodel_config.fiber_model[0]; |
88 | | - if (!conf.fiber_scheduling_name.empty()) { |
89 | | - options.scheduling_name = conf.fiber_scheduling_name; |
90 | | - } |
91 | | - options.group_name = conf.instance_name; |
92 | | - options.concurrency_hint = conf.concurrency_hint; |
93 | | - options.scheduling_group_size = conf.scheduling_group_size; |
94 | | - options.numa_aware = conf.numa_aware; |
95 | | - |
96 | | - if (!conf.fiber_worker_accessible_cpus.empty() && |
97 | | - ParseBindCoreConfig(conf.fiber_worker_accessible_cpus, options.worker_accessible_cpus)) { |
98 | | - // Only when configuring the core binding related settings, strict core binding will take effect. |
99 | | - options.worker_disallow_cpu_migration = conf.fiber_worker_disallow_cpu_migration; |
| 82 | + // It can be restarted if it has not been started before or has already been destroyed |
| 83 | + if (fiber_runtime_state == RuntimeState::kUnknown || fiber_runtime_state == RuntimeState::kDestroyed) { |
| 84 | + trpc::fiber::FiberThreadModel::Options options; |
| 85 | + options.group_id = ThreadModelManager::GetInstance()->GenGroupId(); |
| 86 | + options.group_name = ""; |
| 87 | + options.scheduling_name = "v1"; |
| 88 | + |
| 89 | + const GlobalConfig& global_config = TrpcConfig::GetInstance()->GetGlobalConfig(); |
| 90 | + |
| 91 | + // only support one fiber threadmodel instance |
| 92 | + if (global_config.threadmodel_config.fiber_model.size() == 1) { |
| 93 | + const auto& conf = global_config.threadmodel_config.fiber_model[0]; |
| 94 | + if (!conf.fiber_scheduling_name.empty()) { |
| 95 | + options.scheduling_name = conf.fiber_scheduling_name; |
| 96 | + } |
| 97 | + options.group_name = conf.instance_name; |
| 98 | + options.concurrency_hint = conf.concurrency_hint; |
| 99 | + options.scheduling_group_size = conf.scheduling_group_size; |
| 100 | + options.numa_aware = conf.numa_aware; |
| 101 | + |
| 102 | + if (!conf.fiber_worker_accessible_cpus.empty() && |
| 103 | + ParseBindCoreConfig(conf.fiber_worker_accessible_cpus, options.worker_accessible_cpus)) { |
| 104 | + // Only when configuring the core binding related settings, strict core binding will take effect. |
| 105 | + options.worker_disallow_cpu_migration = conf.fiber_worker_disallow_cpu_migration; |
| 106 | + } |
| 107 | + options.work_stealing_ratio = conf.work_stealing_ratio; |
| 108 | + options.cross_numa_work_stealing_ratio = conf.cross_numa_work_stealing_ratio; |
| 109 | + options.run_queue_size = conf.fiber_run_queue_size; |
| 110 | + options.stack_size = conf.fiber_stack_size; |
| 111 | + options.pool_num_by_mmap = conf.fiber_pool_num_by_mmap; |
| 112 | + options.stack_enable_guard_page = conf.fiber_stack_enable_guard_page; |
| 113 | + options.disable_process_name = global_config.thread_disable_process_name; |
| 114 | + options.enable_gdb_debug = conf.enable_gdb_debug; |
| 115 | + } else { |
| 116 | + options.group_name = "fiber_instance"; |
100 | 117 | } |
101 | | - options.work_stealing_ratio = conf.work_stealing_ratio; |
102 | | - options.cross_numa_work_stealing_ratio = conf.cross_numa_work_stealing_ratio; |
103 | | - options.run_queue_size = conf.fiber_run_queue_size; |
104 | | - options.stack_size = conf.fiber_stack_size; |
105 | | - options.pool_num_by_mmap = conf.fiber_pool_num_by_mmap; |
106 | | - options.stack_enable_guard_page = conf.fiber_stack_enable_guard_page; |
107 | | - options.disable_process_name = global_config.thread_disable_process_name; |
108 | | - options.enable_gdb_debug = conf.enable_gdb_debug; |
109 | | - } else { |
110 | | - options.group_name = "fiber_instance"; |
111 | | - } |
112 | 118 |
|
113 | | - std::shared_ptr<ThreadModel> fiber_model = std::make_shared<FiberThreadModel>(std::move(options)); |
| 119 | + std::shared_ptr<ThreadModel> fiber_model = std::make_shared<FiberThreadModel>(std::move(options)); |
| 120 | + |
| 121 | + fiber_threadmodel = static_cast<FiberThreadModel*>(fiber_model.get()); |
114 | 122 |
|
115 | | - fiber_threadmodel = static_cast<FiberThreadModel*>(fiber_model.get()); |
| 123 | + TRPC_ASSERT(ThreadModelManager::GetInstance()->Register(fiber_model) == true); |
116 | 124 |
|
117 | | - TRPC_ASSERT(ThreadModelManager::GetInstance()->Register(fiber_model) == true); |
| 125 | + fiber_threadmodel->Start(); |
118 | 126 |
|
119 | | - fiber_threadmodel->Start(); |
| 127 | + fiber_runtime_state = RuntimeState::kStarted; |
| 128 | + } |
120 | 129 | } |
121 | 130 |
|
122 | 131 | void TerminateRuntime() { |
| 132 | + if (fiber_runtime_state != RuntimeState::kStarted) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
123 | 136 | fiber_threadmodel->Terminate(); |
124 | 137 |
|
125 | 138 | ThreadModelManager::GetInstance()->Del(fiber_threadmodel->GroupName()); |
126 | 139 |
|
127 | 140 | fiber_threadmodel = nullptr; |
| 141 | + |
| 142 | + fiber_runtime_state = RuntimeState::kDestroyed; |
128 | 143 | } |
129 | 144 |
|
130 | 145 | ThreadModel* GetFiberThreadModel() { return fiber_threadmodel; } |
|
0 commit comments