我的内核模块使用了多少内存?

res*_*way 10 c linux memory kernel

lsmod,/ proc/modules和slabinfo,/ proc/meminfo不会给我的内核模块使用多少内存

有没有办法找到这个?

顺便说一句,我基本上写了一个小测试程序,一个设备驱动程序,它接受ioctl调用以分配1MB,我每秒从我的应用程序发送这个ioctl消息,所以我的驱动器每秒都会执行kmalloc.我无法看到"cat/proc/meminfo | grep Slab"的增加

- 剪断---

int device_ioctl(
         struct file *file,
         unsigned int ioctl_num, 
         unsigned long ioctl_param)
{
    /* 
     * Switch according to the ioctl called 
     */
    printk ( "<l> inside ioctl %d IOCTL_ALLOC_MSG = %d\n", ioctl_num,IOCTL_ALLOC_MSG );
    switch (ioctl_num) {
    case IOCTL_ALLOC_MSG:
        allocfunc(); // kmalloc 1MB // printk in this function is OK
        break;
    case IOCTL_DEALLOC_MSG:
        deallocfunc();
        break;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

应用/用户空间

 while ( !stop )
        {
            ret_val = ioctl(memfile, IOCTL_ALLOC_MSG);

            if (ret_val < 0) {
                printf("ioctl failed. Return code: %d, meaning: %s\n", ret_val, strerror(errno));
                return -1;
            }
            sleep ( 10 );

        }
Run Code Online (Sandbox Code Playgroud)

我看不到slabinfo中记忆的增长.我知道linux会执行cache-> slabs-> pages-> objects,但是在用户区中必须有一些方法来确定特定内核模块的内存大小.

谢谢,

ams*_*ams 0

假设没有办法直接做到这一点(据我所知,可能有)......

您可以使用LTTng来跟踪您的内核事件。如果那里已经没有方便的事件,那么即使每次模块分配内存时,您也应该创建一个新的跟踪。

然后,您可以分析跟踪并绘制内存使用量如何随时间增长和收缩的图表。