从linux内核模块中读出大数组
我已经在这方面进行了大量的搜索,但我肯定可能错过了某些东西,而且我来自http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html和http://www.amazon.com/Essential-Device-Drivers-Sreekrishnan-Venkateswaran/dp/0132396556,但我无法阅读在我的linux内核模块中创建大型数组〜200kB一块。我正在开发基于ARM的ti-omap3530。我分配的数组是这样的:从linux内核模块中读出大数组
static unsigned int* captured_params;
static unsigned int* time_params;
captured_params=vmalloc(3500*7*sizeof(unsigned int));
time_params=vmalloc(3500*7*sizeof(unsigned int));
我读功能看起来像这样(我知道,我不是做这可能是最好的方式):
ssize_t tgdev_read(struct file *file, char *buf, size_t count, loff_t *ppos){
/*This file will expect reads in 8-byte chunks, 4 for the data parameter
and 4 for the time parameter*/
struct tg_dev *tg_devp = file->private_data;
unsigned int num_pairs=sample_counter;
static unsigned int pairs_out=0;
static unsigned int sent_successfully=0;
unsigned int* param_ptr;
unsigned int* time_ptr;
pairs_out=(tg_devp->current_pointer)/8;
num_pairs=sample_counter;
if(pairs_out==num_pairs){//all returned end of file
return 0; //EOF
}
while((pairs_out*8)<count){
//update data pointers
time_ptr=&time_params[pairs_out];
param_ptr=&captured_params[pairs_out];
//send user time first
sent_successfully=copy_to_user(&buf[tg_devp->current_pointer],time_ptr,4);
//send param value for that time
sent_successfully=copy_to_user(&buf[tg_devp->current_pointer+4],param_ptr,4);
//update number of pairs sent to user
pairs_out+=1;
//update file_pointer
tg_devp->current_pointer +=8;
}
return pairs_out*8;
}
我与用户阅读这样的空间程序:
int fp;
char* mode="r";
char* fname="/dev/my_device";
unsigned int param[3500*7]={0};
unsigned int time[3500*7]={0};
unsigned int* param_ptr;
unsigned int* time_ptr;
char buff[3500*7*4*2];
int i=0;
int rc=0;
int completed_reads;
fp=open(fname,O_RDONLY);
if(fp<0){
printf("failed to open file EXITING\n");
return 1;
}
rc=read(fp,buff,3500*7*4*2);
completed_reads=(rc-rc%8)/8;
printf("rc=%i cr=%i\n",rc,completed_reads);
for(i=0;i<completed_reads;i++){
param_ptr=¶m[i];
time_ptr=&time[i];
memcpy(param_ptr,&buff[i*8],4);
memcpy(time_ptr,&buff[i*8+4],4);
printf("[%i]%u,%u\n",i,param[i],time[i]);
}
close(fp);
用户空间读取总是报告读取正确的字节数,但我没有得到正确的数据。
这个解决方案似乎工作正常,如果我用更小的东西(高达1000行为就好)取代3500s,但在上面我得到奇怪的行为,其中我读出的零数组填充每个数组的最后N个在2个阵列中的每一个中的相同点处的元素数量相同)并且在该系列的后期开始,例如, time/param [0] =我想读出的原始数组远远更远的一些值。
我在想这是因为我不明白内存处理的好,但我不知道如何使这做我想做的,这是将这些数据数组存储在模块中,直到我想将它们读入用户空间。任何意见或想法,我会错误的将不胜感激。
在此先感谢您的时间,努力和耐心。
你可以(应该)通过用mmap将数组压缩到用户空间进程的地址空间来避免进行读取操作。请看下面的例子:
http://people.ee.ethz.ch/~arkeller/linux/multi/kernel_user_space_howto-8.html
感谢您的建议!我正在查看它。我是内核开发的新手,我可以使用所有我能得到的建议。 – gorgoth 2012-05-18 01:07:21
我不明白为什么'3500 * 7 * 4 * 2'。为什么''2'在你的'read'中? – 2012-03-21 18:10:13
从2个原始数组中填充缓冲区,每个数组大小为3500 * 7 * 4字节(至少这是我正在尝试做的)。 – gorgoth 2012-03-22 11:26:05
请注意,我的实际代码中的大部分数字,比如'3500','7','4'和'2'都是由'#define'处理的,但我把上面的字面数字_try_帮助沟通我想要做得更清楚,(通常不是我的强项)... – gorgoth 2012-03-22 20:00:14