+ All Categories
Home > Documents > Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random...

Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random...

Date post: 01-Jan-2016
Category:
Upload: gavin-jacobs
View: 216 times
Download: 0 times
Share this document with a friend
64
Storage
Transcript
Page 1: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Storage

Page 2: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

The Memory Hierarchy

fastest, but small

Capacity in megabytes

under a microsecond, random access. Capacity in gigabytes

Access times in milliseconds, great

variability.

Unit of read/write = block or page,

typically 16Kb.

Capacities in terabytes.

Access times in seconds.

Most common: racks of tapes; newer

devices: CD ROM “juke boxes,” tape

“silo's.”

Capacities in petabytes.

Page 3: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Volatile vs. Non-Volatile

A storage device is nonvolatile if it can retain its data after a power shutoff.

Non-Volatile

Volatile

Page 4: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Computer Quantities

K KiloM Mega G GigaT TeraP Peta

102

Roughly:

202302402502

310610910

12101510

Page 5: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Big Data

• Lots of data is being collected and warehoused – Web data, e-commerce– purchases at department/

grocery stores– Bank/Credit Card

transactions– Sensor data– Phone-records?

• The amount of information is doubled every 3 Years.

Page 6: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Disks

•Platters with top and bottom surfaces rotate

around a spindle.

•2--30 surfaces.

•Rotation speed: 3600--7200 rpm.

•One head per surface.

•All heads move in and out in unison.

Page 7: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Tracks and sectors• Surfaces are covered with concentric

tracks. – Tracks at a common radius =

cylinder. – All data of a cylinder can be read

quickly, without moving the heads.

– A big magnetic disk: 216 = 65,536 cylinders

• Tracks are divided into sectors by unmagnetized gaps (which are 10% of track). – Typical track: 256 sectors. – Typical sector: 4096 bytes.

• Sectors are grouped into blocks. – Typical block: 16K = 4 4096 byte

sectors.

Page 8: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

MEGATRON 747 Disk Parameters

• There are 8 platters providing 16 surfaces.• There are 216, or 65,536 tracks per surface.• There are (on average) 28= 256 sectors per track.• There are 212=4096=4K bytes per sector.• Capacity = 16*216*28*212 = 240 = 1 TB

Page 9: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Disk Controller

1. Schedule the disk heads.

2. Buffer data in and out of disk.

3. Manage the "bad blocks'' so they are not used.

Page 10: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Disk access time• Latency of the disk (access time): The time to

bring block X, to main memory, from disk after the “read block” command is issued.

• Main components of access time are:– Seek time = time to move heads to proper

cylinder.

– Rotational delay = time for desired block to come under head.

– Transfer time = time during which the block passes under head.

Page 11: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Cause of rotational delay

On average, the desired sector will be about half On average, the desired sector will be about half way around the circle when the heads arrive at way around the circle when the heads arrive at

the cylinder.the cylinder.

Page 12: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

MEGATRON 747 Timing Example

1. To move the head assembly between cylinders takes 1 ms to start and stop, plus 1 additional millisecond for every 4000 cylinders traveled.

– Thus, moving from the innermost to the outermost track, a distance of 65,536 tracks, is about 17.38 milliseconds.

2. The disk rotates at 7200 rpm; i.e., it makes one rotation in 8.33 milliseconds.

3. Gaps occupy 10% of the space around a track.

Page 13: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

MIN time to read a 16,384-byte block• The minimum time, is just the transfer time. • Since there are 4096 bytes per sector on the Megatron

747, the block occupies 4 sectors. – The heads must therefore pass over 4 sectors and

the 3 gaps between them.

• Recall that: – the gaps represent 10% of the circle and sectors the remaining 90%, – i.e. 36 degrees are occupied by gaps and 324

degrees by the sectors. – there are 256 gaps and 256 sectors around the circle,

• So– a gap is 36/256 = 0.14 degrees, and– a sector is 324/256 = 1.265 degrees

