📄 disk_cache.c
字号:
/* * Copyright (c) 2002 The Board of Trustees of the University of Illinois and * William Marsh Rice University * Copyright (c) 2002 The University of Utah * Copyright (c) 2002 The University of Notre Dame du Lac * * All rights reserved. * * Based on RSIM 1.0, developed by: * Professor Sarita Adve's RSIM research group * University of Illinois at Urbana-Champaign and William Marsh Rice University * http://www.cs.uiuc.edu/rsim and http://www.ece.rice.edu/~rsim/dist.html * ML-RSIM/URSIM extensions by: * The Impulse Research Group, University of Utah * http://www.cs.utah.edu/impulse * Lambert Schaelicke, University of Utah and University of Notre Dame du Lac * http://www.cse.nd.edu/~lambert * Mike Parker, University of Utah * http://www.cs.utah.edu/~map * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal with the Software without restriction, including without * limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to * whom the Software is furnished to do so, subject to the following * conditions: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimers. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimers in the * documentation and/or other materials provided with the distribution. * 3. Neither the names of Professor Sarita Adve's RSIM research group, * the University of Illinois at Urbana-Champaign, William Marsh Rice * University, nor the names of its contributors may be used to endorse * or promote products derived from this Software without specific prior * written permission. * 4. Neither the names of the ML-RSIM project, the URSIM project, the * Impulse research group, the University of Utah, the University of * Notre Dame du Lac, nor the names of its contributors may be used to * endorse or promote products derived from this software without specific * prior written permission. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS WITH THE SOFTWARE. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include "sim_main/simsys.h"#include "sim_main/util.h"#include "Processor/simio.h"#include "Caches/system.h"#include "IO/scsi_disk.h"int DISK_cache_newsegment (SCSI_DISK*);void DISK_cache_touch_segment (SCSI_DISK*, int);/*===========================================================================*//* Create and initialize disk cache. Allocate N segments and reset start/end *//* address, LRU counter and write flags. *//*===========================================================================*/void DISK_cache_init(SCSI_DISK *pdisk){ int n; pdisk->cache = (DISK_CACHE_SEGMENT*)malloc(sizeof(DISK_CACHE_SEGMENT) * pdisk->cache_segments); for (n = 0; n < pdisk->cache_segments; n++) { pdisk->cache[n].start_block = 0; pdisk->cache[n].end_block = 0; pdisk->cache[n].lru = 0; pdisk->cache[n].write = 0; pdisk->cache[n].committed = 0; }}/*===========================================================================*//* Return segment with the highest sector number in the range start_block: *//* start_block + length. For reads look only through read segments. Possibly *//* return segment that does not match but to which the range can be *//* appended. For writes, check only committed write segments that overlap *//* with the range. If none is found, get a new segment. *//*===========================================================================*/int DISK_cache_getsegment(SCSI_DISK *pdisk, int start_block, int length, int write){ int n; int segment_size = (pdisk->cache_size * 1024) / (pdisk->cache_segments * SCSI_BLOCK_SIZE); /* read request can match a segment that ends at start_block-1 ------------*/ if (!write) for (n = 0; n < pdisk->cache_segments; n++) { if ((start_block >= pdisk->cache[n].start_block) && (start_block < pdisk->cache[n].end_block) && (!pdisk->cache[n].write)) { DISK_cache_touch_segment(pdisk, n); return(n); } if ((start_block >= pdisk->cache[n].start_block) && (start_block <= pdisk->cache[n].end_block) && (pdisk->cache[n].end_block - pdisk->cache[n].start_block < segment_size) && (!pdisk->cache[n].write)) { DISK_cache_touch_segment(pdisk, n); return(n); } if ((start_block >= pdisk->cache[n].start_block) && (start_block < pdisk->cache[n].end_block) && (start_block + length > pdisk->cache[n].end_block) && (!pdisk->cache[n].write)) { DISK_cache_touch_segment(pdisk, n); length -= (pdisk->cache[n].end_block - start_block); start_block = pdisk->cache[n].end_block; n = -1; continue; } } else /* write request can match only overlapping, committed write segments ---*/ for (n = 0; n < pdisk->cache_segments; n++) { if ((start_block >= pdisk->cache[n].start_block) && (start_block < pdisk->cache[n].end_block) && (pdisk->cache[n].write && pdisk->cache[n].committed)) { DISK_cache_touch_segment(pdisk, n); return(n); } } n = DISK_cache_newsegment(pdisk); /* no segment found: get a new one */ DISK_cache_touch_segment(pdisk, n); /* make new segment the youngest */ return(n);}/*===========================================================================*//* Get a new segment: look for oldest non-write segment and return its ID *//*===========================================================================*/int DISK_cache_newsegment(SCSI_DISK *pdisk){ int n, age = 0; for (n = 0; n < pdisk->cache_segments; n++) { if ((age == pdisk->cache[n].lru) && (pdisk->cache[n].write)) { age++; n = -1; continue; } if (age == pdisk->cache[n].lru) { pdisk->cache[n].write = 0; pdisk->cache[n].committed = 0; pdisk->cache[n].start_block = 0; pdisk->cache[n].end_block = 0; return(n); } } return(-1); /* shouldn't get here */}/*===========================================================================*//* Determine cache hit for a read: *//* look for segment that contains start_block, return full_hit if range is *//* is completely within that segment, otherwise adjust start_block and *//* length and search again. Return partial hit if a segment contains only *//* a subset of the block range, or return miss otherwise. *//*===========================================================================*/int DISK_cache_hit(SCSI_DISK *pdisk, int start_block, int length){ int n, rc = DISK_CACHE_MISS; for (n = 0; n < pdisk->cache_segments; n++) { if ((pdisk->cache[n].write) && (!pdisk->cache[n].committed)) continue; if ((start_block >= pdisk->cache[n].start_block) && (start_block < pdisk->cache[n].end_block) && (start_block + length > pdisk->cache[n].end_block)) { rc = DISK_CACHE_HIT_PARTIAL; length -= (pdisk->cache[n].end_block - start_block); start_block = pdisk->cache[n].end_block; n = -1; continue; } if ((start_block >= pdisk->cache[n].start_block) && (start_block < pdisk->cache[n].end_block) && (start_block + length <= pdisk->cache[n].end_block)) return(DISK_CACHE_HIT_FULL); } return(rc);}/*===========================================================================*//* Insert blocks into cache segment *//* append blocks if first block is at end of segment; *//* append partial if start is within segment limits but length exceeds *//* last block; overwrite otherwise *//* make segment the youngest segment by setting its LRU value to the highest *//* and decrementing the LRU value of all other segments with values higher *//* then the old age of that segment *//*===========================================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -