|
28 | 28 |
|
29 | 29 | #include <folly/executors/CPUThreadPoolExecutor.h> |
30 | 30 | #include <folly/executors/IOThreadPoolExecutor.h> |
| 31 | +#include <folly/executors/thread_factory/PriorityThreadFactory.h> |
| 32 | + |
| 33 | +/** |
| 34 | + * Thread factory for CPU pool threads. |
| 35 | + * - Gives each thread name based on the given prefix. |
| 36 | + * - Sets each thread to the given priority. |
| 37 | + */ |
| 38 | +class CBPriorityThreadFactory : public folly::ThreadFactory { |
| 39 | +public: |
| 40 | + CBPriorityThreadFactory(std::string prefix, int priority) |
| 41 | + : priorityThreadFactory( |
| 42 | + std::make_shared<folly::NamedThreadFactory>(prefix), |
| 43 | + priority) { |
| 44 | + } |
| 45 | + |
| 46 | + std::thread newThread(folly::Func&& func) override { |
| 47 | + return priorityThreadFactory.newThread(std::move(func)); |
| 48 | + } |
| 49 | + |
| 50 | + folly::PriorityThreadFactory priorityThreadFactory; |
| 51 | +}; |
31 | 52 |
|
32 | 53 | /** |
33 | 54 | * Proxy object recorded for each registered (scheduled) Task. Inherits from |
@@ -433,19 +454,37 @@ FollyExecutorPool::FollyExecutorPool(size_t maxThreads, |
433 | 454 | maxWriters(calcNumWriters(maxWriters_)), |
434 | 455 | maxAuxIO(calcNumAuxIO(maxAuxIO_)), |
435 | 456 | maxNonIO(calcNumNonIO(maxNonIO_)) { |
| 457 | + /* |
| 458 | + * Define a function to create thread factory with a given prefix, |
| 459 | + * and priority where supported. |
| 460 | + * |
| 461 | + * Only setting priority for Linux at present: |
| 462 | + * - On Windows folly's getpriority() compatibility function changes the |
| 463 | + * priority of the entire process. |
| 464 | + * - On macOS setpriority(PRIO_PROCESS) affects the entire process (unlike |
| 465 | + * Linux where it's only the current thread), hence calling |
| 466 | + * setpriority() would be pointless. |
| 467 | + */ |
| 468 | + auto makeThreadFactory = [](std::string prefix, task_type_t taskType) { |
| 469 | +#if defined(__linux__) |
| 470 | + return std::make_shared<CBPriorityThreadFactory>( |
| 471 | + prefix, ExecutorPool::getThreadPriority(taskType)); |
| 472 | +#else |
| 473 | + return std::make_shared<folly::NamedThreadFactory>(prefix); |
| 474 | +#endif |
| 475 | + }; |
| 476 | + |
436 | 477 | futurePool = std::make_unique<folly::IOThreadPoolExecutor>( |
437 | 478 | 1, std::make_shared<folly::NamedThreadFactory>("SchedulerPool")); |
438 | 479 |
|
439 | 480 | readerPool = std::make_unique<folly::CPUThreadPoolExecutor>( |
440 | | - maxReaders, |
441 | | - std::make_shared<folly::NamedThreadFactory>("ReaderPool")), |
| 481 | + maxReaders, makeThreadFactory("ReaderPool", READER_TASK_IDX)); |
442 | 482 | writerPool = std::make_unique<folly::CPUThreadPoolExecutor>( |
443 | | - maxWriters, |
444 | | - std::make_shared<folly::NamedThreadFactory>("WriterPool")); |
| 483 | + maxWriters, makeThreadFactory("WriterPool", WRITER_TASK_IDX)); |
445 | 484 | auxPool = std::make_unique<folly::CPUThreadPoolExecutor>( |
446 | | - maxAuxIO, std::make_shared<folly::NamedThreadFactory>("AuxIoPool")); |
| 485 | + maxAuxIO, makeThreadFactory("AuxIoPool", AUXIO_TASK_IDX)); |
447 | 486 | nonIoPool = std::make_unique<folly::CPUThreadPoolExecutor>( |
448 | | - maxNonIO, std::make_shared<folly::NamedThreadFactory>("NonIoPool")); |
| 487 | + maxNonIO, makeThreadFactory("NonIoPool", NONIO_TASK_IDX)); |
449 | 488 | } |
450 | 489 |
|
451 | 490 | FollyExecutorPool::~FollyExecutorPool() { |
|
0 commit comments