• The total degrees covered by 3 gaps and 4 sectors is: 3*0.14+4*1.265 = 5.48 degrees

• The transfer time is thus (5.48/360) x 8.33 = 0.13 ms.

That is, the block might be on a

track over which the head is

positioned already, and the first sector of the block might be about to pass under the head.

That is, 5.48/360 is the fraction of a rotation

need to read the entire block, and 8.33 ms is the amount of time for a 360-degree rotation.

Page 14: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

MAX time to read a 16,384-byte block• Thus, the first thing the controller must do is

move the heads. As we observed above, the time to move the Megatron 747 heads across all cylinders is about 17.38 ms. This quantity is the seek time for the read.

• The worst thing that can happen when the heads arrive at the correct cylinder is that the beginning of the desired block has just passed under the head.

• Assuming we must read the block starting at the beginning, we have to wait essentially a full rotation, or 8.33 ms for the beginning of the block to reach the head again.

• Once that happens, we have only to wait an amount equal to the transfer time, 0.13 ms, to read the entire block.

• Thus, the worst-case latency is 17.38+8.33+0.13=25.84 ms.

The heads are positioned at the innermost cylinder, and the block we want to read is on the outermost cylinder (or vice versa).

Page 15: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

AVG time to read a 16,384-byte block• Two of the components of the latency are easy to compute:

– the transfer time is always 0.13 milliseconds and – the average rotational latency is the time to rotate the disk half way around,

or 4.17 milliseconds.

• What about seek time?

• If at cylinder 1 or cylinder 65,536, the average number of tracks to move is (1 + 2 +... + 65,536)/65,536, or about 32,000 tracks.

• If at the middle cylinder 32,000, the head is equally likely to move in or out, and either way, it will move on average about a quarter of the tracks

• So, what’s the average number of tracks to travel?

Page 16: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

AVG time to read a 16,384-byte block

n

inin

n

ii

22

i

n

Average number of cyls to travel, if the heads are currently positioned at cyl i.

Avg number of cyls to travel if the block is on

the left.

Probability the block is on the left

Avg number of cyls to

travel if the block is on the

right.

Probability the block is on the right

Page 17: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,
Page 18: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

AVG time to read a 16,384-byte block• Transfer time is always 0.13 milliseconds and

• Average rotational latency is the time to rotate the disk half way around, or 4.17 milliseconds.

• Average seek time is: 1+(65536/3)/4000 +1 = 7.46 ms• Total: 7.46 + 4.17 + 0.13 = 11.76 ms• In the following we will approximate the above by 11 ms.

Page 19: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Writing and Modifying Blocks• Writing same as reading, unless we verify written blocks.

– Takes another rotation of disk.

– Verify by observing parity check bits.

– Try again if fail (same for reading a block).

• Modifying a block requires: 1. Read the block into main memory.

2. Modify the block there.

3. Write the block back to disk.

Page 20: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Disk Access Model

Page 21: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Using Secondary Storage Effectively• In most studies of algorithms, one assumes the “RAM

model”: – Data is in main memory, – Access to any item of data takes as much time as any other.

• When implementing a DBMS, one must assume that the data does not fit into main memory.

• Often, the best algorithms for processing very large amounts of data differ from the best main-memory algorithms for the same problem.– There is a great advantage in choosing an algorithm that uses

few disk accesses, even if the algorithm is not very efficient when viewed as a main-memory algorithm.

Page 22: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

I/O model of computation• Disk I/O = read or write of a block is very expensive

compared with what is likely to be done with the block once it arrives in main memory. – Perhaps 1,000,000 machine instructions in the time to

do one random disk I/O.

Page 23: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Assumptions• One processor • One disk controller, and one disk. • The database itself is much too large to fit in main memory.

• Many users, and each user issues disk-I/O requests frequently, • Disk controller serving on a first-come-first-served basis. • Requests for a given user might appear random even if the table that a

