Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

计算机架构代写 | CMPSC311 – Introduction to Systems Programming A4

计算机架构代写 | CMPSC311 – Introduction to Systems Programming A4



You just completed implementing mdadm and it is working. The software engineers who plan to build secure crypto wallet on top of your storage system have been torturing your storage system by throwing at it all sorts of I/O patterns, and they have been unable to find any inconsistency in your implementation. This is great, because now you have a working system, even though it may not be performant. As professor John Ousterhout of Stanford says, “the best performance improvement is the transition from nonworking state to working state”.

The software engineers are happy that your storage system is working correctly, but now they want you to make it fast as well. To this end, you are going to implement a block cache in mdadm. Caching is one of the oldest tricks in the book for reducing request latency by saving often used data in a faster (and smaller) storage medium than your main storage medium. Since we covered caching extensively in the class, we are skipping its details in this document. You must watch the lecture to understand what caching is, and how the least-recently used (LRU) algorithm that you are going to implement in this assignment works. Specifically, you will be implementing a look-aside cache with write-through write policy.


In general, caches store key and value pairs in a fast storage medium. For example, in a CPU cache, the key is the memory address, and the value is the data that lives at that address. When the CPU wants to access data at some memory address, it first checks to see if that address appears as a key in the cache; if it does, the CPU reads the corresponding data from the cache directly, without going to memory because reading data from memory is slow.

In a browser cache, the key is the URL of an image, and the value is the image file. When you visit a web site, the browser fetches the HTML file from the web server, parses the HTML file and finds the URLs for the images appearing on the web page. Before making another trip to retrieve the images from the web server, it first checks its cache to see if the URL appears as a key in the cache, and if it does, the browser reads the image from local disk, which is much faster than reading it over the network from a web server.

In this assignment you will implement a block cache for mdadm. In the case of mdadm, the key will be the tuple consisting of disk number and block number that identifies a specific block in JBOD, and the value will be the contents of the block. When the users of mdadm system issue mdadm_read call, your implementation of mdadm_read will first look if the block corresponding to the address specified by the user is in the cache, and if it is, then the block will be copied from the cache without issuing a slow JBOD_READ_BLOCK call to JBOD. If the block is not in the cache, then you will read it from JBOD and insert it to the cache, so that if a user asks for the block again, you can serve it faster from the cache.

Cache Implementation

Typically, a cache is an integral part of a storage system and it is not accessible to the users of the storage system. However, to make the testing easy, in this assignment we are going to implement cache as a separate module, and then integrate it to mdadm_read and mdadm_write calls.

Please take a look at cache.h file. Each entry in your cache is the following struct.

typedef struct {
  bool valid;
  int disk_num;


int block_num;
  uint8_t block[JBOD_BLOCK_SIZE];
  int access_time;
} cache_entry_t;

The valid field indicates whether the cache entry is valid. The disk_num and block_num fields identify the block that this cache entry is holding and the block field holds the data for the corresponding block. The access_time field stores when the cache element was last accessed—either written or read.

The file cache.c contains the following predefined variables.

static cache_entry_t *cache = NULL;
static int cache_size = 0;
static int clock = 0;
static int num_queries = 0;
static int num_hits = 0;

Now let’s go over the functions declared in cache.h that you will implement and describe how the above variables relate to these functions. You must look at cache.h for more information about each function.

  1. int cache_create(int num_entries);Dynamicallyallocatespacefornum_entriescache entries and should store the address in the cache global variable. It should also set cache_size to num_entries, since that describes the size of the cache and will also be used by other functions. Calling this function twice without an intervening cache_destroy call (see below) should fail. The num_entries argument can be 2 at minimum and 4096 at maximum.
  2. int cache_destroy(void);Freethedynamicallyallocatedspaceforcache,andshouldsetcache to NULL, and cache_size to zero. Calling this function twice without an intervening cache_- create call should fail.
  3. int cache_lookup(int disk_num, int block_num, uint8_t *buf);Lookuptheblock identified by disk_num and block_num in the cache. If found, copy the block into buf, which can- not be NULL. This function must increment num_queries global variable every time it performs a lookup. If the lookup is successful, this function should also increment num_hits global variable; it should also increment clock variable and assign it to the access_time field of the corresponding entry, to indicate that the entry was used recently. We are going to use num_queries and num_hits variables to compute your cache’s hit ratio.
  4. int cache_insert(int disk_num, int block_num, uint8_t *buf);Inserttheblock identified by disk_num and block_num into the cache and copy buf—which cannot be NULL—to the corresponding cache entry. Insertion should never fail: if the cache is full, then an entry should be overwritten according to the LRU policy using data from this insert operation. This function should also increment and assign clock variable to the access_time of the newly inserted entry.
  5. void cache_update(int disk_num, int block_num, const uint8_t *buf);Ifthe entry exists in cache, updates its block content with the new data in buf. Should also update the access_time if successful.
  6. bool cache_enabled(void); Returns true if cache is enabled. This will be useful when integrat- ing the cache to your mdadm_read and mdadm_write functions.