: Lower cpuload when using the usb-tablet
PATCH 1/1 : Lower cpuload when using the usb-tablet
And here then is my "piece de resistance" (at least for the last few weeks), my final version of the patchset to lower the cpuload when using the usb-tablet device, or for that matter when using any usb devices with interrupt eps connected to the ehci controller.
This final version is actually quite simple, and since the high cpuload with the usb-tablet is an often heared complaint I would like to advocate that this is a bugfix and thus belongs in 1.3, I hope you will agree and send this to Anthony for inclusion into 1.3 :)
Regards,
Hans
PATCH 1/5 usb: Call wakeup when data becomes available for all devices with int eps
This is necessary for proper interaction with the xhci controller, and it will allow other hcds to lower there frame timer while waiting for interrupt data.
--- hw/usb/dev-hub.c | 2 ++ hw/usb/dev-network.c | 7 +++++++ hw/usb/dev-wacom.c | 4 ++++ hw/usb/redirect.c | 4 ++++ 4 files changed, 17 insertions(+)
hw/usb/dev-hub.c +2 -0
@@ -184,6 +184,7 @@ static void usb_hub_detach(USBPort *port1) port->wPortStatus &= ~PORT_STAT_ENABLE ; port->wPortChange |=PORT_STAT_C_ENABLE ; } +usb_wakeup (s->intr); }static void usb_hub_child_detach (USBPort *port1,USBDevice *child)
@@ -363,6 +364,7 @@ static void usb_hub_handle_control(USBDevice *dev, USBPacket *p, port->wPortChange |=PORT_STAT_C_RESET ;/* set enable bit */ port->wPortStatus |=PORT_STAT_ENABLE ; +usb_wakeup (s->intr); }break ;case PORT_POWER :
hw/usb/dev-network.c +7 -0
@@ -639,6 +639,8 @@ typedef struct USBNetState {unsigned int in_ptr, in_len;uint8_t in_buf[2048 ]; +USBEndpoint *intr; +char usbstring_mac[13 ];NICState *nic;NICConf conf;
@@ -851,6 +853,10 @@ static void *rndis_queue_response(USBNetState *s, unsigned int length)struct rndis_response *r =g_malloc0 (sizeof (struct rndis_response ) + length); +if (QTAILQ_EMPTY (&s->rndis_resp)) { +usb_wakeup (s->intr); + } +QTAILQ_INSERT_TAIL (&s->rndis_resp, r, entries); r->length = length;
@@ -1349,6 +1355,7 @@ static int usb_net_initfn(USBDevice *dev) s->media_state =0 ;/* NDIS_MEDIA_STATE_CONNECTED */ ; s->filter =0 ; s->vendorid =0x1234 ; + s->intr =usb_ep_get (dev,USB_TOKEN_IN ,1 );qemu_macaddr_default_if_unset (&s->conf.macaddr); s->nic = qemu_new_nic(&net_usbnet_info, &s->conf,
hw/usb/dev-wacom.c +4 -0
@@ -43,6 +43,7 @@ typedef struct USBWacomState { USBDevice dev; + USBEndpoint *intr; QEMUPutMouseEntry *eh_entry; int dx, dy, dz, buttons_state; int x, y;
@@ -137,6 +138,7 @@ static void usb_mouse_event(void *opaque, s->dz += dz1; s->buttons_state = buttons_state; s->changed =1 ; +usb_wakeup (s->intr); } static void usb_wacom_event(void *opaque,
@@ -150,6 +152,7 @@ static void usb_wacom_event(void *opaque, s->dz += dz; s->buttons_state = buttons_state; s->changed =1 ; +usb_wakeup (s->intr); }static inline int int_clamp (int val,int vmin,int vmax)
@@ -337,6 +340,7 @@ static int usb_wacom_initfn(USBDevice *dev)USBWacomState *s =DO_UPCAST (USBWacomState, dev, dev);usb_desc_create_serial (dev);usb_desc_init (dev); + s->intr =usb_ep_get (dev,USB_TOKEN_IN ,1 ); s->changed =1 ;return 0 ; }
hw/usb/redirect.c +4 -0
@@ -1667,6 +1667,10 @@ static void usbredir_interrupt_packet(void *priv, uint64_t id,return ; } +if (QTAILQ_EMPTY (&dev->endpoint[EP2I (ep)].bufpq)) { +usb_wakeup (usb_ep_get (&dev->dev,USB_TOKEN_IN , ep &0x0f )); + } +/* bufp_alloc also adds the packet to the ep queue */ bufp_alloc (dev, data, data_len, interrupt_packet->status, ep); }else {
Signature · 1 line
1.8.0
PATCH 2/5 usb: Don't allow USB_RET_ASYNC for interrupt packets
In reply to Hans de GoedeIt is tempting to use USB_RET_ASYNC for interrupt packets, rather then the current NAK + polling approach, but this causes issues for migration, as an async completed packet will not getting written back to guest memory until the next poll time, and if a migration happens in between it will get lost!
Make an exception for host devices, because:
1) host-linux actually uses async completion for interrupt endpoints
2) host devices don't migrate anyways
Ideally we would convert host-linux.c to handle (input) interrupt endpoints in a buffered manner like it does for isoc endpoints, keeping multiple urbs submitted to ensure the devices timing requirements are met, as well as making its interrupt ep handling the same as other usb-devices.
--- hw/usb.h | 1 + hw/usb/core.c | 4 ++++ hw/usb/host-bsd.c | 1 + hw/usb/host-linux.c | 1 + 4 files changed, 7 insertions(+)
hw/usb.h +1 -0
@@ -197,6 +197,7 @@ struct USBEndpoint {enum USBDeviceFlags {USB_DEV_FLAG_FULL_PATH , +USB_DEV_FLAG_IS_HOST , };/* definition of a USB device */
hw/usb/core.c +4 -0
@@ -406,7 +406,11 @@ void usb_handle_packet(USBDevice *dev, USBPacket *p)if (QTAILQ_EMPTY (&p->ep->queue) || p->ep->pipeline) {usb_process_one (p);if (p->status ==USB_RET_ASYNC ) { +/* hcd drivers cannot handle async for isoc */ assert (p->ep->type !=USB_ENDPOINT_XFER_ISOC ); +/* using async for interrupt packets breaks migration */ +assert (p->ep->type !=USB_ENDPOINT_XFER_INT || + (dev->flags &USB_DEV_FLAG_IS_HOST ));usb_packet_set_state (p,USB_PACKET_ASYNC );QTAILQ_INSERT_TAIL (&p->ep->queue, p, queue); }else if (p->status ==USB_RET_ADD_TO_QUEUE ) {
hw/usb/host-bsd.c +1 -0
@@ -292,6 +292,7 @@ static void usb_host_handle_destroy(USBDevice *opaque)static int usb_host_initfn (USBDevice *dev) { + dev->flags |= (1 <<USB_DEV_FLAG_IS_HOST );return 0 ; }
hw/usb/host-linux.c +1 -0
@@ -1476,6 +1476,7 @@ static int usb_host_initfn(USBDevice *dev) {USBHostDevice *s =DO_UPCAST (USBHostDevice, dev, dev); + dev->flags |= (1 <<USB_DEV_FLAG_IS_HOST ); dev->auto_attach =0 ; s->fd =-1 ; s->hub_fd =-1 ;
Signature · 1 line
1.8.0
PATCH 3/5 usb: Allow overriding of usb_desc at the device level
In reply to Hans de GoedeThis allows devices to present a different set of descriptors based on device properties.
--- hw/usb.h | 1 + hw/usb/bus.c | 3 +++ 2 files changed, 4 insertions(+)
hw/usb.h +1 -0
@@ -230,6 +230,7 @@ struct USBDevice {USBEndpoint ep_out[USB_MAX_ENDPOINTS ]; QLIST_HEAD(, USBDescString) strings; +const USBDesc *usb_desc;/* Overrides class usb_desc if not NULL */ const USBDescDevice *device;int configuration;
hw/usb/bus.c +3 -0
@@ -166,6 +166,9 @@ const char *usb_device_get_product_desc(USBDevice *dev)const USBDesc *usb_device_get_usb_desc (USBDevice *dev) {USBDeviceClass *klass =USB_DEVICE_GET_CLASS (dev); +if (dev->usb_desc) { +return dev->usb_desc; + }return klass->usb_desc; }
Signature · 1 line
1.8.0
PATCH 4/5 ehci: Lower timer freq when the periodic schedule is idle
In reply to Hans de GoedeLower the timer freq if no iso schedule packets complete for 64 frames in a row.
We can safely do this, without adding latency, because:
1) If there is isoc traffic this will never trigger
2) For async handled interrupt packets (only usb-host), the completion handler will immediately schedule the frame_timer from a bh
3) All devices using NAK to signal no data for interrupt endpoints now use wakeup, which will immediately schedule the frame_timer from a bh
The advantage of this is that when we only have interrupt packets in the periodic schedule, async_stepdown can do its work and significantly lower the frequency at which the frame_timer runs.
--- hw/usb/hcd-ehci.c | 39 +++++++++++++++++++++++++++++++++------ hw/usb/hcd-ehci.h | 1 + 2 files changed, 34 insertions(+), 6 deletions(-)
hw/usb/hcd-ehci.c +33 -6
@@ -114,6 +114,7 @@#define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction#define MAX_QH 100 // Max allowable queue heads in a chain#define MIN_FR_PER_TICK 3 // Min frames to process when catching up +#define PERIODIC_ACTIVE 64/* Internal periodic / asynchronous schedule state machine states */
@@ -738,6 +739,19 @@ static int ehci_register_companion(USBBus *bus, USBPort *ports[],return 0 ; } +static void ehci_wakeup_endpoint (USBBus *bus,USBEndpoint *ep) +{ +EHCIState *s =container_of (bus, EHCIState, bus); +uint32_t portsc = s->portsc[ep->dev->port->index]; + +if (portsc &PORTSC_POWNER ) { +return ; + } + + s->periodic_sched_active =PERIODIC_ACTIVE ; +qemu_bh_schedule (s->async_bh); +} +static USBDevice *ehci_find_device (EHCIState *ehci,uint8_t addr) {USBDevice *dev;
@@ -1188,9 +1202,10 @@ static void ehci_async_complete_packet(USBPort *port, USBPacket *packet)trace_usb_ehci_packet_action (p->queue, p,"wakeup" ); p->async =EHCI_ASYNC_FINISHED ; -if (p->queue->async) { -qemu_bh_schedule (p->queue->ehci->async_bh); +if (!p->queue->async) { + s->periodic_sched_active =PERIODIC_ACTIVE ; } +qemu_bh_schedule (s->async_bh); }static void ehci_execute_complete (EHCIQueue *q)
@@ -1344,6 +1359,8 @@ static int ehci_process_itd(EHCIState *ehci,uint32_t i, len, pid, dir, devaddr, endp;uint32_t pg, off, ptr1, ptr2, max, mult; + ehci->periodic_sched_active =PERIODIC_ACTIVE ; + dir =(itd->bufptr[1 ] &ITD_BUFPTR_DIRECTION ); devaddr =get_field (itd->bufptr[0 ],ITD_BUFPTR_DEVADDR ); endp =get_field (itd->bufptr[0 ],ITD_BUFPTR_EP );
@@ -2033,6 +2050,9 @@ static void ehci_advance_state(EHCIState *ehci, int async)case EST_WRITEBACK :assert (q !=NULL ); again =ehci_state_writeback (q); +if (!async) { + ehci->periodic_sched_active =PERIODIC_ACTIVE ; + }break ;default :
@@ -2198,7 +2218,6 @@ static void ehci_frame_timer(void *opaque)if (ehci_periodic_enabled (ehci) || ehci->pstate !=EST_INACTIVE ) { need_timer++; - ehci->async_stepdown =0 ;if (frames > ehci->maxframes) { skipped_frames = frames - ehci->maxframes;
@@ -2222,18 +2241,25 @@ static void ehci_frame_timer(void *opaque)break ; } } +if (ehci->periodic_sched_active) { + ehci->periodic_sched_active--; + }ehci_update_frindex (ehci,1 );ehci_advance_periodic_state (ehci); ehci->last_run_ns +=FRAME_TIMER_NS ; } }else { -if (ehci->async_stepdown < ehci->maxframes /2 ) { - ehci->async_stepdown++; - } + ehci->periodic_sched_active =0 ;ehci_update_frindex (ehci, frames); ehci->last_run_ns +=FRAME_TIMER_NS * frames; } +if (ehci->periodic_sched_active) { + ehci->async_stepdown =0 ; + }else if (ehci->async_stepdown < ehci->maxframes /2 ) { + ehci->async_stepdown++; + } +/* Async is not inside loop since it executes everything it can once * called */
@@ -2301,6 +2327,7 @@ static USBPortOps ehci_port_ops = {static USBBusOps ehci_bus_ops = { .register_companion = ehci_register_companion, + .wakeup_endpoint = ehci_wakeup_endpoint, };static int usb_ehci_post_load (void *opaque,int version_id)
hw/usb/hcd-ehci.h +1 -0
@@ -311,6 +311,7 @@ struct EHCIState {uint64_t last_run_ns;uint32_t async_stepdown; +uint32_t periodic_sched_active;bool int_req_by_async; };
Signature · 1 line
1.8.0
PATCH 5/5 usb-tablet: Allow connecting to ehci
In reply to Hans de GoedeOur ehci code has is capable of significantly lowering the wakeup rate for the hcd emulation while the device is idle. It is possible to add similar code ot the uhci emulation, but that simply is not there atm, and there is no reason why a (virtual) usb-tablet can not be a USB-2 device.
Making usb-hid devices connect to the emulated ehci controller instead of the emulated uhci controller on vms which have both lowers the cpuload for a fully idle vm from 20% to 2-3% (on my laptop).
An alternative implementation to using a property to select the tablet type, would be simply making it a new device type, ie usb-tablet2, but the downside of that is that this will require libvirt changes to be available through libvirt at all, and then management tools changes to become the default for new vms, where as using a property will automatically get any pc-1.3 type vms the lower cpuload.
--- hw/pc_piix.c | 4 +++ hw/usb/dev-hid.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-)
hw/pc_piix.c +4 -0
@@ -386,6 +386,10 @@ static QEMUMachine pc_machine_v1_3 = { .driver = "VGA",\ .property = "mmio",\ .value = "off",\ + },{\ + .driver = "usb-tablet",\ + .property = "usb_version",\ + .value = stringify(1),\ }static QEMUMachine pc_machine_v1_2 = {
hw/usb/dev-hid.c +84 -1
130 lines not on this page · raw message
Signature · 1 line
1.8.0