user is reading is stored on a single cylinder of the disk.

• The disk is a Megatron 747, with 16K blocks and the timing characteristics determined before.

• In particular, the average time to read or write a block is about 11ms

Page 24: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Merge Sort• Common main -memory sorting algorithms don't look so

good when you take disk I/O's into account. Variants of Merge Sort do better.

• Merge = take two sorted lists and repeatedly chose the smaller of the “heads” of the lists (head = first of the unchosen). – Example: merge 1,3,4,8 with 2,5,7,9 = 1,2,3,4,5,7,8,9.

• Merge Sort based on recursive algorithm: – divide records into two parts;

– recursively mergesort the parts, and

– merge the resulting lists.

Page 25: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Merge-Sort in Main Memory

How many times each element is touched?

Page 26: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Two Phase, Multiway Merge SortMerge Sort still not very good in disk I/O model.

• log2n passes, so each record is read/written from disk log2n times.

• The secondary memory variant operates in a small number of passes; – in one pass every record is read into main memory once and written

out to disk once.

• 2PMMS: 2 reads + 2 writes per block. • Phase 1

1. Fill main memory with records.

2. Sort using favorite main memory sort.

3. Write sorted sublist to disk.

4. Repeat until all records have been put into one of the sorted lists.

Page 27: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Phase 2

• Manage the buffers as needed: • If an input block is exhausted, get the

next block from the same file.

• If the output block is full, write it to disk.

Page 28: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Phase 2 in words• Use one buffer for each of the sorted sublists and one buffer for an

output block.• Initially load input buffers with the first blocks of their respective sorted

lists. • Repeatedly run a competition among the first unchosen records of each

of the buffered blocks. • Move the record with the least key to the output block; it is now “chosen.”

• Manage the buffers as needed: • If an input block is exhausted, get the next block from the same file.

• If the output block is full, write it to disk.

Page 29: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,
Page 30: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy Example• 24 tuples with keys:

– 12 10 25 20 40 30 27 29 14 18 45 23 70 65 35 11 49 47 22 21 46 34 29 39

• Suppose 1 block can hold 2 tuples. • Suppose main memory (MM) can hold 4 blocks i.e. 8 tuples.

Phase 1. • Load 12 10 25 20 40 30 27 29 in MM, sort them and write

the sorted sublist: 10 12 20 25 27 29 30 40• Load 14 18 45 23 70 65 35 11 in MM, sort them and write

the sorted sublist: 11 14 18 23 35 45 65 70• Load 49 47 22 21 46 34 29 39 in MM, sort them and write

the sorted sublist: 21 22 29 34 39 46 47 49

Page 31: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 10 12 20 25 27 29 30 40

Sublist 2: 11 14 18 23 35 45 65 70

Sublist 3: 21 22 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1:

Input Buffer2:

Input Buffer3:

Output Buffer:

Sorted list:

Page 32: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 20 25 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 10 12

Input Buffer2: 11 14

Input Buffer3: 21 22

Output Buffer:

Sorted list:

Page 33: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 20 25 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 12

Input Buffer2: 11 14

Input Buffer3: 21 22

Output Buffer: 10

Sorted list:

Page 34: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 20 25 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 12

Input Buffer2: 14

Input Buffer3: 21 22

Output Buffer: 10 11

Sorted list:

Page 35: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 20 25 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 12

Input Buffer2: 14

Input Buffer3: 21 22

Output Buffer:

Sorted list: 10 11

Page 36: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 20 25 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1:

Input Buffer2: 14

Input Buffer3: 21 22

Output Buffer: 12

Sorted list: 10 11

Page 37: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 20 25

Input Buffer2: 14

Input Buffer3: 21 22

Output Buffer: 12

Sorted list: 10 11

Page 38: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 20 25

Input Buffer2:

Input Buffer3: 21 22

Output Buffer: 12 14

