参考了xtang的教程,测试如下:
server.c
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/neutrino.h>
_uint8 buf[ 153600 ];
int main( int argc, char **argv )
{
int chid, rcvid, status;
if( ( chid = ChannelCreate( 0 ) ) == -1 ) {
perror ( "ChannelCreate" );
return -1;
}
printf( "Server is ready, pid = %d, chid = %d\n", getpid( ), chid );
for( ; ; ) {
if( ( rcvid = MsgReceive( chid, buf, sizeof(buf), NULL ) ) == -1 ) {
perror( "MsgReceive" );
continue;
}
MsgReply( rcvid, 0, 0, 0 );
}
ChannelDestroy( chid );
return 0;
}
client.c
#include <stdio.h>
#include <string.h>
#include <sys/neutrino.h>
#include <sys/syspage.h>
_uint8 buf[ 153600 ];
int main( int argc, char **argv )
{
pid_t spid;
int chid, coid;
_uint32 i;
_uint8 reply_buf[ 64 ];
double us;
_uint64 cps, cycle1, cycle2, ncycles;
cps = SYSPAGE_ENTRY( qtime ) -> cycles_per_sec;
printf( "this system has %lld cycles/s.\n", cps );
if( argc < 3 ) {
fprintf( stderr, "Usage: client <pid> <chid>\n" );
return -1;
}
spid = atoi(argv[ 1 ]);
chid = atoi(argv[ 2 ]);
if( ( coid = ConnectAttach( 0, spid, chid, 0, 0 ) ) == -1 ) {
perror( "ConnectAttach" );
return -1;
}
for( i = 0; i < 153600; i += 2 ) {
buf[ i ] = 0xAA;
buf[ i + 1 ] = 0x55;
}
cycle1 = ClockCycles( );
for( i = 0; i < 100; i ++ ) {
if( MsgSend( coid, buf, sizeof(buf), reply_buf, sizeof(reply_buf) ) != 0 ) {
perror( "MsgSend" );
return -1;
}
}
cycle2 = ClockCycles( );
ncycles = cycle2 - cycle1;
us = ( float )ncycles * 1000000 / cps / 100;
printf( "time to send 153600-byte-sized data: %lf us.\n", us );
ConnectDetach( coid );
return 0;
}
结果是从客户端向服务器端传送 153600byte 的数据,耗时 35us 左右。
实际情况是,自制的USB驱动注册到资源管理器,用户程序调用驱动从外部设备读取数据,USB驱动调用函数 resmgr_msgwrite() 将得到的 153600byte数据传送到用户程序的缓存,耗时 7ms。
参考下面的驱动,实际上就是 resmgr_msgwrite() 这个函数耗时间。
USB驱动的 read() 函数大致如下:
int USB_io_read( resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb )
{
USB_device_t *USB_device;
uint32_t pos;
uint8_t *ibuf;
uint32_t nbytes;
uint32_t status;
uint32_t alen;
USB_device = (USB_device_t *)ocb->attr;
pos = 0;
ibuf = USB_device->buffer;
nbytes = msg->i.nbytes;
/*一些设置步骤略*/
while( 1 ) {
if( status = USB_io( USB_device, USB_device->ep_in, URB_DIR_IN, ibuf, min( nbytes, MAX_BUFFER ) ) ) {
break;
}
usbd_urb_status( USB_device->urb, NULL, &alen );
resmgr_msgwrite( ctp, ibuf, alen, pos ); // 就是这里
nbytes -= alen;
pos += alen;
if( !nbytes || !alen ) {
atomic_clr_value( &USB_device->status, USB_READING );
_IO_SET_READ_NBYTES( ctp, pos );
return( EOK );
}
}
atomic_clr_value( &USB_device->status, USB_READING );
return( status );
}
简单说明,用户程序调用 read( fd, &buf, 153600 );
而驱动里面的 MAX_BUFFER 也设置为 153600 了。
所以,resmgr_msgwrite() 仅仅被调用一次。现在想要知道如何缩短这个函数的时间。