From: =?gb2312?q?=CB=B8=20=C2=C0?= Subject: virtual device driver for help Date: Wed, 14 Nov 2007 10:30:33 +0800 (CST) Message-ID: <217696.85027.qm@web15004.mail.cnb.yahoo.com> Reply-To: device-mapper development Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0323595544==" Return-path: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com To: dm-devel@redhat.com List-Id: dm-devel.ids --===============0323595544== Content-Type: multipart/alternative; boundary="0-490143424-1195007433=:85027" Content-Transfer-Encoding: 7bit --0-490143424-1195007433=:85027 Content-Type: text/plain; charset=gb2312 Content-Transfer-Encoding: quoted-printable I am implementing a virtual device driver for learning purpose,now i find= it's performance is bad,i cann't get reason for it. I read the raid0 sou= rce code,you are the author,please you help me. =20 disk read /dev/vbd(virtual) =20 [root@server_1 vbd_test_7]# dd if=3D/dev/vbd0 of=3D/dev/zero count=3D10= 00000 bs=3D1k 1000000+0 records in 1000000+0 records out 1024000000 bytes (1.0 GB) copied, 15.8438 seconds, 64.6 MB/s =20 [root@server_1 vbd_test_7]# dd if=3D/dev/sdb of=3D/dev/zero count=3D100= 0000 bs=3D1k 1000000+0 records in 1000000+0 records out 1024000000 bytes (1.0 GB) copied, 6.18553 seconds, 166 MB/s =20 what make the performance difference?=20 =20 =20 code: =20 MODULE_LICENSE("Dual BSD/GPL"); static int vbd_major =3D 0; module_param(vbd_major, int, 0); static int hardsect_size =3D 512; module_param(hardsect_size, int, 0); static int nsectors =3D 1024*1024*2; /* How big the drive is */ module_param(nsectors, int, 0); #define WINDOWS_ROBOT_NUM 4 #define SNOPSHOT_EACH_WINDOWS_ROBOT 4 static int ndevices =3D 1; module_param(ndevices, int, 0); /* * We can tweak our hardware sector size, but the kernel talks to us * in terms of small sectors, always. */ #define KERNEL_SECTOR_SIZE 512 #define VBD_MINORS 1 /* * The internal representation of our device. */ struct vbd_dev=20 { short users; /* How many users */ short media_change; /* Flag a media change? */ spinlock_t lock; /* For mutual exclusion */ struct request_queue *queue; /* The device request queue */ struct gendisk *gd; /* The gendisk structure */ }; typedef struct vbd_dev vbd_dev_t ; static vbd_dev_t *Devices =3D NULL; static struct block_device *bdev =3D NULL; =20 static int vbd_endio(struct bio* bio,unsigned int done,int error) { struct bio* ori_bio; ori_bio =3D bio->bi_private; //printk(KERN_INFO"clone_endio:%d\n",ori_bio->bi_size); bio_endio(ori_bio,ori_bio->bi_size,error); //printk(KERN_INFO"clone_endio\n"); bio_put(bio); return 0; } =20 static int vbd_make_request(request_queue_t *q, struct bio *bio) { //struct bio_vec *bvec; struct bio* newbio; =20 =20 //sector_t sector =3D bio->bi_sector; //sector_t total_sector_num =3D 0; //int i; #if 0 printk("********************start*************************\n"); if(bio->bi_sector%8!=3D0)printk("**************what we need happen******= ***\n"); printk("bio->bi_sector=3D%d\n",bio->bi_sector); printk("bio->bi_vcnt=3D%d\n",bio->bi_vcnt); printk("bio->bi_idx=3D%d\n",bio->bi_idx); printk("bio->bi_max_vecs=3D%d\n",bio->bi_max_vecs); printk("bio->bi_size=3D%d secotrs\n",(bio->bi_size)>>9);=20 for(i=3D0;ibi_vcnt;i++) { total_sector_num+=3D(bio->bi_io_vec[i].bv_len)>>9; printk("total sector num =3D%d\n",total_sector_num); } printk("********************finish*************************\n"); #endif=20 =20 newbio =3D bio_clone(bio,GFP_NOIO);=20 newbio->bi_bdev =3D bdev; newbio->bi_end_io =3D vbd_endio; newbio->bi_private =3D bio;=20 newbio->bi_flags &=3D ~(1 << BIO_SEG_VALID); printk(KERN_INFO"newbio->bi_flags=3D%d\n",newbio->bi_flags); printk(KERN_INFO"bio->bi_flags=3D%d\n",bio->bi_flags);=20 generic_make_request(newbio);=20 return 0; } =20 /* * Open and close. */ static int vbd_open(struct inode *inode, struct file *filp) { // static int i=3D10; vbd_dev_t *vbd_dev =3D inode->i_bdev->bd_disk->private_data; dev_t testdev; filp_open("/dev/sdb",O_RDWR,0); =20 printk("********************start*************************\n"); testdev =3D MKDEV(8,16); bdev =3D bdget(testdev); if(bdev=3D=3DNULL) { printk(KERN_INFO"notice:null device**********\n"); return 0; } =20 filp->private_data =3D vbd_dev; spin_lock(&vbd_dev->lock); if (! vbd_dev->users)=20 { check_disk_change(inode->i_bdev); } vbd_dev->users++; spin_unlock(&vbd_dev->lock); return 0; } =20 static int vbd_release(struct inode *inode, struct file *filp) { struct vbd_dev *dev =3D inode->i_bdev->bd_disk->private_data; spin_lock(&dev->lock); dev->users--; spin_unlock(&dev->lock); return 0; } /* * Look for a (simulated) media change. */ int vbd_media_changed(struct gendisk *gd) { struct vbd_dev *dev =3D gd->private_data; =20 return dev->media_change; } /* * Revalidate. WE DO NOT TAKE THE LOCK HERE, for fear of deadlocking * with open. That needs to be reevaluated. */ int vbd_revalidate(struct gendisk *gd) { struct vbd_dev *dev =3D gd->private_data; =20 if (dev->media_change)=20 { dev->media_change =3D 0; =20 } return 0; } =20 /* * The ioctl() implementation */ int vbd_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { struct hd_geometry geo; //struct vbd_dev *dev =3D filp->private_data; //static int i=3D0; =20 switch(cmd) { case HDIO_GETGEO: /* * Get geometry: since we are a virtual device, we have to make * up something plausible. So we claim 16 sectors, four heads, * and calculate the corresponding number of cylinders. We set the * start of data at sector four. */ geo.heads =3D 4; geo.sectors =3D 16; geo.start =3D 4; if (copy_to_user((void __user *) arg, &geo, sizeof(geo))) return -EFAULT; return 0; } printk(KERN_INFO"ioctl:%d\n",cmd); =20 return -ENOTTY; /* unknown command */ } =20 /* * The device operations structure. */ static struct block_device_operations vbd_ops =3D { .owner =3D THIS_MODULE, .open =3D vbd_open, .release =3D vbd_release, .media_changed =3D vbd_media_changed, .revalidate_disk =3D vbd_revalidate, .ioctl =3D vbd_ioctl }; =20 /* * Set up our internal device. */ static void setup_device(struct vbd_dev *dev, int which) { /* * Get some memory. */ memset (dev, 0, sizeof (struct vbd_dev)); spin_lock_init(&dev->lock); dev->queue =3D blk_alloc_queue(GFP_KERNEL); if (dev->queue =3D=3D NULL) return; /**/ blk_queue_make_request(dev->queue, vbd_make_request); =20 blk_queue_hardsect_size(dev->queue, hardsect_size); dev->queue->queuedata =3D dev; /* * And the gendisk structure.we just want 1 minor here */ dev->gd =3D alloc_disk(VBD_MINORS); if (! dev->gd)=20 { printk (KERN_NOTICE "alloc_disk failure\n"); return; } /* * Set some other parameters */ dev->gd->major =3D vbd_major; dev->gd->first_minor =3D which*VBD_MINORS; dev->gd->fops =3D &vbd_ops; dev->gd->queue =3D dev->queue; dev->gd->private_data =3D dev; snprintf (dev->gd->disk_name, 32, "vbd%d", which ); set_capacity(dev->gd, nsectors*(hardsect_size/KERNEL_SECTOR_SIZE)); add_disk(dev->gd); return; =20 } =20 static int __init vbd_init(void) { int i; /* * Get registered. */ vbd_major =3D register_blkdev(vbd_major, "vbd"); if (vbd_major <=3D 0) { printk(KERN_WARNING "vbd: unable to get major number\n"); return -EBUSY; } /* * Allocate the device array, and initialize each one. */ Devices =3D kmalloc(ndevices*sizeof (struct vbd_dev), GFP_KERNEL); if (Devices =3D=3D NULL) { goto out_unregister; } for (i =3D 0; i < ndevices; i++)=20 { setup_device(Devices + i, i); } =20 return 0; out_unregister: unregister_blkdev(vbd_major, "vbd"); return -ENOMEM; } static void vbd_exit(void) { int i; for (i =3D 0; i < ndevices; i++)=20 { struct vbd_dev *dev =3D Devices + i; if (dev->gd)=20 { del_gendisk(dev->gd); put_disk(dev->gd); } if (dev->queue)=20 { blk_cleanup_queue(dev->queue); } =20 } unregister_blkdev(vbd_major, "vbd"); kfree(Devices); } =20 module_init(vbd_init); module_exit(vbd_exit =20 --------------------------------- @yahoo.cn =D0=C2=D3=F2=C3=FB=A1=A2=CE=DE=CF=DE=C1=BF=A3=AC=BF=EC=C0=B4=C7= =C0=D7=A2=A3=A1 --0-490143424-1195007433=:85027 Content-Type: text/html; charset=gb2312 Content-Transfer-Encoding: quoted-printable
I am implementing a virtual device driver for learning purpose,now i= find it's performance is bad,i cann't get reason for it. I read the raid= 0 source code,you are the author,please you help me.
 
