12
12
13
13
namespace NActors {
14
14
15
- class TSharedExecutorPool : public ISharedExecutorPool {
16
- public:
17
- TSharedExecutorPool (const TSharedExecutorPoolConfig &config, i16 poolCount, std::vector<i16> poolsWithThreads);
18
-
19
- // IThreadPool
20
- void Prepare (TActorSystem* actorSystem, NSchedulerQueue::TReader** scheduleReaders, ui32* scheduleSz) override ;
21
- void Start () override ;
22
- void PrepareStop () override ;
23
- void Shutdown () override ;
24
- bool Cleanup () override ;
25
-
26
- TSharedExecutorThreadCtx *GetSharedThread (i16 poolId) override ;
27
- void GetSharedStats (i16 pool, std::vector<TExecutorThreadStats>& statsCopy) override ;
28
- void GetSharedStatsForHarmonizer (i16 pool, std::vector<TExecutorThreadStats>& statsCopy) override ;
29
- TCpuConsumption GetThreadCpuConsumption (i16 poolId, i16 threadIdx) override ;
30
- std::vector<TCpuConsumption> GetThreadsCpuConsumption (i16 poolId) override ;
31
-
32
- i16 ReturnOwnHalfThread (i16 pool) override ;
33
- i16 ReturnBorrowedHalfThread (i16 pool) override ;
34
- void GiveHalfThread (i16 from, i16 to) override ;
35
-
36
- i16 GetSharedThreadCount () const override ;
37
-
38
- TSharedPoolState GetState () const override ;
39
-
40
- void Init (const std::vector<IExecutorPool*>& pools, bool withThreads) override ;
41
-
42
- private:
43
- TSharedPoolState State;
44
-
45
- std::vector<IExecutorPool*> Pools;
46
-
47
- i16 PoolCount;
48
- i16 SharedThreadCount;
49
- std::unique_ptr<TSharedExecutorThreadCtx[]> Threads;
50
-
51
- std::unique_ptr<NSchedulerQueue::TReader[]> ScheduleReaders;
52
- std::unique_ptr<NSchedulerQueue::TWriter[]> ScheduleWriters;
53
-
54
- TDuration TimePerMailbox;
55
- ui32 EventsPerMailbox;
56
- ui64 SoftProcessingDurationTs;
57
- }; // class TSharedExecutorPool
58
-
59
15
TSharedExecutorPool::TSharedExecutorPool (const TSharedExecutorPoolConfig &config, i16 poolCount, std::vector<i16> poolsWithThreads)
60
16
: State(poolCount, poolsWithThreads.size())
61
17
, Pools(poolCount)
@@ -73,47 +29,40 @@ TSharedExecutorPool::TSharedExecutorPool(const TSharedExecutorPoolConfig &config
73
29
}
74
30
}
75
31
76
- void TSharedExecutorPool::Init (const std::vector<IExecutorPool*>& pools, bool withThreads) {
77
- std::vector<IExecutorPool*> poolByThread (SharedThreadCount);
78
- for (IExecutorPool* pool : pools) {
79
- Pools[pool->PoolId ] = pool;
80
- i16 threadIdx = State.ThreadByPool [pool->PoolId ];
81
- if (threadIdx >= 0 ) {
82
- poolByThread[threadIdx] = pool;
32
+ void TSharedExecutorPool::Prepare (TActorSystem* actorSystem, NSchedulerQueue::TReader** scheduleReaders, ui32* scheduleSz) {
33
+ // ActorSystem = actorSystem;
34
+
35
+ ScheduleReaders.reset (new NSchedulerQueue::TReader[SharedThreadCount]);
36
+ ScheduleWriters.reset (new NSchedulerQueue::TWriter[SharedThreadCount]);
37
+
38
+ std::vector<IExecutorPool*> poolsBasic = actorSystem->GetBasicExecutorPools ();
39
+ std::vector<IExecutorPool*> poolByThread (SharedThreadCount);
40
+ for (IExecutorPool* pool : poolsBasic) {
41
+ Pools[pool->PoolId ] = dynamic_cast <TBasicExecutorPool*>(pool);
42
+ i16 threadIdx = State.ThreadByPool [pool->PoolId ];
43
+ if (threadIdx >= 0 ) {
44
+ poolByThread[threadIdx] = pool;
45
+ }
83
46
}
84
- }
85
47
86
- for (i16 i = 0 ; i != SharedThreadCount; ++i) {
87
- // !TODO
88
- Threads[i].ExecutorPools [0 ].store (dynamic_cast <TBasicExecutorPool*>(poolByThread[i]), std::memory_order_release);
89
- if (withThreads) {
48
+ for (i16 i = 0 ; i != SharedThreadCount; ++i) {
49
+ // !TODO
50
+ Threads[i].ExecutorPools [0 ].store (dynamic_cast <TBasicExecutorPool*>(poolByThread[i]), std::memory_order_release);
90
51
Threads[i].Thread .reset (
91
52
new TSharedExecutorThread (
92
53
-1 ,
93
- nullptr ,
94
- &Threads[i],
95
- PoolCount,
96
- " SharedThread" ,
97
- SoftProcessingDurationTs,
98
- TimePerMailbox,
54
+ actorSystem ,
55
+ &Threads[i],
56
+ PoolCount,
57
+ " SharedThread" ,
58
+ SoftProcessingDurationTs,
59
+ TimePerMailbox,
99
60
EventsPerMailbox));
61
+ ScheduleWriters[i].Init (ScheduleReaders[i]);
100
62
}
101
- }
102
- }
103
-
104
- void TSharedExecutorPool::Prepare (TActorSystem* actorSystem, NSchedulerQueue::TReader** scheduleReaders, ui32* scheduleSz) {
105
- ScheduleReaders.reset (new NSchedulerQueue::TReader[SharedThreadCount]);
106
- ScheduleWriters.reset (new NSchedulerQueue::TWriter[SharedThreadCount]);
107
-
108
- std::vector<IExecutorPool*> poolsBasic = actorSystem->GetBasicExecutorPools ();
109
- Init (poolsBasic, true );
110
63
111
- for (i16 i = 0 ; i != SharedThreadCount; ++i) {
112
- ScheduleWriters[i].Init (ScheduleReaders[i]);
113
- }
114
-
115
- *scheduleReaders = ScheduleReaders.get ();
116
- *scheduleSz = SharedThreadCount;
64
+ *scheduleReaders = ScheduleReaders.get ();
65
+ *scheduleSz = SharedThreadCount;
117
66
}
118
67
119
68
void TSharedExecutorPool::Start () {
@@ -150,27 +99,24 @@ TSharedExecutorThreadCtx* TSharedExecutorPool::GetSharedThread(i16 pool) {
150
99
return &Threads[threadIdx];
151
100
}
152
101
153
- i16 TSharedExecutorPool::ReturnOwnHalfThread (i16 pool) {
102
+ void TSharedExecutorPool::ReturnOwnHalfThread (i16 pool) {
154
103
i16 threadIdx = State.ThreadByPool [pool];
155
- IExecutorPool * borrowingPool = Threads[threadIdx].ExecutorPools [1 ].exchange (nullptr , std::memory_order_acq_rel);
104
+ TBasicExecutorPool * borrowingPool = Threads[threadIdx].ExecutorPools [1 ].exchange (nullptr , std::memory_order_acq_rel);
156
105
Y_ABORT_UNLESS (borrowingPool);
157
- i16 borrowedPool = State.PoolByBorrowedThread [threadIdx];
158
- State.BorrowedThreadByPool [borrowedPool] = -1 ;
106
+ State.BorrowedThreadByPool [State.PoolByBorrowedThread [threadIdx]] = -1 ;
159
107
State.PoolByBorrowedThread [threadIdx] = -1 ;
160
108
// TODO(kruall): Check on race
161
109
borrowingPool->ReleaseSharedThread ();
162
- return borrowedPool;
163
110
}
164
111
165
- i16 TSharedExecutorPool::ReturnBorrowedHalfThread (i16 pool) {
112
+ void TSharedExecutorPool::ReturnBorrowedHalfThread (i16 pool) {
166
113
i16 threadIdx = State.BorrowedThreadByPool [pool];
167
- IExecutorPool * borrowingPool = Threads[threadIdx].ExecutorPools [1 ].exchange (nullptr , std::memory_order_acq_rel);
114
+ TBasicExecutorPool * borrowingPool = Threads[threadIdx].ExecutorPools [1 ].exchange (nullptr , std::memory_order_acq_rel);
168
115
Y_ABORT_UNLESS (borrowingPool);
169
116
State.BorrowedThreadByPool [State.PoolByBorrowedThread [threadIdx]] = -1 ;
170
117
State.PoolByBorrowedThread [threadIdx] = -1 ;
171
118
// TODO(kruall): Check on race
172
119
borrowingPool->ReleaseSharedThread ();
173
- return State.PoolByThread [threadIdx];
174
120
}
175
121
176
122
void TSharedExecutorPool::GiveHalfThread (i16 from, i16 to) {
@@ -181,14 +127,14 @@ void TSharedExecutorPool::GiveHalfThread(i16 from, i16 to) {
181
127
if (borrowedThreadIdx != -1 ) {
182
128
i16 originalPool = State.PoolByThread [borrowedThreadIdx];
183
129
if (originalPool == to) {
184
- ReturnOwnHalfThread (to);
130
+ return ReturnOwnHalfThread (to);
185
131
} else {
186
132
ReturnOwnHalfThread (originalPool);
187
133
}
188
134
from = originalPool;
189
135
}
190
136
i16 threadIdx = State.ThreadByPool [from];
191
- IExecutorPool * borrowingPool = Pools[to];
137
+ TBasicExecutorPool * borrowingPool = Pools[to];
192
138
Threads[threadIdx].ExecutorPools [1 ].store (borrowingPool, std::memory_order_release);
193
139
State.BorrowedThreadByPool [to] = threadIdx;
194
140
State.PoolByBorrowedThread [threadIdx] = to;
@@ -197,16 +143,16 @@ void TSharedExecutorPool::GiveHalfThread(i16 from, i16 to) {
197
143
}
198
144
199
145
void TSharedExecutorPool::GetSharedStats (i16 poolId, std::vector<TExecutorThreadStats>& statsCopy) {
200
- statsCopy.resize (SharedThreadCount);
146
+ statsCopy.resize (SharedThreadCount + 1 );
201
147
for (i16 i = 0 ; i < SharedThreadCount; ++i) {
202
- Threads[i].Thread ->GetSharedStats (poolId, statsCopy[i]);
148
+ Threads[i].Thread ->GetSharedStats (poolId, statsCopy[i + 1 ]);
203
149
}
204
150
}
205
151
206
152
void TSharedExecutorPool::GetSharedStatsForHarmonizer (i16 poolId, std::vector<TExecutorThreadStats>& statsCopy) {
207
- statsCopy.resize (SharedThreadCount);
153
+ statsCopy.resize (SharedThreadCount + 1 );
208
154
for (i16 i = 0 ; i < SharedThreadCount; ++i) {
209
- Threads[i].Thread ->GetSharedStatsForHarmonizer (poolId, statsCopy[i]);
155
+ Threads[i].Thread ->GetSharedStatsForHarmonizer (poolId, statsCopy[i + 1 ]);
210
156
}
211
157
}
212
158
@@ -235,34 +181,4 @@ TSharedPoolState TSharedExecutorPool::GetState() const {
235
181
return State;
236
182
}
237
183
238
- ISharedExecutorPool *CreateSharedExecutorPool (const TSharedExecutorPoolConfig &config, i16 poolCount, std::vector<i16> poolsWithThreads) {
239
- return new TSharedExecutorPool (config, poolCount, poolsWithThreads);
240
- }
241
-
242
- TString TSharedPoolState::ToString () const {
243
- TStringBuilder builder;
244
- builder << ' {' ;
245
- builder << " ThreadByPool: [" ;
246
- for (ui32 i = 0 ; i < ThreadByPool.size (); ++i) {
247
- builder << ThreadByPool[i] << (i == ThreadByPool.size () - 1 ? " " : " , " );
248
- }
249
- builder << " ], " ;
250
- builder << " PoolByThread: [" ;
251
- for (ui32 i = 0 ; i < PoolByThread.size (); ++i) {
252
- builder << PoolByThread[i] << (i == PoolByThread.size () - 1 ? " " : " , " );
253
- }
254
- builder << " ], " ;
255
- builder << " BorrowedThreadByPool: [" ;
256
- for (ui32 i = 0 ; i < BorrowedThreadByPool.size (); ++i) {
257
- builder << BorrowedThreadByPool[i] << (i == BorrowedThreadByPool.size () - 1 ? " " : " , " );
258
- }
259
- builder << " ], " ;
260
- builder << " PoolByBorrowedThread: [" ;
261
- for (ui32 i = 0 ; i < PoolByBorrowedThread.size (); ++i) {
262
- builder << PoolByBorrowedThread[i] << (i == PoolByBorrowedThread.size () - 1 ? " " : " , " );
263
- }
264
- builder << ' ]' ;
265
- return builder << ' }' ;
266
- }
267
-
268
184
}
0 commit comments