blob: 6d42895bffc4feec0835a382bd94a97825a50700 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "sequencedtaskexecutor.h"
#include "adaptive_sequenced_executor.h"
#include "singleexecutor.h"
#include <vespa/vespalib/util/blockingthreadstackexecutor.h>
namespace vespalib {
namespace {
constexpr uint32_t stackSize = 128 * 1024;
}
std::unique_ptr<ISequencedTaskExecutor>
SequencedTaskExecutor::create(uint32_t threads, uint32_t taskLimit, OptimizeFor optimize)
{
if (optimize == OptimizeFor::ADAPTIVE) {
return std::make_unique<AdaptiveSequencedExecutor>(threads, threads, 0, taskLimit);
} else {
auto executors = std::make_unique<std::vector<std::unique_ptr<SyncableThreadExecutor>>>();
executors->reserve(threads);
for (uint32_t id = 0; id < threads; ++id) {
if (optimize == OptimizeFor::THROUGHPUT) {
executors->push_back(std::make_unique<SingleExecutor>(taskLimit));
} else {
executors->push_back(std::make_unique<BlockingThreadStackExecutor>(1, stackSize, taskLimit));
}
}
return std::unique_ptr<ISequencedTaskExecutor>(new SequencedTaskExecutor(std::move(executors)));
}
}
SequencedTaskExecutor::~SequencedTaskExecutor()
{
sync();
}
SequencedTaskExecutor::SequencedTaskExecutor(std::unique_ptr<std::vector<std::unique_ptr<vespalib::SyncableThreadExecutor>>> executors)
: ISequencedTaskExecutor(executors->size()),
_executors(std::move(executors))
{
}
void
SequencedTaskExecutor::setTaskLimit(uint32_t taskLimit)
{
for (const auto &executor : *_executors) {
executor->setTaskLimit(taskLimit);
}
}
void
SequencedTaskExecutor::executeTask(ExecutorId id, vespalib::Executor::Task::UP task)
{
assert(id.getId() < _executors->size());
auto rejectedTask = (*_executors)[id.getId()]->execute(std::move(task));
assert(!rejectedTask);
}
void
SequencedTaskExecutor::sync()
{
for (auto &executor : *_executors) {
SingleExecutor * single = dynamic_cast<vespalib::SingleExecutor *>(executor.get());
if (single) {
//Enforce parallel wakeup of napping executors.
single->startSync();
}
}
for (auto &executor : *_executors) {
executor->sync();
}
}
SequencedTaskExecutor::Stats
SequencedTaskExecutor::getStats()
{
Stats accumulatedStats;
for (auto &executor :* _executors) {
accumulatedStats += executor->getStats();
}
return accumulatedStats;
}
} // namespace search
|