THINK FIRST·CODE LATER

← All labs

Why buffering matters: counting system calls

Problem

An EdgeCampus sensor logger writes totalBytes bytes to a file. Each write system call costs syscallMicros microseconds, plus nanosPerByte nanoseconds per byte copied.

Input: first line totalBytes syscallMicros nanosPerByte, second line: one or more buffer sizes in bytes.

For each buffer size B print buffer B: calls C, time T ms where C = ⌈totalBytes / B⌉ and T = C × syscallMicros / 1000 + totalBytes × nanosPerByte / 1,000,000 (three decimals). Then print Speedup of best vs 1-byte writes: X (one decimal) — the time with B = 1 divided by the smallest time among the listed sizes (compute the 1-byte time even if 1 is not listed).

Input:

1000000 1 1
1 512 4096 65536

Output:

buffer 1: calls 1000000, time 1001.000 ms
buffer 512: calls 1954, time 2.954 ms
buffer 4096: calls 245, time 1.245 ms
buffer 65536: calls 16, time 1.016 ms
Speedup of best vs 1-byte writes: 985.2

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

os2-3-data-1.txt

1000000 1 1
1 512 4096 65536
What your program must print for os2-3-data-1.txt
buffer 1: calls 1000000, time 1001.000 ms
buffer 512: calls 1954, time 2.954 ms
buffer 4096: calls 245, time 1.245 ms
buffer 65536: calls 16, time 1.016 ms
Speedup of best vs 1-byte writes: 985.2

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < os2-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content os2-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.