2012-12-01 41 views
10

मैं कर्नेल प्रोग्रामिंग & पर एक नौसिखिया हूं, मैं इस कर्नेल मॉड्यूल को चलाने के लिए चाहता हूं (नीचे पोस्ट किया गया है ... और मैंने इसके लिए मेकफ़ाइल (नीचे पोस्ट किया) चलाया, लेकिन मुझे निम्न त्रुटियां मिल रही हैं: किसी कृपया मेरी मदद कर सकते मुझे समझने यह कैसे काबू पाने के लिए: गिरी कार्यक्रम मुक्त त्रुटि चलाना चाहिए के रूप में यह Intel's implementation से लिया जाता है:संकलन त्रुटि: कर्नेल मॉड्यूल

obj-m += hello-1.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 

यह त्रुटि है:

[email protected]:~/Desktop/measure$ make 
make -C /lib/modules/3.0.0-12-generic/build M=/home/snehil/Desktop/measure modules 
make[1]: Entering directory `/usr/src/linux-headers-3.0.0-12-generic' 
CC [M] /home/snehil/Desktop/measure/measure1.o 
/home/snehil/Desktop/measure/measure1.c: In function ‘hello_start’: 
/home/snehil/Desktop/measure/measure1.c:108:2: error: implicit declaration of function 
    ‘kmalloc’ [-Werror=implicit-function-declaration] 
/home/snehil/Desktop/measure/measure1.c:108:8: warning: assignment makes pointer from 
integer without a cast [enabled by default] 
/home/snehil/Desktop/measure/measure1.c:115:11: warning: assignment makes pointer from 
integer without a cast [enabled by default] 
/home/snehil/Desktop/measure/measure1.c:124:12: warning: assignment makes pointer from 
integer without a cast [enabled by default] 
/home/snehil/Desktop/measure/measure1.c:130:13: warning: assignment makes pointer from 
integer without a cast [enabled by default] 
cc1: some warnings being treated as errors 

make[2]: *** [/home/snehil/Desktop/measure/measure1.o] Error 1 
make[1]: *** [_module_/home/snehil/Desktop/measure] Error 2 
make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-12-generic' 
make: *** [all] Error 2 
[email protected]:~/Desktop/measure$ gcc measure1 
gcc: error: measure1: No such file or directory 
gcc: fatal error: no input files 
compilation terminated. 

कर्नल मॉड्यूल है कोड:

#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/init.h> 
#include <linux/hardirq.h> 
#include <linux/preempt.h> 
#include <linux/sched.h> 

#define SIZE_OF_STAT 100000 
#define BOUND_OF_LOOP 1000 
#define UINT64_MAX (18446744073709551615ULL) 

void inline Filltimes(uint64_t **times) { 
unsigned long flags; 
int i, j; 
uint64_t start, end; 
unsigned cycles_low, cycles_high, cycles_low1, cycles_high1; 
volatile int variable = 0; 

asm volatile ("CPUID\n\t" 
"RDTSC\n\t" 
"mov %%edx, %0\n\t" 
"mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low)::"%rax", "%rbx", "%rcx", 
      "%rdx"); 
asm volatile ("CPUID\n\t" 
"RDTSC\n\t" 
"CPUID\n\t" 
"RDTSC\n\t" 
"mov %%edx, %0\n\t" 
"mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low):: "%rax", "%rbx", "%rcx", 
"%rdx"); 
asm volatile ("CPUID\n\t" 
"RDTSC\n\t"::: "%rax", "%rbx", "%rcx", "%rdx"); 


for (j=0; j<BOUND_OF_LOOP; j++) { 
for (i =0; i<SIZE_OF_STAT; i++) { 

variable = 0; 

preempt_disable(); 
raw_local_irq_save(flags); 

asm volatile (
"CPUID\n\t" 
"RDTSC\n\t" 
"mov %%edx, %0\n\t" 
"mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low):: "%rax", "%rbx", "%rcx", 
"%rdx"); 
/*call the function to measure here*/ 
asm volatile(
"CPUID\n\t" 
"RDTSC\n\t" 
"mov %%edx, %0\n\t" 
"mov %%eax, %1\n\t": "=r" (cycles_high1), "=r" (cycles_low1):: "%rax", "%rbx", "%rcx", 
"%rdx"); 

raw_local_irq_restore(flags); 
preempt_enable(); 


start = (((uint64_t)cycles_high << 32) | cycles_low); 

end = (((uint64_t)cycles_high1 << 32) | cycles_low1); 

if ((end - start) < 0) { 
printk(KERN_ERR "\n\n>>>>>>>>>>>>>> CRITICAL ERROR IN TAKING THE TIME!!!!!!\n loop(%d) 
stat(%d) start = %llu, end = %llu, variable = %u\n", j, i, start, end, variable); 
times[j][i] = 0; 
} 
else 
{ 
times[j][i] = end - start; 
} 
} 
} 
return; 
} 
uint64_t var_calc(uint64_t *inputs, int size) 
{ 
int i; 
uint64_t acc = 0, previous = 0, temp_var = 0; 
for (i=0; i< size; i++) { 
if (acc < previous) goto overflow; 
previous = acc; 
acc += inputs[i]; 
} 
acc = acc * acc; 
if (acc < previous) goto overflow; 
previous = 0; 
for (i=0; i< size; i++){ 
if (temp_var < previous) goto overflow; 
previous = temp_var; 
temp_var+= (inputs[i]*inputs[i]); 
} 
temp_var = temp_var * size; 
if (temp_var < previous) goto overflow; 
temp_var =(temp_var - acc)/(((uint64_t)(size))*((uint64_t)(size))); 
return (temp_var); 
overflow: 
printk(KERN_ERR "\n\n>>>>>>>>>>>>>> CRITICAL OVERFLOW ERROR IN var_calc!!!!!!\n\n"); 
return -EINVAL; 
} 
static int __init hello_start(void) 
{ 
int i = 0, j = 0, spurious = 0, k =0; 
uint64_t **times; 
uint64_t *variances; 
uint64_t *min_values; 
uint64_t max_dev = 0, min_time = 0, max_time = 0, prev_min =0, tot_var=0,  
max_dev_all=0, var_of_vars=0, var_of_mins=0; 

printk(KERN_INFO "Loading hello module...\n"); 

times = kmalloc(BOUND_OF_LOOP*sizeof(uint64_t*), GFP_KERNEL); 
if (!times) { 
printk(KERN_ERR "unable to allocate memory for times\n"); 
return 0; 
} 

for (j=0; j<BOUND_OF_LOOP; j++) { 
times[j] = kmalloc(SIZE_OF_STAT*sizeof(uint64_t), GFP_KERNEL); 
if (!times[j]) { 
printk(KERN_ERR "unable to allocate memory for times[%d]\n", j); 
for (k=0; k<j; k++) 
kfree(times[k]); 
return 0; 
} 
} 

variances = kmalloc(BOUND_OF_LOOP*sizeof(uint64_t), GFP_KERNEL); 
if (!variances) { 
printk(KERN_ERR "unable to allocate memory for variances\n"); 
return 0; 
} 

min_values = kmalloc(BOUND_OF_LOOP*sizeof(uint64_t), GFP_KERNEL); 
if (!min_values) { 
printk(KERN_ERR "unable to allocate memory for min_values\n"); 
return 0; 
} 


Filltimes(times); 

for (j=0; j<BOUND_OF_LOOP; j++) { 

max_dev = 0; 
min_time = 0; 
max_time = 0; 

for (i =0; i<SIZE_OF_STAT; i++) { 
if ((min_time == 0)||(min_time > times[j][i])) 
min_time = times[j][i]; 
if (max_time < times[j][i]) 
max_time = times[j][i]; 
} 

max_dev = max_time - min_time; 
min_values[j] = min_time; 

if ((prev_min != 0) && (prev_min > min_time)) 
spurious++; 
if (max_dev > max_dev_all) 
max_dev_all = max_dev; 

variances[j] = var_calc(times[j], SIZE_OF_STAT); 
tot_var += variances[j]; 

printk(KERN_ERR "loop_size:%d >>>> variance(cycles): %llu; max_deviation: %llu ;min 
time: %llu", j, variances[j], max_dev, min_time); 

prev_min = min_time; 
} 

var_of_vars = var_calc(variances, BOUND_OF_LOOP); 
var_of_mins = var_calc(min_values, BOUND_OF_LOOP); 
printk(KERN_ERR "\n total number of spurious min values = %d", spurious); 
printk(KERN_ERR "\n total variance = %llu", (tot_var/BOUND_OF_LOOP)); 
printk(KERN_ERR "\n absolute max deviation = %llu", max_dev_all); 
printk(KERN_ERR "\n variance of variances = %llu", var_of_vars); 
printk(KERN_ERR "\n variance of minimum values = %llu", var_of_mins); 

for (j=0; j<BOUND_OF_LOOP; j++) { 
kfree(times[j]); 
} 
kfree(times); 
kfree(variances); 
kfree(min_values); 
return 0; 
} 

static void __exit hello_end(void) 
{ 
printk(KERN_INFO "Goodbye Mr.\n"); 
} 

module_init(hello_start); 
module_exit(hello_end); 

उत्तर

25

यदि आप स्मृति आवंटन के लिए kmalloc या kzalloc() का उपयोग कर रहे हैं तो आपको #include<linux/slab.h> शामिल करना होगा। उन्हें स्लैब आवंटकों के रूप में बुलाया जाता है, ये स्लैब भाग हैं i.e. "कैश" रैम में वर्तमान और शारीरिक रूप से संगत हैं। ये स्लैब आवंटक अंतर्निहित उपयोग करते हैं "बडी सिस्टम एल्गोरिदम", दोस्त आवंटन अधिक जुर्माना आवंटन प्रदान करने के लिए।

फोर अधिक संदर्भ के नीचे दिए गए लिंक के माध्यम से जाना: http://en.wikipedia.org/wiki/Slab_allocation http://en.wikipedia.org/wiki/Buddy_algorithm

आशा यह आपके सवाल का जवाब !!!!!।

+0

धन्यवाद गौथम..मैं एक नज़र डालूंगा..मैं अभी भी इसे हल करने के लिए काम कर रहा हूं .. – Rookie

+0

हैलो गौथम, मैंने आपके समाधान की कोशिश की और कोड संकलित करने में सक्षम था, लेकिन .ko फ़ाइल में "__udivdi3" चेतावनी के साथ अपरिभाषित .. मैं इस कोड को चलाने की इच्छा रखता हूं ... मुझे लगता है कि इसे "insmod ./measure1.ko" कमांड का उपयोग करके कर्नेल में जोड़ा जाना है, लेकिन मुझे एक त्रुटि मिल रही है .: "insmod: error inserting './ measure.ko ': मॉड्यूल में -1 अज्ञात प्रतीक "... क्या आप मुझे इसे हल करने के बारे में कोई संकेत दे सकते हैं? धन्यवाद!! – Rookie

+0

हैलो रुकी, मैंने आपके कर्नेल कोड को डाउनलोड और संकलित किया है, यह मेरे लिए किसी भी चेतावनी के बिना संकलित है। मेरे मामले में कर्नेल मॉड्यूल "test.ko" भी डाला जा रहा है। "Dmesg" का आउटपुट "समय के लिए स्मृति आवंटित करने में असमर्थ है [702]"। –

7

आपने kmalloc के लिए हेडर शामिल नहीं किया है। अपने कोड में #include <linux/slab.h> जोड़ें।

+0

धन्यवाद icepack! मैं ऐसा करने की कोशिश करूंगा और देख सकता हूं कि यह काम करता है ..! – Rookie