Skip to content

Commit 29cf37f

Browse files
committed
Make it obvious that it is "mono time" being used
1 parent 0e196fa commit 29cf37f

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

core/src/com/biglybt/core/peermanager/control/impl/PeerControlSchedulerPrioritised.java

+43-43
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@
3838
final List<instanceWrapper> pending_registrations = new ArrayList<>();
3939

4040
private volatile boolean registrations_changed;
41-
private volatile long latest_time;
41+
private volatile long latest_time_mono;
4242

4343
private final Object instance_lock = new Object();
4444

4545
private final SpeedTokenDispenserPrioritised tokenDispenser = new SpeedTokenDispenserPrioritised();
4646

47-
private long next_peer_count_time = SystemTime.getMonotonousTime();
48-
private long next_piece_count_time = SystemTime.getMonotonousTime();
47+
private long next_peer_count_time_mono = SystemTime.getMonotonousTime();
48+
private long next_piece_count_time_mono = SystemTime.getMonotonousTime();
4949

50-
private volatile long peer_count_active_time = 0;
51-
private volatile long piece_count_active_time = 0;
50+
private volatile long peer_count_active_time_mono = 0;
51+
private volatile long piece_count_active_time_mono = 0;
5252

5353
private volatile int[] last_peer_count = { 0, 0 };
5454
private volatile int[] last_piece_count = { 0, 0 };
@@ -57,54 +57,54 @@
5757
protected void
5858
schedule()
5959
{
60-
latest_time = SystemTime.getMonotonousTime();
60+
latest_time_mono = SystemTime.getMonotonousTime();
6161

6262
SystemTime.registerMonotonousConsumer(
6363
new SystemTime.TickConsumer()
6464
{
6565
@Override
6666
public void
67-
consume( long time )
67+
consume( long time_mono )
6868
{
6969
boolean count_peers = false;
7070
boolean count_pieces = false;
7171

7272
synchronized( PeerControlSchedulerPrioritised.this ){
7373

74-
if ( peer_count_active_time > 0 ){
74+
if ( peer_count_active_time_mono > 0 ){
7575

76-
if ( time >= next_peer_count_time ){
76+
if ( time_mono >= next_peer_count_time_mono ){
7777

78-
if ( time - peer_count_active_time > 15*1000 ){
78+
if ( time_mono - peer_count_active_time_mono > 15*1000 ){
7979

80-
peer_count_active_time = 0;
80+
peer_count_active_time_mono = 0;
8181

8282
}else{
8383
count_peers = true;
8484

85-
next_peer_count_time = time+900;
85+
next_peer_count_time_mono = time_mono+900;
8686
}
8787
}
8888
}
8989

90-
if ( piece_count_active_time > 0 ){
90+
if ( piece_count_active_time_mono > 0 ){
9191

92-
if ( time >= next_piece_count_time ){
92+
if ( time_mono >= next_piece_count_time_mono ){
9393

94-
if ( time - piece_count_active_time > 15*1000 ){
94+
if ( time_mono - piece_count_active_time_mono > 15*1000 ){
9595

96-
piece_count_active_time = 0;
96+
piece_count_active_time_mono = 0;
9797

9898
}else{
9999

100100
count_pieces = true;
101101

102-
next_piece_count_time = time+900;
102+
next_piece_count_time_mono = time_mono+900;
103103
}
104104
}
105105
}
106106

107-
latest_time = time;
107+
latest_time_mono = time_mono;
108108

109109
if ( instance_map.size() > 0 || pending_registrations.size() > 0 ){
110110

@@ -153,20 +153,20 @@
153153
});
154154

155155

156-
ArrayList instances = new ArrayList();
156+
ArrayList<instanceWrapper> instances = new ArrayList<>();
157157

158-
long latest_time_used = 0;
159-
int scheduledNext = 0;
160-
long currentScheduleStart = latest_time;
161-
long last_stats_time = latest_time;
158+
long latest_time_used_mono = 0;
159+
int scheduledNext = 0;
160+
long currentScheduleStartMono = latest_time_mono;
161+
long last_stats_time_mono = latest_time_mono;
162162

163163
while( true ){
164164

165165
if ( registrations_changed ){
166166
synchronized( instance_lock ){
167-
Iterator it = instances.iterator();
167+
Iterator<instanceWrapper> it = instances.iterator();
168168
while( it.hasNext()){
169-
if (((instanceWrapper)it.next()).isUnregistered()){
169+
if (it.next().isUnregistered()){
170170
it.remove();
171171
}
172172
}
@@ -185,18 +185,18 @@
185185
}
186186

187187
scheduledNext = 0;
188-
currentScheduleStart = latest_time;
188+
currentScheduleStartMono = latest_time_mono;
189189

190190
registrations_changed = false;
191191
}
192192
}
193193

194-
tokenDispenser.update(latest_time);
194+
tokenDispenser.update(latest_time_mono);
195195

196196
for (int i = scheduledNext; i < instances.size(); i++)
197197
{
198-
instanceWrapper inst = (instanceWrapper) instances.get(i);
199-
if (currentScheduleStart + inst.getScheduleOffset() > latest_time_used)
198+
instanceWrapper inst = instances.get(i);
199+
if (currentScheduleStartMono + inst.getScheduleOffset() > latest_time_used_mono)
200200
break; // too early for next task, continue waiting
201201
if (i == 0 || !useWeights)
202202
tokenDispenser.refill();
@@ -208,11 +208,11 @@
208208
{
209209
scheduledNext = 0;
210210
// try to run every task every SCHEDULE_PERIOD_MILLIS on average
211-
currentScheduleStart += SCHEDULE_PERIOD_MILLIS;
211+
currentScheduleStartMono += SCHEDULE_PERIOD_MILLIS;
212212
// if tasks hog too much time then delay to prevent massive
213213
// catch-up-hammering
214-
if (latest_time_used - currentScheduleStart > SCHEDULE_PERIOD_MAX_CATCHUP )
215-
currentScheduleStart = latest_time_used + SCHEDULE_PERIOD_MILLIS;
214+
if (latest_time_used_mono - currentScheduleStartMono > SCHEDULE_PERIOD_MAX_CATCHUP )
215+
currentScheduleStartMono = latest_time_used_mono + SCHEDULE_PERIOD_MILLIS;
216216
}
217217
}
218218

@@ -235,7 +235,7 @@
235235
}*/
236236

237237
synchronized( this ){
238-
if ( latest_time == latest_time_used ){
238+
if ( latest_time_mono == latest_time_used_mono ){
239239
wait_count++;
240240
try{
241241
long wait_start = SystemTime.getHighPrecisionCounter();
@@ -251,14 +251,14 @@
251251
Thread.yield();
252252
}
253253

254-
latest_time_used = latest_time;
254+
latest_time_used_mono = latest_time_mono;
255255
}
256256

257-
long stats_diff = latest_time_used - last_stats_time;
257+
long stats_diff = latest_time_used_mono - last_stats_time_mono;
258258

259259
if ( stats_diff > 10000 ){
260260
// System.out.println( "stats: time = " + stats_diff + ", ticks = " + tick_count + ", inst = " + instances.size());
261-
last_stats_time = latest_time_used;
261+
last_stats_time_mono = latest_time_used_mono;
262262
}
263263
}
264264
}
@@ -272,7 +272,7 @@
272272

273273
synchronized( instance_lock ){
274274

275-
Map new_map = new HashMap( instance_map );
275+
Map<PeerControlInstance,instanceWrapper> new_map = new HashMap<>( instance_map );
276276

277277
new_map.put( instance, wrapper );
278278

@@ -291,7 +291,7 @@
291291
{
292292
synchronized( instance_lock ){
293293

294-
Map new_map = new HashMap( instance_map );
294+
Map<PeerControlInstance,instanceWrapper> new_map = new HashMap<>( instance_map );
295295

296296
instanceWrapper wrapper = (instanceWrapper)new_map.remove(instance);
297297

@@ -325,21 +325,21 @@ public void updateScheduleOrdering() {
325325
@Override
326326
public int[] getPeerCount()
327327
{
328-
peer_count_active_time = SystemTime.getMonotonousTime();
328+
peer_count_active_time_mono = SystemTime.getMonotonousTime();
329329

330330
return( last_peer_count );
331331
}
332332

333333
@Override
334334
public int[] getPieceCount()
335335
{
336-
piece_count_active_time = SystemTime.getMonotonousTime();
336+
piece_count_active_time_mono = SystemTime.getMonotonousTime();
337337

338338
return( last_piece_count );
339339
}
340340

341341
protected static class
342-
instanceWrapper implements Comparable
342+
instanceWrapper implements Comparable<instanceWrapper>
343343
{
344344
private final PeerControlInstance instance;
345345
private boolean unregistered;
@@ -397,8 +397,8 @@ public int[] getPieceCount()
397397
}
398398

399399
@Override
400-
public int compareTo(Object o) {
401-
return instance.getSchedulePriority()-((instanceWrapper)o).instance.getSchedulePriority();
400+
public int compareTo(instanceWrapper o) {
401+
return instance.getSchedulePriority()-o.instance.getSchedulePriority();
402402
}
403403
}
404404
}

core/src/com/biglybt/core/peermanager/control/impl/SpeedTokenDispenserPrioritised.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ public void parameterChanged(String parameterName) {
4646
}
4747

4848
threshold = Math.max(BUCKET_THRESHOLD_FACTOR*rateKiB, BUCKET_THRESHOLD_LOWER_BOUND);
49-
lastTime = currentTime - 1; // shortest possible delta
49+
lastTimeMono = currentTimeMono - 1; // shortest possible delta
5050
refill(); // cap buffer to threshold in case something accumulated
5151
}
5252
});
5353
}
5454
private long threshold;
5555
private long bucket = 0;
56-
private long lastTime = SystemTime.getCurrentTime();
57-
private long currentTime;
56+
private long lastTimeMono = SystemTime.getMonotonousTime();
57+
private long currentTimeMono;
5858

59-
public void update(long newTime) {
60-
currentTime = newTime;
59+
public void update(long newTimeMono) {
60+
currentTimeMono = newTimeMono;
6161
}
6262

6363
// allow at least 2 outstanding requests
@@ -68,20 +68,20 @@ public void update(long newTime) {
6868
private static final int BUCKET_THRESHOLD_FACTOR = 1024 * BUCKET_RESPONSE_TIME;
6969

7070
public void refill() {
71-
if (lastTime == currentTime || rateKiB == 0)
71+
if (lastTimeMono == currentTimeMono || rateKiB == 0)
7272
return;
7373

74-
if ( lastTime > currentTime ){
75-
lastTime = currentTime;
74+
if ( lastTimeMono > currentTimeMono ){
75+
lastTimeMono = currentTimeMono;
7676
return;
7777
}
7878

7979
if ( bucket < 0 ){
8080
Debug.out( "Bucket is more than empty! - " + bucket );
8181
bucket = 0;
8282
}
83-
long delta = currentTime - lastTime;
84-
lastTime = currentTime;
83+
long delta = currentTimeMono - lastTimeMono;
84+
lastTimeMono = currentTimeMono;
8585
// upcast to long since we might exceed int-max when rate and delta are
8686
// large enough; then downcast again...
8787
long tickDelta = ( rateKiB * 1024L * delta) / 1000;

0 commit comments

Comments
 (0)