Skip to content

Commit 12ef5e4

Browse files
committed
back to syncronized
1 parent 5771c46 commit 12ef5e4

File tree

2 files changed

+25
-53
lines changed

2 files changed

+25
-53
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/common/util/concurrent/ThreadPoolUtilizationBenchmark.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929

3030
@Threads(Threads.MAX)
3131
@Warmup(iterations = 3, time = 200, timeUnit = TimeUnit.MILLISECONDS)
32-
@Measurement(iterations = 5, time = 600, timeUnit = TimeUnit.MILLISECONDS)
32+
@Measurement(iterations = 1, time = 60, timeUnit = TimeUnit.SECONDS)
3333
@BenchmarkMode(Mode.SampleTime)
3434
@OutputTimeUnit(TimeUnit.MICROSECONDS)
3535
@State(Scope.Benchmark)
3636
@Fork(1)
3737
public class ThreadPoolUtilizationBenchmark {
3838

39-
@Param({ "1000" })
39+
@Param({ "10000" })
4040
private int callIntervalTicks;
4141

4242
/**
4343
* This makes very little difference, all the overhead is in the synchronization
4444
*/
45-
@Param({ "30000" })
45+
@Param({ "100" })
4646
private int utilizationIntervalMs;
4747
private TaskExecutionTimeTrackingEsThreadPoolExecutor.FramedTimeTracker timeTracker;
4848

@@ -61,18 +61,9 @@ public void baseline() {
6161

6262
@Group("StartAndEnd")
6363
@Benchmark
64-
public void startAndStopTasks(TaskState state) {
64+
public void startAndStopTasks() {
6565
timeTracker.startTask();
6666
Blackhole.consumeCPU(callIntervalTicks);
6767
timeTracker.endTask();
6868
}
69-
70-
@State(Scope.Thread)
71-
public static class TaskState {
72-
boolean running = false;
73-
74-
boolean shouldStart() {
75-
return (running = running == false);
76-
}
77-
}
7869
}

server/src/main/java/org/elasticsearch/common/util/concurrent/TaskExecutionTimeTrackingEsThreadPoolExecutor.java

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,12 @@ public boolean trackingMaxQueueLatency() {
260260
* Can be extended to remember multiple past frames.
261261
*/
262262
public static class FramedTimeTracker {
263-
private final long interval;
263+
final long interval;
264264
private final Supplier<Long> timeNow;
265-
private final ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock();
266-
private final AtomicLong ongoingTasks = new AtomicLong();
267-
private final AtomicLong currentFrame = new AtomicLong();
268-
private final AtomicLong currentTime = new AtomicLong();
269-
private final AtomicLong previousTime = new AtomicLong();
265+
private long ongoingTasks;
266+
private long currentFrame;
267+
private long currentTime;
268+
private long previousTime;
270269

271270
// for testing
272271
public FramedTimeTracker(long intervalNano, Supplier<Long> timeNow) {
@@ -298,62 +297,44 @@ public long interval() {
298297
*/
299298
private void updateFrame0(long nowTime) {
300299
var now = nowTime / interval;
301-
var current = currentFrame.get();
302-
if (current < now) {
303-
rwlock.readLock().unlock();
304-
rwlock.writeLock().lock();
305-
current = currentFrame.get(); // make sure it didnt change during lock acquisition
306-
if (current < now) {
307-
var tasks = ongoingTasks.get();
308-
if (current == now - 1) {
309-
previousTime.set(currentTime.get());
310-
} else {
311-
previousTime.set(tasks * interval);
312-
}
313-
currentTime.set(tasks * interval);
314-
currentFrame.set(now);
300+
if (currentFrame < now) {
301+
if (currentFrame == now - 1) {
302+
previousTime = currentTime; //
303+
} else {
304+
previousTime = ongoingTasks * interval;
315305
}
316-
rwlock.readLock().lock();
317-
rwlock.writeLock().unlock();
306+
currentTime = ongoingTasks * interval;
307+
currentFrame = now;
318308
}
319309
}
320310

321311
/**
322312
* Start tracking new task, assume that task runs indefinitely, or at least till end of frame.
323313
* If task finishes sooner than end of interval {@link FramedTimeTracker#endTask()} will deduct remaining time.
324314
*/
325-
public void startTask() {
326-
rwlock.readLock().lock();
315+
public synchronized void startTask() {
327316
var now = timeNow.get();
328317
updateFrame0(now);
329-
ongoingTasks.incrementAndGet();
330-
currentTime.addAndGet((now + 1) * interval - now);
331-
rwlock.readLock().unlock();
318+
currentTime += (currentFrame + 1) * interval - now;
319+
++ongoingTasks;
332320
}
333321

334322
/**
335323
* Stop task tracking. We already assumed that task runs till end of frame, here we deduct not used time.
336324
*/
337-
public void endTask() {
338-
rwlock.readLock().lock();
325+
public synchronized void endTask() {
339326
var now = timeNow.get();
340327
updateFrame0(now);
341-
ongoingTasks.decrementAndGet();
342-
currentTime.addAndGet(-((now + 1) * interval - now));
343-
rwlock.readLock().unlock();
328+
currentTime -= (currentFrame + 1) * interval - now;
329+
--ongoingTasks;
344330
}
345331

346332
/**
347333
* Returns previous frame total execution time.
348334
*/
349-
public long previousFrameTime() {
350-
try {
351-
rwlock.readLock().lock();
352-
updateFrame0(timeNow.get());
353-
return previousTime.get();
354-
} finally {
355-
rwlock.readLock().unlock();
356-
}
335+
public synchronized long previousFrameTime() {
336+
updateFrame0(timeNow.get());
337+
return previousTime;
357338
}
358339
}
359340
}

0 commit comments

Comments
 (0)