disk read /dev/vbd(virtual) 
[root@server_1 = vbd_test_7]# dd if=3D/dev/vbd0 of=3D/dev/zero count=3D1000000 bs=3D1k
= 1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB= ) copied, 15.8438 seconds, 64.6 MB/s
 
[root= @server_1 vbd_test_7]# dd if=3D/dev/sdb of=3D/dev/zero count=3D1000000 bs= =3D1k
1000000+0 records in
1000000+0 records out
1024000000 byte= s (1.0 GB) copied, 6.18553 seconds, 166 MB/s
 
what make the performance difference?
 
=  
code:
 
MODULE_LICENSE("D= ual BSD/GPL");
static int vbd_major =3D 0;
module_param(vb= d_major, int, 0);
static int hardsect_size =3D 512;
module_param(hardsect_size, int, 0);
static int nsectors =3D = 1024*1024*2; /* How big the drive is */
module_param(nsectors, in= t, 0);
#define WINDOWS_ROBOT_NUM  4
#define SNOPSHOT_= EACH_WINDOWS_ROBOT 4

static int ndevices =3D 1;
module_param(nd= evices, int, 0);
/*
 * We can tweak our hardware sect= or size, but the kernel talks to us
 * in terms of small sectors,= always.
 */
#define KERNEL_SECTOR_SIZE 512
#define VB= D_MINORS           1
=
/*
 * The internal representation of our device.
 */=