Sorted list: 10 11

Page 39: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 27 29 30 40

Sublist 2: 18 23 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 20 25

Input Buffer2:

Input Buffer3: 21 22

Output Buffer:

Sorted list: 10 11 12 14

Page 40: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Toy example (continued)Phase 2.

Sublist 1: 27 29 30 40

Sublist 2: 35 45 65 70

Sublist 3: 29 34 39 46 47 49

Main Memory (4 buffers)

Input Buffer1: 20 25

Input Buffer2: 18 23

Input Buffer3: 21 22

Output Buffer:

Sorted list: 10 11 12 14

We continue in this way until the sorted sublists are finished and we get the whole sorted list of tuples.

Page 41: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Real Life Example• 10,000,000 tuples of 160 bytes ≈ 1.6 GB file.

– Stored on Megatron 747 disk, with 16K blocks, each holding 100 tuples

– Entire file takes 100,000 blocks

• 100MB available main memory– The number of blocks that can fit in 100MB of memory (which,

recall, is really 100 x 220 bytes), is

100 x 220 / 214, or 6400 blocks 1/16th of file.

Page 42: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Analysis – Phase 1• 6400 of the 100,000 blocks will fill main memory. • We thus fill memory 100,000/6,400=16 times, sort the

records in main memory, and write the sorted sublists out to disk.

• How long does this phase take? • We read each of the 100,000 blocks once, and we write

100,000 new blocks. Thus, there are 200,000 disk I/O's for 200,000*11ms = 2200 seconds, or 37 minutes.

Avg. time for reading a

block.

Page 43: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Analysis – Phase 2• Every block holding records from one of the sorted lists is

read from disk exactly once. – Thus, the total number of block reads is 100,000 in the

second phase, just as for the first.

• Likewise, each record is placed once in an output block, and each of these blocks is written to disk once. – Thus, the number of block writes in the second phase is also

100,000.

• We conclude that the second phase takes another 37 minutes.

• Total: Phase 1 + Phase 2 = 74 minutes.

Page 44: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

How Big Should Blocks Be?• We have assumed a 16K byte block in our analysis. • Would a larger block size be advantageous?

• If we doubled the size of blocks, we would halve the number of disk I/O's. But, how much a disk I/O would cost in such a case?

• Recall it takes about 0.13 ms for transfer time of a 16K block and 11.63 ms for average seek time and rotational latency.

• Now, the only change in the time to access a block would be that the transfer time increases to 0.13*2=0.26 ms, i.e. only slightly more than before.

• We would thus approximately halve the time the sort takes.

Page 45: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Another example: Block Size = 512K• For a block size of 512K the transfer time is 0.13*32=4.16 milliseconds.

• Average block access time would be

11.63 + 4.17 = 15.8 ms (as opposed to the 11ms we had before)

• However, now a block can hold 100*32 = 3200 tuples and the whole table will be 10,000,000 / 3200 = 3125 blocks (as opposed to 100,000 blocks we had before).

• Thus, we would need only 3125 * 2 * 2 disk I/Os for 2PMMS for a total time of 3125 * 2 * 2 * 15.8 = 197,500ms or about 3.29 min.

• Speedup: 74 / 3.29 = 22.5 fold.

Page 46: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Reasons to limit the block size1. First, we cannot use blocks that cover several tracks

effectively.

2. Second, small relations would occupy only a fraction of a block, so large blocks would waste space on the disk.

3. Third, the larger the blocks are, the fewer records we can sort by 2PMMS (see next slide).

• Nevertheless, as machines get more memory and disks more capacious, there is a tendency for block sizes to grow.

Page 47: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

How many records can we sort? 1. Block size is B bytes.2. Main memory available for buffering blocks is M bytes.3. Record is R bytes.

• Number of main memory buffers = M/B blocks• We need one output buffer, so we can actually use (M/B)-1 input buffers.

