Share heap-based transaction row allocation across readers
This commit is contained in:
1 parent
d31fe15000
commit
8d4ec071e5
4 files changed
+122
-90
No files matched your search
+9
-46
@@ -10,8 +10,6 @@
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.ftr;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.EventList;
|
||||
@@ -20,7 +18,7 @@ import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.WaveformType;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.TxRowAllocator;
|
||||
import com.minres.scviewer.database.tx.ITxEvent;
|
||||
|
||||
/**
|
||||
@@ -126,7 +124,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
* @return the width
|
||||
*/
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
public synchronized int getRowCount() {
|
||||
if (rowCount<0)
|
||||
calculateConcurrency();
|
||||
return rowCount;
|
||||
@@ -140,46 +138,11 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
/**
|
||||
* Calculate concurrency.
|
||||
*/
|
||||
void calculateConcurrency() {
|
||||
if (rowCount>=0)
|
||||
return;
|
||||
ArrayList<Long> rowEndTime = new ArrayList<>();
|
||||
HashMap<Long, Integer> rowByTxId = new HashMap<>();
|
||||
for(EventEntry entry: getEvents()) {
|
||||
for(IEvent evt:entry.events) {
|
||||
TxEvent txEvt = (TxEvent) evt;
|
||||
ITx tx = txEvt.getTransaction();
|
||||
int rowIdx = 0;
|
||||
switch(evt.getKind()) {
|
||||
case END: //TODO: might throw NPE in concurrent execution
|
||||
Long txId = txEvt.getTransaction().getId();
|
||||
Integer row = rowByTxId.get(txId);
|
||||
if(row!=null) {
|
||||
txEvt.setConcurrencyIndex(row);
|
||||
rowByTxId.remove(txId);
|
||||
}
|
||||
break;
|
||||
case SINGLE:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
break;
|
||||
case BEGIN:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
rowByTxId.put(tx.getId(), rowIdx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
rowCount=rowEndTime.size()>0?rowEndTime.size():1;
|
||||
getChildNodes().parallelStream().forEach(c -> ((TxGenerator)c).calculateConcurrency());
|
||||
}
|
||||
synchronized void calculateConcurrency() {
|
||||
if (rowCount >= 0) return;
|
||||
int count = TxRowAllocator.assign(getEvents(), (event, row) -> ((TxEvent) event).setConcurrencyIndex(row));
|
||||
for (com.minres.scviewer.database.IHierNode child : getChildNodes())
|
||||
((TxGenerator) child).calculateConcurrency();
|
||||
rowCount = count;
|
||||
}
|
||||
}
|
||||
+9
-44
@@ -11,8 +11,6 @@
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.EventList;
|
||||
@@ -21,7 +19,7 @@ import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.WaveformType;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.TxRowAllocator;
|
||||
import com.minres.scviewer.database.tx.ITxEvent;
|
||||
|
||||
/**
|
||||
@@ -137,7 +135,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
* @return the width
|
||||
*/
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
public synchronized int getRowCount() {
|
||||
if (rowCount<0)
|
||||
calculateConcurrency();
|
||||
return rowCount;
|
||||
@@ -150,44 +148,11 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
/**
|
||||
* Calculate concurrency.
|
||||
*/
|
||||
void calculateConcurrency() {
|
||||
if (rowCount>=0)
|
||||
return;
|
||||
ArrayList<Long> rowEndTime = new ArrayList<>();
|
||||
HashMap<Long, Integer> rowByTxId = new HashMap<>();
|
||||
for(EventEntry entry: events) {
|
||||
for(IEvent evt:entry.events) {
|
||||
TxEvent txEvt = (TxEvent) evt;
|
||||
ITx tx = txEvt.getTransaction();
|
||||
int rowIdx = 0;
|
||||
switch(evt.getKind()) {
|
||||
case END:
|
||||
Long txId = txEvt.getTransaction().getId();
|
||||
txEvt.setConcurrencyIndex(rowByTxId.get(txId));
|
||||
rowByTxId.remove(txId);
|
||||
break;
|
||||
case SINGLE:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
break;
|
||||
case BEGIN:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
rowByTxId.put(tx.getId(), rowIdx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
rowCount=rowEndTime.size()>0?rowEndTime.size():1;
|
||||
//getChildNodes().parallelStream().forEach(c -> ((TxGenerator)c).calculateConcurrency());
|
||||
getChildNodes().stream().forEach(c -> ((TxGenerator)c).calculateConcurrency());
|
||||
}
|
||||
synchronized void calculateConcurrency() {
|
||||
if (rowCount >= 0) return;
|
||||
int count = TxRowAllocator.assign(getEvents(), (event, row) -> ((TxEvent) event).setConcurrencyIndex(row));
|
||||
for (com.minres.scviewer.database.IHierNode child : getChildNodes())
|
||||
((TxGenerator) child).calculateConcurrency();
|
||||
rowCount = count;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.minres.scviewer.database.tx;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.function.ObjIntConsumer;
|
||||
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.EventKind;
|
||||
import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
|
||||
/** Assigns the lowest available row in O(N log C), where C is peak concurrency. */
|
||||
public final class TxRowAllocator {
|
||||
private record Active(long end, int row) { }
|
||||
private TxRowAllocator() { }
|
||||
|
||||
public static int assign(IEventList events, ObjIntConsumer<ITxEvent> setRow) {
|
||||
PriorityQueue<Active> active = new PriorityQueue<>((a, b) -> {
|
||||
int cmp = Long.compareUnsigned(a.end(), b.end());
|
||||
return cmp != 0 ? cmp : Integer.compare(a.row(), b.row());
|
||||
});
|
||||
PriorityQueue<Integer> free = new PriorityQueue<>();
|
||||
Map<Long, Integer> rowByTx = new HashMap<>();
|
||||
Map<ITxEvent, Integer> assignments = new IdentityHashMap<>();
|
||||
int count = 0;
|
||||
for (EventEntry entry : events) {
|
||||
for (IEvent raw : entry.events) {
|
||||
ITxEvent event = (ITxEvent) raw;
|
||||
ITx tx = event.getTransaction();
|
||||
if (tx == null) throw new IllegalStateException("Missing transaction for " + event);
|
||||
if (event.getKind() == EventKind.END) {
|
||||
Integer row = rowByTx.remove(tx.getId());
|
||||
if (row == null) throw new IllegalStateException("End without begin for transaction " + tx.getId());
|
||||
assignments.put(event, row);
|
||||
continue;
|
||||
}
|
||||
while (!active.isEmpty() && Long.compareUnsigned(active.peek().end(), tx.getBeginTime()) <= 0)
|
||||
free.add(active.remove().row());
|
||||
int row = free.isEmpty() ? count++ : free.remove();
|
||||
active.add(new Active(tx.getEndTime(), row));
|
||||
assignments.put(event, row);
|
||||
if (event.getKind() == EventKind.BEGIN) rowByTx.put(tx.getId(), row);
|
||||
}
|
||||
}
|
||||
// Validate the whole layout before changing any event indexes.
|
||||
assignments.forEach((event, row) -> setRow.accept(event, row));
|
||||
return Math.max(1, count);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user