首页 > 解决方案 > 如何在linux中查找内核级进程分配的内存?

问题描述

我编写了一个简单的内核模块来删除给定字符串中的所有元音。我想了解进程分配了多少内存、页面交换次数和上下文切换次数。但由于某种原因,sudo pmap pid不返回任何东西。

内核级进程是否有任何其他命令,如 pmap ?

这是c代码(t2.c):

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");

MODULE_AUTHOR("K Jivitesh Narayan");

MODULE_DESCRIPTION("A program to remove all the vowels from a string");

int init_module(void)
{
    char str[20];
    int len, i, j;
    len=strlen(str);
    strcpy(str,"Hello World");
    for(i=0; i<len; i++)
    {
        if(str[i]=='a' || str[i]=='e' || str[i]=='o' || str[i]=='i' || str[i]=='u')
        {
            for(j=i; j<len; j++)
            {
                str[j]=str[j+1];
            }
            len--;
        }

    }
    printk(KERN_INFO "After deleting the vowels, the string will be : %s",str);
    printk(KERN_INFO "The process is \"%s\" (pid %i)\n",
        current->comm, current->pid);

    return 0;
}

void cleanup_module(void){
    printk(KERN_INFO "The program is removed \n"); 
}

我的 Makefile 是:

obj-m += t2.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

在和之后make,我得到输出(最后 2 行):sudo insmod t2.kodmesg

[ 1108.915141] After deleting the vowels, the string will be : Hll Wrld
[ 1108.915144] The process is "insmod" (pid 3299)

的输出sudo pmap 3299什么都没有。没有错误。

**编辑:这是添加 jiffies 模块后的代码

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/jiffies.h>
MODULE_LICENSE("GPL");

MODULE_AUTHOR("K Jivitesh Narayan");

MODULE_DESCRIPTION("A program to remove all the vowels from a string");

int init_module(void)
{
    unsigned long j1 = jiffies + 3 * HZ;
    char str[20];
    int len, i, j;
    len=strlen(str);
    strcpy(str,"Hello World");
    printk(KERN_INFO "The process is \"%s\" (pid %i)\n", current->comm, current->pid);
    while(jiffies < j1);
    for(i=0; i<len; i++)
    {
        if(str[i]=='a' || str[i]=='e' || str[i]=='o' || str[i]=='i' || str[i]=='u')
        {
            for(j=i; j<len; j++)
            {
                str[j]=str[j+1];
            }
            len--;
        }

    }
    printk(KERN_INFO "After deleting the vowels, the string will be : %s",str);

    return 0;
}

void cleanup_module(void){
    printk(KERN_INFO "The program is removed \n"); 
}

标签: clinuxterminallinux-kernel

解决方案


推荐阅读