struct vbd_dev
{
    short users;  &n= bsp;           &nb= sp;     /* How many users */
    sh= ort media_change;         &n= bsp;   /* Flag a media change? */
    spinlock_t lock;     &= nbsp;          /* For mutual= exclusion */
    struct request_queue *queue; &nb= sp;  /* The device request queue */
    struct gen= disk *gd;          &nbs= p;  /* The gendisk structure */
};
typedef struct vbd= _dev vbd_dev_t ;
static vbd_dev_t *Devices =3D NULL;
stati= c struct block_device *bdev =3D NULL;

static int vbd_endi= o(struct bio* bio,unsigned int done,int error)
{
 struct bio*&= nbsp; ori_bio;
 ori_bio =3D bio->bi_private;
 = ;//printk(KERN_INFO"clone_endio:%d\n",ori_bio->bi_size);
 bio_= endio(ori_bio,ori_bio->bi_size,error);
 //printk(KERN_INFO"clo= ne_endio\n");
  bio_put(bio);
 return 0;}

static int vbd_make_request(request_queue_t *q, struct bio *bio)
{
 //st= ruct bio_vec *bvec;
 struct bio* newbio;
 
 
&= nbsp;//sector_t sector =3D bio->bi_sector;
 //sector_t total_s= ector_num =3D 0;
 //int i;
#if 0
 printk("***= *****************start*************************\n");
 if(bio->= bi_sector%8!=3D0)printk("**************what we need happen*********\n");<= BR> printk("bio->bi_sector=3D%d\n",bio->bi_sector);
 p= rintk("bio->bi_vcnt=3D%d\n",bio->bi_vcnt);
 printk("bio->= ;bi_idx=3D%d\n",bio->bi_idx);
 printk("bio->bi_max_vecs=3D%= d\n",bio->bi_max_vecs);
 printk("bio->bi_size=3D%d secotrs\= n",(bio->bi_size)>>9); 
 for(i=3D0;i<bio->bi_= vcnt;i++)
 {
  total_sector_num+=3D(bio->bi_io_ve= c[i].bv_len)>>9;
  printk("total sector num =3D%d\n",total_sector_num);
 }
 printk("****************= ****finish*************************\n");
#endif

 = ;newbio =3D bio_clone(bio,GFP_NOIO);
 newbio->bi_bdev =3D bde= v;
 newbio->bi_end_io =3D vbd_endio;
 newbio->bi_pr= ivate =3D bio;
 newbio->bi_flags &=3D ~(1 <&l= t; BIO_SEG_VALID);
 printk(KERN_INFO"newbio->bi_flags=3D%d\n",= newbio->bi_flags);
 printk(KERN_INFO"bio->bi_flags=3D%d\n",= bio->bi_flags);
 generic_make_request(newbio); 
<= DIV> return 0;
}

