⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cahe.java

📁 主存--辅存模拟 主存--cash模拟
💻 JAVA
字号:
package cahe;

import java.util.Vector;

public class Cahe
{
	int				memoryNum;									//主存块数
	int				memoryContent	;						//主存容量
	int				part			;						//块大小
	int				caheNum;									//cahe块数
	int				caheContent		;						//cahe容量
	int				team		;						//组数
	Vector<Integer>	cahe			= new Vector<Integer>();	//cahe空间
	Vector<Vector>	in				= new Vector<Vector>();
	Vector<String>	inf				= new Vector<String>();
	Vector<String>	inf1			= new Vector<String>();
	Vector<Integer>	vAddress		= new Vector<Integer>();
	int				targetNum;									//命中次数
	float			targetRate;
	AllLinkMap		all;
	DirectLinkMap	direct;
	GroupLinkMap	group;
	LRU				lru;										//LRU算法
	int				time[];									//计数器
	
	public Cahe(int bigContent, int smallContent, int part, int team,
			Vector<Integer> vAddress)
	{
		this.memoryContent = bigContent;
		this.caheContent = smallContent;
		this.part = part;
		this.team = team;
		this.vAddress = vAddress;
		all = new AllLinkMap(part, memoryContent, caheContent);
		direct = new DirectLinkMap(part, memoryContent, caheContent);
		group = new GroupLinkMap(part, memoryContent, caheContent, team);
	}
	
	//分析过程
	public Vector<Vector> anylzeAddress(int map, int replace)
	{
		switch (map)
		{
			case 0:
				allLinkReplace(replace);
				break;
			case 1:
				directLinkReplace(replace);
				break;
			case 2:
				groupLinkReplace(replace);
				break;
		}
		countTargetRate();//计算命中率
		in.clear();
		in.add(inf);
		in.add(inf1);
		return in;
	}
	
	//全相联映射
	private void allLinkReplace(int replace)
	{
		this.memoryNum = all.getMemoryNum();
		this.caheNum = all.getCaheNum();
		if (replace == 0)//LRU
		{
			inf.clear();
			inf1.clear();
			targetNum = 0;
			lru = new LRU(caheNum);
			this.time = lru.getTime();
			for (int i = 0; i < vAddress.size(); i++)//分析地址流
			{
				int temp = vAddress.get(i);//取地址流的下一个元素
				int temp1 = 0;
				if ((temp1 = all.isTarget(temp, cahe)) != -1)//命中
				{
					lru.cleanTime(time, temp1);//清空计数器
					lru.addTime(cahe, time, temp1, 0, cahe.size() - 1);//其余计数器的加1
					targetNum++;
					printCahe();
					printTarget(temp);
				}
				else if ((temp1 = all.map(temp, cahe)) != -1)//调进
				{
					cahe.add(temp);
					lru.addTime(cahe, time, -1, 0, cahe.size() - 1);
					printCahe();
					printIn(temp);
				}
				else
				//替换
				{
					int partNum = lru.getRelpacePart(time, 0, cahe.size() - 1);
					String str = "";
					str = str + "   替换" + cahe.get(partNum);
					cahe.set(partNum, temp);
					lru.cleanTime(time, partNum);//清空计数器
					lru.addTime(cahe, time, partNum, 0, cahe.size() - 1);//其余计数器的加1
					printCahe();
					
					inf.add(str);
				}
				System.out.println();
			}
		}
		else if (replace == 1)//FIFO
		{
			inf.clear();
			inf1.clear();
			targetNum = 0;
			FIFO ff = new FIFO();
			for (int i = 0; i < vAddress.size(); i++)//分析地址流
			{
				int temp = vAddress.get(i);//取地址流的下一个元素
				int temp1 = 0;
				if ((temp1 = all.isTarget(temp, cahe)) != -1)//命中
				{
					targetNum++;
					printCahe();
					printTarget(temp);
				}
				else if ((temp1 = all.map(temp, cahe)) != -1)//调进
				{
					ff.comeIn(temp1);
					cahe.add(temp);
					printCahe();
					printIn(temp);
				}
				else
				//替换
				{
					int partNum = ff.goOut();
					String str = "";
					str = str + "   替换" + cahe.get(partNum);
					cahe.set(partNum, temp);
					ff.comeIn(cahe.indexOf(temp));
					printCahe();
					
					inf.add(str);
				}
			}
		}
	}
	
