参考Linux内核代码: 3.10
crash> kmem -S sock_inode_cache > \
kmem-S-sock_inode_cache
cat ./kmem-S-sock_inode_cache
struct kmem_cache *sock_inode_cache 的地址为 ffff881020880fc0
struct slab *slab的地址为 ffff8801de2ca000
slab->s_mem的地址为 0xffff8801de2ca080
crash> struct slab ffff8801de2ca000
struct slab {
nodeid = 0
s_mem = 0xffff8801de2ca080,(指向第一个object)
..
}
可以看到上面的slab在节点0上,有6个object. 其中有三个是share cache,说明是给本节点上所有cpu公用,其中有一个在cpu4上。另外两个还未指定用途。
ffff8801de2ca080 (shared cache)
ffff8801de2ca300
ffff8801de2ca580 (cpu 4 cache)
ffff8801de2ca800 (shared cache)
ffff8801de2caa80
ffff8801de2cad00 (shared cache)
上面的struct slab结构挂在kmem_list3结构的slabs_partial、slabs_full或者slabs_free链表上。
struct kmem_list3 {
struct list_head slabs_partial;
struct list_head slabs_full;
struct list_head slabs_free;
...
}
先看看这个cpu4上的object,既然这个是在cpu4上的object,看看是否能在cpu4上的sock_inode_cache缓存找到这个object ffff8801de2ca580
然后再找出(ffff8801de2ca080 (shared cache))。
找出这两个值,就知道slab到底是怎么回事了。
struct kmem_cache {
/* 1) per-cpu data, touched during every alloc/free */
struct array_cache *array[NR_CPUS];
const char *name;
struct list_head next;
...
struct kmem_list3 *nodelists
[MAX_NUMNODES];
}
crash> struct kmem_cache \
ffff881020880fc0
struct kmem_cache {
array = {0xffff881020843a00, \
0xffff881020843800, \
0xffff881020843600, \
0xffff881020843400,…}
....
name = 0xffffffff817d2db7 "sock_inode_cache",
next = {
next = 0xffff881020878fd8,
prev = 0xffff881020939058
},
nodelists = {0xffff881020846dc0, 0xffff8820202f4540,..}
}
struct array_cache {
unsigned int avail;
..
void *entry[]; /*
}
可以看到entry是一个指针数组,数组里面每一个元素就是本cpu上一个object的值。
上面struct kmem_cache中的array数组里面有多个元素,其中第四项就是0xffff881020843400 (对应cpu4)
crash> struct array_cache \
0xffff881020843400
struct array_cache {
…
entry = 0xffff881020843418
}
输出0x10个object, 发现第一个值就是ffff88023f5ff580,
crash> rd ffff881020843418 0x10
ffff881020843418: ffff88023f5ff580 ffff88107f4ffd00
ffff881020843428: ffff8801bc2bd0c0 ffff880e5d3c00c0
ffff881020843438: ffff8806a7453d40 ffff880209cfcac0
crash> struct kmem_list3 \
0xffff881020846dc0
struct kmem_list3 {
slabs_partial = {
slabs_full = {
slabs_free = {
...
},
shared = 0xffff881020844000,
alien = 0xffff88102083d900,
...
}
crash> struct array_cache \
0xffff881020844000
struct array_cache {
…
entry = 0xffff881020844018
}
同样的entry数组中的每个值就是本节点上share cache Object.
crash> rd 0xffff881020844018 0x100 \
> rd-0xffff881020844018
vim ./rd-0xffff881020844018
在59行看到
(ffff8801de2ca080 (shared cache))
Line58 ffff8810208443a8: ffff8802ec1165c0 \
ffff88107f554800
Line59 ffff8810208443b8: ffff8801de2ca080 \
ffff8804b1641340
Note: linux在3.12以后去掉了struct slab结构,但是原理没什么变化。
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8