4.  Performance Improvements

      This section outlines the changes made to the system since the 4.2BSD distribution. The changes reported here were made in response to the problems described in Section 3. The improvements fall into two major classes; changes to the kernel that are described in this section, and changes to the system libraries and utilities that are described in the following section.

4.1.  Performance Improvements in the Kernel

      Our goal has been to optimize system performance for our general timesharing environment. Since most sites running 4.2BSD have been forced to take advantage of declining memory costs rather than replace their existing machines with ones that are more powerful, we have chosen to optimize running time at the expense of memory. This tradeoff may need to be reconsidered for personal workstations that have smaller memories and higher latency disks. Decreases in the running time of the system may be unnoticeable because of higher paging rates incurred by a larger kernel. Where possible, we have allowed the size of caches to be controlled so that systems with limited memory may reduce them as appropriate.

4.1.1.  Name Cacheing

      Our initial profiling studies showed that more than one quarter of the time in the system was spent in the pathname translation routine, namei, translating path names to inodes[note 15] . An inspection of namei shows that it consists of two nested loops. The outer loop is traversed once per pathname component. The inner loop performs a linear search through a directory looking for a particular pathname component.

      Our first idea was to reduce the number of iterations around the inner loop of namei by observing that many programs step through a directory performing an operation on each entry in turn. To improve performance for processes doing directory scans, the system keeps track of the directory offset of the last component of the most recently translated path name for each process. If the next name the process requests is in the same directory, the search is started from the offset that the previous name was found (instead of from the beginning of the directory). Changing directories invalidates the cache, as does modifying the directory. For programs that step sequentially through a directory with [equation] files, search time decreases from [equation] to [equation] .

      The c[equation] the cache is ab[equation] c(aband 16 bytes per prst[equation] Iuser vect

      As a quick benchmark t[equation] y the maximum effectiveness the cache we ran ``ls -l'' [equation] iles. Befused 22.3 sec[equation] system time. After adding the cache the pr user time, but the system time dr

      This change pr[equation] iled system [equation] Inamei. The results sh[equation] Inamei drstill acc[equation] [equation] the system call time, 18% the kernel, [equation] all the machine cycles. This am[equation] rThe results are sh

D'l |192u 0'
parttime%  kernel
D'l |192u 0'
self11.0 ms/call9.2%
child10.6 ms/call8.9%
D'l |192u 0'
t        D'l 0 |0u-1v'
D'l 0 |0u-1v'
Table 9. Call times f[equation]
Inamei with per-pr

      The small perfwas caused by a lAlth[equation] fective when hit, it was [equation] the names being translated. An additi[equation] alth[equation] time spent in namei itself decreased substantially, msince each direct[equation] rand [equation] r

      Frequent requests f[equation] names are best handled with a cache recent name translatiThis has the effect eliminating the inner l[equation] namei. Fnamei first l[equation] recent translatifIf it exists, the direct

      The system already maintained a cache recently accessed insmaintained a simple name-incheck each c[equation] a path name during name translatiWe cwith its mbut eventually decided tkept names with pTagging inmany inthe in[equation] [equation] time, but are never lup by name. Other infrequently by many different names (e.g. ``..''). By keeping a separate table names, the cache can truly reflect the mAn added benefit is that the table can be sized independently the in[equation] memcan reduce the size the cache (with[equation] ying the in

      Anh[equation] erences tN[equation] erences'' by incrementing the reference c[equation] erence. Since the system reuses [equation] erence ca hard reference insures that the inH[equation] the name cache h[equation] erences, it is limited t[equation] racti[equation] the size the insince s[equation] t free f[equation] iles. It als[equation] [equation] the kernel t[equation] y s[equation] a device [equation] ile. These reas[equation] erences with[equation] fecting the behavi[equation] the inThus, we ch[equation] t references'' prby a capability - a 32-bit number guaranteed tWhen an entry is made in the name cache, the capability its inWhen an inWhen a name cache hit the capability the name cache entry is cwith the capability the in[equation] erences. If the capabilities dSince the name cache h[equation] t references, it may be sized independent the size the inA final benefit using capabilities is that all cached names fsearching thrinstead all y

      The c[equation] the name cache is ab[equation] c(aband 48 bytes per cache entry. Depending [equation] the system, ab[equation] igured, using 10-50 kil[equation] physical memThe name cache is resident in mem

      After adding the system wide name cache we reran ``ls -l'' The user time remained the same, hThis was n[equation] Inamei nbut was never able t[equation] it.

      An[equation] iled system was created and measurements were csh[equation] Inamei, with namei acc[equation] [equation] the system call time, 13% the time in the kernel, [equation] all the machine cycles. System time dr[equation] rThe results are sh

D'l |192u 0'
parttime%  kernel
D'l |192u 0'
self4.2 ms/call6.2%
child4.4 ms/call6.6%
D'l |192u 0'
t        D'l 0 |0u-1v'
D'l 0 |0u-1v'
Table 10.  Call times f[equation]
Inamei with b

      On [equation] ind that during the twelve h[equation] rname translatiStatistics [equation] [equation] bthe large perfcaused by the high hit ratiThe name cache has a hit rate 70%-80%; the direct[equation] fset cache gets a hit rate 5%-15%. The c[equation] the twWith the additi[equation] the twthe percentage system time devdr[equation] rWhile the system wide cache reduces b[equation] time in the r[equation] Inamei calls as well as namei itself (since fewer directit is interesting t[equation] system time spent in namei itself increases even thactual time per call decreases. This is because less thence a smaller abs

4.1.2.  Intelligent Aut

      Mit can either generate an interrupt each time a character is received, T[equation] [equation] la silAscii terminals nan input rate less than 30 characters per secAt this input rate they are m[equation] ficiently handled with interrupt per character msince this generates fewer interrupts than draining the input sil the terminal multiplexWhen input is being generated by an[equation] unctithe input rate is usually mIt is m[equation] ficient tsince this generates fewer interrupts than handling each character as a separate interrupt. Since a given dialup pit is imp[equation] ficient input m

      We thereft[equation] the sil[equation] per-character interrupts. At linterrupt basis, av checking each interface During peri[equation] sustained input, the handler enables the siland starts a timer tThis timer runs less frequently than the cland is used [equation] input. The transiti[equation] rdamped t[equation] transiti[equation] fic (such as in netwInput characters serve tlush the silBy switching between these tw[equation] the [equation] checking the silwhen necessary.

      In additithe cla stware interrupt after each hardware interrupt tThe stware-interrupt level p[equation] the clneeded when timers expire an executi[equation] ile. Thus, the number interrupts attributable tis substantially reduced.

4.1.3.  Pr

      As systems have gr[equation] the prhas gr[equation] ar past 200 entries. With large tables, linear searches must be eliminated fr[equation] requently used facility. The kernel pr active and zA third list threads unused prFree slfr[equation] r[equation] the free list. The number prSince the 4.2BSD release, the kernel maintained linked lists the descendents each prThis linkage is nparents seeking the exit status children n the prIn additi[equation] [equation] inding all descendents an exiting pr[equation] the prThis has been changed t

      When fthe system must assign it a unique pr[equation] ier. The system previa new pr[equation] ier that was nN[equation] the system c[equation] unused identifiers that can be directly assigned. Only when the set identifiers is exhausted is anscan required.

4.1.4.  Scheduling

      PreviPr[equation] priPrbeen sleeping fOn systems running many prthe scheduler represented nearly 20% the system time. Tthe scheduler has been changed trunnable prT[equation] still get their apprtheir priSince the set runnable pr[equation] racti the t[equation] prthe c[equation] inv

4.1.5.  Cl

      The hardware clat high priAs m[equation] the clthe system schedules a l[equation] tware interrupt ttime-critical events such as cpu scheduling and timeOften there are n[equation] tware interrupt handler finds nThe high pri[equation] there are levents tif there is n[equation] tware interrupt is nOften, the high primachine had been running at lRather than p[equation] tware interrupt that wsthe hardware cland calls the stware clBetween these tw[equation] the 100 stware interrupts per sec

4.1.6.  File System

      The file system uses a large blT[equation] iles t[equation] ficiently, the large blbe br[equation] ragments, typically multiples 1024 bytes. T[equation] full-sized blintragments, the file system uses a best fit strategy. Pr[equation] iles using write 1024 bytes can f[equation] ile system tsuccessively larger and larger fragments until it finally gr[equation] ull sized blThe file system still uses a best fit strategy the first time a fragment is written. H[equation] irst time that the file system is ffragment it places it at the beginning a full sized blC[equation] urther cby using up the rest the blIf the file ceases t[equation] the blavailable f[equation] ragments.

      When creating a new file name, the entire directtFBecause there was n[equation] a direct[equation] illed will increase the c file creati[equation] ter the [equation] illing is cThus, fafter it is cleared up. Tthat it finds at the end a directscan t

4.1.7.  Netw

      The default am[equation] buffer space all[equation] pipes) has been increased tStream s[equation] fer sizes in the bl[equation] ield the stat structure. This inf[equation] fering. Unix din the stat structure twith The TCP maximum segment size is calculated accand interface in use; nf

      On multiply-ht[equation] ace that will be used in transmitting data packets fcSeveral bugs in the calculati[equation] rTCP n[equation] ails, ICMP srate TCP streams by temp[equation] icially small send windrather than resending all queued data. A send pthat decreases the number small packets f[equation] fic [Nagle84], pr[equation] netwThe [equation] packet rc[equation]

      The buffer management strategy implemented by s[equation] P has been changed t[equation] the increased size the s[equation] fers and a better tuned delayed acknR[equation] ied t[equation] the last rMultiple messages send with the same destinatiPerf[equation] leither the sender hAlsthe thrWe have [equation] their cycles transmitting netw

4.1.8.  Exec

      When exec-ing a new prprfragain [equation] the newly created prThese tware nThis an argument list by a fact[equation] ten; the average time t[equation] Iexec call decreased by 25%.

4.1.9.  C

      The kernel used t[equation] tware event when it wanted ta prOften the pr[equation] [equation] exiting the kernel, delaying the event trap. At sbe selected tfinally causing the event tThe event wselecting the same prThe fix t[equation] tware reschedule events when saving a prThis change dcan synchr

4.1.10.  Setjmp/L

      The kernel r[equation] Isetjmp, that saves the current system c[equation] registers than necessary under mBy trimming its the [equation] [equation] 13%.

4.1.11.  C[equation] [equation] C

      The current c[equation] d[equation] icant Gthe C language is nbecause its rampant use unbThus, many classical analysis and selecti[equation] register variables must be dby hand using ``exteri[equation] when such [equation] e.

      Anis inline expansi[equation] small [equation] requently used rIn past Berkeley systems this has been d[equation] Ised trun r[equation] [equation] the r[equation] ten a single VAX instructiWhile this [equation] the subrcall and return, it did n[equation] several arguments tThe sed script has been replaced by a minline, that merges the pushes and pF[equation] the C c

if (scanc(map[i], 1, 47, i - 63))
is cin the left hand c[equation] Table 11. The sed inline expander changes this cshThe newer [equation] the stack
D'l |408u 0'
Alternative C Language CD'l |408u 0'
ccsedinline
D'l |408u 0'
subl3$64,_i,-(sp)subl3$64,_i,-(sp)subl3$64,_i,r5
pushl$47pushl$47mpushl$1pushl$1pushl$1
mull2$16,_i,r3mull2$16,_i,r3mull2$16,_i,r3
pushl-56(fp)[r3]pushl-56(fp)[r3]m[equation]
p)[r3],r2
calls$4,_scancmtstlr0mjeqlL7mmscancr2,(r3),(r4),r5
tstlr0
jeqlL7
            D'l 0 |0u-1v'
      D'l 0 |0u-1v'
                 D'l 0 |0u-1v'
D'l 0 |0u-1v'
Table 11. Alternative inline c

      Anexisting data structures in the c[equation] the current system. F[equation] fer hashing was implemented when the system typically had thirty tifty buffers. M[equation] fers. C[equation] the hash chains cten t[equation] fers each! The running time the l[equation] fer management primitives was dramatically impr[equation] the hash table.

4.2.  Impr

      Intuitively, changes tpayf since they affect all prH[equation] [equation] [equation] icant imprBy c[equation] the libraries and utilities had never been tuned. F[equation] [equation] their running time dChanging the utility trunning time by a fact[equation] five! Thus, while m[equation] m[equation] the speedups are because impr[equation] the system. S[equation] the m[equation] subsecti

4.2.1.  Hashed Databases

      UNIX pr[equation] database management r[equation] Idbm, that can be used t[equation] iles with an external hashed index file. The [equation] dbm was designed tdatabase at a time. These rmultiple database files, enabling them t the passw[equation] ile lused t[equation] ile significantly imprtime many impthe C-shell (in d[equation] Ils -l, etc.

4.2.2.  Buffered I/O

      The new filesystem with its larger blperf[equation] by perf[equation] ers rather than using appr[equation] fers. The standard I/O library aut[equation] fer size f[equation] ile. SI/O [equation] fering, hSeveral impand were buffering I/O using the [equation] fer size, 1Kbytes; the pr[equation] fer I/O acc[equation] ile system blThese include the edit[equation] ile cthe text f

      The standard err[equation] fered tand t[equation] r[equation] buffers are n[equation] lushed. The in[equation] sending single-byte packets thrthe netw[equation] fering scheme errWithin a single call t[equation] Ifprintf, all [equation] fered tempBef[equation] lushed and the stream is again marked unbuffered. As bef[equation] fering mechanisms can be used instead the default behavi

      It is p[equation] defeat the standard I/O library's ch[equation] I/O buffer size by using the setbuf call t[equation] fer. Because p[equation] ault buffer size prby setbuf is 1024 bytes; this can lead, One such pr[equation] Icat; there are undas the system has changed much since they were

4.2.3.  Mail System

      The pr[equation] icant w[equation] irst pr[equation] ied was a bug in the sysl[equation] P pr[equation] Isendmail lc[equation] acilities. Sysl[equation] P then rec[equation] a l[equation] ile. Unf[equation] Isysl[equation] P was perf[equation] Isync [equation] ter each message it received, whether it was l[equation] ile [equation] fectiveness the buffer cache and explained, textent, why sending mail theavy l[equation] message recipient causing alm[equation] sync

      The hashed data base files were installed in all mail pr[equation] magnitude speedup [equation] I/bin/mail that n[equation] ies the c[equation] P pra user was changed tspeedup

      Next, the file l[equation] acilities pr[equation] Ifl[equation] P(2), were used in place the lThe mail system previ[equation] Ilink and unlink in implementing file lBecause these [equation] y the c[equation] directthey require synchradvantage the name cache maintained by the system. Unlink requires that the entry be fit can be remlink requires that the directdBy c[equation] acility in 4.2BSD is efficient because it is all dThus, the mail system was m[equation] ied t[equation] ile lThis yielded an[equation] delivering mail. Extensive priling and tuning sendmail and c

4.2.4.  Netw

      With the intr[equation] the netw[equation] acilities in 4.2BSD, a myriad services became available, each which required its Many these daem[equation] ever used, yet they lay asleep in the prsystem resRather than having many servers started at binetd was substituted. This pr[equation] igurati[equation] ile that specifies the services the system is willing tand listens fWhen a client requests service the apprand passed a service cthat require the identity their client may use the getpeername system call; likewise gets[equation] P may be used tind a server's l[equation] iles. This scheme is attractive f

      With an increased numbers netwwe f[equation] the rinSeveral changes were made in the rR[equation] ault gateway. This reduces the am[equation] netw[equation] fic and the time required tIn additi[equation] iled and functi[equation] time were The maj[equation] aster hashing scheme, and inline expansi[equation] the ubiquit[equation] uncti

      Under certain circumstances, when attempts by the remtalth[equation] Iselect call had indicated that data cThis resulted in cuser restarted This prand the

4.2.5.  The C Run-time Library

      Several pe[equation] in frequently used r[equation] In particular the running time the string rcut in half by rewriting them using the VAX string instructiThe memmem[equation] [equation] twCertain library r[equation] ile input in have been cOther library r[equation] Ifread and fwrite have been rewritten f[equation] ficiency.

4.2.6.  Csh

      The C-shell was cwriting a set rWhile this pr[equation] unctiit was gr[equation] ficient, generating up tThe C-shell has been m[equation] ied tfacilities directly, cutting the number system calls per pr[equation] . Additi[equation] priling t[equation] frequently used facilities.