+ All Categories
Home > Documents > Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux...

Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux...

Date post: 11-Apr-2018
Category:
Upload: nguyenhanh
View: 238 times
Download: 1 times
Share this document with a friend
38
Building, Running and Monitoring the Linux kernel Prak6kum Kernel Programming University of Hamburg Scien6fic Compu6ng Winter semester 2015/2016 Konstan6nos Chasapis [email protected]
Transcript
Page 1: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Building,RunningandMonitoringtheLinuxkernel

Prak6kumKernelProgrammingUniversityofHamburgScien6ficCompu6ng

Wintersemester2015/2016

[email protected]

Page 2: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Outline

•  TheLinuxkernelsourcetree•  Configure,compile,installtheLinuxkernel•  Linuxkernelbootandini6aliza6on•  Monitoring

11/11/15 Kernel Build 1

Page 3: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

WherecanIfindit

•  DownloadfromhUp://www.kernel.org•  Treenaviga6on

– Webbrowserviewlxr•  hUp://lxr.sourceforge.com•  hUp://lxr.free-electrons.com

–  Symbolicdatabasefromallfiles,cscope•  hUp://cscope.sourceforge.net

11/11/15 Kernel Build 2

Page 4: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelsourcetree•  arch

–  containsallofthearchitecturespecifickernelcode.Ithasfurthersubdirectories,onepersupportedarchitecture,forexamplei386andalpha.

•  block–  containstheimplementa6onoftheblockI/Olayer

•  crypto–  implementscipheropera6onsandthecryptography

API

11/11/15 Kernel Build 3

Page 5: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelsourcetree•  Documenta2on

–  kernelsourcedocumenta6on•  drivers

–  allofthesystem'sdevicedriversliveinthisdirectory.Theyarefurthersub-dividedintoclassesofdevicedriver,forexampleblock

•  firmware–  devicefirmwarethatisneededtousecertaindrivers

11/11/15 Kernel Build 4

Page 6: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelsourcetree•  fs

–  filesystemcode.Thisisfurthersub-dividedintodirectories,onepersupportedfilesystem

•  include–  containsmostoftheincludefilesneededtobuildthe

kernelcode.Ittoohasfurthersubdirectoriesincludingoneforeveryarchitecturesupported

•  init–  containstheini6aliza6oncodeforthekernelanditisa

verygoodplacetostartlookingathowthekernelworks

11/11/15 Kernel Build 5

Page 7: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelsourcetree•  ipc

–  containsthekernelsInter-ProcessCommunica6on(IPC)mechanismsuchasmessagequeues,semaphores,sharedmemory.

•  kernel–  coresubsystems,forexamplethescheduler.The

architecturespecifickernelcodeisinarch/*/kernel.•  lib

–  thisdirectorycontainsthekernel'slibrarycode.Thearchitecturespecificlibrarycodecanbefoundinarch/*

11/11/15 Kernel Build 6

Page 8: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelsourcetree•  mm

–  containsallofthememorymanagementcode.Thearchitecturespecificmemorymanagementcodelivesdowninarch/*/mm/

•  modules–  directoryusedtoholdbuiltmodules

•  net–  thekernel'snetworkingcode,(ethernet,ipv4)

•  samples–  demonstra6vecode

11/11/15 Kernel Build 7

Page 9: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelsourcetree•  scripts

–  containsthescripts(forexampleawkandtkscripts)thatareusedwhenthekernelisconfigured

•  security–  Linuxsecuritymodule,includingSELinux

•  sound–  AdvanceLinuxSoundArchitecture(ALSA),soundcard

drivers.

11/11/15 Kernel Build 8

Page 10: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelsourcetree•  usr

–  user-spaceinterac6on(initramfs)•  tools

–  kernelanduserdevelopmenttools,mostlyusedforperformancecoun6ng

•  virt–  thevirtualiza6oninfrastructure

11/11/15 Kernel Build 9

Page 11: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Outline

•  TheLinuxkernelsourcetree•  Configure,compile,installtheLinuxkernel•  Linuxkernelbootandini6aliza6on•  Monitoring

11/11/15 Kernel Build 10

Page 12: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Configura6on•  Typicalkernelhas>>1000configura6onop6ons•  Defaultconfigura6onpartoftheboardsupportpackage

(BSP)•  Configura6onfile.config•  Configura6onop6onsaretypicallyBooleansorTristate

–  Yes–  No–  Module

•  Examples–  CONFIG_SMP,enablesordisablesSMPsupports–  CONFIG_LOCK_STAT,enablesordisableslocksta6s6cs

11/11/15 Kernel Build 11

Page 13: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Tweakconfigura6onusingmake,targets