/*
 * Open and close. */
static int vbd_open(struct inode *inode, struct fil= e *filp)
{
   // static int i=3D10;
 vbd_dev_t *v= bd_dev =3D inode->i_bdev->bd_disk->private_data;
 dev_t&= nbsp; testdev;
 filp_open("/dev/sdb",O_RDWR,0);
=20

 printk("********************start************************= *\n");
 testdev =3D MKDEV(8,16);
 bdev =3D bdget(testdev)= ;
 if(bdev=3D=3DNULL)
 {
  printk(KERN_INFO"= notice:null device**********\n");
  return 0;
 }

 filp->private_data =3D vbd_dev;
 spin_lock(= &vbd_dev->lock);
 if (! vbd_dev->users)
 {
=   check_disk_change(inode->i_bdev);
 }
 vbd_= dev->users++;
 spin_unlock(&vbd_dev->lock);
 re= turn 0;
}

static int vbd_release(struct inode *inode, = struct file *filp)
{
 struct vbd_dev *dev =3D inode->i_bdev= ->bd_disk->private_data;
 spin_lock(&dev->l= ock);
 dev->users--;
 spin_unlock(&dev->lock);<= /DIV>
 return 0;
}
/*
 * Look for a (si= mulated) media change.
 */
int vbd_media_changed(struct gendisk *gd)
{
 struct vbd_dev *dev = =3D gd->private_data;
 
 return dev->media_change;<= BR>}
/*
 * Revalidate.  WE DO NOT TAKE THE LOCK = HERE, for fear of deadlocking
 * with open.  That needs to b= e reevaluated.
 */
int vbd_revalidate(struct gendisk *gd)
{=
 struct vbd_dev *dev =3D gd->private_data;
 
 = ;if (dev->media_change)
 {
  dev->media_chang= e =3D 0;  
 }
 return 0;
}
 = ;
/*
 * The ioctl() implementation
 */
