EmbeddedLinuxoptimizations
EmbeddedLinux optimizations
Size,RAM,speed, power,cost
MichaelOpdenacker ThomasPetazzoni FreeElectrons
Copyright20042009,FreeElectrons. CreativeCommonsBYSA3.0license Latestupdate:Dec20,2010, Documentsources,updatesandtranslations: [Link] Corrections,suggestions,contributionsandtranslationsarewelcome!
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Penguinweightwatchers
Makeyourpenguinslimmer,faster,andreduceitsconsumptionoffish! Before 2weeksafter
2
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
CELinuxForum
[Link] Nonprofitorganization,whosemembersareembeddedLinux companiesandConsumerElectronics(CE)devicesmakers. Mission:developtheuseofLinuxinCEdevices HostsmanyprojectstoimprovethesuitabilityofLinuxforCE [Link] includedinthemainlineLinuxkernel. Mostoftheideasintroducedinthispresentationhavebeen gatheredorevenimplementedbyCELinuxForumprojects!
3
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Contents
IdeasforoptimizingtheLinuxkernelandexecutables Increasingspeed Reducingsize:diskfootprintandRAM Reducingpowerconsumption Globalperspective:costandcombinedoptimizationeffects Theultimateoptimizationtool!
4
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
EmbeddedLinuxOptimizations
Increasingspeed
Reducingkernelboottime
5
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Measuringkernelboottime
CONFIG_PRINTK_TIME ConfigureitintheKernelHackingsection. [Link]. Notaccurateenoughonsomeplatforms(1jiffy=10msonarm!) See[Link]
... [42949372.970000]Memory:64MB=64MBtotal [42949372.970000]Memory:54784KBavailable(1404Kcode,296Kdata,72Kinit) [42949373.180000]Mountcachehashtableentries:512 [42949373.180000]CPU:Testingwritebuffercoherency:ok [42949373.180000]checkingifimageisinitramfs...itisn't(badgzipmagicnumb ers);lookslikeaninitrd [42949373.200000]Freeinginitrdmemory:8192K [42949373.210000]NET:Registeredprotocolfamily16 ...
6
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Boottracer
CONFIG_BOOT_TRACERinkernelconfiguration IntroducedinLinux2.6.28 Basedontheftracetracinginfrastructure Allowstorecordthetimingsofinitcalls Bootwiththeinitcall_debugandprintk.time=1parameters, rundmesg>[Link],run [Link]|perlscripts/[Link]>[Link] togenerateagraphicalrepresentation ExampleonaboardwithatAtmelAT91CPU:
5s
tty_init
pty_init
atmel_nand_init
ip_auto_config
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Grabserial
FromTimBird [Link] Asimplescripttoaddtimestampstomessagescomingfroma serialconsole. Keyadvantage:startscountingveryearly(bootloader), anddoesn'tjuststartwhenthekernelinitializes. Anotheradvantage:nooverheadonthetarget,becauserunon thehostmachine.
8
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
DisableIPautoconfig
StoppedinitializingtheIPaddressonthekernelcommandline (oldremainsfromNFSbooting,wasconvenientnottohardcode theIPaddressintherootfilesystem.) Instead,diditinthe/etc/init.d/rcSscript. Thissaved1.56sonourAT91board. Youwillsaveevenmoreifyouhadotherrelatedoptionsinyour kernel(DHCP,BOOP,RARP)
9
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
ReducingthenumberofPTYs
PTYsareneededforremoteterminals(throughSSH) Theyarenotneededinourdedicatedsystem! ThenumberofPTYscanbereducedthroughthe CONFIG_LEGACY_PTY_COUNTkernelparameter. Ifthisnumberissetto4,wesave0.63sonourAtmelboard. Aswe'renotusingPTYsatallinourproductionsystem, wedisabledthemwithcompletelywithCONFIG_LEGACY_PTYS. Wesaved0.64s. Notethatthiscanalsobeachievedwithoutrecompilingthe kernel,usingthepty.legacy_countkernelparameter.
10
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Disableconsoleoutput
Theoutputofkernelbootupmessagestotheconsoletakes time!Evenworse:scrollingupinframebufferconsoles! Consoleoutputnotneededinproductionsystems. Consoleoutputcanbedisabledwiththequiet argumentintheLinuxkernelcommandline(bootloadersettings) Example: root=/dev/ram0rwinit=/[Link] Benchmarks:canreduceboottimeby30oreven50%! See[Link]
11
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Presetloops_per_jiffy
Ateachboot,theLinuxkernelcalibratesadelayloop(forthe udelayfunction).Thismeasuresaloops_per_jiffy(lpj) value.Thistakesabout25jiffies(1jiffy=timebetween2timer interrupts). Inembeddedsystems,itcanbeabout250ms! Youjustneedtomeasurethisonce!Findthelpjvalueinkernel bootmessages(ifyoudon'tgetitintheconsole,bootLinuxwith theloglevel=8parameter).Example:
Calibratingusingtimerspecificroutine...187.59 BogoMIPS(lpj=937984)
Atthenextboots,startLinuxwiththebelowoption: lpj=<value>
12
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
LZOkerneldecompression
LZOisacompressionalgorithmthatismuchfasterthangzip, atthecostofaslightlydegradecompressionratio(+10%). Itwasalreadyinuseinthekernelcode(JFFS2,UBIFS...) AlbinTonnerrefromFreeElectronsaddedsupportforLZO [Link] [Link][Link]
13
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
LZOdecompressionresults
Savesapproximately0.25sofboottime See[Link] OurpatchalsoallowsLZOtobeusedforinitramfs decompression(CONFIG_INITRAMFS_COMPRESSION_LZO=y) Anothersolutionistouseanuncompressedkernel (anotherpatchwillbesent),inwhichcasekernelexecutionisjust marginallyfasterthanwithLZO,attheexpenseofadoublesize.
14
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
DirectlybootLinuxfrombootstrapcode
Idea:makeaslightchangetoat91bootstraptodirectlyloadand executetheLinuxkernelimageinsteadoftheUbootone. RatherstraightforwardwhenbootUbootandthekernelare loadedfromNANDflash. Requirestohardcodethekernelcommandlineinthekernel image(CONFIG_CMDLINE) RequiresmoredevelopmentworkwhenUbootisloadedfroma differenttypeofstorage(SPIdataflash,forexample). Inthiscase,youcankeepUboot,butremoveallthefeaturesnot neededinproduction(USB,Ethernet,tftp...) Timesavings:about2s See[Link]
15
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Reducethekernelsize
ThroughtheCONFIG_EMBEDDEDoption Removethingsthatarenotneededinyourdedicatedsystem (features,debuggingfacilitiesandmessages) Makesureyouhavenounusedkerneldrivers Disablesupportforloadablekernelmodulesandmakeallyour driversstatic(unlesstherearemultipledriversthancanbe loadedlater). Asmallerkernelisfastertoload Asimplerkernelexecutesfaster Atleast,compiledriversasmodulesfordevicesnotusedatboot [Link].
16
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Fasterrebooting(1)
kexecsystemcall:executesanewkernelfromarunningone. Mustfasterrebooting:doesn'tgothroughbootstrap/bootloader code. Greatsolutionforrebootingaftersystem(firmware)upgrades. Usefulforautomaticrebootingafterkernelpanics. See[Link] andDocumentation/kdump/[Link].
17
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Fasterrebooting(2)
Anotheroption:usereboot=softinthekernelcommandline Whenyoureboot,thefirmwarewillbeskipped. Drawback:unlikekexec,cannotbechosenfromuserspace. Supportedplatforms:i386,x86_64,arm,arm26(Aug.2006) SeeDocumentation/[Link] [Link].
18
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Skipmemoryallocation
Idea:sparememoryatboottimeandmanageitbyyourself! Assumeyouhave32MBofRAM Bootyourkernelwithmem=30 Thekernelwilljustmanagethefirst30MBofRAM. Drivercodecannowreclaimthe2MBleft: buf=ioremap( 0x1e00000, /*Start:30MB*/ 0x200000 /*Size:2MB*/ );
Thissavestimeallocatingmemory. CriticaldriversarealsosuretoalwayshavetheRAMtheyneed.
19
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
KernelboottimeOtherideas
CopykernelandinitramfsfromflashtoRAMusingDMA (UsedbyMontaVistainDellLatitudeON) Fastboot,asynchronousinitcalls:[Link] Mainlined,butAPIstillusedbyveryfewdrivers. MostlyusefulwhenyourCPUhasidletimeinthebootprocess. Usedeferredinitcalls See[Link] NAND:justcheckforbadblocksonce Atmel:see[Link] See[Link]
20
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
EmbeddedLinuxOptimizations
Increasingspeed
Systemstartuptimeandapplicationspeed
21
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Startingsystemservices
SysVinit: [Link] becompletetostartthenextone!Whiledependenciesexist, sometaskscanberuninparallel! Initng:[Link] NewalternativetoSysVinit,whichcanstartservicesinparallel, assoonastheirpreconditionsaremet.
22
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
[Link]:bootcharts
Initngwins! Systemutilizationismuch better. YoucanhuntsystemstartuptroublebyusingtheBootchartprogram ([Link] Bootchartisslow(Java)andnotveryaccurate. See[Link]
23
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Readingahead
LinuxkeepsthecontentsofallthefilesitreadsinRAM(inthe pagecache),aslongasitdoesn'tneedtheRAMpagesfor somethingelse. Idea:loadfiles(programsandlibrariesinparticular)inRAM [Link] doinganyI/O. Thankstothis,programsarenotstuckwaitingforI/O. UsedtheKnoppixdistributiontoachieveverynicebootspeed ups. AlsoplannedtobeusedbyInitng. NotveryusefulforsystemswithverylittleRAM: cachedpagesarerecycledbeforethefilesareaccessed.
24
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Implementingreadahead
Youcanusethesys_readahead()systemcall [Link]. Youcanalsousethereadaheadlistutility,whichreadsafile containingthelistoffilestoloadincache. Availableon:[Link] InembeddedsystemsusingBusybox,youcanusethe readaheadcommand(implementedbyFreeElectrons).
25
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Compilerspeedoptimizations
Bydefault,mosttoolsarecompiledwithcompileroptimizations. Makesureyouusethemforyourownprograms! O2isthemostcommonoptimizationswitchofgcc. Lotsofoptimizationtechniquesareavailable. See[Link] O3canbealsobeusedforspeedcriticalexecutables. However,thereisdoneattheexpenseofcodesize(forexample inlining:replacingfunctioncallsbythefunctioncodeitself).
26
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Usingprocessoraccelerationinstructions
liboil[Link] Libraryoffunctionsoptimizedforspecialinstructions fromseveralprocessors(Altivec,MMX,SSE,etc.) Mainlyfunctionsimplementingloopsondataarrays: typeconversion,copying,simplearithmetics,directcosine transform,randomnumbergeneration... Transparent:keepsyourapplicationportable! Sofarmainlysupportsdesktopprocessors License:BSDtype
27
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Prelinking(1)
Appliestoexecutablesusingsharedlibraries Toloadandstartanexecutable,thedynamiclinkerhasa significantamountofworktodo(mainlyaddressrelocation) Itcantakealotoftimeforexecutablesusingmanyshared libraries! Inmanysystemsinwhichexecutablesandsharedlibrariesnever change,thesamejobisdoneeverytimetheexecutableis started.
28
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Prelinking(2)
prelink [Link] prelinkmodifiesexecutablesandsharedlibrariestosimplify thedynamiclinkerrelocationwork. Thiscangreatlyreducestartuptimeforbigapplications(50% lessforKDE!).Thisalsosavesmemoryconsumedby relocations. CanbeusedtoreducethestartuptimeofaLinuxsystem. Justneedstoberunagainwhenlibrariesorexecutablesare updated. Detailson[Link]
29
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
UsesimplerUnixexecutables
Big,featurerichexecutablestaketimetoload. Particularlytrueforshellscriptscallingthebashshell! Idea:replacestandardUnix/GNUexecutablesbylightweight,simplified implementationsbybusybox([Link] ImplementedbyUbuntu6.10toreduceboottime,replacingbash(649K) bydash(79K,see[Link] Thisbrokevariousshellscriptswhichusedbashspecificfeatures (bashisms). InnonembeddedLinuxsystems wherefeaturerichexecutablesarestillneeded, shouldatleastusebusyboxashforsystemscripts.
30
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Shells:reducingforking
fork/execsystemcallsareveryheavy. Becauseofthis,callstoexecutablesfromshellsareslow. Evenexecutingechoinbusyboxshellsresultsinaforksyscall! SelectShells>Standaloneshellinbusybox configurationtomakethebusyboxshellcallappletswhenever possible. Pipesandbackquotesarealsoimplementedbyfork/exec. [Link]: cat/proc/cpuinfo|grepmodel Replaceitwith:grepmodel/proc/cpuinfo See[Link]
31
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Usefasterfilesystems
Runfasterbyusingthemostappropriatefilesystems! Compressedreadonlyfilesystem(blockdevice): useSquashFS([Link] insteadofCramFS(muchslower,gettingobsolete). NANDflashstorage:youshouldtryUBIFS ([Link] [Link] [Link] ([Link]
32
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Usefasterfilesystems(2)
UseRAMfilesystemsfortemporary,speedcriticalfiles [Link] sources:Documentation/filesystems/[Link] Benchmarkyoursystemandapplicationoncompeting filesystems!Reiser4ismoreinnovativeandbenchmarks founditfasterthanext3. GoodtobenchmarkyoursystemwithJFSorXFStoo. XFSisreportedtobethefastesttomount(goodfor startuptime),andJFStohavethelowestCPUutilization. See[Link]
ext4isalsoreadytobeusednow.
33
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Speedupapplicationswithtmpfs
WhenenoughRAMisavailable,theOSkeepsrecentlyaccessedfilesand applicationsinRAM(pagecache).Thissignificantlyspeedsupanynewusage. However,dependingonsystemactivity,thismaynotlastlong. Forprogramsthatneedfaststartupeveniftheyhaven'tbeenrunforalongtime: copythemtoatmpfsfilesystematsystemstartup!Thismakessuretheyare alwaysaccessedfromthefilecacheinRAM(providedyoudonothaveaswap partition). SeeDocumentation/filesystems/[Link] tmpfs. Caution:don'tuseramdisksinstead! RamdisksduplicatefilesinRAMandunusedspacecannotbereclaimed. Caution:[Link]. Notneededifthere'senoughRAMtocacheallfilesandprograms.
34
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Bootfromahibernateimage
Theultimatetechniqueforinstantboot! Indevelopment:startthesystem,requiredapplicationsandthe [Link]/flashinthisstate. Inproduction:bootthekernelandrestorethesystemstatefrom withthispredefinedhibernationimage. Thisway,youdon'thavetoinitializetheprogramsonebyone. Youjustgetthebacktoavalidstate. UsedinSonycamerastoachieveinstantpowerontime. UnlikeSuspendtoRAM,stillallowstoremovebatteries!
35
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Useaprofiler
Usingaprofilercanhelptoidentifyunexpectedbehaviordegrading applicationperformance. Forexample,aprofilercantellyouinwhichfunctionsmostofthe timeisspent. Possibletostartwithstraceandltrace AdvancedprofilingwithValgrind:[Link]
Compileyourapplicationforx86architecture YoucanthenprofileitwiththewholeValgrindtoolsuite: Cachegrind:sourcesofcachemissesandfunctionstatistics. Massif:sourcesofmemoryallocation.
SeeourSoftwareDevelopmentpresentationfordetails: [Link]
36
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
EmbeddedLinuxOptimizations
Reducingsize
KernelsizeandRAMusage
37
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
LinuxTiny
Goal:reducethediskfootprintandRAMsizeoftheLinuxkernel [Link] SetofpatchesagainstthemainstreamLinuxkernel. Mergeabilityinmainstreamisapriority. Manychangeshavealreadybeenmergedinrecentkernels. Allfeaturescanbeselectedinkernelconfiguration (CONFIG_EMBEDDED). Alsoshipsutilitiesorpatchesfortrackingsources ofmemoryusageorcodesize.
38
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
LinuxTinyideas(1)
Removekernelmessages(printk,BUG,panic...) Huntexcessinlining([Link]) 2.6.26:canallowgcctouninlinefunctionsmarkedasinline: (CONFIG_OPTIMIZE_INLINING=y).Onlyusedbyx86sofar. Huntexcessmemoryallocations Memory(slobinsteadofslab)allocatormorespaceefficientfor smallsystems. Reducethesizeofkerneldatastructures(mayimpact performance) Simpleralternativeimplementationsofkernelfunctionalities withlessfeatures,ornotsupportingspecialcases.
39
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
LinuxTinyideas(2)
Removesomefeatureswhichmaynotbeneeded insomesystems. Compilingoptimizationsforsize. AsmallerkernelexecutablealsosavesRAM (unlessexecutedinplacefromstorage).
40
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
LinuxTiny:kernelconfigurationscreenshot
Manyfeatures configuredout
41
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
WithandwithoutCONFIG_EMBEDDED
TestsonLinux2.6.29,onaminimalisticbutworkingx86kernel
1800
1600
1400
1200
1000
Raw Compressed
800
600
400
200
0 CONFIG_EMBEDDED=n CONFIG_EMBEDDED=y
Raw:272KB(17%),Compressed:136KB(20%)
42
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Replaceinitrdbyinitramfs
Replaceinitramdisks(initrd)withinitramfs: muchlessoverheadandramwaste!
Access tofile VirtualFile System Filesystem driver Block driver Copy Blockstorage File cache
Regular blockdevice
Access tofile VirtualFile System Filesystem driver Block driver
Ramdisk blockdevice
Access tofile VirtualFile System
ramfs
File cache Copy Blockstorage RAM
File cache
RAM
RAM
43
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
ramfsadvantagesoverramdisks
Noblockandfilesystemoverhead. NoduplicationinRAM. Filescanberemoved(reclaimingRAM)afteruse. Initramfs:ramfsarchiveembeddedintheLinuxkernelfile.
44
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
EmbeddedLinuxOptimizations
Reducingsize
ApplicationsizeandRAMusage
45
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Staticordynamiclinking?(1)
Staticlinking Allsharedlibrarycodeduplicatedintheexecutables AllowsnottocopytheClibraryinthefilesystem. Simplerandsmallerwhenveryfewexecutables(busybox) Librarycodeduplication:badforsystemswithmoreexecutables (codesizeandRAM) Bestforsmallsystems(<12MB)withfewexecutables!
46
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Staticordynamiclinking?(2)
Dynamiclinking Sharedlibrarycodenotduplicatedintheexecutables Makesmuchsmallerexecutables SavesspaceinRAM(biggerexecutablestakemoreRAM) Requiresthelibrarytothecopiedtothefilesystem Bestformediumtobigsystems(>500KB1MB)
47
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
UsingalighterClibrary
glibc(GNUClibrary):[Link] FoundonmostcomputertypeGNU/Linuxmachines Sizeonarm:approx1.7MB uClibc:[Link] FoundinmoreandmoreembeddedLinuxsystems! Sizeonarm:approx400KB(yousave1.2MB!) Executablesareslightlysmallertoo:
Cprogram Plainhelloworld Busybox Compiledwithsharedlibraries glibc 4.6K 245K uClibc 4.4K 231K Compiledstatically glibc 475K 843K uClibc 25K 311K
48
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
HowtouseuClibc?
NeedtocompileallyourexecutableswithauClibctoolchain. Readytousetoolchainscanbefoundon [Link] Youcanveryeasilybuildyourownwithbuildroot: [Link] YoualsohavetocopytheuClibcfilesfromthetoolchaintothe /libdirectoryinthetargetrootfilesystem. Readytousefilesystemscanalsobegeneratedbybuildroot. Youjustneedtoaddyourspecificstuffthen.
49
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Needforstripping
Compiledexecutablesandlibrariescontainextrainformation whichcanbeusedtoinvestigateproblemsinadebugger. Thiswasusefulforthetooldeveloper,butnotforthefinaluser. Toremovedebugginginformation,usethestripcommand. Thiscansaveaverysignificantamountofspace! gccohellohello.c (outputsize:4635bytes) striphello (outputsize:2852bytes,38.5%) Don'tforgettostriplibrariestoo!
50
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Aremyexecutablesstripped?
Youcanusethefilecommandtogettheanswer gccohellohello.c filehello hello:ELF32bitLSBexecutable,Intel80386,version1 (SYSV),forGNU/Linux2.2.5,dynamicallylinked(uses sharedlibs),notstripped striphello hello:ELF32bitLSBexecutable,Intel80386,version1 (SYSV),forGNU/Linux2.2.5,dynamicallylinked(uses sharedlibs),stripped Youcanusefindstrip([Link] tofindallexecutablesandlibrariesthatneedstrippinginyoursystem.
51
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Howtostrip
Somelightweighttools,likebusybox,areautomaticallystripped whenyoubuildthem. Makefilesformanystandardtoolsofferaspecialcommand: makeinstallstrip Caution:strippingisarchitecturedependent. Usethestripcommandfromyourcrosscompilingtoolchain: armlinuxstrippotato
52
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
sstrip:superstrip
[Link] Goesbeyondstripandcanstripoutafewmorebitsthatare notusedbyLinuxtostartanexecutable. [Link]:processed librariescannolongerbeusedtocompilenewexecutables. CanalsobefoundintoolchainsmadebyBuildroot(optional)
Hello World Regular stripped sstripped 4691 B 2904 B (-38 %) 1392 B (-70 %)
Busybox 287783 B
Inkscape 11397 KB
230408 B (-19.9 %) 9467 KB (-16.9 %) 229701 B (-20.2 %) 9436 KB (-17.2 %)
Bestfortiny executables!
53
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
LibraryOptimizer
[Link] ContributedbyMontaVista Examinesthecompletetargetfilesystem,resolvesallshared librarysymbolreferences,andrebuildsthesharedlibrarieswith onlytheobjectfilesrequiredtosatisfythesymbolreferences. Canalsotakecareofstrippingexecutablesandlibraries. However,requirestorebuildallthecomponentsfromsource. WouldbenicertoachievethisonlywithELFmanipulations.
54
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Compilerspaceoptimizations
Regularcompileroptimizationssimplifyingcodealsoreducesize Youcanalsoreducethesizeofexecutablesbyaskinggccto optimizegeneratedcodesize: gccOsohusbandhusband.c OscorrespondstoO2optimizationsexcepttheonesincreasing size,plusextrasizespecificones. Osisalreadyusedbydefaulttobuildbusybox. Possibletofurtherreducethesizebycompilingandoptimizingall sourcesatonce,withthefwholeprogramcombinegcc options. See[Link] forallgccoptimizationoptions.
55
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Simplegccoptimizationbenchmark
Executable size
250000
200000
150000
None O2 (generic) O3 (speed) Os (size)
100000
50000
Busybox
Dropbear
56
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Restartableapplications
WhenRAMisscarce,canbeusefultoabortapplicationsthatare notinuse(forexamplehiddengraphicalinterfaces). BettertodoitbeforetheLinuxKernelOOM (OutOfMemory)killercomesandmakesbaddecisions. YoucanusetheLinuxCheckpoint/Restartprojecttohavethe Linuxkernelsavethestateofarunningapplicationsothatitcan laterresumeitsexecutionfromthetimeatwhichitwas checkpointed. See[Link]
57
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Compressingfilesystems
Cansignificantlyincreaseyourstoragecapacity MTD(flashorROM)storage:useUBIFS orJFFS2forsmallpartitions. Blockstorage:useSquashFS([Link] [Link] betterandismuchfastertoo.
58
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Mergingduplicatefiles
Softwarecompilingandinstallingoftencreateduplicatefiles... Checkthatyourrootfilesystemdoesntcontainany! dupmerge2:[Link] Replacesduplicatefilesbyhardlinks. clink:[Link] Replacesduplicatefilesbysymboliclinks. Example:saves4%oftotalspaceinFedoraCore5. finddup:[Link] Findsduplicatefiles.
59
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
EmbeddedLinuxOptimizations
Reducingpowerconsumption
60
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Ticklesskernel
Kernelconfiguration:NO_HZsettinginProcessortypeandfeatures Toimplementmultitasking,theprocessorreceivesatimerinterrupt atagivenfrequency(every4msbydefaultonLinux2.6).Onidlesystems, thiswakesuptheprocessorallthetime,justtorealizethereisnothingtodo! Idea:whenallprocessorsareidle,disablethetimerinterrupt,andreenableit whensomethinghappens(arealinterrupt).Thissavespowerinlaptops,in embeddedsystemsandwithvirtualservers! 2.6.24:supportsx86,arm,mipsandpowerpc
61
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
PowerTOP
[Link] Withdynamicticks,allowstofixpartsofkernelcodeand applicationsthatwakeupthesystemtoooften. PowerTOPallowstotracktheworstoffenders NowavailableonARMcpusimplementingCPUidle Alsogivesyouusefulhints forreducingpower.
62
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
PowerTOPinaction
63
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
cpufreq
Configuration:CPU_FREQinPowermanagementoptions AllowstochangetheCPUfrequencyonthefly Supportedarchitectures(2.6.20): i386,sh,ia64,sparc64,x86_64,powerpc,arm([Link]). Usuallycontrolledfromuserspacethrough/sysbyauser configurablegovernorprocess,accordingtoCPUload,heat,battery status...Themostcommoniscpuspeed: [Link] Savesasignificantamountofbatterylifeinnotebooks.
64
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
SuspendhiddenGUIs
Idea:suspendhiddenuserinterfacestosaveCPUandpower. Sendasuspend(stop)signal: killSIGTSTP<pid> Sendacontinuesignal: killSIGCONT<pid>
65
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Softwaresuspend
[Link] Lotsofgreatfeaturesfornotebookusers,suchasRAMsuspend orhibernatetodisk. Unfortunately,restrictedonsomeIntelcompatibleprocessors andtargetingonlymachineswithAPMorACPI(rarelyfoundin nonPCembeddedsystems!). Notaddressingtherequirementsofembeddedsystems(support forotherCPUs,voltagereduction...).
66
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Powermanagementresources
[Link] OurpresentationonpowermanagementintheLinuxkernel WhatyouneedtoimplementinyourBSPanddevicedrivers. [Link] IntelefforttryingtocreateaLinuxpowersavingcommunity. MainlytargetsIntelprocessors. Lotsofusefulresources. [Link] OngoingdevelopmentontheARMplatform. Tipsandideasforprolongingbatterylife: [Link]
67
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
EmbeddedLinuxOptimizations
Globalperspective
Costandcombinedoptimizationeffects
68
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Combinedbenefits
Speedincrease Morespeed LessRAM
Fasterallocations Lessswapping Sometimesless cacheflushing Fasterapplication loadingfromstorage andinRAM. Sometimes,simpler, fastercode. LessRAMusage
RAMreduction Powerreduction
CPUcanrunslowerorstaylonger inpowersavingmode Fewer/smallerRAMchips:less dynamicandstandbypower. CPUwithlesscache:lesspower Fewer/smallerstoragechips:less power
Costreduction
Slower,cheaper CPU Fewer/cheaper RAMchips CPUwithless cache:cheaper Fewer/cheaper storage
Lessspace
Lesspower
Cheaperbatteries orcheaperAC/DC converter
69
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Theultimateoptimizationtool!
Wehaveseenmanywaystooptimize anexistingsystem. However,nothingreplacesagooddesign! So,firstcarefullydesignandimplement yoursystemandapplicationswiththeirrequirements inmind. Then,usetheoptimizationtechniquestofurtherimprove yoursystemandthepartsthatyoureused(kerneland applications).
70
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Relateddocuments
Allourtechnicalpresentations on[Link] Linuxkernel Devicedrivers Architecturespecifics EmbeddedLinuxsystemdevelopment
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Howtohelp
Youcanhelpustoimproveandmaintainthisdocument... Bysendingcorrections,suggestions,contributionsand translations Byaskingyourorganizationtoorderdevelopment,consulting andtrainingservicesperformedbytheauthorsofthese documents(see[Link] Bysharingthisdocumentwithyourfriends,colleagues andwiththelocalFreeSoftwarecommunity. Byaddinglinksonyourwebsitetoouronlinematerials, toincreasetheirvisibilityinsearchengineresults.
[Link],driversandembeddedLinuxdevelopment,consulting,[Link]//[Link]
Linuxkernel Linuxdevicedrivers Boardsupportcode Mainstreamingkernelcode Kerneldebugging EmbeddedLinuxTraining Allmaterialsreleasedwithafreelicense! UnixandGNU/Linuxbasics Linuxkernelanddriversdevelopment RealtimeLinux,uClinux Developmentandprofilingtools Lightweighttoolsforembeddedsystems Rootfilesystemcreation Audioandmultimedia Systemoptimization
FreeElectrons
Ourservices
CustomDevelopment Systemintegration EmbeddedLinuxdemosandprototypes Systemoptimization Applicationandinterfacedevelopment Consultingandtechnicalsupport Helpindecisionmaking Systemarchitecture Systemdesignandperformancereview Developmenttoolandapplicationsupport Investigatingissuesandfixingtoolbugs