Received: from eggs.gnu.org ([2001:4830:134:3::10]:57800) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWA8i-0006Y0-Mb for qemu-devel@nongnu.org; Sun, 30 Aug 2015 17:27:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZWA8f-0007Gw-BP for qemu-devel@nongnu.org; Sun, 30 Aug 2015 17:27:48 -0400 Received: from mail-qk0-f173.google.com ([209.85.220.173]:36233) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZWA8f-0007DJ-41 for qemu-devel@nongnu.org; Sun, 30 Aug 2015 17:27:45 -0400 Received: by qkda128 with SMTP id a128so53147139qkd.3 for ; Sun, 30 Aug 2015 14:26:36 -0700 (PDT) From: Programmingkid Content-Type: multipart/alternative; boundary=Apple-Mail-3-953810382 Date: Sun, 30 Aug 2015 17:26:34 -0400 Message-Id: <1DE4A749-5B47-4E27-9B52-DB3FDBEAAA62@gmail.com> Mime-Version: 1.0 (Apple Message framework v1084) Subject: [Qemu-devel] Help with USB packet transmission rate List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell , Gerd Hoffmann Cc: qemu-devel qemu-devel --Apple-Mail-3-953810382 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii The USB Start of Frame packet is sent 1000 times a second from the host = to a USB device. This packet is used to synchronize audio data being = sent to a USB sound card. On my system the Start of Frame packet is only = sent about 830 times a second. This makes audio that is played thru the = USB sound card sound kind of slow.=20 Research into the problem indicates the rate QEMU is suppose to send the = packet isn't actually being done. How it is set up now the emulated = sound hardware just assumes the rate it is suppose to send data is what = actually happens. This isn't true. More variables need to be accounted = for.=20 What I propose is code be made that actively monitors how many Start of = Frame packets are being sent in a given second. If the value goes down, = then QEMU should compensate and try to send more Start of Frame packets. = If the value goes too high, QEMU should throttle back on Start of Frame = packet transmission.=20 This code is basically what I think should be put into place to help = maintain a consistent Start of Frame packet transmission rate. This code = is easy to build yourself and try out. What it does is makes two = threads. One is a consumer and the other is a producer thread. The = consumer thread monitors the production of the producer thread. The = consumer thread can actively adjust the rate of the producer thread to = help it maintain a steady pace. You can test this program out by having = it run and seeing how its values change when you do CPU intensive stuff. = What I do is open a YouTube video and scroll in the window really fast = to see how the program adapts.=20 I would like to know if this looks like something that could do a good = job at maintaining a steady Start of Frame packet transmission rate. I'm = hoping for more suggestions and improvements I could make to this = program before I try to implement it in the hcd-ohci.c file.=20 #include #include #include #include #include int counter, producer_sleep_value; int goalPerSecond =3D 1000; // the number of cycles we want the = producer function to run at in a second int range =3D 0; int delta_sleep_value =3D 1; #define MAX_NUM_VALUES 5 int sampleArray[MAX_NUM_VALUES]; int totalValue; int totalNumberOfValues; int checkInterval =3D 1000*30; // in microseconds sets how often we = check on the producer float secondsPerCheckInterval; int goalPerCheckInterval; // Set some of the global variables void init() { secondsPerCheckInterval =3D (float)checkInterval/(1000*1000); goalPerCheckInterval =3D goalPerSecond*secondsPerCheckInterval;=09= producer_sleep_value =3D 1000; } // Obtain a sample count value and place it into the sampleArray void inputSample(int new_value) { static int index; =09 totalNumberOfValues++; totalValue +=3D new_value; sampleArray[index++] =3D new_value; if (index >=3D MAX_NUM_VALUES) { index =3D 0; } } // Return the average value of all stored samples float getAverageValue() { float total =3D 0; for (int i =3D 0; i < MAX_NUM_VALUES; i++) { total +=3D sampleArray[i]; } return total/MAX_NUM_VALUES; } // Return the average sample size over the last 30 seconds float getTotalAverage() { int valuesPerTimeFrame; float returnValue; =09 /* 1 value/checkInterval microseconds x 1,000,000 = microseconds/second x 30 seconds =3D 1 * 1,000,000 * 30 / = (checkInterval*1) =3D=20 30,000,000/checkInterval */ =09 valuesPerTimeFrame =3D 30000000/checkInterval; =09 // only last 30 seconds if (totalNumberOfValues >=3D valuesPerTimeFrame) { totalNumberOfValues =3D 0; totalValue =3D 0; =09 printf("30 SECOND RESET ************\n"); } =09 if (totalNumberOfValues =3D=3D 0) { returnValue =3D 0; } else { returnValue =3D (float)totalValue/totalNumberOfValues; } return returnValue; } // Figure out how close we are to the goal float getGoalPercent() { return getTotalAverage() * ((float)1/goalPerCheckInterval) * = 100; } // Tries to maintain a consistent amount of output from the producer void *consumerFunc(void *dummy) { while(1) { inputSample(counter); printf("current =3D %3d, current average: %3.2f, percent = goal: %3.2f%c\n", counter, getAverageValue(), getGoalPercent(), '%'); =09 // If the produced amount is too low if (counter+range < goalPerCheckInterval) { producer_sleep_value -=3D delta_sleep_value; printf("Reducing sleep value: %d\n", = producer_sleep_value); =09 // If the produced amount is too high } else if (counter-range > goalPerCheckInterval) { producer_sleep_value +=3D delta_sleep_value; printf("Increasing sleep value: %d\n", = producer_sleep_value); } =09 counter =3D 0; usleep(checkInterval); } } // Produces the output we need void *producerFunc(void *dummy) { while(1) { counter++; if (usleep(producer_sleep_value) !=3D 0)=20 printf("Problem detected with usleep!\n"); } } int main (int argc, const char * argv[]) { init(); =09 pthread_t consumerThread, producerThread; =09 pthread_create(&producerThread, NULL, producerFunc, NULL); =09 pthread_create(&consumerThread, NULL, consumerFunc, NULL); =09 pthread_join(producerThread, NULL); pthread_join(consumerThread, NULL); =09 =09 return 0; } --Apple-Mail-3-953810382 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii The = USB Start of Frame packet is sent 1000 times a second from the host to a = USB device. This packet is used to synchronize audio data being sent to = a USB sound card. On my system the Start of Frame packet is only sent = about 830 times a second. This makes audio that is played thru the USB = sound card sound kind of slow. 

