| 211 | {} |
| 212 | |
| 213 | int TaskControl::init(int concurrency) { |
| 214 | if (_concurrency != 0) { |
| 215 | LOG(ERROR) << "Already initialized"; |
| 216 | return -1; |
| 217 | } |
| 218 | if (concurrency <= 0) { |
| 219 | LOG(ERROR) << "Invalid concurrency=" << concurrency; |
| 220 | return -1; |
| 221 | } |
| 222 | _concurrency = concurrency; |
| 223 | |
| 224 | if (!FLAGS_cpu_set.empty()) { |
| 225 | if (parse_cpuset(FLAGS_cpu_set, _cpus) == -1) { |
| 226 | LOG(ERROR) << "invalid cpuset=" << FLAGS_cpu_set; |
| 227 | return -1; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // task group group by tags |
| 232 | for (int i = 0; i < FLAGS_task_group_ntags; ++i) { |
| 233 | _tagged_ngroup[i].store(0, std::memory_order_relaxed); |
| 234 | auto tag_str = std::to_string(i); |
| 235 | _tagged_nworkers.push_back(new bvar::Adder<int64_t>("bthread_worker_count", tag_str)); |
| 236 | _tagged_cumulated_worker_time.push_back(new bvar::PassiveStatus<double>( |
| 237 | get_cumulated_worker_time_from_this_with_tag, new CumulatedWithTagArgs{this, i})); |
| 238 | _tagged_worker_usage_second.push_back(new bvar::PerSecond<bvar::PassiveStatus<double>>( |
| 239 | "bthread_worker_usage", tag_str, _tagged_cumulated_worker_time[i], 1)); |
| 240 | _tagged_nbthreads.push_back(new bvar::Adder<int64_t>("bthread_count", tag_str)); |
| 241 | if (_priority_queues[i].init(BTHREAD_MAX_CONCURRENCY) != 0) { |
| 242 | LOG(ERROR) << "Fail to init _priority_q"; |
| 243 | return -1; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Make sure TimerThread is ready. |
| 248 | if (get_or_create_global_timer_thread() == NULL) { |
| 249 | LOG(ERROR) << "Fail to get global_timer_thread"; |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | #ifdef BRPC_BTHREAD_TRACER |
| 254 | if (!_task_tracer.Init()) { |
| 255 | LOG(ERROR) << "Fail to init TaskTracer"; |
| 256 | return -1; |
| 257 | } |
| 258 | #endif // BRPC_BTHREAD_TRACER |
| 259 | |
| 260 | _workers.resize(_concurrency); |
| 261 | for (int i = 0; i < _concurrency; ++i) { |
| 262 | auto arg = new WorkerThreadArgs(this, i % FLAGS_task_group_ntags); |
| 263 | const int rc = pthread_create(&_workers[i], NULL, worker_thread, arg); |
| 264 | if (rc) { |
| 265 | delete arg; |
| 266 | PLOG(ERROR) << "Fail to create _workers[" << i << "]"; |
| 267 | return -1; |
| 268 | } |
| 269 | } |
| 270 | _worker_usage_second.expose("bthread_worker_usage"); |
no test coverage detected