求助有关mmap物理内存映射长度受限的问题。感谢
我写了一个简单的物理内存搬移程序从1个arm通过TCP向另1个arm传输物理地址上一定长度的内容。可是现在不知道为什么mmap,send,recv受到了长度的限制。
若只是映射了1次,大概能传输7M的数据,数据量再大接收方系统崩溃,不知道是否是与栈区大小有关,可是我改过了ulimit -s并没有效果。千兆网下速度大约30MB/s
若是映射多次,虽然可以避免系统崩溃,但每次每次只能映射25K的物理内存到虚拟内存中。mmap操作影响速度,千兆网下只有13MB/s。
求教有什么增大映射长度,提高速度的方法。
非常感谢!!!
static int recvBX(int sock, void *buf, int len)
{
int rvNum=0;
int total=0;
while(total
{
rvNum=recv(sock, (char*)buf+total, len-total, 0);
if(rvNum<=0)
{
total=-1;
break;
}
else
{
total+=rvNum;
}
//pthread_delay(2);
}
return total;
}
int recv_mem(int sock_connect, char *buffer, char *dst, int xlen, int bsize)
{
char *mem = 0;
int fd_mem = 0;
int nrecv = 0;
int nsend = 0;
float time_val = 0;
float speed = 0;
//open /dev/mem with read and write mode
if((fd_mem = open ("/dev/mem", O_RDWR)) < 0)
{
printf ("open error");
mem = 0;
return -1;
}
unsigned int crcVal = 0;
int j = 0;
int loop = xlen/bsize;
int remainder = xlen%bsize;
printf("loop is %d and remainder is 0x%x\n", loop, remainder);
gettimeofday(&curr_time_1, NULL);
for(j=0;j
{
//map physical memory bsize bytes
mem = mmap (dst+j*bsize, bsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd_mem, 0);
// printf("mem is 0x%x\n", mem);
if (mem == MAP_FAILED)
{
printf ("mmap error\n");
munmap(mem, bsize);
mem = 0;
return -2;
}
// nrecv = recvBX(sock_connect, mem, bsize);
if((nrecv = recvBX(sock_connect, mem, bsize)) < 0)
{
printf("recvBX error\n");
munmap(mem, bsize);
mem = 0;
return -3;
}
crcVal=crc32(crcVal, mem, bsize);
munmap(mem, bsize);
}
//map physical memory remainder bytes
mem = mmap (dst+loop*bsize, remainder, PROT_READ | PROT_WRITE, MAP_SHARED, fd_mem, 0);
// printf("mem is 0x%x\n", mem);
if (mem == MAP_FAILED)
{
printf ("mmap error\n");
munmap(mem, remainder);
mem = 0;
return -4;
}
// nrecv = recvBX(sock_connect, mem, remainder);
if((nrecv = recvBX(sock_connect, mem, remainder)) < 0)
{
printf("recvBX error\n");
munmap(mem, bsize);
mem = 0;
return -5;
}
gettimeofday(&curr_time_2, NULL);
time_val = (curr_time_2.tv_sec - curr_time_1.tv_sec) + (float)((curr_time_2.tv_usec - curr_time_1.tv_usec)*1.0/1000000);
printf("time_val is %f\n", time_val);
speed = xlen/time_val/1024/1024;
printf("%d %d \n", curr_time_2.tv_sec, curr_time_1.tv_sec);
printf("%d %d \n", curr_time_2.tv_usec, curr_time_1.tv_usec);
printf("speed %f MB/s\n", speed);
crcVal=crc32(crcVal, mem, remainder);
printf("server crcVal is 0x%x\n", crcVal);
munmap(mem, remainder);
mem = 0;
return 0;
}
内核很早就对mmap /dev/mem做了限制。另外mmap以page为单位映射,第一个参数只是对内核的建议,也就是说内核可能会调整地址。所以,你这段代码,删了吧,没法搞。
,你mmap的物理地址是独立的吗,操作系统的运行会不会使用到你映射的地址
独立的,我在引导参数中将linux的内存改为768M,希望映射的范围在768M之后