Skip to content

Commit 263e1ac

Browse files
authored
Merge pull request #9 from vanilladb/develop
Update the interface of VanillaDb and timers for debugging
2 parents ca5c49f + 1d73444 commit 263e1ac

File tree

5 files changed

+355
-271
lines changed

5 files changed

+355
-271
lines changed

src/main/java/org/vanilladb/core/server/VanillaDb.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,12 @@
4747
* be initialized by the method {@link #init(String) init} before use. The
4848
* methods {@link #initFileMgr(String) initFileMgr},
4949
* {@link #initFileAndLogMgr(String) initFileAndLogMgr},
50-
* {@link #initTaskMgr() initTaskMgr},
51-
* {@link #initTxMgr() initTxMgr},
52-
* {@link #initCatalogMgr(boolean, Transaction) initCatalogMgr},
53-
* {@link #initStatMgr(Transaction) initStatMgr},
54-
* {@link #initSPFactory() initSPFactory}, and
55-
* {@link #initCheckpointingTask() initCheckpointingTask} provide limited
50+
* {@link #initFileLogAndBufferMgr(String) initFileLogAndBufferMgr}, and
51+
* {@link #initCatalogMgr(boolean, Transaction) initCatalogMgr} provide limited
5652
* initialization, and are useful for debugging purposes.
5753
*/
5854
public class VanillaDb {
5955

60-
public static enum BufferMgrType {
61-
DefaultBufferMgr, DummyBufferMgr
62-
};
63-
6456
// Logger
6557
private static Logger logger = Logger.getLogger(VanillaDb.class.getName());
6658

@@ -91,24 +83,27 @@ public static enum BufferMgrType {
9183
* the name of the database directory
9284
*/
9385
public static void init(String dirName) {
94-
init(dirName, BufferMgrType.DefaultBufferMgr);
86+
init(dirName, new SampleStoredProcedureFactory());
9587
}
9688

9789
/**
9890
* Initializes the system. This method is called during system startup.
9991
*
10092
* @param dirName
10193
* the name of the database directory
102-
* @param bufferType
103-
* the type of the buffer manager for storage engine
94+
* @param factory
95+
* the stored procedure factory for generating stored procedures
10496
*/
105-
public static void init(String dirName, BufferMgrType bufferType) {
97+
public static void init(String dirName, StoredProcedureFactory factory) {
10698

10799
if (inited) {
108100
if (logger.isLoggable(Level.WARNING))
109101
logger.warning("discarding duplicated init request");
110102
return;
111103
}
104+
105+
// Set the stored procedure factory
106+
spFactory = factory;
112107

113108
/*
114109
* Note: We read properties file here before, but we moved it to a

src/main/java/org/vanilladb/core/util/TxComponentTimer.java renamed to src/main/java/org/vanilladb/core/util/Timer.java

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,26 @@
2020
import java.util.List;
2121
import java.util.Map;
2222

23-
public class TxComponentTimer {
23+
public class Timer {
2424

25-
private static final boolean PRINT_COUNTS = false;
25+
private static final String EXE_TIME_KEY = "Execution Time";
26+
27+
private static final ThreadLocal<Timer> LOCAL_TIMER =
28+
new ThreadLocal<Timer>() {
29+
@Override
30+
protected Timer initialValue() {
31+
return new Timer();
32+
}
33+
};
34+
35+
/**
36+
* Get the timer local to this thread.
37+
*
38+
* @return
39+
*/
40+
public static Timer getLocalTimer() {
41+
return LOCAL_TIMER.get();
42+
}
2643

2744
private static class SubTimer {
2845
private long start = 0, startTimes = 0, totalTime = 0, count = 0;
@@ -52,14 +69,13 @@ public long getCount() {
5269
}
5370

5471
private Map<Object, SubTimer> subTimers = new HashMap<Object, SubTimer>();
72+
// We want to preserve the order of creating timers so that
73+
// we use a list to record the order.
5574
private List<Object> componenents = new LinkedList<Object>();
56-
private Object[] metadata;
57-
private long executionStart, executionTime = 0;
5875

59-
private long txNum;
60-
61-
public TxComponentTimer(long txNum) {
62-
this.txNum = txNum;
76+
public void reset() {
77+
subTimers.clear();
78+
componenents.clear();
6379
}
6480

6581
public void startComponentTimer(Object component) {
@@ -78,59 +94,49 @@ public void stopComponentTimer(Object component) {
7894
timer.stopTimer();
7995
}
8096

81-
public long getComponentCount(Object component) {
82-
return subTimers.get(component).getCount();
83-
}
84-
85-
public void startExecution() {
86-
executionStart = System.nanoTime();
87-
}
88-
89-
public void stopExecution() {
90-
executionTime = (System.nanoTime() - executionStart) / 1000;
91-
}
92-
9397
public long getComponentTime(Object component) {
9498
SubTimer timer = subTimers.get(component);
9599
if (timer == null)
96100
return -1;
97101
return timer.getTotalTime();
98102
}
99103

100-
public long getExecutionTime() {
101-
return executionTime;
104+
public long getComponentCount(Object component) {
105+
return subTimers.get(component).getCount();
102106
}
103107

104-
public long getTransactionNumber() {
105-
return txNum;
108+
public List<Object> getComponents() {
109+
return new LinkedList<Object>(componenents);
106110
}
107111

108-
public Object getMetadata() {
109-
return metadata;
112+
public void startExecution() {
113+
startComponentTimer(EXE_TIME_KEY);
110114
}
111115

112-
public void setMetadata(Object[] m) {
113-
this.metadata = m;
116+
public void stopExecution() {
117+
stopComponentTimer(EXE_TIME_KEY);
118+
}
119+
120+
public long getExecutionTime() {
121+
return getComponentTime(EXE_TIME_KEY);
122+
}
123+
124+
public void addToGlobalStatistics() {
125+
TimerStatistics.add(this);
114126
}
115127

116128
public String toString() {
117129
StringBuilder sb = new StringBuilder();
118130

119-
if (!componenents.isEmpty()) {
120-
sb.append("===== Profile for Tx." + txNum + " =====\n");
121-
for (Object com : componenents) {
122-
sb.append(String.format("%-40s: %d us", com, subTimers.get(com)
123-
.getTotalTime()));
124-
if (PRINT_COUNTS) {
125-
sb.append(String.format(", with %d counts",
126-
subTimers.get(com).getCount()));
127-
}
128-
sb.append("\n");
131+
sb.append("==============================\n");
132+
for (Object com : componenents) {
133+
if (!com.equals("Execution Time")) {
134+
sb.append(String.format("%-40s: %d us, with %d counts\n", com, subTimers.get(com).getTotalTime(),
135+
subTimers.get(com).getCount()));
129136
}
130-
sb.append(String.format("%-40s: %d us\n", "Execution Time:",
131-
executionTime));
132-
sb.append("==============================\n");
133137
}
138+
sb.append(String.format("%-40s: %d us\n", EXE_TIME_KEY, subTimers.get(EXE_TIME_KEY).getTotalTime()));
139+
sb.append("==============================\n");
134140

135141
return sb.toString();
136142
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.vanilladb.core.util;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.concurrent.atomic.AtomicBoolean;
8+
9+
public class TimerStatistics {
10+
11+
private static final long REPORT_PERIOD = 3000;
12+
private static final AtomicBoolean IS_REPORTING = new AtomicBoolean(false);
13+
14+
private static List<Object> components = new LinkedList<Object>();
15+
private static Map<Object, ComponentStatistic> stats = new ConcurrentHashMap<Object, ComponentStatistic>();
16+
private static Object[] anchors = new Object[1099];
17+
18+
private static class ComponentStatistic {
19+
long recordTime;
20+
int lastCount, count;
21+
22+
synchronized void addTime(long time) {
23+
recordTime += time;
24+
count++;
25+
}
26+
27+
synchronized double calAverage() {
28+
double avg = 0;
29+
if (count != 0) {
30+
avg = ((double) recordTime) / ((double) count);
31+
}
32+
recordTime = 0;
33+
lastCount = count;
34+
count = 0;
35+
return avg;
36+
}
37+
38+
synchronized int getLastCount() {
39+
return lastCount;
40+
}
41+
}
42+
43+
static {
44+
for (int i = 0; i < anchors.length; i++)
45+
anchors[i] = new Object();
46+
47+
new Thread(new Runnable() {
48+
49+
@Override
50+
public void run() {
51+
long startTime = System.currentTimeMillis();
52+
long lastTime = startTime;
53+
54+
while (true) {
55+
long currentTime = System.currentTimeMillis();
56+
57+
if (currentTime - lastTime > REPORT_PERIOD) {
58+
lastTime = currentTime;
59+
60+
double t = (currentTime - startTime) / 1000;
61+
StringBuilder sb = new StringBuilder();
62+
63+
sb.append("===================================\n");
64+
sb.append(String.format("At %.2f:\n", t));
65+
66+
synchronized (components) {
67+
for (Object key : components) {
68+
ComponentStatistic stat = stats.get(key);
69+
double avg = stat.calAverage();
70+
int count = stat.getLastCount();
71+
72+
if (count > 0) {
73+
sb.append(String.format("%s: average %.2f us", key, avg));
74+
sb.append(String.format(" with count %d\n", count));
75+
}
76+
}
77+
}
78+
79+
sb.append("===================================\n");
80+
81+
if (IS_REPORTING.get())
82+
System.out.println(sb.toString());
83+
}
84+
85+
try {
86+
Thread.sleep(REPORT_PERIOD / 10);
87+
} catch (InterruptedException e) {
88+
e.printStackTrace();
89+
}
90+
}
91+
}
92+
93+
}).start();
94+
}
95+
96+
public static void startReporting() {
97+
IS_REPORTING.set(true);
98+
}
99+
100+
public static void stopReporting() {
101+
IS_REPORTING.set(false);
102+
}
103+
104+
static void add(Timer timer) {
105+
if (IS_REPORTING.get()) {
106+
for (Object com : timer.getComponents()) {
107+
ComponentStatistic stat = stats.get(com);
108+
if (stat == null)
109+
stat = createNewStatistic(com);
110+
stat.addTime(timer.getComponentTime(com));
111+
}
112+
}
113+
}
114+
115+
private static Object getAnchor(Object key) {
116+
int code = key.hashCode();
117+
code = Math.abs(code); // avoid negative value
118+
return anchors[code % anchors.length];
119+
}
120+
121+
private static ComponentStatistic createNewStatistic(Object key) {
122+
Object anchor = getAnchor(key);
123+
synchronized (anchor) {
124+
125+
ComponentStatistic stat = stats.get(key);
126+
127+
if (stat == null) {
128+
stat = new ComponentStatistic();
129+
stats.put(key, new ComponentStatistic());
130+
synchronized (components) {
131+
components.add(key);
132+
}
133+
}
134+
135+
return stat;
136+
}
137+
}
138+
}

src/main/java/org/vanilladb/core/util/Timers.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)