=
int vbd_ioctl (struct inode *inode, struct file *filp,
 &nb= sp;           &nbs= p;   unsigned int cmd, unsigned long arg)
{
&nbs= p;struct hd_geometry geo;
 //struct vbd_dev *dev =3D filp->pri= vate_data;
    //static int i=3D0;
 
 switch(cmd) {
     case= HDIO_GETGEO:
         /*
&= nbsp;  * Get geometry: since we are a virtual device, we have to mak= e
   * up something plausible.  So we claim 16 sectors,= four heads,
   * and calculate the corresponding number of = cylinders.  We set the
   * start of data at sector fou= r.
   */
  geo.heads =3D 4;
 &= nbsp;geo.sectors =3D 16;
  geo.start =3D 4;
  i= f (copy_to_user((void __user *) arg, &geo, sizeof(geo)))
 &nb= sp; return -EFAULT;
  return 0;
 }
 pri= ntk(KERN_INFO"ioctl:%d\n",cmd);
 
 return -ENOTTY; /* unk= nown command */
}
 
/*
 * The dev= ice operations structure.
 */
static struct block_device_opera= tions vbd_ops =3D {
 .owner         &= nbsp; =3D THIS_MODULE,
 .open       = ;    =3D vbd_open,
 .release   =3D vbd_releas= e,
 .media_changed   =3D vbd_media_changed,
 .r= evalidate_disk =3D vbd_revalidate,
 .ioctl    = ;      =3D vbd_ioctl
};

/* * Set up our internal device.
 */
static void setup_dev= ice(struct vbd_dev *dev, int which)
{
 /*
  * Get some= memory.
  */
 memset (dev, 0, sizeof (struct vbd_dev));<= BR> spin_lock_init(&dev->lock);
 dev->que= ue =3D blk_alloc_queue(GFP_KERNEL);
 if (dev->queue =3D=3D NUL= L)
  return;
 /**/
 blk_queue_make_= request(dev->queue, vbd_make_request);
  
 blk_qu= eue_hardsect_size(dev->queue, hardsect_size);
 dev->queue->queuedata =3D dev;
 /*
  * And the gendisk structure.we just want 1 minor her= e
  */
 dev->gd =3D alloc_disk(VBD_MINORS);
 i= f (! dev->gd)
 {
  printk (KERN_NOTICE "alloc_di= sk failure\n");
  return;
 }
 /*  * Set some other parameters
  */
 dev->gd->= major =3D vbd_major;
 dev->gd->first_minor =3D which*VBD_MI= NORS;
 dev->gd->fops =3D &vbd_ops;
 dev->gd-= >queue =3D dev->queue;
 dev->gd->private_data =3D dev= ;
 snprintf (dev->gd->disk_name, 32, "vbd%d", which );
&= nbsp;set_capacity(dev->gd, nsectors*(hardsect_size/KERNEL_SECTOR_SIZE)= );
 add_disk(dev->gd);
 return;
 
}
=
 
static int __init vbd_init(void)
{
 in= t i;
 /*
  * Get registered.
  */
 vbd_major =3D register_blkdev(vbd_majo= r, "vbd");
 if (vbd_major <=3D 0) {
  printk(KERN= _WARNING "vbd: unable to get major number\n");
  return -EBU= SY;
 }
 /*
  * Allocate the device array, and ini= tialize each one.
  */
 Devices =3D kmalloc(ndevices*size= of (struct vbd_dev), GFP_KERNEL);
 if (Devices =3D=3D NULL)
&n= bsp;{
  goto out_unregister;
 }
 for (i =3D = 0; i < ndevices; i++)
 {
  setup_device(Devices = + i, i);
 }
   
 return 0;
  out_unregister:
 unregister_blkdev(vbd_major, "vbd");
=  return -ENOMEM;
}
static void vbd_exit(void)
{ int i;
 for (i =3D 0; i < ndevices; i++)
&= nbsp;{
  struct vbd_dev *dev =3D Devices + i;
  = ;if (dev->gd)
  {
   del_gendisk(dev->gd);
 = ;  put_disk(dev->gd);
  }
  if (de= v->queue)
  {
   blk_cleanup_queue(dev= ->queue);
  }   
 }
 unreg= ister_blkdev(vbd_major, "vbd");
 kfree(Devices);
}
 module_init(vbd_init);
module_exit(vbd_exit


=20 @yahoo.cn =D0=C2=D3=F2=C3=FB=A1=A2=CE=DE=CF=DE=C1=BF=A3=AC=BF=EC=C0=B4=C7= =C0=D7=A2=A3=A1 --0-490143424-1195007433=:85027-- --===============0323595544== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============0323595544==--