【问题标题】:how much memory is my kernel module using?我的内核模块使用了多少内存?
【发布时间】:2013-02-23 07:32:25
【问题描述】:

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;
}

应用程序/用户空间

 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 );

        }

我没有看到slabinfo中的内存增长。我知道 linux 确实 cache->slabs->pages->objects 但在用户空间中必须有某种方法来确定特定内核模块的内存大小。

谢谢,

【问题讨论】:

    标签: c linux memory kernel


    【解决方案1】:

    我不确定它是否适合您,但是您可以使用 cat /proc/modules 获取模块占用的内存量,第二列是第一列中模块的大小(以字节为单位)是使用。

    显示 drm 模块使用多少内存的示例输出:

    cat /proc/modules |grep ^drm|awk '{print $1 " " $2}'
    

    示例答案:

    drm_kms_helper 49394
    drm 286028
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-14
        • 1970-01-01
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        • 2016-06-30
        • 1970-01-01
        相关资源
        最近更新 更多