728x90



Kernel 단위에서 System Call 과 함께 이용하면 좀 더 정밀한 ns 단위로 결과 측정이 가능하다. 물론 이 부분에서 insmod 와 rmmod 를 해줘야 한다는 단점이 있지만, 원하는 시간대에 ns 단위로 측정이 가능하다는 소리 자체가 엄청난 이득이기 때문이다.


#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/time.h>
#include <linux/delay.h>
static int __init initialize(void)
{
    ktime_t start, end;
    s64 actual_time;
    int i;
    for(i=0;i<10;i++)
    {
        start = ktime_get();
            ndelay(100);            
        end = ktime_get();
        actual_time = ktime_to_ns(ktime_sub(end, start));
        printk("%lld\n",(long long)actual_time);    
    }
    return 0;
}

static void __exit final(void)
{
     printk(KERN_INFO "Unload module\n");
}

module_init(initialize);
module_exit(final);

MODULE_AUTHOR("Remoted");
MODULE_DESCRIPTION("delay of 100ns");
MODULE_LICENSE("GPL");


조금더 복잡한 이유는 일부 모듈이 환경변수 Path 에 존재하지 않는다는 점이다. Kernel 단계에서 컴파일 되는 영역이기 때문에, 여기서 필요한 라이브러리들을 불러다 쓰려면 Makefile 을 작성해야 하는 점이 있다.


You need to build the module within the Context of the Linux tree. By default, the compiler will look for user-space headers in /usr/include. There ARE some linux headers there (/usr/include/linux), but module.h is not one of them, as it deals with kernel constructs directly.

In short, you need a Makefile. Also, get rid of the #define MODULE. save the following to a file called Makefile and run make:

Code:
obj-m += foo.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

This triggers the kernel build system.

Given that you are using Ubuntu, you probably already have the kernel headers installed at /usr/src/linux-headers-$(uname -r). module.h lives here:

Code:
jameson@aqua:~$ ls /usr/src/linux-headers-$(uname -r)/include/linux/module.h
/usr/src/linux-headers-3.0.0-12-generic/include/linux/module.h


+ Recent posts