Research into = the problem indicates the rate QEMU is suppose to send the packet isn't = actually being done. How it is set up now the emulated sound hardware = just assumes the rate it is suppose to send data is what actually = happens. This isn't true. More variables need to be accounted = for. 

What I propose is code be made that = actively monitors how many Start of Frame packets are being sent in a = given second. If the value goes down, then QEMU should compensate and = try to send more Start of Frame packets. If the value goes too high, = QEMU should throttle back on Start of Frame packet = transmission. 

This code is basically what = I think should be put into place to help maintain a consistent Start of = Frame packet transmission rate. This code is easy to build yourself and = try out. What it does is makes two threads. One is a consumer and the = other is a producer thread. The consumer thread monitors the production = of the producer thread. The consumer thread can actively adjust the rate = of the producer thread to help it maintain a steady pace. You can test = this program out by having it run and seeing how its values change when = you do CPU intensive stuff. What I do is open a YouTube video and scroll = in the window really fast to see how the program = adapts. 

I would like to know if this = looks like something that could do a good job at maintaining a steady = Start of Frame packet transmission rate. I'm hoping for more suggestions = and improvements I could make to this program before I try to implement = it in the hcd-ohci.c file. 

#include <stdio.h>
#include <pthread.h>
#include = <unistd.h>
#include <memory.h>
#include <sched.h>

int counter, = producer_sleep_value;
int goalPerSecond =3D = 1000;  // the number of cycles we want the producer = function to run at in a second
int range =3D 0;
int = delta_sleep_value =3D 1;
#define MAX_NUM_VALUES 5
int = sampleArray[MAX_NUM_VALUES];
int totalValue;
int totalNumberOfValues;
int = checkInterval =3D 1000*30; // in = microseconds sets how often we check on the producer
float secondsPerCheckInterval;
int goalPerCheckInterval;

// Set some = of the global variables
void init()
{
= secondsPerCheckInterval =3D = (float)checkInterval/(1000*1000);
= goalPerCheckInterval =3D = goalPerSecond*secondsPerCheckInterval; =
= producer_sleep_value =3D = 1000;
}

// = Obtain a sample count value and place it into the sampleArray
void inputSample(int new_value)
{
static int = index;


totalNumberOfValues++;
totalValue +=3D new_value;
sampleArray[index++] =3D new_value;
if (index >=3D MAX_NUM_VALUES) {
= index =3D 0;
= }
}

// = Return the average value of all stored samples
float getAverageValue()
float total =3D 0;
for (int i =3D = 0; i < MAX_NUM_VALUES; i++) {
total +=3D sampleArray[i];
= }
= return total/MAX_NUM_VALUES;
}

// = Return the average sample size over the last 30 seconds
float getTotalAverage()
int valuesPerTimeFrame;
float returnValue;


/* 1 = value/checkInterval microseconds x 1,000,000 microseconds/second x 30 = seconds =3D 1 * 1,000,000 * 30 / (checkInterval*1) =3D 
= 30,000,000/checkInterval
*/


valuesPerTimeFrame =3D 30000000/checkInterval;


// only last 30 = seconds
if (totalNumberOfValues >=3D valuesPerTimeFrame) = {
= totalNumberOfValues =3D = 0;
= totalValue =3D 0;


printf("30 SECOND RESET ************\n");
}


if = (totalNumberOfValues =3D=3D = 0) {
returnValue =3D 0;
} else {
= returnValue =3D (float)totalValue/totalNumberOfValues;
}
return returnValue;
}


// Figure out = how close we are to the goal
float getGoalPercent()
{
return getTotalAverage() * ((float)1/goalPerCheckInterval) * = 100;
}

// = Tries to maintain a consistent amount of output from the = producer
void = *consumerFunc(void = *dummy)
{
while(1) = {
= inputSample(counter);
= printf("current =3D %3d, current average: = %3.2f, percent goal: %3.2f%c\n", = counter, getAverageValue(), = getGoalPercent(), '%');


        // If the = produced amount is too low
if (counter+range < = goalPerCheckInterval) = {
= producer_sleep_value -=3D delta_sleep_value;
= printf("Reducing sleep value: %d\n", producer_sleep_value);


        // If the = produced amount is too high
} else = if (counter-range > = goalPerCheckInterval) = {
= producer_sleep_value +=3D delta_sleep_value;
= printf("Increasing sleep value: %d\n", producer_sleep_value);
}  =
= counter =3D 0;
= usleep(checkInterval);
}
}
// Produces the output we = need
void = *producerFunc(void = *dummy)
{
while(1) = {
= counter++;
= if (usleep(producer_sleep_value) !=3D= 0) 
printf("Problem detected with usleep!\n");
}
}
int main = (int argc, const char * = argv[]) {

init();


pthread_t consumerThread, producerThread;


pthread_create(&producerThread, NULL, producerFunc, NULL);


pthread_create(&consumerThread, NULL, consumerFunc, NULL);


pthread_join(producerThread, NULL);
pthread_join(consumerThread, NULL);



    return 0;
}

= --Apple-Mail-3-953810382--