📄 synchronizationtimer.java
字号:
static final int PRECISION = 10; // microseconds
static String formatTime(long ns, boolean showDecimal) {
long intpart = ns / PRECISION;
long decpart = ns % PRECISION;
if (!showDecimal) {
if (decpart >= PRECISION/2)
++intpart;
return Long.toString(intpart);
}
else {
String sint = Long.toString(intpart);
String sdec = Long.toString(decpart);
if (decpart == 0) {
int z = PRECISION;
while (z > 10) {
sdec = "0" + sdec;
z /= 10;
}
}
String ts = sint + "." + sdec;
return ts;
}
}
static class ThreadInfo {
final String name;
final int number;
Boolean enabled;
ThreadInfo(int nthr) {
number = nthr;
name = p2ToString(nthr);
enabled = new Boolean(true);
}
synchronized Boolean getEnabled() { return enabled; }
synchronized void setEnabled(Boolean v) { enabled = v; }
synchronized void toggleEnabled() {
enabled = new Boolean(!enabled.booleanValue());
}
}
final ThreadInfo[] threadInfo = new ThreadInfo[nthreadsChoices.length];
boolean threadEnabled(int nthreads) {
return threadInfo[nthreads].getEnabled().booleanValue();
}
// This used to be a JTable datamodel, but now just part of this class ...
final static int headerRows = 1;
final static int classColumn = 0;
final static int headerColumns = 1;
final int tableRows = TestedClass.classes.length + headerRows;
final int tableColumns = nthreadsChoices.length + headerColumns;
final JComponent[][] resultTable_ = new JComponent[tableRows][tableColumns];
JPanel resultPanel() {
JPanel[] colPanel = new JPanel[tableColumns];
for (int col = 0; col < tableColumns; ++col) {
colPanel[col] = new JPanel();
colPanel[col].setLayout(new GridLayout(tableRows, 1));
if (col != 0)
colPanel[col].setBackground(Color.white);
}
Color hdrbg = colPanel[0].getBackground();
Border border = new LineBorder(hdrbg);
Font font = new Font("Dialog", Font.PLAIN, 12);
Dimension labDim = new Dimension(40, 16);
Dimension cbDim = new Dimension(154, 16);
JLabel cornerLab = new JLabel(" Classes \\ Threads");
cornerLab.setMinimumSize(cbDim);
cornerLab.setPreferredSize(cbDim);
cornerLab.setFont(font);
resultTable_[0][0] = cornerLab;
colPanel[0].add(cornerLab);
for (int col = 1; col < tableColumns; ++col) {
final int nthreads = col - headerColumns;
JCheckBox tcb = new JCheckBox(threadInfo[nthreads].name, true);
tcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
threadInfo[nthreads].toggleEnabled();
}});
tcb.setMinimumSize(labDim);
tcb.setPreferredSize(labDim);
tcb.setFont(font);
tcb.setBackground(hdrbg);
resultTable_[0][col] = tcb;
colPanel[col].add(tcb);
}
for (int row = 1; row < tableRows; ++row) {
final int cls = row - headerRows;
JCheckBox cb = new JCheckBox(TestedClass.classes[cls].name, true);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
TestedClass.classes[cls].toggleEnabled();
}});
resultTable_[row][0] = cb;
cb.setMinimumSize(cbDim);
cb.setPreferredSize(cbDim);
cb.setFont(font);
colPanel[0].add(cb);
for (int col = 1; col < tableColumns; ++col) {
int nthreads = col - headerColumns;
JLabel lab = new JLabel("");
resultTable_[row][col] = lab;
lab.setMinimumSize(labDim);
lab.setPreferredSize(labDim);
lab.setBorder(border);
lab.setFont(font);
lab.setBackground(Color.white);
lab.setForeground(Color.black);
lab.setHorizontalAlignment(JLabel.RIGHT);
colPanel[col].add(lab);
}
}
JPanel tblPanel = new JPanel();
tblPanel.setLayout(new BoxLayout(tblPanel, BoxLayout.X_AXIS));
for (int col = 0; col < tableColumns; ++col) {
tblPanel.add(colPanel[col]);
}
return tblPanel;
}
void setTime(final long ns, int clsIdx, int nthrIdx) {
int row = clsIdx+headerRows;
int col = nthrIdx+headerColumns;
final JLabel cell = (JLabel)(resultTable_[row][col]);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
cell.setText(formatTime(ns, true));
}
});
}
void clearTable() {
for (int i = 1; i < tableRows; ++i) {
for (int j = 1; j < tableColumns; ++j) {
((JLabel)(resultTable_[i][j])).setText("");
}
}
}
void setChecks(final boolean setting) {
for (int i = 0; i < TestedClass.classes.length; ++i) {
TestedClass.classes[i].setEnabled(new Boolean(setting));
((JCheckBox)resultTable_[i+1][0]).setSelected(setting);
}
}
public SynchronizationTimer() {
for (int i = 0; i < threadInfo.length; ++i)
threadInfo[i] = new ThreadInfo(nthreadsChoices[i]);
}
final SynchronizedInt nextClassIdx_ = new SynchronizedInt(0);
final SynchronizedInt nextThreadIdx_ = new SynchronizedInt(0);
JPanel mainPanel() {
new PrintStart(); // classloader bug workaround
JPanel paramPanel = new JPanel();
paramPanel.setLayout(new GridLayout(5, 3));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 3));
startstop_.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (running_.get())
cancel();
else {
try {
startTestSeries(new TestSeries());
}
catch (InterruptedException ex) {
endTestSeries();
}
}
}});
paramPanel.add(startstop_);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1, 2));
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!running_.get()) {
try {
startTestSeries(new TestSeries(nextClassIdx_.get(),
nextThreadIdx_.get()));
}
catch (InterruptedException ex) {
endTestSeries();
}
}
}});
p1.add(continueButton);
JButton clearButton = new JButton("Clear cells");
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
clearTable();
}
});
p1.add(clearButton);
paramPanel.add(p1);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1, 2));
JButton setButton = new JButton("All classes");
setButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
setChecks(true);
}
});
p3.add(setButton);
JButton unsetButton = new JButton("No classes");
unsetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
setChecks(false);
}
});
p3.add(unsetButton);
paramPanel.add(p3);
JPanel p2 = new JPanel();
// p2.setLayout(new GridLayout(1, 2));
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
JCheckBox consoleBox = new JCheckBox("Console echo");
consoleBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
echoToSystemOut.complement();
}
});
JLabel poolinfo = new JLabel("Active threads: 0");
p2.add(poolinfo);
p2.add(consoleBox);
paramPanel.add(p2);
paramPanel.add(contentionBox());
paramPanel.add(itersBox());
paramPanel.add(cloopBox());
paramPanel.add(barrierBox());
paramPanel.add(exchangeBox());
paramPanel.add(biasBox());
paramPanel.add(capacityBox());
paramPanel.add(timeoutBox());
paramPanel.add(syncModePanel());
paramPanel.add(producerSyncModePanel());
paramPanel.add(consumerSyncModePanel());
startPoolStatus(poolinfo);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel tblPanel = resultPanel();
mainPanel.add(tblPanel);
mainPanel.add(paramPanel);
return mainPanel;
}
JComboBox syncModePanel() {
JComboBox syncModeComboBox = new JComboBox();
for (int j = 0; j < syncModes.length; ++j) {
String lab = "Locks: " + modeToString(syncModes[j]);
syncModeComboBox.addItem(lab);
}
syncModeComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
JComboBox src = (JComboBox)(evt.getItemSelectable());
int idx = src.getSelectedIndex();
RNG.syncMode.set(syncModes[idx]);
}
});
RNG.syncMode.set(syncModes[0]);
syncModeComboBox.setSelectedIndex(0);
return syncModeComboBox;
}
JComboBox producerSyncModePanel() {
JComboBox producerSyncModeComboBox = new JComboBox();
for (int j = 0; j < syncModes.length; ++j) {
String lab = "Producers: " + modeToString(syncModes[j]);
producerSyncModeComboBox.addItem(lab);
}
producerSyncModeComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
JComboBox src = (JComboBox)(evt.getItemSelectable());
int idx = src.getSelectedIndex();
RNG.producerMode.set(syncModes[idx]);
}
});
RNG.producerMode.set(syncModes[0]);
producerSyncModeComboBox.setSelectedIndex(0);
return producerSyncModeComboBox;
}
JComboBox consumerSyncModePanel() {
JComboBox consumerSyncModeComboBox = new JComboBox();
for (int j = 0; j < syncModes.length; ++j) {
String lab = "Consumers: " + modeToString(syncModes[j]);
consumerSyncModeComboBox.addItem(lab);
}
consumerSyncModeComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
JComboBox src = (JComboBox)(evt.getItemSelectable());
int idx = src.getSelectedIndex();
RNG.consumerMode.set(syncModes[idx]);
}
});
RNG.consumerMode.set(syncModes[0]);
consumerSyncModeComboBox.setSelectedIndex(0);
return consumerSyncModeComboBox;
}
JComboBox contentionBox() {
final Fraction[] contentionChoices = {
new Fraction(0, 1),
new Fraction(1, 16),
new Fraction(1, 8),
new Fraction(1, 4),
new Fraction(1, 2),
new Fraction(1, 1)
};
JComboBox contentionComboBox = new JComboBox();
for (int j = 0; j < contentionChoices.length; ++j) {
String lab = contentionChoices[j].asDouble() * 100.0 +
"% contention/sharing";
contentionComboBox.addItem(lab);
}
contentionComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
JComboBox src = (JComboBox)(evt.getItemSelectable());
int idx = src.getSelectedIndex();
contention_.set(contentionChoices[idx]);
}
});
contention_.set(contentionChoices[3]);
contentionComboBox.setSelectedIndex(3);
return contentionComboBox;
}
JComboBox itersBox() {
final int[] loopsPerTestChoices = {
1,
16,
256,
1024,
2 * 1024,
4 * 1024,
8 * 1024,
16 * 1024,
32 * 1024,
64 * 1024,
128 * 1024,
256 * 1024,
512 * 1024,
1024 * 1024,
};
JComboBox precComboBox = new JComboBox();
for (int j = 0; j < loopsPerTestChoices.length; ++j) {
String lab = p2ToString(loopsPerTestChoices[j]) +
" calls per thread per test";
precComboBox.addItem(lab);
}
precComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
JComboBox src = (JComboBox)(evt.getItemSelectable());
int idx = src.getSelectedIndex();
loopsPerTest_.set(loopsPerTestChoices[idx]);
}
});
loopsPerTest_.set(loopsPerTestChoices[8]);
precComboBox.setSelectedIndex(8);
return precComboBox;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -