Storage 1: Caches, memory hierarchy, buffer cache, system calls

Overview

In this lecture, we introduce the memory hierarchy and discuss how files are read and written.

Full lecture notes on storageTextbook readings

File I/O via system calls

Write data to a file one byte at a time

    size_t n = 0;
    while (n < size) {
        int ch = fputc(buf[0], f);
        if (ch == EOF) {
            perror("write");
            exit(1);
        }
        ++n;
    }

Write data to a file one byte at a time II

    size_t n = 0;
    while (n < size) {
        ssize_t r = write(fd, buf, 1);
        if (r != 1) {
            perror("write");
            exit(1);
        }
        ++n;
    }

Write data to memory one byte at a time

Question 1

Why is membyte faster than osbyte?

Why is stdiobyte faster than osbyte?

Why is osbyte faster than syncbyte?

Storage hierarchy

Storage hierarchy

Expense of storage

Question 2

Historical costs of storage

Absolute costs ($/MB):

Year Memory (DRAM) Flash/SSD Hard disk
~1955 $411,000,000 $6,230
1970 $734,000 $260.00
1990 $148.20 $5.45
2003 $0.09 $0.305 $0.00132
2010 $0.019 $0.00244 $0.000073
2018 $0.0059 $0.00015 $0.000020

Relative costs (relative to hard disk storage in 2018):

Year Memory Flash/SSD Hard disk
~1955 20,500,000,000,000 312,000,000
1970 36,700,000,000 13,000,000
1990 7,400,000 270,000
2003 4,100 15,200 6.6
2010 950 122 3.6
2018 29.5 7.5 1

(Processor speed has also increased a lot—from 0.002 MIPS in 1951 to 750,000 MIPS now—but this is “only” 375,000,000x.)

Caching

Investigating the standard I/O cache