slab.ps

来自「这是一个介绍 linux 编程知识的文章。」· PS 代码 · 共 1,690 行 · 第 1/5 页

PS
1,690
字号
(\347)3110 6210 w
(\347)3674 6770 w
(\347)3674 6760 w
(\347)3674 6660 w
(\347)3674 6560 w
(\347)3674 6460 w
(\347)3674 6360 w
(\347)4388 6770 w
(\347)4388 6760 w
(\347)4388 6660 w
(\347)4388 6560 w
(\347)4388 6460 w
(\347)4388 6360 w
(\347)4963 6770 w
(\347)4963 6760 w
(\347)4963 6660 w
(\347)4963 6560 w
(\347)4963 6460 w
(\347)4963 6360 w
(\347)5270 6770 w
(\347)5270 6710 w
(\347)5270 6610 w
(\347)5270 6510 w
(\347)5270 6410 w
(\347)5270 6310 w
(\347)5270 6210 w
10 R f
( in a mul-)3 488(Caching is particularly bene\256cial)3 1422 2 3360 6974 t
(tithreaded environment, where many of the most)6 2160 1 3110 7094 t
cleartomark
showpage
restore
%%EndPage: 1 1
%%Page: 2 2
save
mark
2 pagesetup
10 R f
( one or more)3 624(frequently allocated objects contain)3 1536 2 590 696 t
( and other con-)3 637(embedded locks, condition variables,)3 1523 2 590 816 t
(structible state.)1 613 1 590 936 t
( an object cache is)4 1019(The design of)2 691 2 840 1140 t
(straightforward:)590 1260 w
10 B f
(To allocate an object:)3 942 1 790 1422 t
10 R f
(if \(there's an object in the cache\))6 1360 1 922 1602 t
(take it \(no construction required\);)4 1374 1 1054 1722 t
(else)922 1842 w
10 S f
({)1110 1842 w
10 R f
(allocate memory;)1 704 1 1054 1962 t
(construct the object;)2 826 1 1054 2082 t
10 S f
(})922 2202 w
10 B f
(To free an object:)3 780 1 790 2382 t
10 R f
(return it to the cache \(no destruction required\);)7 1921 1 922 2562 t
10 B f
(To reclaim memory from the cache:)5 1571 1 790 2742 t
10 R f
(take some objects from the cache;)5 1395 1 922 2922 t
(destroy the objects;)2 793 1 922 3042 t
(free the underlying memory;)3 1169 1 922 3162 t
( must be initial-)3 694(An object's constructed state)3 1216 2 840 3366 t
( \256rst brought)2 557(ized only once \320 when the object is)7 1603 2 590 3486 t
( is populated, allo-)3 810( the cache)2 440( Once)1 289(into the cache.)2 621 4 590 3606 t
(cating and freeing objects are fast, trivial operations.)7 2154 1 590 3726 t
11 B f
( Example)1 452(2.1. An)1 379 2 590 4026 t
10 R f
(Consider the following data structure:)4 1547 1 590 4230 t
9 CW f
(struct foo {)2 648 1 790 4392 t
(kmutex_t foo_lock;)1 1080 1 1006 4512 t
(kcondvar_t foo_cv;)1 972 1 1006 4632 t
(struct bar *foo_barlist;)2 1296 1 1006 4752 t
(int foo_refcnt;)1 1188 1 1006 4872 t
(};)790 4992 w
10 R f
(Assume that a)2 600 1 590 5196 t
10 CW f
(foo)1259 5196 w
10 R f
(structure cannot be freed until)4 1269 1 1481 5196 t
( it)1 177(there are no outstanding references to)5 1983 2 590 5316 t
(\()590 5436 w
10 CW f
(foo_refcnt == 0)2 910 1 623 5436 t
10 R f
( all of its pending)4 756(\) and)1 215 2 1533 5436 t
10 CW f
(bar)2570 5436 w
10 R f
( are\) have completed)3 1103(events \(whatever they)2 1057 2 590 5556 t
(\()590 5676 w
10 CW f
(foo_barlist == NULL)2 1170 1 623 5676 t
10 R f
( a)1 93( life cycle of)3 570(\). The)1 294 3 1793 5676 t
( something like)2 653(dynamically allocated foo would be)4 1507 2 590 5796 t
(this:)590 5916 w
9 CW f
(foo = kmem_alloc\(sizeof \(struct foo\),)4 1998 1 3310 696 t
(KM_SLEEP\);)3526 816 w
(mutex_init\(&foo->foo_lock, ...\);)1 1728 1 3310 936 t
(cv_init\(&foo->foo_cv, ...\);)1 1458 1 3310 1056 t
(foo->foo_refcnt = 0;)2 1080 1 3310 1176 t
(foo->foo_barlist = NULL;)2 1296 1 3310 1296 t
(use foo;)1 432 1 3310 1536 t
(ASSERT\(foo->foo_barlist == NULL\);)2 1782 1 3310 1776 t
(ASSERT\(foo->foo_refcnt == 0\);)2 1566 1 3310 1896 t
(cv_destroy\(&foo->foo_cv\);)3310 2016 w
(mutex_destroy\(&foo->foo_lock\);)3310 2136 w
(kmem_free\(foo\);)3310 2256 w
10 R f
(Notice that between each use of a foo object we)9 2160 1 3110 2418 t
( constitutes)1 486(perform a sequence of operations that)5 1674 2 3110 2538 t
( of)1 128( All)1 206( no-op.)1 303(nothing more than a very expensive)5 1523 4 3110 2658 t
( everything other than ``use foo'')5 1392(this overhead \(i.e.,)2 768 2 3110 2778 t
(above\) can be eliminated by object caching.)6 1802 1 3110 2898 t
11 B f
( the)1 213( Case for Object Caching in)5 1495(2.2. The)1 452 3 3110 3210 t
(Central Allocator)1 833 1 3110 3330 t
10 R f
(Of course, object caching can be implemented)6 2160 1 3110 3492 t
( \320 any)2 332(without any help from the central allocator)6 1828 2 3110 3612 t
( private implementation of the)4 1262(subsystem can have a)3 898 2 3110 3732 t
( there are)2 472( However,)1 500(algorithm described above.)2 1188 3 3110 3852 t
(several disadvantages to this approach:)4 1590 1 3110 3972 t
( between an object)3 835( is a natural tension)4 893(\(1\) There)1 432 3 3110 4134 t
( the)1 170(cache, which wants to keep memory, and)6 1790 2 3310 4254 t
( system, which wants that memory)5 1513(rest of the)2 447 2 3310 4374 t
( handle)1 306( caches cannot)2 613(back. Privately-managed)1 1041 3 3310 4494 t
( have limited)2 658( They)1 331(this tension sensibly.)2 971 3 3310 4614 t
(insight into the system's overall memory needs)6 1960 1 3310 4734 t
(and)3310 4854 w
10 I f
(no)3499 4854 w
10 R f
( Simi-)1 302(insight into each other's needs.)4 1324 2 3644 4854 t
( system has no knowledge)4 1113(larly, the rest of the)4 847 2 3310 4974 t
( hence has)2 447(of the existence of these caches and)6 1513 2 3310 5094 t
(no way to ``pull'' memory from them.)6 1582 1 3310 5214 t
( central alloca-)2 630( private caches bypass the)4 1108(\(2\) Since)1 422 3 3110 5376 t
(tor, they also bypass any accounting mechan-)6 1960 1 3310 5496 t
( allocator may)2 597(isms and debugging features that)4 1363 2 3310 5616 t
( the operating system)3 1011( makes)1 334(possess. This)1 615 3 3310 5736 t
(more dif\256cult to monitor and debug.)5 1495 1 3310 5856 t
( to)1 117( many instances of the same solution)6 1549(\(3\) Having)1 494 3 3110 6018 t
(a common problem increases kernel code size)6 1960 1 3310 6138 t
(and maintenance costs.)2 939 1 3310 6258 t
( of coopera-)2 503(Object caching requires a greater degree)5 1657 2 3110 6420 t
( the)1 172(tion between the allocator and its clients than)7 1988 2 3110 6540 t
(standard)3110 6660 w
10 CW f
(kmem_alloc\(9F\)/kmem_free\(9F\))3590 6660 w
10 R f
( next section develops an)4 1192( The)1 261(interface allows.)1 707 3 3110 6780 t
(interface to support constructed object caching in)6 2160 1 3110 6900 t
(the central allocator.)2 833 1 3110 7020 t
cleartomark
showpage
restore
%%EndPage: 2 2
%%Page: 3 3
save
mark
3 pagesetup
11 B f
( Cache Interface)2 788(2.3. Object)1 554 2 590 696 t
10 R f
( here follows from two)4 1123(The interface presented)2 1037 2 590 858 t
(observations:)590 978 w
( of objects \(name, size, alignment,)5 1455(\(A\) Descriptions)1 705 2 590 1140 t
( in the)2 374(constructor, and destructor\) belong)3 1586 2 790 1260 t
( The)1 251( the central allocator.)3 956(clients \320 not in)3 753 3 790 1380 t
( just ``know'' that)3 988(allocator should not)2 972 2 790 1500 t
10 CW f
( inode\))1 426(sizeof \(struct)1 845 2 790 1620 t
10 R f
(is a useful pool)3 650 1 2100 1620 t
( assumptions are brittle)3 960( Such)1 269(size, for example.)2 731 3 790 1740 t
( needs)1 263([Grunwald93A] and cannot anticipate the)4 1697 2 790 1860 t
( modules)1 387(of third-party device drivers, streams)4 1573 2 790 1980 t
(and \256le systems.)2 680 1 790 2100 t
( belong in the)3 667( management policies)2 949(\(B\) Memory)1 544 3 590 2262 t
( The)1 254( in its clients.)3 657(central allocator \320 not)3 1049 3 790 2382 t
( want to allocate and free objects)6 1499(clients just)1 461 2 790 2502 t
( about)1 270( shouldn't have to worry)4 1078(quickly. They)1 612 3 790 2622 t
( underlying memory)2 972(how to manage the)3 988 2 790 2742 t
(ef\256ciently.)790 2862 w
( cache creation must)3 868(It follows from \(A\) that object)5 1292 2 590 3024 t
( a full speci\256cation)3 787(be client-driven and must include)4 1373 2 590 3144 t
(of the objects:)2 582 1 590 3264 t
9 CW f
( kmem_cache *kmem_cache_create\()2 1674(\(1\) struct)1 504 2 590 3426 t
(char *name,)1 594 1 878 3546 t
(size_t size,)1 648 1 878 3666 t
(int align,)1 540 1 878 3786 t
(void \(*constructor\)\(void *, size_t\),)3 1944 1 878 3906 t
(void \(*destructor\)\(void *, size_t\)\);)3 1944 1 878 4026 t
10 R f
(Creates a cache of objects, each of size)7 1632 1 790 4188 t
10 CW f
(size)2485 4188 w
10 R f
(,)2725 4188 w
( an)1 142(aligned on)1 443 2 790 4308 t
10 CW f
(align)1450 4308 w
10 R f
( align-)1 281(boundary. The)1 671 2 1798 4308 t
( rounded up to the)4 975(ment will always be)3 985 2 790 4428 t
(minimum allowable value, so)3 1255 1 790 4548 t
10 CW f
(align)2121 4548 w
10 R f
(can be)1 280 1 2470 4548 t
( alignment is required.)3 926(zero whenever no special)3 1034 2 790 4668 t
10 CW f
(name)790 4788 w
10 R f
( statistics and)2 621(identi\256es the cache for)3 1032 2 1097 4788 t
(debugging.)790 4908 w
10 CW f
(constructor)1336 4908 w
10 R f
(is a function that)3 714 1 2036 4908 t
( one-time ini-)2 594(constructs \(that is, performs the)4 1366 2 790 5028 t
( cache;)1 288(tialization of\) objects in the)4 1131 2 790 5148 t
10 CW f
(destruc-)2270 5148 w
(tor)790 5268 w
10 R f
( construc-)1 416( The)1 233( if applicable.)2 586(undoes this,)1 499 4 1016 5268 t
( destructor take a)3 765(tor and)1 306 2 790 5388 t
10 CW f
(size)1940 5388 w
10 R f
(argument so)1 518 1 2232 5388 t
( can support families of similar)5 1551(that they)1 409 2 790 5508 t
(caches, e.g. streams messages.)3 1960 1 790 5628 t
10 CW f
(kmem_cache_create)790 5748 w
10 R f
( opaque)1 381(returns an)1 465 2 1904 5748 t
(descriptor for accessing the cache.)4 1402 1 790 5868 t
(Next, it follows from \(B\) that clients should need)8 2160 1 590 6030 t
( simple functions to allocate and free)6 1787(just two)1 373 2 590 6150 t
(objects:)590 6270 w
9 CW f
( *kmem_cache_alloc\()1 1026(\(2\) void)1 396 2 590 6432 t
(struct kmem_cache *cp,)2 1188 1 878 6552 t
(int flags\);)1 594 1 878 6672 t
10 R f
( object will)2 472( The)1 224( cache.)1 287(Gets an object from the)4 977 4 790 6834 t
(be in its constructed state.)4 1147 1 790 6954 t
10 CW f
(flags)2050 6954 w
10 R f
(is either)1 347 1 2403 6954 t
10 CW f
(KM_SLEEP)790 7074 w
10 R f
(or)1385 7074 w
10 CW f
(KM_NOSLEEP)1610 7074 w
10 R f
(, indicating)1 540 1 2210 7074 t
( to wait for memory if)5 995(whether it's acceptable)2 965 2 3310 696 t
(none is currently available.)3 1105 1 3310 816 t
9 CW f
( kmem_cache_free\()1 918(\(3\) void)1 396 2 3110 978 t
(struct kmem_cache *cp,)2 1188 1 3398 1098 t
(void *buf\);)1 594 1 3398 1218 t
10 R f
( object)1 306( The)1 249(Returns an object to the cache.)5 1405 3 3310 1380 t
(must still be in its constructed state.)6 1479 1 3310 1500 t
( needed the client can)4 900(Finally, if a cache is no longer)6 1260 2 3110 1662 t
(destroy it:)1 411 1 3110 1782 t
9 CW f
( kmem_cache_destroy\()1 1080(\(4\) void)1 396 2 3110 1944 t
(struct kmem_cache *cp\);)2 1242 1 3398 2064 t
10 R f
( associated)1 455(Destroys the cache and reclaims all)5 1505 2 3310 2226 t
( objects must have)3 888( allocated)1 435(resources. All)1 637 3 3310 2346 t
(been returned to the cache.)4 1103 1 3310 2466 t
( a \257exible allocator)3 810(This interface allows us to build)5 1350 2 3110 2628 t
( In)1 158( needs of its clients.)4 859(that is ideally suited to the)5 1143 3 3110 2748 t
( it)1 101( However,)1 468( is a ``custom'' allocator.)4 1086(this sense it)2 505 4 3110 2868 t
( compile-time)1 617(does not have to be built with)6 1543 2 3110 2988 t
( its clients as most custom allocators)6 1593(knowledge of)1 567 2 3110 3108 t
( Margolin71], nor)2 781(do [Bozman84A, Grunwald93A,)2 1379 2 3110 3228 t
( adaptive-\256t)1 498(does it have to keep guessing as in the)8 1662 2 3110 3348 t
(methods [Bozman84B, Leverett82, Oldehoeft85].)3 2160 1 3110 3468 t
( clients to)2 439(Rather, the object-cache interface allows)4 1721 2 3110 3588 t
(specify the allocation services they need on the \257y.)8 2102 1 3110 3708 t
11 B f
( Example)1 452(2.4. An)1 379 2 3110 4008 t
10 R f
( the use of object cach-)5 1027(This example demonstrates)2 1133 2 3110 4170 t
( objects introduced in Section 2.1.)5 1418(ing for the ``foo'')3 742 2 3110 4290 t
(The constructor and destructor routines are:)5 1783 1 3110 4410 t
9 CW f
(void)3110 4572 w
(foo_constructor\(void *buf, int size\))3 1944 1 3110 4692 t
({)3110 4812 w
(struct foo *foo = buf;)4 1188 1 3326 4932 t
(mutex_init\(&foo->foo_lock, ...\);)1 1728 1 3326 5172 t
(cv_init\(&foo->foo_cv, ...\);)1 1458 1 3326 5292 t
(foo->foo_refcnt = 0;)2 1080 1 3326 5412 t
(foo->foo_barlist = NULL;)2 1296 1 3326 5532 t
(})3110 5652 w
(void)3110 5892 w
(foo_destructor\(void *buf, int size\))3 1890 1 3110 6012 t
({)3110 6132 w
(struct foo *foo = buf;)4 1188 1 3326 6252 t
(ASSERT\(foo->foo_barlist == NULL\);)2 1782 1 3326 6492 t
(ASSERT\(foo->foo_refcnt == 0\);)2 1566 1 3326 6612 t
(cv_destroy\(&foo->foo_cv\);)3326 6732 w
(mutex_destroy\(&foo->foo_lock\);)3326 6852 w
(})3110 6972 w
cleartomark
showpage
restore
%%EndPage: 3 3
%%Page: 4 4
save
mark
4 pagesetup
10 R f
(To create the foo cache:)4 989 1 590 738 t
9 CW f
(foo_cache = kmem_cache_create\("foo_cache",)2 2268 1 590 900 t
(sizeof \(struct foo\), 0,)3 1242 1 806 1020 t

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?