course overview -...
TRANSCRIPT
1
Instructor:Fatma CORUT ERGİN
Slides adapted from Bryant & O’Hallaron’s slides
CourseOverview
CSE238/2038/2138:SystemsProgramming
2
Overview
¢ Coursetheme¢ Fiverealities¢ Logistics
3
CourseTheme:SystemsKnowledgeisPower!¢ Systemsknowledge§ Howhardwareandsoftwarecombinetosupporttheexecutionofapplicationprograms
§ Howyou(asaprogrammer)canbestusetheseresources
¢ Usefuloutcomesofthecourse§ Becomemoreeffectiveprogrammers
§ Abletofindandeliminatebugsefficiently§ Abletounderstandandtuneforprogramperformance
§ Prepareforlater“systems”classesinCSE§ OperatingSystems,DigitalLogicDesign,ComputerOrganization,etc.
4
UnderstandHowThingsWork
¢ WhydoIneedtoknowthisstuff?§ Abstractionisgood,butdon’tforgetreality
¢ MostCSEcoursesemphasizeabstraction§ Abstractdatatypes§ Asymptoticanalysis
¢ Theseabstractionshavelimits§ Especiallyinthepresenceofbugs§ Needtounderstanddetailsofunderlyingimplementations§ Sometimestheabstractinterfacesdon’tprovidethelevelofcontrolorperformanceyouneed
5
GreatReality#1:IntsarenotIntegers,FloatsarenotReals¢ Example1:Isx2≥0?§ Float’s:Yes!§ Int’s:
§ 40000*40000=1600000000
§ 50000*50000=??
¢ Example2:Is(x+y)+z=x+(y+z)?§ Unsigned&SignedInt’s:Yes!§ Float’s:
§ (1e20+-1e20)+3.14-->3.14§ 1e20+(-1e20+3.14)-->??
Source:xkcd.com/571
6
ComputerArithmetic
¢ Doesnotgeneraterandomvalues§ Arithmeticoperationshaveimportantmathematicalproperties
¢ Cannotassumeall“usual”mathematicalproperties§ Duetofinitenessofrepresentations§ Integeroperationssatisfy“ring”properties
§ Commutativity,associativity,distributivity§ Floatingpointoperationssatisfy“ordering”properties
§ Monotonicity,valuesofsigns
¢ Observation§ Needtounderstandwhichabstractionsapplyinwhichcontexts§ Importantissuesforcompilerwritersandseriousapplicationprogrammers
7
GreatReality#2:You’veGottoKnowAssembly¢ Chancesare,you’llneverwriteprogramsinassembly§ Compilersaremuchbetter&morepatientthanyouare
¢ But:Understandingassemblyiskeytomachine-levelexecutionmodel§ Behaviorofprogramsinpresenceofbugs
§ High-levellanguagemodelsbreakdown§ Tuningprogramperformance
§ Understandoptimizationsdone/notdonebythecompiler§ Understandingsourcesofprograminefficiency
§ Implementingsystemsoftware§ Compilerhasmachinecodeastarget§ Operatingsystemsmustmanageprocessstate
§ Creating/fightingmalware§ x86assemblyisthelanguageofchoice!
8
GreatReality#3:MemoryMattersRandomAccessMemoryIsanUnphysicalAbstraction
¢ Memoryisnotunbounded§ Itmustbeallocatedandmanaged§ Manyapplicationsarememorydominated
¢ Memoryreferencingbugsespeciallypernicious§ Effectsaredistantinbothtimeandspace
¢ Memoryperformanceisnotuniform§ Cacheandvirtualmemoryeffectscangreatlyaffectprogramperformance§ Adaptingprogramtocharacteristicsofmemorysystemcanleadtomajorspeedimprovements
9
MemoryReferencingBugExample
§ Resultissystemspecific
fun(0) à 3.14fun(1) à 3.14fun(2) à 3.1399998664856fun(3) à 2.00000061035156fun(4) à 3.14fun(6) à Segmentationfault
typedef struct {int a[2];double d;
} struct_t;
double fun(int i) {volatile struct_t s;s.d = 3.14;s.a[i] = 1073741824; /* Possibly out of bounds */return s.d;
}
10
MemoryReferencingBugExampletypedef struct {
int a[2];double d;
} struct_t;
fun(0) à 3.14fun(1) à 3.14fun(2) à 3.1399998664856fun(3) à 2.00000061035156fun(4) à 3.14fun(6) à Segmentationfault
Locationaccessedbyfun(i)
Explanation:
CriticalState 6
? 5
? 4
d7 ... d4 3
d3 ... d0 2
a[1] 1
a[0] 0
struct_t
11
WhatAboutThis?typedef struct {
double d;int a[2];
} struct_t;
fun(0) à 3.14fun(1) à 3.14fun(2) à 3.14fun(3) à 3.14fun(4) à 3.14fun(6) à Segmentationfault
Locationaccessedbyfun(i)
Explanation:
CriticalState 6
? 5
? 4
a[1] 3
a[0] 2
d7 ... d4 1
d3 ... d0 0
struct_t
12
MemoryReferencingErrors
¢ CandC++donotprovideanymemoryprotection§ Outofboundsarrayreferences§ Invalidpointervalues§ Abusesofmalloc/free
¢ Canleadtonastybugs§ Whetherornotbughasanyeffectdependsonsystemandcompiler§ Actionatadistance
§ Corruptedobjectlogicallyunrelatedtoonebeingaccessed§ Effectofbugmaybefirstobservedlongafteritisgenerated
¢ HowcanIdealwiththis?§ PrograminJava,Ruby,Python,ML,…§ Understandwhatpossibleinteractionsmayoccur§ Useordeveloptoolstodetectreferencingerrors(e.g.Valgrind)
13
GreatReality#4:There’smoretoperformancethanasymptoticcomplexity
¢ Constantfactorsmattertoo!¢ Andevenexactopcountdoesnotpredictperformance
§ Easilysee10:1performancerangedependingonhowcodewritten§ Mustoptimizeatmultiplelevels:algorithm,datarepresentations,procedures,andloops
¢ Mustunderstandsystemtooptimizeperformance§ Howprogramscompiledandexecuted§ Howtomeasureprogramperformanceandidentifybottlenecks§ Howtoimproveperformancewithoutdestroyingcodemodularityandgenerality
14
MemorySystemPerformanceExample
¢ Hierarchicalmemoryorganization¢ Performancedependsonaccesspatterns
§ Includinghowstepthroughmulti-dimensionalarray
void copyji(int src[2048][2048],int dst[2048][2048])
{int i,j;for (j = 0; j < 2048; j++)
for (i = 0; i < 2048; i++)dst[i][j] = src[i][j];
}
void copyij(int src[2048][2048],int dst[2048][2048])
{int i,j;for (i = 0; i < 2048; i++)
for (j = 0; j < 2048; j++)dst[i][j] = src[i][j];
}
81.8ms4.3ms2.0GHzIntelCorei7Haswell
15
CoursePerspective
¢ OurCourseisProgrammer-Centric§ Purposeistoshowthatbyknowingmoreabouttheunderlyingsystem,onecanbemoreeffectiveasaprogrammer
§ Enableyouto§ Writeprogramsthataremorereliableandefficient§ IncorporatefeaturesthatrequirehooksintoOS
– E.g.,concurrency,signalhandlers
16
Cheating:Description
¢ Whatischeating?§ Sharingcode:bycopying,retyping,lookingat,orsupplyingafile§ Describing:verbaldescriptionofcodefromonepersontoanother.§ Coaching:helpingyourfriendtowritealab,linebyline§ SearchingtheWebforsolutions§ Copyingcodefromanykindofsource
§ Youareonlyallowedtousecodewesupply
¢ WhatisNOTcheating?§ Explaininghowtousesystemsortools§ Helpingotherswithhigh-leveldesignissues
17
Cheating:Consequences¢ Penaltyforcheating:§ “0”gradeforbothparties§ Permanentmarkonyourrecordinthedepartment“black-list”
¢ Detectionofcheating:§ Wehavesophisticatedtoolsfordetectingcodeplagiarism
¢ Don’tdoit!§ Startearly§ Askthestaffforhelpwhenyougetstuck
18
TeachingAssistants¢ Birol GENÇYILMAZ§ [email protected]§ Office:MB453
¢ Lokman ALTIN§ [email protected]§ Office:MB342
19
Textbook
¢ RandalE.BryantandDavidR.O’Hallaron,§ ComputerSystems:AProgrammer’sPerspective,ThirdEdition(CS:APP3e),Pearson,2016
§ http://csapp.cs.cmu.edu§ Thisbookreallymattersforthecourse!
§ Howtosolvelabs§ Practiceproblemstypicalofexamproblems
20
Grading
¢ Midterm (20%)¢ Final (40%)¢ Projects+Lab (40%)
21
WelcomeandEnjoy!