•  config–  interac6veforeachop6on

•  menuconfig–  ncursestextmenu

•  xconfig–  graphicalmenususingQt

•  gconfig–  graphicalmenususingGtk+

•  defconfig–  defaultconfigura6onbasedonthearchitecture

11/11/15 Kernel Build 12

Page 14: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Tweakconfigura6onusingmake,targets

•  oldconfig–  validateandupdatetheconfigura6on

•  randconfig–  randomanswertoallop6ons

•  allmodconfig–  selec6ngmoduleswhenpossible

•  allyesconfig–  allop6onsareacceptedwithyes

•  allnoconfig–  allop6onsareansweredwithno

11/11/15 Kernel Build 13

Page 15: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

NamingthenewKernel

•  EditthetoplevelMakefile•  VERSION=3

•  PATCHLEVEL=6

•  SUBLEVEL=35

•  EXTRAVERSION=-rc2

•  NAME=mykernel

11/11/15 Kernel Build 14

Page 16: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Compile

•  Requiredpackages–  developmenttools,make,gcc,gzip,etc.–  Severaldistribu6onsofferpackages

•  Crosscompile–  exportARCH=...–  exportCROSS_COMPILE=...

11/11/15 Kernel Build 15

Page 17: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Compilemaketargets•  default

–  buildskernel+modules•  bzImage

–  buildskernel–  generates:arch/arm/boot/bzImage

•  modules–  buildsloadablemodules–  generates:lib/modules/<kernel.versio-name>

11/11/15 Kernel Build 16

Page 18: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Compilemaketargets•  -j<n>e.g.-j2

–  spawnmul6plebuildjobs•  clean

–  generatedfiles•  mrproper

–  generatedfiles+config+backupfiles•  distclean

–  alltheabove+patchfiles

11/11/15 Kernel Build 17

Page 19: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Install

•  makeinstall–  copykernelimagetotheproperdirectory/

boot•  makemodule_install

–  installbuildmodulesinthecorrecthomeunder/lib/modules

•  Updatethebootloader–  LILOorgrubconfigura6onfile–  addthenewentryforthenewlybuildkernel

11/11/15 Kernel Build 18

Page 20: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Kernelcommandline•  Kernelbehavioursetbyboot“commandline”•  seeDocumenta6on/kernel-parameters.txt•  Examples

–  root:setdevicetoloadrootfilesystemfrom,•  e.g.root=/dev/sda1

–  quiet:outputfewerconsolemessages–  debug:outputallconsolemessages–  maxcpus:controltheac6veCPU

•  CanbesetinBootloader,e.g.GRUB

11/11/15 Kernel Build 19

Page 21: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Therootfilesystem•  Mountedbythekernelduringboot

–  Providesaddi6onalkernelmodulesthatareneeded•  specifiedtherootkernelcommandlineparameter•  Loadedfrom:

–  memory(ramdisk/initramfs)–  storagedevice–  network

•  The“module”thatprovidesaccessmustbeembeddedinthekerneloritcannotmount..

11/11/15 Kernel Build 20

Page 22: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Initramfs•  ini6alramfilesystem

–  successorofinitrd•  cpioarchiveoftheini6alfilesystem

–  cpio•  filearchiveandfileformat•  copyinandout

•  getsloadedintomemoryduringstartup•  containsdevicedriversandtoolsneededto

mounttherealfilesystem11/11/15 Kernel Build 21

Page 23: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Outline

•  TheLinuxkernelsourcetree•  Configure,compile,installtheLinuxkernel•  Linuxkernelbootandini6aliza6on•  Monitoring

11/11/15 Kernel Build 22

Page 24: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Bootprocessofthekernel•  BIOSloadsMasterBootRecord(MBR)fromtheboot

device•  CodethatexistintheMBRreadsthepar66ontableof

thebootdeviceandreadsthebootloader(GRUB,LILO)fromthebootpar66on

•  Thebootloaderreadsthecompressedkernelimage–  Passesthecontroltoitusingthecommandline

op6ons•  Thekernelun-compressesitself

11/11/15 Kernel Build 23

Page 25: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Bootprocessofthekernel•  Proceedsto"real"modewherethefirstlevel

ini6aliza6onsaredone–  Inreadmode,itcanaccessonlythefirst1MBof

memory•  Startupisperformedinthe"protected"modeand

beginsini6alizingtheCPUsubsystem–  In"protected"modeyoucanusemanyadvance

featureoftheprocessorsuchaspaging•  Itfollowsthememoryandtheprocessmanagements

subsystems•  Peripheralbuses,I/Obusesarestatednext

11/11/15 Kernel Build 24

