Title: CS 213 Introduction to Computer Systems Thinking Inside the Box
1CS 213Introduction to Computer SystemsThinking
Inside the Box
- Instructor Brian M. Dennis
- bmd_at_cs.northwestern.edu
- Teaching Assistant Dong Lu
- donglu_at_cs.northwestern.edu
2Todays Topics
- Introductions
- Course Operation
- Course Overview
- 5 Truths of CS
- An alternative view
3Introductions
4Course Operation
- Labs
- Linux/Intel based
- Done in pairs
- Homework
- Mostly pencil paper
- Done individually
- Meeting Times
- 200 - 320 M, W
- Office Hours
- BMD 130 - 3, Th
- Dong 3-4, T
- Grading
- 50 Labs
- 10 HW
- 20 Midterm
- 20 Final
- Lose 10 / late day
5Course Overview
- Goals
- Demystify the machine (in detail)
- Expose the abstraction hierarchy
- Prepare you for systems programming
- Improve programming skills
- Layout
- Data rep (1 week)
- Machine arch program execution (2 weeks)
- Memory hierarchy I (1 week)
- Program execution II (1 week)
- Virtual memory (1.5 weeks)
- I/O (1.5 weeks)
- Concurrent systems (1 week)
6Course Overview
- How (in lecture)?
- System overview
- Physics to distributed systems (soup to nuts)
- Closely Investigate
- Data representation
- Machine model
- Memory hierarchy
- Program execution
- Executable format
- UNIX systems programming
- High level discussions
- Physics to logic
- I/O
- Concurrent systems
7Course Overview
- How (in practice)?
- Learn by doing
- Write some assembly
- Nitty gritty of machine model
- Use UNIX tools to investigate programs
- Static properties
- Dynamic behavior\
- Write C programs
- Not C!
8Course Overview
- Philosophy
- According to authors
- Abstraction good
- But dont lose sight
- 5 Great Realities
- C numbers aint Math numbers
- Every programmer needs some assembly
- Memory organization matters
- Performance details matter
- Computers communicate
9C Numbers Aint Math Numbers
10Great Reality 1
- Ints are not Integers, Floats are not Reals
- Examples
- Is x2 0?
- Floats Yes!
- Ints
- 40000 40000 --gt 1600000000
- 50000 50000 --gt ??
- Is (x y) z x (y z)?
- Unsigned Signed Ints Yes!
- Floats
- (1e20 -1e20) 3.14 --gt 3.14
- 1e20 (-1e20 3.14) --gt ??
11Bump Out Show ics math
12Computer Arithmetic
- Does not generate random values
- Arithmetic operations have important mathematical
properties - Cannot assume usual properties
- Due to finiteness of representations
- Integer operations satisfy ring properties
- Commutativity, associativity, distributivity
- Floating point operations satisfy ordering
properties - Monotonicity, values of signs
- Observation
- Need to understand which abstractions apply in
which contexts - Important issues for compiler writers and serious
application programmers
13Every Programmer NeedsSome Assembly
14Great Reality 2
- Youve got to know assembly
- Chances are, youll never write program in
assembly - Compilers are much better more patient than you
are
- Understanding assembly key to machine-level
execution model - Behavior of programs in presence of bugs
- High-level language model breaks down
- Tuning program performance
- Understanding sources of program inefficiency
- Implementing system software
- Compiler has machine code as target
- Operating systems must manage process state
15Assembly Code Example
- Time Stamp Counter
- Special 64-bit register in Intel-compatible
machines - Incremented every clock cycle
- Read with rdtsc instruction
- Application
- Measure time required by procedure
- In units of clock cycles
double t start_counter() P() t
get_counter() printf("P required f clock
cycles\n", t)
16Code to Read Counter
- Write small amount of assembly code using GCCs
asm facility - Inserts assembly code into machine code generated
by compiler
17Code to Read Counter
static unsigned cyc_hi 0 static unsigned
cyc_lo 0 / Set hi and lo to the high and
low order bits of the cycle counter. / void
access_counter(unsigned hi, unsigned lo)
asm("rdtsc movl edx,0 movl eax,1"
"r" (hi), "r" (lo) "edx", "eax")
18/ Record the current value of the cycle counter.
/ void start_counter() access_counter(cyc_
hi, cyc_lo) / Number of cycles since the
last call to start_counter. / double
get_counter() unsigned ncyc_hi, ncyc_lo
unsigned hi, lo, borrow / Get cycle
counter / access_counter(ncyc_hi,
ncyc_lo) / Do double precision subtraction
/ lo ncyc_lo - cyc_lo borrow lo gt
ncyc_lo hi ncyc_hi - cyc_hi - borrow
return (double) hi (1 ltlt 30) 4 lo
19Measuring Time
- Trickier than it Might Look
- Many sources of variation
- Example
- Sum integers from 1 to n
- n Cycles Cycles/n
- 100 961 9.61
- 1,000 8,407 8.41
- 1,000 8,426 8.43
- 10,000 82,861 8.29
- 10,000 82,876 8.29
- 1,000,000 8,419,907 8.42
- 1,000,000 8,425,181 8.43
- 1,000,000,000 8,371,2305,591 8.37
20Bump Out Run Code
21Memory Organization Matters
22Great Reality 3
- Memory Matters
- Memory is not unbounded
- It must be allocated and managed
- Many applications are memory dominated
- Memory referencing bugs especially pernicious
- Effects are distant in both time and space
- Memory performance is not uniform
- Cache and virtual memory effects can greatly
affect program performance - Adapting program to characteristics of memory
system can lead to major speed improvements
23(No Transcript)
24Memory Referencing Bug Example
(Linux version gives correct result, but
implementing as separate function gives
segmentation fault.) Allegedly
main () long int a2 double d 3.14
a2 1073741824 / Out of bounds reference /
printf("d .15g\n", d) exit(0)
25Memory Referencing Errors
- C and C do not provide any memory protection
- Out of bounds array references
- Invalid pointer values
- Abuses of malloc/free
- Can lead to nasty bugs
- Whether or not bug has any effect depends on
system and compiler - Action at a distance
- Corrupted object logically unrelated to one being
accessed - Effect of bug may be first observed long after it
is generated
- How can I deal with this?
- Program in Java, Lisp, or ML
- Understand what possible interactions may occur
- Use or develop tools to detect referencing errors
26Memory Performance Example
- Implementations of Matrix Multiplication
- Multiple ways to nest loops
/ ijk / for (i0 iltn i) for (j0 jltn
j) sum 0.0 for (k0 kltn k)
sum aik bkj cij sum
/ jik / for (j0 jltn j) for (i0 iltn
i) sum 0.0 for (k0 kltn k)
sum aik bkj cij sum
27Matmult Performance (Alpha 21164)
Too big for L1 Cache
Too big for L2 Cache
jki
kij
kji
28Blocked matmult perf (Alpha 21164)
29Performance Details Matter
30Great Reality 4
- Theres more to performance than asymptotic
complexity - Constant factors matter too!
- Easily see 101 performance range depending on
how code written - Must optimize at multiple levels algorithm, data
representations, procedures, and loops
- Alan Perlis
- "Lisp programmers know the value of everything
and the cost of nothing" - Must understand system to optimize performance
- How programs compiled and executed
- How to measure program performance and identify
bottlenecks - How to improve performance without destroying
code modularity and generality
31Computers Communicate
32Great Reality 5
- Computers do more than execute programs
- They need to get data in and out
- I/O system critical to program reliability and
performance
- They communicate with each other over networks
- Many system-level issues arise in presence of
network - Concurrent operations by autonomous processes
- Coping with unreliable media
- Cross platform compatibility
- Complex performance issues
33A Different View
- The science of computer science is abstraction
design - Understand complex systems
- Design simplicity to hide complexity
- Engineering
- Making it work
- Making it fast
- Make it scalable
- Make it reliable
- Make it maintainable
- Make it usable
- Abstraction goes a long way, but sometimes it
breaks - Bugs
- Performance
- Poor design
- To fix?
- Sometimes need to Get under the hood
34The Machine or The System Architecture
Instruction Set Architecture
Microarchitecture
Microprocessors
Memory Systems
Buses
NICs,
Disk Systems
Compiler Toolchains To Make This Easier
Combinational Logic
Memory
Transistors
Semiconductors and photolithography
Classical Physics
Chemistry
Quantum Physics
35Distributed systems
Interop Standards
Communiation Toolchains (CORBA)
Program
Programming Language
Libraries
Virtual Environment (processes, threads, virtual
memory, I/O, signals)
Compiler Toolchain
Operating System Network Stack
Object Code (assembler)
Exceptions
System Architecture
Instruction Set Architecture
Microarchitecture
Memory System
Buses, Disk, NICs, etc
36Bump Out strace thttpd
37A Simple Web Server
- Start with a bunch of bits on disk
- Written in a higher level language (itself an
abstraction) - Run compiler on it
- Get another bunch of bits out
- In executable format
- Run program in shell
- shell is a program itself
- calls fork, system call
- OS copies process
- OS calls exec
- loads first page of new program
- transfers control
38A Simple Web Server
- Still in exec
- Entry point loads shared libraries
- mapped into process address space
- Jump to main
- Runs to end of page
- Loads rest of program
- Network loop
- Make socket
- Bind to address
- Listen
- Accept requests
- Block
- Packet arrives, causes exception
39A Simple Web Server
- Network loop
- 3 Way Handshake
- Connection established
- Passed to OS
- OS notices new connection
- Unblocks process
- Process runs
- Gets back an fd
- Reads on fd
- Does work
- Other arriving data gets buffered
- Writes data
40Wrapup
- Reading
- All of Chapter 1
- UNIX powwow
- Tomorrow 6 PM right here
- If you have never used UNIX on a regular basis,
this will help your grade!!
- Lab 1 Out Today
- Due 2 weeks from today
- Not super urgent, but you may want to scan