• How many sorted sublists makes sense to produce? – (M/B)-1.

• What’s the total number of records we can sort?– Each time we fill in the memory with M/R records. – Hence, we are able to sort (M/R)*[(M/B)-1] or approximately M2/RB.

If we use the parameters in the example about 2PMMS we have:M = 100MB = 100 * 220 BytesR = 160 Bytes = 10 * 24 BytesB = 16 KB = 16 * 210 Bytes

So, M2/RB = (100 * 220)2 / (160 * 16*210) ≈ 4 * 230 = 4 GB

Page 48: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Sorting larger relations• If our relation is bigger, then, we can use 2PMMS to create

sorted sublists of M2/RB records.• Then, in a third pass we can merge (M/B)-1 of these sorted

sublists. • Thus, the third phase let’s us sort

• [(M/B)-1]*[M2/RB] M3/RB2 records

• For our example, the third phase let’s us sort 75 trillion records occupying 7500 Petabytes!!

Page 49: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Sometimes we need many passes in phase 2

• R = 12 tuples, tuple = 1 byte,block size = 1 tuple, main memory size = 3 blocks,

(A toy example to illustrate the point)

# of RUNs main memory blocks (buffers) (Here: 4 3)

Page 50: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

How many passes are needed?

• Block size B bytes, • Main memory M bytes,• Tuple R bytes, relation N tuples File/relation size = N×R Bytes• # of main memory buffers = M/B blocks• We need one output buffer, so we can actually use

(M/B)-1 input buffers• Phase 1 gives r = NR/M runs (sorted sublists)• We can merge (M/B)-1 runs• After pass 1, we have r/((M/B) -1) runs• After pass 2, we have r/((M/B) -1)2 runs• After pass k, we have r/((M/B) -1)k runs

Page 51: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

• We are finished when there is only one run left, i.e.,

r/((M/B) -1)k = 1 NR/M = ((M/B) -1)k

• In our toy example:• Total number of disk I/O = (2k+1)(NR/B) (we don’t count the last write, it might be the input to another operation)

• In our toy example, total I/O = 5 ×12 = 60