Page 26: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Initprocess•  Atlastthekernelinvokestheinitprogramthatisthe

parentofallLinuxprocesses•  Firstprogramtoberun/sbin/init

–  Beginsbyreading/etc/iniUab•  Runlevels(systemstates)forSystemVinit

–  0ishalt–  1issingleuser–  2-5aremul6-user–  6reboot

•  Startsini6alisa6onscripts–  Fountat/ect/init.d

11/11/15 Kernel Build 25

Page 27: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Outline

•  TheLinuxkernelsourcetree•  Configure,compile,installtheLinuxkernel•  Linuxkernelbootandini6aliza6on•  Monitoring

– Metrics– Tools

11/11/15 Kernel Build 26

Page 28: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

CPUmetrics/proc/stat•  U2liza2on

–  overallu6liza6onperprocessor•  User2me

–  percentagespentonuserprocesses•  System2me

–  percentagespentonkernelopera6ons•  Wai2ng

–  6mespentwai6ngforI/Oopera6ons•  Idle2me

–  systemwasidlewai6ngtasks

11/11/15 Kernel Build 27

Page 29: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

CPUmetrics/proc/stat•  Nice2me

–  6mespentonre-nicingprocesses•  Runnableprocesses

–  processesreadytorun•  Blocked

–  processesblockedbyI/Oopera6ons•  Contextswitch

–  numberofcontextswitches•  Interrupts

–  numberofhardandsorinterrupts

11/11/15 Kernel Build 28

Page 30: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Memorymetrics•  Freememory

–  amountoffreememoryinthesystem•  Swapusage

–  amountofswapused•  Bufferandcache

–  memoryallocatedforI/O•  Slabs

–  kernelusageofmemory•  Ac2veVS.inac2vememory

–  Inac6vememoryisacandidatetobeswapped

11/11/15 Kernel Build 29

Page 31: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Networkinterfacemetrics•  Packetssent/received•  Bytessent/received•  Collisionspersecond

–  SustainedvaluesindicatenetworkinfrastructureboUlenecks•  Packetsdropped

–  Canbecausedbythefirewallorlimitedbuffers•  Overruns

–  howmany6mesrunnedoutofbuffers•  Errors

–  countthepacketsthataremarkedfaulty

11/11/15 Kernel Build 30

Page 32: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Blockdevicemetrics•  Iowait

–  the6meCPUspendswai6ngforI/Otocomplete•  Averagequeuelength

–  amountofoutstandingI/Orequests–  highvalueindicateI/OboUleneck

•  Averagewait–  average6meinmsthattakesforanI/Oopera6onto

complete•  Transferpersecond

–  howmanyI/Oopera6onsareperformed

11/11/15 Kernel Build 31

Page 33: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Blockdevicemetrics•  Blocksread/writepersecond

–  numberofblocksthatwerereadorwriUen(usuallyeachblockis1024Bytes)

•  Kilobytesread/writepersecond–  numberofblocksthatwerereadorwriUeninKBytes

11/11/15 Kernel Build 32

Page 34: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Genericadmintools

•  dmesg–  Printsthemessagebufferofthekernel

•  strace–  Monitorinterac6onbetweenuserandkernel

•  oprofile–  System-widesta6s6calprofilingtool

11/11/15 Kernel Build 33

Page 35: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

CPUTools•  top

–  Processac6vity•  ps,pstree

–  Displaytherunningprocesses•  kill

–  SendstheSIGTERMsignaltotheprocess•  mpstat

–  Displaysac6vi6esforeachavailableprocessor•  numastat

–  NUMA-relatedsta6s6cs•  pmap

–  Processmemoryusage

11/11/15 Kernel Build 34

Page 36: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

I/OTools•  vmstat

–  Reportvirtualmemorysta6s6cs•  free

–  Displaytheamountoffreeandusedmemory•  iostat

–  Reportblockdevicesta6s6cs•  lsblk

–  Listblockdevices•  lsof

–  Listopenfiles11/11/15 Kernel Build 35

Page 37: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

NetworkTools

•  ping–  checkifaserverresponds

•  traceroute–  displaytheroutepath

•  nslookup–  getdomainnameorIPaddress

•  netstat–  displaysnetworkstats

•  tcpdump–  dumptrafficonanetwork

11/11/15 Kernel Build 36

Page 38: Building, running and monitoring the Linux kernel · Building, Running and Monitoring the Linux kernel ... – contains all of the architecture specific kernel code. It has further

Procfiles

•  /proc/stat–  cpu

•  /proc/diskstats–  disk

•  /proc/meminfo–  Memorystats

11/11/15 Kernel Build 37


Recommended