From: Andrew Xu Subject: Re: [Xen-users] Re: VM disk I/O limit patch Date: Wed, 22 Jun 2011 20:06:23 +0800 Message-ID: <20110622200623.8CE4.3A8D29D5@cloudex.cn> References: <20110621162935.F4A1.3A8D29D5@cloudex.cn> <20110621133337.GD5650@dumpdata.com> Mime-Version: 1.0 Content-Type: text/plain; charset="GB2312" Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <20110621133337.GD5650@dumpdata.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Konrad Rzeszutek Wilk Cc: xen-devel@lists.xensource.com, xen-users@lists.xensource.com List-Id: xen-devel@lists.xenproject.org On Tue, 21 Jun 2011 09:33:37 -0400 Konrad Rzeszutek Wilk wrote: > On Tue, Jun 21, 2011 at 04:29:35PM +0800, Andrew Xu wrote: > > Hi all, > >=20 > > I add a blkback QoS patch. >=20 > What tree is this against?=20 This patch is based on suse11.sp1(2.6.32) xen-blkback source.=20 (2.6.18 "Xenlinux" based source trees?) > There is a xen-blkback in 3.0-rc4, can you rebase > it against that please. >=20 Ok, I will rebase it. > What is the patch solving?=20 >=20 With this path, you can set different speed I/O for different VM disk. For example, I set vm17-disk1 4MKB/s=20 vm17-disk2 1MKB/s=20 vm18-disk3 3MKB/s=20 I/O speed, by writing follow xenstore key-values. /local/domain/17/device/vbd/768/tokens-rate =3D "4096" /local/domain/17/device/vbd/2048/tokens-rate =3D "1024" /local/domain/18/device/vbd/768/tokens-rate =3D "3096" > Why can't it be done with dm-ioband? Of cause, I/O speed limit also can be done with dm-ioband. But with my patch, there is no need to load dm-ioband any more. This patch do speed-limit more close disk, more lightweight. How it doing? 1) It record how many sectors(512Bytes) have submit to disk in 1 second. 2) If it exceed 2*tokens-rate, then sleep (1 - time passed) seconds. 3) Next second come, going to 1) > > You can config(dynamic/static) different I/O speed for different VM d= isk > > by this patch. > >=20 > > ---------------------------------------------------------------------= ------- > >=20 > > diff -urNp blkback/blkback.c blkback-qos/blkback.c > > --- blkback/blkback.c 2011-06-22 07:54:19.000000000 +0800 > > +++ blkback-qos/blkback.c 2011-06-22 07:53:18.000000000 +0800 > > @@ -44,6 +44,11 @@ > > #include > > #include "common.h" > > =20 > > +#undef DPRINTK > > +#define DPRINTK(fmt, args...) \ > > + printk("blkback/blkback (%s:%d) " fmt ".\n", \ > > + __FUNCTION__, __LINE__, ##args) > > + > > /* > > * These are rather arbitrary. They are fairly large because adjacen= t requests > > * pulled from a communication ring are quite likely to end up being= part of > > @@ -110,7 +115,8 @@ static inline unsigned long vaddr(pendin > > static int do_block_io_op(blkif_t *blkif); > > static int dispatch_rw_block_io(blkif_t *blkif, > > blkif_request_t *req, > > - pending_req_t *pending_req); > > + pending_req_t *pending_req, > > + int *done_nr_sects); > > static void make_response(blkif_t *blkif, u64 id, > > unsigned short op, int st); > > =20 > > @@ -206,10 +212,20 @@ static void print_stats(blkif_t *blkif) > > blkif->st_pk_req =3D 0; > > } > > =20 > > +static void refill_reqcount(blkif_t *blkif) > > +{ > > + blkif->reqtime =3D jiffies + msecs_to_jiffies(1000); > > + blkif->reqcount =3D blkif->reqrate; > > + if (blkif->reqcount < blkif->reqmin) > > + blkif->reqcount =3D blkif->reqmin; > > +} > > + > > int blkif_schedule(void *arg) > > { > > blkif_t *blkif =3D arg; > > struct vbd *vbd =3D &blkif->vbd; > > + int ret =3D 0; > > + struct timeval cur_time; > > =20 > > blkif_get(blkif); > > =20 > > @@ -232,12 +248,34 @@ int blkif_schedule(void *arg) > > blkif->waiting_reqs =3D 0; > > smp_mb(); /* clear flag *before* checking for work */ > > =20 > > - if (do_block_io_op(blkif)) > > + ret =3D do_block_io_op(blkif); > > + if (ret) > > blkif->waiting_reqs =3D 1; > > unplug_queue(blkif); > > =20 > > + if(blkif->reqmin){ > > + if(2 =3D=3D ret && (blkif->reqtime > jiffies)){ > > + jiffies_to_timeval(jiffies, &cur_time); > > + if(log_stats && (cur_time.tv_sec % 10 =3D=3D1 )) > > + printk(KERN_DEBUG "%s: going to sleep %d millsecs(rate=3D%d)\n"= , > > + current->comm, > > + jiffies_to_msecs(blkif->reqtime - jiffies), > > + blkif->reqrate); > > + =09 > > + set_current_state(TASK_INTERRUPTIBLE); > > + schedule_timeout(blkif->reqtime - jiffies); > > + =09 > > + if(log_stats && (cur_time.tv_sec % 10 =3D=3D1 )) > > + printk(KERN_DEBUG "%s: sleep end(rate=3D%d)\n", > > + current->comm,blkif->reqrate); > > + } > > + if (time_after(jiffies, blkif->reqtime)) > > + refill_reqcount(blkif); > > + } > > + > > if (log_stats && time_after(jiffies, blkif->st_print)) > > print_stats(blkif); > > + =09 > > } > > =20 > > if (log_stats) > > @@ -306,7 +344,6 @@ irqreturn_t blkif_be_int(int irq, void * > > /****************************************************************** > > * DOWNWARD CALLS -- These interface with the block-device layer pro= per. > > */ > > - > > static int do_block_io_op(blkif_t *blkif) > > { > > blkif_back_rings_t *blk_rings =3D &blkif->blk_rings; > > @@ -314,15 +351,27 @@ static int do_block_io_op(blkif_t *blkif > > pending_req_t *pending_req; > > RING_IDX rc, rp; > > int more_to_do =3D 0, ret; > > + static int last_done_nr_sects =3D 0;=09 > > =20 > > rc =3D blk_rings->common.req_cons; > > rp =3D blk_rings->common.sring->req_prod; > > rmb(); /* Ensure we see queued requests up to 'rp'. */ > > +=09 > > + if (blkif->reqmin && blkif->reqcount <=3D 0) > > + return (rc !=3D rp) ? 2 : 0; > > =20 > > while ((rc !=3D rp) || (blkif->is_suspended_req)) { > > =20 > > if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc)) > > break; > > + =09 > > + if(blkif->reqmin){ > > + blkif->reqcount -=3D last_done_nr_sects; > > + if (blkif->reqcount <=3D 0) { > > + more_to_do =3D 2; > > + break; > > + } > > + } > > =20 > > if (kthread_should_stop()) { > > more_to_do =3D 1; > > @@ -367,14 +416,14 @@ handle_request: > > switch (req.operation) { > > case BLKIF_OP_READ: > > blkif->st_rd_req++; > > - ret =3D dispatch_rw_block_io(blkif, &req, pending_req);=20 > > + ret =3D dispatch_rw_block_io(blkif, &req, pending_req,&last_done_= nr_sects);=20 > > break; > > case BLKIF_OP_WRITE_BARRIER: > > blkif->st_br_req++; > > /* fall through */ > > case BLKIF_OP_WRITE: > > blkif->st_wr_req++; > > - ret =3D dispatch_rw_block_io(blkif, &req, pending_req); > > + ret =3D dispatch_rw_block_io(blkif, &req, pending_req,&last_done_= nr_sects); > > break; > > case BLKIF_OP_PACKET: > > DPRINTK("error: block operation BLKIF_OP_PACKET not implemented\n= "); > > @@ -412,9 +461,29 @@ handle_request: > > return more_to_do; > > } > > =20 > > +static char* operation2str(int operation) > > +{ > > + char* ret_str =3D NULL; > > + switch (operation) { > > + case BLKIF_OP_READ: > > + ret_str =3D "READ"; > > + break; > > + case BLKIF_OP_WRITE: > > + ret_str =3D "WRITE"; > > + break; > > + case BLKIF_OP_WRITE_BARRIER: > > + ret_str =3D "WRITE_BARRIER"; > > + break; > > + default: > > + ret_str =3D "0"; > > + } > > + return ret_str; > > +} > > + > > static int dispatch_rw_block_io(blkif_t *blkif, > > blkif_request_t *req, > > - pending_req_t *pending_req) > > + pending_req_t *pending_req, > > + int *done_nr_sects) > > { > > extern void ll_rw_block(int rw, int nr, struct buffer_head * bhs[])= ; > > struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST]; > > @@ -426,6 +495,9 @@ static int dispatch_rw_block_io(blkif_t > > struct bio *bio =3D NULL; > > int ret, i; > > int operation; > > + struct timeval cur_time; > > + > > + *done_nr_sects =3D 0; > > =20 > > switch (req->operation) { > > case BLKIF_OP_READ: > > @@ -582,6 +654,12 @@ static int dispatch_rw_block_io(blkif_t > > else if (operation =3D=3D WRITE || operation =3D=3D WRITE_BARRIER) > > blkif->st_wr_sect +=3D preq.nr_sects; > > =20 > > + *done_nr_sects =3D preq.nr_sects; > > + jiffies_to_timeval(jiffies, &cur_time); > > + if ((log_stats =3D=3D 2) && (cur_time.tv_sec % 10 =3D=3D1 )) > > + printk(KERN_DEBUG " operation=3D%s sects=3D%d\n", > > + operation2str(req->operation),preq.nr_sects); > > + > > return 0; > > =20 > > fail_flush: > > @@ -695,6 +773,8 @@ static int __init blkif_init(void) > > =20 > > blkif_xenbus_init(); > > =20 > > + DPRINTK("blkif_inited\n"); > > + > > return 0; > > =20 > > out_of_memory: > > diff -urNp blkback/cdrom.c blkback-qos/cdrom.c > > --- blkback/cdrom.c 2010-05-20 18:07:00.000000000 +0800 > > +++ blkback-qos/cdrom.c 2011-06-22 07:34:50.000000000 +0800 > > @@ -35,9 +35,9 @@ > > #include "common.h" > > =20 > > #undef DPRINTK > > -#define DPRINTK(_f, _a...) \ > > - printk("(%s() file=3D%s, line=3D%d) " _f "\n", \ > > - __PRETTY_FUNCTION__, __FILE__ , __LINE__ , ##_a ) > > +#define DPRINTK(fmt, args...) \ > > + printk("blkback/cdrom (%s:%d) " fmt ".\n", \ > > + __FUNCTION__, __LINE__, ##args) > > =20 > > =20 > > #define MEDIA_PRESENT "media-present" > > diff -urNp blkback/common.h blkback-qos/common.h > > --- blkback/common.h 2010-05-20 18:07:00.000000000 +0800 > > +++ blkback-qos/common.h 2011-06-22 07:34:50.000000000 +0800 > > @@ -100,8 +100,17 @@ typedef struct blkif_st { > > =20 > > grant_handle_t shmem_handle; > > grant_ref_t shmem_ref; > > + > > + /* qos information */ > > + unsigned long reqtime; > > + int reqcount; > > + int reqmin; > > + int reqrate;=20 > > + > > } blkif_t; > > =20 > > +#define VBD_QOS_MIN_RATE_LIMIT 2*1024 /* 1MBs */ > > + > > struct backend_info > > { > > struct xenbus_device *dev; > > @@ -111,6 +120,8 @@ struct backend_info > > unsigned major; > > unsigned minor; > > char *mode; > > + struct xenbus_watch rate_watch; > > + int have_rate_watch;=20 > > }; > > =20 > > blkif_t *blkif_alloc(domid_t domid); > > diff -urNp blkback/vbd.c blkback-qos/vbd.c > > --- blkback/vbd.c 2010-05-20 18:07:00.000000000 +0800 > > +++ blkback-qos/vbd.c 2011-06-22 07:34:50.000000000 +0800 > > @@ -35,6 +35,11 @@ > > #define vbd_sz(_v) ((_v)->bdev->bd_part ? \ > > (_v)->bdev->bd_part->nr_sects : get_capacity((_v)->bdev->bd_disk)) > > =20 > > +#undef DPRINTK > > +#define DPRINTK(fmt, args...) \ > > + printk("blkback/vbd (%s:%d) " fmt ".\n", \ > > + __FUNCTION__, __LINE__, ##args) > > + > > unsigned long long vbd_size(struct vbd *vbd) > > { > > return vbd_sz(vbd); > > @@ -87,7 +92,7 @@ int vbd_create(blkif_t *blkif, blkif_vde > > if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) > > vbd->type |=3D VDISK_REMOVABLE; > > =20 > > - DPRINTK("Successful creation of handle=3D%04x (dom=3D%u)\n", > > + DPRINTK("Successful creation of handle=3D%04x (dom=3D%u)", > > handle, blkif->domid); > > return 0; > > } > > diff -urNp blkback/xenbus.c blkback-qos/xenbus.c > > --- blkback/xenbus.c 2010-05-20 18:07:00.000000000 +0800 > > +++ blkback-qos/xenbus.c 2011-06-22 07:34:50.000000000 +0800 > > @@ -25,13 +25,14 @@ > > =20 > > #undef DPRINTK > > #define DPRINTK(fmt, args...) \ > > - pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \ > > + printk("blkback/xenbus (%s:%d) " fmt ".\n", \ > > __FUNCTION__, __LINE__, ##args) > > =20 > > static void connect(struct backend_info *); > > static int connect_ring(struct backend_info *); > > static void backend_changed(struct xenbus_watch *, const char **, > > unsigned int); > > +static void unregister_rate_watch(struct backend_info *be); > > =20 > > static int blkback_name(blkif_t *blkif, char *buf) > > { > > @@ -59,8 +60,10 @@ static void update_blkif_status(blkif_t > > char name[TASK_COMM_LEN]; > > =20 > > /* Not ready to connect? */ > > - if (!blkif->irq || !blkif->vbd.bdev) > > + if (!blkif->irq || !blkif->vbd.bdev){ > > + DPRINTK("Not ready to connect"); > > return; > > + } > > =20 > > /* Already connected? */ > > if (blkif->be->dev->state =3D=3D XenbusStateConnected) > > @@ -193,6 +196,8 @@ static int blkback_remove(struct xenbus_ > > be->cdrom_watch.node =3D NULL; > > } > > =20 > > + unregister_rate_watch(be); > > + > > if (be->blkif) { > > blkif_disconnect(be->blkif); > > vbd_free(&be->blkif->vbd); > > @@ -251,6 +256,10 @@ static int blkback_probe(struct xenbus_d > > =20 > > err =3D xenbus_watch_path2(dev, dev->nodename, "physical-device", > > &be->backend_watch, backend_changed); > > + > > + DPRINTK("blkback_probe called"); > > + DPRINTK("dev->nodename=3D%s/physical-device",dev->nodename); > > +=09 > > if (err) > > goto fail; > > =20 > > @@ -266,7 +275,6 @@ fail: > > return err; > > } > > =20 > > - > > /** > > * Callback received when the hotplug scripts have placed the physic= al-device > > * node. Read it and the mode node, and create a vbd. If the front= end is > > @@ -283,8 +291,9 @@ static void backend_changed(struct xenbu > > struct xenbus_device *dev =3D be->dev; > > int cdrom =3D 0; > > char *device_type; > > + char name[TASK_COMM_LEN]; > > =20 > > - DPRINTK(""); > > + DPRINTK("backend_changed called"); > > =20 > > err =3D xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x= :%x", > > &major, &minor); > > @@ -322,6 +331,34 @@ static void backend_changed(struct xenbu > > kfree(device_type); > > } > > =20 > > + /* gather information about QoS policy for this device. */ > > + err =3D blkback_name(be->blkif, name); > > + if (err) { > > + xenbus_dev_error(be->dev, err, "get blkback dev name"); > > + return; > > + } > > +=09 > > + err =3D xenbus_gather(XBT_NIL, dev->otherend, > > + "tokens-rate", "%d", &be->blkif->reqrate,=20 > > + NULL); > > + if(err){ > > + DPRINTK("%s xenbus_gather(tokens-min,tokens-rate) error",name); > > + }else{ > > + if(be->blkif->reqrate <=3D 0){ > > + be->blkif->reqmin =3D 0 ; > > + DPRINTK("%s tokens-rate =3D=3D 0,no limit",name);=09 > > + }else{ > > + DPRINTK("%s xenbus_gather(tokens-rate=3D%d)",name,be->blkif->reqr= ate); > > + be->blkif->reqrate *=3D 2; > > + be->blkif->reqmin =3D VBD_QOS_MIN_RATE_LIMIT; > > + if(be->blkif->reqmin > be->blkif->reqrate){ > > + be->blkif->reqrate =3D be->blkif->reqmin; > > + DPRINTK("%s reset default value(tokens-rate=3D%d)",name,be->blki= f->reqrate); > > + } > > + } > > + } > > + be->blkif->reqtime =3D jiffies; > > + > > if (be->major =3D=3D 0 && be->minor =3D=3D 0) { > > /* Front end dir is a number, which is used as the handle. */ > > =20 > > @@ -414,6 +451,49 @@ static void frontend_changed(struct xenb > > =20 > > /* ** Connection ** */ > > =20 > > +static void unregister_rate_watch(struct backend_info *be) > > +{ > > + if (be->have_rate_watch) { > > + unregister_xenbus_watch(&be->rate_watch); > > + kfree(be->rate_watch.node); > > + } > > + be->have_rate_watch =3D 0; > > +} > > + > > +static void rate_changed(struct xenbus_watch *watch, > > + const char **vec, unsigned int len) > > +{ > > + > > + struct backend_info *be=3Dcontainer_of(watch,struct backend_info, r= ate_watch); > > + int err; > > + char name[TASK_COMM_LEN]; > > + > > + err =3D blkback_name(be->blkif, name); > > + if (err) { > > + xenbus_dev_error(be->dev, err, "get blkback dev name"); > > + return; > > + } > > + > > + err =3D xenbus_gather(XBT_NIL,be->dev->otherend,=20 > > + "tokens-rate", "%d",=20 > > + &be->blkif->reqrate,NULL); > > + if(err){ > > + DPRINTK("%s xenbus_gather(tokens-rate) error",name); > > + }else{ > > + if(be->blkif->reqrate <=3D 0){ > > + be->blkif->reqmin =3D 0; > > + DPRINTK("%s tokens-rate =3D=3D 0,no limit",name);=09 > > + }else{ > > + DPRINTK("%s xenbus_gather(tokens-rate=3D%d)",name,be->blkif->reqr= ate); > > + be->blkif->reqrate *=3D 2; > > + be->blkif->reqmin =3D VBD_QOS_MIN_RATE_LIMIT; > > + if(be->blkif->reqmin > be->blkif->reqrate){ > > + be->blkif->reqrate =3D be->blkif->reqmin; > > + DPRINTK("%s reset default value(tokens-rate=3D%d)",name,be->blki= f->reqrate); > > + } > > + } > > + } > > +} > > =20 > > /** > > * Write the physical details regarding the block device to the stor= e, and > > @@ -439,6 +519,14 @@ again: > > if (err) > > goto abort; > > =20 > > + /*add by andrew for centos pv*/ > > + err =3D xenbus_printf(xbt, dev->nodename,"feature-flush-cache", "1"= ); > > + if (err){ > > + xenbus_dev_fatal(dev, err, "writing %s/feature-flush-cache", > > + dev->nodename); > > + goto abort; > > + } > > + > > err =3D xenbus_printf(xbt, dev->nodename, "sectors", "%llu", > > vbd_size(&be->blkif->vbd)); > > if (err) { > > @@ -469,11 +557,22 @@ again: > > if (err) > > xenbus_dev_fatal(dev, err, "ending transaction"); > > =20 > > + DPRINTK("xenbus_switch_to XenbusStateConnected"); > > + > > err =3D xenbus_switch_state(dev, XenbusStateConnected); > > if (err) > > xenbus_dev_fatal(dev, err, "switching to Connected state", > > dev->nodename); > > =20 > > + unregister_rate_watch(be); > > + err=3Dxenbus_watch_path2(dev, dev->otherend, "tokens-rate", > > + &be->rate_watch,rate_changed); > > + if (!err) > > + be->have_rate_watch =3D 1; > > + else > > + xenbus_dev_fatal(dev, err, "watching tokens-rate", > > + dev->nodename); > > + > > return; > > abort: > > xenbus_transaction_end(xbt, 1); >=20 >=20 > > _______________________________________________ > > Xen-devel mailing list > > Xen-devel@lists.xensource.com > > http://lists.xensource.com/xen-devel >=20 >=20 > _______________________________________________ > Xen-users mailing list > Xen-users@lists.xensource.com > http://lists.xensource.com/xen-users ************************************************** =D0=EC=B0=B2=A3=A8Andrew Xu=A3=A9 =B2=BF=C3=C5 =A3=BA=D4=C6=BF=EC=CF=DF - =D4=CB=D3=AA=D6=A7=B3=C5=D6=D0=D0= =C4 - =D1=D0=B7=A2=D6=D0=D0=C4 =CA=D6=BB=FA : 18910391796 E-mail=A3=BAxu.an@cloudex.cn =B5=D8=D6=B7 =A3=BA=B1=B1=BE=A9=CA=D0=B3=AF=D1=F4=C7=F8=BE=C6=CF=C9=C7=C5= =B6=AB=C2=B71=BA=C5M5=C2=A5 **************************************************