-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiProcessor.java
More file actions
448 lines (400 loc) · 16.5 KB
/
Copy pathMultiProcessor.java
File metadata and controls
448 lines (400 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
// 定义缓存行的状态
enum CacheLineState {
INVALID, // 失效
SHARED, // 共享
EXCLUSIVE // 独占
}
// 定义主存类
class MainMemory {
public final int size;
private final String[] data;
public MainMemory(int size) {
this.size = size;
data = new String[size];
for (int i = 0; i < size; i++) {
data[i] = String.valueOf(i);
}
}
public void clean() {
for (int i = 0; i < size; i++) {
data[i] = String.valueOf(i);
}
}
public String readData(int address) {
return data[address];
}
public void writeData(int address, String newData) {
data[address] = newData;
}
}
// 定义处理器类
class Processor {
public final static int cacheNum = 4;
public static Vector<String> messages = new Vector<>();
private final String id;
private final CacheLineState[] cacheStates; // 每个缓存行的状态
private final String[] cacheData; // 每个缓存行的数据
private final int[] cacheAddress; // 用于存储每个缓存行中的主存地址
private final MainMemory mainMemory;
public Processor(String id, MainMemory mainMemory) {
this.id = id;
this.cacheStates = new CacheLineState[Processor.cacheNum]; // 每个处理器有4个缓存行
this.cacheData = new String[Processor.cacheNum]; // 数据缓存
this.cacheAddress = new int[Processor.cacheNum];
this.mainMemory = mainMemory;
for (int i = 0; i < Processor.cacheNum; i++) {
cacheStates[i] = CacheLineState.INVALID;
cacheData[i] = "";
cacheAddress[i] = mainMemory.size;
}
}
public static void addMessage(String string) {
System.out.println(string);
Processor.messages.add(string);
}
public void clean() {
for (int i = 0; i < Processor.cacheNum; i++) {
cacheStates[i] = CacheLineState.INVALID;
cacheData[i] = "";
cacheAddress[i] = mainMemory.size;
}
}
public String getId() {
return id;
}
public CacheLineState getCacheStates(int index) {
return cacheStates[index];
}
public String getCacheData(int index) {
return cacheData[index];
}
public String readData(int address) {
int cacheIndex = address % cacheNum; // 使用取余操作确定缓存行索引
if (checkHit(address)) {
addMessage("CPU:" + id + " 读:" + address + " cache命中");
} else {
addMessage("CPU:" + id + " 读:" + address + " cache未命中 读主存...");
checkCacheStatus(address, "r");
updateCache(address, mainMemory.readData(address), CacheLineState.SHARED);
}
addMessage("CPU:" + id + " 得到结果:" + cacheData[cacheIndex]);
addMessage("---------------==========+==========---------------");
return cacheData[cacheIndex];
}
public void writeData(int address, String newData) {
if (checkHit(address)) {
addMessage("CPU:" + id + " 写:" + address + " cache命中");
} else {
addMessage("CPU:" + id + " 写:" + address + " cache未命中");
}
checkCacheStatus(address, "w");
updateCache(address, newData, CacheLineState.EXCLUSIVE);
addMessage("---------------==========+==========---------------");
}
private void checkCacheStatus(int address, String fun) {
int cacheIndex = address % cacheNum; // 使用取余操作确定缓存行索引
for (Processor p : MultiProcessor.processors) {
if (!this.equals(p) && p.checkHit(address)) {
if (p.cacheStates[cacheIndex] == CacheLineState.SHARED && fun.equals("w")) {
p.updateCache(address, "", CacheLineState.INVALID);
}
if (p.cacheStates[cacheIndex] == CacheLineState.EXCLUSIVE) {
p.writeBack(address);
p.updateCache(address, p.getCacheData(cacheIndex), CacheLineState.SHARED);
return;
}
} else {
if (cacheStates[cacheIndex] == CacheLineState.EXCLUSIVE) {
writeBack(cacheAddress[cacheIndex]);
updateCache(cacheAddress[cacheIndex], cacheData[cacheIndex], CacheLineState.SHARED);
}
}
}
}
private void writeBack(int address) {
int cacheIndex = address % cacheNum; // 使用取余操作确定缓存行索引
addMessage("CPU:" + id + " 写回主存:" + address);
mainMemory.writeData(address, cacheData[cacheIndex]);
}
public void updateCache(int address, String newData, CacheLineState newState) {
int cacheIndex = address % cacheNum; // 使用取余操作确定缓存行索引
if (newState == CacheLineState.INVALID) {
addMessage("CPU:" + id + " Cache:" + cacheIndex + " 作废" + " 状态: " + newState);
cacheData[cacheIndex] = "";
cacheStates[cacheIndex] = CacheLineState.INVALID;
cacheAddress[cacheIndex] = mainMemory.size;
return;
}
addMessage("CPU:" + id + " Cache:" + cacheIndex + " 更新主存地址:" + address + " 状态: " + newState);
cacheData[cacheIndex] = newData;
cacheStates[cacheIndex] = newState;
cacheAddress[cacheIndex] = address;
}
public boolean checkHit(int address) {
int cacheIndex = address % cacheNum; // 使用取余操作确定缓存行索引
// 处理缓存命中的逻辑
return cacheAddress[cacheIndex] == address;
}
}
public class MultiProcessor extends JFrame {
static List<Processor> processors = new ArrayList<>();
MainMemory mainMemory;
Processor p1;
Processor p2;
Processor p3;
Processor p4;
int processorSize;
JTextField[] dataFields;
JLabel[][] cacheLabels;
JLabel[] memoryLabels;
JComboBox<Integer>[] addressBoxes;
JComboBox<String>[] operationComboBoxes;
JList<String> messageList;
public MultiProcessor() {
mainMemory = new MainMemory(32);
p1 = new Processor("A", mainMemory);
p2 = new Processor("B", mainMemory);
p3 = new Processor("C", mainMemory);
p4 = new Processor("D", mainMemory);
processors.add(p1);
processors.add(p2);
processors.add(p3);
processors.add(p4);
processorSize = processors.size();
// 设置 CPU 部分的相关组件
cacheLabels = new JLabel[processorSize][Processor.cacheNum];
dataFields = new JTextField[processorSize];
addressBoxes = new JComboBox[processorSize];
operationComboBoxes = new JComboBox[processorSize];
for (int i = 0; i < processorSize; i++) {
Processor p = processors.get(i);
dataFields[i] = new JTextField(1);
addressBoxes[i] = new JComboBox<>(new DefaultComboBoxModel<>(generateNumbers(0, 31)));
operationComboBoxes[i] = new JComboBox<>(new String[]{"Read", "Write"});
for (int j = 0; j < 4; j++) {
cacheLabels[i][j] = new JLabel("Cache " + j + " 数据: " + p.getCacheData(j) + " 状态: " + p.getCacheStates(j));
switch (p.getCacheStates(j)) {
case INVALID:
cacheLabels[i][j].setForeground(Color.GRAY);
break;
case SHARED:
cacheLabels[i][j].setForeground(Color.BLUE);
break;
case EXCLUSIVE:
cacheLabels[i][j].setForeground(Color.RED);
break;
}
}
}
// 重新设置内存部分的相关组件
memoryLabels = new JLabel[32];
for (int i = 0; i < 32; i++) {
memoryLabels[i] = new JLabel("Block " + i + " 数据: " + mainMemory.readData(i));
}
setTitle("多Cache一致性模拟器--监听法");
setSize(1000, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel functionPanel = new JPanel();
functionPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
JButton resetButton = new JButton("复位");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
init();
}
});
JButton doButton = new JButton("执行指令文件");
doButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String filePath = "Commands.txt";
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
processLine(line);
}
} catch (IOException ex) {
Processor.addMessage("无指令文件");
Processor.addMessage("---------------==========+==========---------------");
}
update();
}
});
buttonPanel.add(resetButton, BorderLayout.NORTH);
buttonPanel.add(doButton, BorderLayout.SOUTH);
functionPanel.add(buttonPanel, BorderLayout.NORTH);
// CPUs
JPanel[] cpuPanels = new JPanel[processorSize];
JButton[] startButtons = new JButton[processorSize];
JPanel cpuContainer = new JPanel();
cpuContainer.setLayout(new GridLayout(2, processorSize / 2));
for (int i = 0; i < processorSize; i++) {
Processor p = processors.get(i);
cpuPanels[i] = new JPanel();
cpuPanels[i].setLayout(new BoxLayout(cpuPanels[i], BoxLayout.Y_AXIS));
cpuPanels[i].setBorder(BorderFactory.createTitledBorder("CPU " + p.getId()));
cpuPanels[i].add(dataFields[i]);
cpuPanels[i].add(addressBoxes[i]);
cpuPanels[i].add(operationComboBoxes[i]);
startButtons[i] = new JButton("运行");
int finalI = i;
startButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int address = (int) addressBoxes[finalI].getSelectedItem();
String operationText = (String) operationComboBoxes[finalI].getSelectedItem();
if (operationText.equals("Read")) {
p.readData(address);
} else if (operationText.equals("Write")) {
p.writeData(address, dataFields[finalI].getText());
}
update();
}
});
cpuPanels[i].add(startButtons[i]);
JPanel cachePanel = new JPanel();
cachePanel.setBorder(BorderFactory.createTitledBorder("Caches"));
cachePanel.setLayout(new GridLayout(4, 1));
for (int j = 0; j < 4; j++) {
cachePanel.add(cacheLabels[i][j]);
}
cpuPanels[i].add(cachePanel);
cpuContainer.add(cpuPanels[i]);
}
functionPanel.add(cpuContainer, BorderLayout.CENTER);
// Memory
JPanel memoryPanel = new JPanel();
memoryPanel.setLayout(new GridLayout(8, 4));
memoryPanel.setBorder(BorderFactory.createTitledBorder("存储器"));
for (int i = 0; i < 32; i++) {
memoryPanel.add(memoryLabels[i]);
}
functionPanel.add(memoryPanel, BorderLayout.SOUTH);
add(functionPanel);
messageList = new JList<>();
JScrollPane scrollPane = new JScrollPane(messageList);
scrollPane.setBorder(BorderFactory.createTitledBorder("操作记录"));
add(scrollPane, BorderLayout.EAST);
}
private static void processLine(String line) {
char firstChar = line.charAt(0);
String[] parts = line.split(" ", 4);
switch (firstChar) {
case 'r':
if (parts.length > 2) {
String id = parts[1];
int address = Integer.parseInt(parts[2]);
try {
getProcessorById(id).readData(address);
} catch (NullPointerException ex) {
Processor.addMessage("错误的处理器ID");
Processor.addMessage("---------------==========+==========---------------");
}
}
break;
case 'w':
if (parts.length > 3) {
String id = parts[1];
int address = Integer.parseInt(parts[2]);
String data = parts[3];
try {
getProcessorById(id).writeData(address, data);
} catch (NullPointerException ex) {
Processor.addMessage("错误的处理器ID");
Processor.addMessage("---------------==========+==========---------------");
}
}
break;
default:
Processor.addMessage("错误的指令: " + line + "(r/w 处理器ID 地址 [数据])");
Processor.addMessage("---------------==========+==========---------------");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MultiProcessor().setVisible(true));
}
private static Processor getProcessorById(String id) {
for (Processor p : processors) {
if (p.getId().equalsIgnoreCase(id)) {
return p;
}
}
return null;
}
private Integer[] generateNumbers(int start, int end) {
Integer[] numbers = new Integer[end - start + 1];
for (int i = start; i <= end; i++) {
numbers[i - start] = i;
}
return numbers;
}
private void init() {
mainMemory.clean();
for (Processor p : processors) {
p.clean();
}
Processor.messages.clear();
// 设置 CPU 部分的相关组件
for (int i = 0; i < processorSize; i++) {
Processor p = processors.get(i);
dataFields[i].setText("");
operationComboBoxes[i].setSelectedIndex(0);
for (int j = 0; j < 4; j++) {
cacheLabels[i][j].setText("Cache " + j + " 数据: " + p.getCacheData(j) + " 状态: " + p.getCacheStates(j));
switch (p.getCacheStates(j)) {
case INVALID:
cacheLabels[i][j].setForeground(Color.GRAY);
break;
case SHARED:
cacheLabels[i][j].setForeground(Color.BLUE);
break;
case EXCLUSIVE:
cacheLabels[i][j].setForeground(Color.RED);
break;
}
}
}
// 重新设置内存部分的相关组件
for (int i = 0; i < 32; i++) {
memoryLabels[i].setText("Block " + i + " 数据: " + mainMemory.readData(i));
}
messageList.setListData(new Vector<>());
revalidate();
repaint();
}
private void update() {
for (int i = 0; i < processorSize; i++) {
Processor p = processors.get(i);
for (int j = 0; j < 4; j++) {
cacheLabels[i][j].setText("Cache " + j + " 数据: " + p.getCacheData(j) + " 状态: " + p.getCacheStates(j));
switch (p.getCacheStates(j)) {
case INVALID:
cacheLabels[i][j].setForeground(Color.GRAY);
break;
case SHARED:
cacheLabels[i][j].setForeground(Color.BLUE);
break;
case EXCLUSIVE:
cacheLabels[i][j].setForeground(Color.RED);
break;
}
}
}
for (int i = 0; i < 32; i++) {
memoryLabels[i].setText("Block " + i + " 数据: " + mainMemory.readData(i));
}
messageList.setListData(Processor.messages);
revalidate();
repaint();
}
}