2)3/12(log2 k

kMNRBM )/(log 1/

How many passes are needed …

Page 52: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,
Page 53: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,
Page 54: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

A Tape algorithm (Knuth, Vol. 3, 1973)

Page 55: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

A Tape algorithm (Knuth, Vol. 3, 1973)• Balanced 2-way merge:

– Use four "working tapes" – During the first phase, sorted runs produced by Main Memory

sorting are placed alternately on Tapes 1 and 2,– Then Tapes 1 and 2 are rewound to their beginnings, and we merge

the runs from these tapes, obtaining new runs which are twice as long as the original ones;

– The new runs are written alternately on Tapes 3 and 4 as they are being formed.

– Then all tapes are rewound, and the contents of Tapes 3 and 4 are merged into quadruple-length runs recorded alternately on Tapes 1 and 2.

• The process continues, doubling the length of runs each time, until only one run is left (namely the entire sorted file).

• If S runs were produced during the internal sorting phase this balanced 2-way merge procedure makes k = Iog S merging passes over all the data.

Page 56: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Example• 5000 records are to be sorted with a main memory capacity of 1000

Tape 1: R1 . . . R1000; R2001 . . . R3000; R4001 . . . R5000

Tape 2: R1001 . . R2000; R3001 . . . R4000

Tape 3: (empty)

Tape 4: (empty)

Tape 3: R1 . . . R2000; R4001 . . . R5000

Tape 4: R2001 . . . R4000

Tape 1: R1 . . . R4000

Tape 2: R4001 . . . R5000

Page 57: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

The Megatron 747 Example• Let’s adapt the previous algorithm for disk (consider 4

disks). (main mem. 100 MB, 16K block size, main mem 6400 blocks, 16 sorted runs in phase 1)

– Now we don’t have rewind time. • What’s the time to read a full load of 100MB memory?

– Time to transfer 6,400 blocks at 0.13 ms/block = .832 sec• What’s the time for a sequential pass?

– About 16 * .832 = 13 sec• Producing sorted runs would take 13*2 = 26 sec (ignoring

the time to sort…)• What about the merge?

– It would take log 16 = 4 passes• Total: 26 + 4*2*13 = 130 sec or about 2 mins.

– Might be a bit optimistic, though. We are completely ignoring the time to sort.

Page 58: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Improving the Running Time of 2PMMS• Here are some techniques that sometimes make

secondary memory algorithms more efficient:

• Group blocks by cylinder. • Several smaller disks (instead of one big disk). • “Prefetching” or “double buffering.”

Page 59: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

CylindrificationIf we are going to read or write blocks in a known order, place them by

cylinder, so once we reach that cylinder we can read block after block, with no seek time or rotational latency

(Assuming this is the only process running in the machine that needs disk access).

Application to Phase 1 of 2PMMS 1. Initially, records on 103 consecutive cylinders. 2. Load main memory from 6 consecutive cylinders.

Order of blocks read is unimportant, so only time besides transfer time is one random seek and 6 1 cylinder seeks (neglect). Time to transfer 6,400 blocks at 0.13 ms/block = .832 sec.

3. Write each sorted list onto 6 consecutive cylinders, so write time is also about .832 sec.

Total for Phase1 is about .832*2*(103/6)=28.56 sec

But, Phase 2 …?

Page 60: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Cylindrification – Phase 2• Storage by cylinders does not help in the second phase.

• Blocks are read from the fronts of the sorted lists in an order that is determined by which list next exhausts its current block.

• Output blocks are written one at a time, interspersed with block reads

• Thus, the second phase will still take 37 min.

• We have cut the sorting time almost half, but cannot do better by cylindrification alone.

Page 61: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Multiple Disks• Use several disks with independent heads• Example: Instead of a large disk of 1TB (Megatron 747), let’s use 4 smaller

disks of 250GB each (Megatron 737)• We divide the given records among the four disks; the data will occupy 103

adjacent cylinders on each disk.

• Phase 1 of 2PMMS: – Load in main memory from all 4 disks in parallel. Speedup is 4-fold.– We also write in parallel the result of the first phase, distributing each sorted

list onto the four disks, occupying about 6 adjacent cylinders on each disk.• Hence, speed-up is 4-fold for phase 1, i.e. 28.56/4 = 7.14 sec.

1 2 3 45 6 7 8

48

37

26

15

Page 62: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Multiple Disks – Phase 2• Phase 2:

– Use 4 output buffers, one per disk, and and cut writing time in about 1/4.

– What about the reading part? – If we wait until an exhausted input buffer is completely filled in

again, then there is no speedup of this part. – However, why wait? We could start using a buffer as soon as the

first records are in. If so, then several lists might be having their blocks loaded into memory at the same time.

• As long as they are on separate disks, we can perform several block reads at the same time. Potential for 4-fold speedup.

– In practice, we get 2-3 fold speedup for Phase 2.

• Total time 7.14/60sec + 37min/3 12.5 min

Page 63: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Prefetching and large scale buffering• If we have extra space for main memory buffers, consider

loading buffers in advance of need. Similarly, keep output blocks buffered in main memory until it is convenient to write them to disk.

• Example: Phase 2 of 2PMMS • With 600Mb of main memory, we can afford to buffer 2

cylinders for each of 16 sublists and for the output. • Consume one cylinder for each sublist while the other is

being loaded from main memory. • Similarly, write one output cylinder while the other is being

constructed. • Thus, seek and rotational latency are almost eliminated,

lowering total read and write times to about 26sec each.

Page 64: Storage. The Memory Hierarchy fastest, but small Capacity in megabytes under a microsecond, random access. Capacity in gigabytes Access times in milliseconds,

Recommended