	//直接映射
	private void directLinkReplace(int replace)
	{
		this.memoryNum = direct.getMemoryNum();
		this.caheNum = direct.getCaheNum();
		cahe.setSize(caheNum);
		inf.clear();
		inf1.clear();
		targetNum = 0;
		for (int i = 0; i < vAddress.size(); i++)//分析地址流
		{
			int temp = vAddress.get(i);//取地址流的下一个元素
			int temp1 = 0;
			if ((temp1 = direct.isTarget(temp, cahe)) != -1)//命中
			{
				targetNum++;
				printCahe();
				printTarget(temp);
			}
			else
			{
				temp1 = direct.map(temp, cahe);
				if (cahe.get(temp1) == null)//调进
				{
					cahe.set(temp1, temp);
					printCahe();
					printIn(temp);
				}
				else
				//替换
				{
					String str = "";
					str = str + "   替换" + cahe.get(temp1);
					cahe.set(temp1, temp);
					printCahe();
					inf.add(str);
				}
			}
		}
	}
	
	//组相联映射
	private void groupLinkReplace(int replace)
	{
		this.memoryNum = group.getMemoryNum();
		this.caheNum = group.getCaheNum();
		cahe.setSize(caheNum);
		if (replace == 0)//LRU
		{
			inf.clear();
			inf1.clear();
			targetNum = 0;
			lru = new LRU(caheNum);
			this.time = lru.getTime();
			for (int i = 0; i < vAddress.size(); i++)//分析地址流
			{
				int temp = vAddress.get(i);//取地址流的下一个元素
				int min = group.getMinNum();
				int max = group.getMaxNum();
				int temp1 = 0;
				if ((temp1 = group.isTarget(temp, cahe)) != -1)//命中
				{
					lru.cleanTime(time, temp1);//清空计数器
					lru.addTime(cahe, time, temp1, min, max);//其余计数器的加1
					targetNum++;
					printCahe();
					printTarget(temp);
				}
				else if ((temp1 = group.map(temp, cahe)) != -1)//调进
				{
					cahe.set(temp1, temp);
					lru.addTime(cahe, time, -1, min, max);
					printCahe();
					printIn(temp);
				}
				else
				//替换
				{
					int partNum = lru.getRelpacePart(time, min, max);
					String str = "";
					str = str + "   替换" + cahe.get(partNum);
					cahe.set(partNum, temp);
					lru.cleanTime(time, partNum);//清空计数器
					lru.addTime(cahe, time, partNum, min, max);//其余计数器的加1
					printCahe();
					
					inf.add(str);
				}
			}
		}
		else if (replace == 1)//FIFO
		{
			inf.clear();
			inf1.clear();
			targetNum = 0;
			FIFO ff[] = new FIFO[team];
			for (int j = 0; j < team; j++)
			{
				ff[j] = new FIFO();
			}
			for (int i = 0; i < vAddress.size(); i++)//分析地址流
			{
				int temp = vAddress.get(i);//取地址流的下一个元素
				int temp1 = 0;
				if ((temp1 = group.isTarget(temp, cahe)) != -1)//命中
				{
					targetNum++;
					printCahe();
					printTarget(temp);
				}
				else if ((temp1 = group.map(temp, cahe)) != -1)//调进
				{
					int number = group.caheTeamNum(temp);
					ff[number].comeIn(temp1);
					cahe.add(temp);
					printCahe();
					printIn(temp);
				}
				else
				//替换
				{
					int number = group.caheTeamNum(temp);
					int partNum = ff[number].goOut();
					String str = "";
					str = str + "   替换" + cahe.get(partNum);
					cahe.set(partNum, temp);
					ff[number].comeIn(cahe.indexOf(temp));
					printCahe();
					
					inf.add(str);
				}
			}
		}
	}
	
	//打印命中信息
	public void printTarget(int temp)
	{
		String str = "";
		for (int q = 0; q < (caheNum - cahe.size()) * 2; q++)//对齐输出列
		{
			str = str + " ";
		}
		str = str + "   命中" + temp;
		inf.add(str);
	}
	
	//打印调进信息
	public void printIn(int temp)
	{
		String str = "";
		for (int q = 0; q < (caheNum - cahe.size()) * 2; q++)//对齐输出列
		{
			str = str + " ";
		}
		str = str + "   调进" + temp;
		inf.add(str);
	}
	
	//计算命中率
	public void countTargetRate()
	{
		targetRate = ((float) targetNum) / (vAddress.size());
	}
	
	//打印Cahe里面内容
	public void printCahe()
	{
		String str = "";
		for (int i = 0; i < cahe.size(); i++)
		{
			if (cahe.get(i) == null)
			{
				str = str + "  ";
			}
			else
			{
				str = str + cahe.get(i) + " ";
			}
		}
		inf1.add(str);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -