Developer Portal Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    Improve USB write performance

    wqrelkkmaf
    New Poster

    Improve USB write performance

    Hi everyone,

    I actually have an issue quite similar to Verena's here. While she wanted to improve write performance to sd, i want to optimize write performance to USB. Here's what I have - everything else is unchanged from the StreamDataViaUsb sample project.

     

    static void AppControllerFire(void* pvParameters)
    {
        BCDS_UNUSED(pvParameters);
    
        Retcode_T retcode = RETCODE_OK;
        Sensor_Value_T sensorValue;
    
        int bufsize = 100;
        char msg[37];
        char s[sizeof(msg)*bufsize];
        char *ptr;
        ptr = s;
        TickType_t timer;
        unsigned long int j = 0;
    
        while (1)
        {
        	for (int i = 0; i < bufsize; i++){
    
            retcode = Sensor_GetData(&sensorValue);
        	timer = xTaskGetTickCount();
            j += 1;
    			if (RETCODE_OK == retcode)
    			{
            		int sz = snprintf(msg,sizeof(msg),"%ld,%ld,%ld,%f;%ldx%ld",
    					(long int) sensorValue.Accel.X,
    					(long int) sensorValue.Accel.Y,
    					(long int) sensorValue.Accel.Z,
    					(double) sensorValue.Noise,
    					(unsigned long int) timer,
    					(unsigned long int) j);
    
    				strcpy(ptr,msg);
    				ptr+=sz;
                    memset(&sensorValue, 0x00, sizeof(sensorValue));
                    memset(&timer,0x00, sizeof(timer));
                    vTaskDelay(pdMS_TO_TICKS(0));
            	}
            	printf("%s\n", s);
            	memset(&s,0x00,sizeof(s));
            	ptr = s;
            }
            if (RETCODE_OK != retcode)
            {
                Retcode_RaiseError(retcode);
            }
            //vTaskDelay(pdMS_TO_TICKS(APP_CONTROLLER_TX_DELAY));
        }
    }

     

     Now - I get about 140 messages per second. The code runs stable. If I keep increasing the buffersize, the OS throws a buffer overflow. Now my questions:

    1. Does this code still have obvious design or implementation flaws?
    2. What could I do to improve performance?
    3 REPLIES 3
    Shanmathi
    Established Member

    Re: Improve USB write performance

    Hello wqrelkkmaf,

    The reason for using bufzise is unclear to me.

    I have modified your code such that it will continously stream the desired sensor values via USB, in the expected format.

    static void AppControllerFire(void* pvParameters)
    {
        BCDS_UNUSED(pvParameters);
    
        Retcode_T retcode = RETCODE_OK;
        Sensor_Value_T sensorValue;
    
    //    int bufsize = 100;
          char msg[37] = {0};
    //    char s[sizeof(msg)*bufsize];
    //    char *ptr;
    //    ptr = s;
        TickType_t timer;
        unsigned long int j = 0;
    
        while (1)
        {
    //    	for (int i = 0; i < bufsize; i++)
        	{
    			retcode = Sensor_GetData(&sensorValue);
    			timer = xTaskGetTickCount();
    			j += 1;
    			if (RETCODE_OK == retcode)
    			{
            		int sz = snprintf(msg,sizeof(msg),"%ld,%ld,%ld,%f;%ldx%ld",
    					(long int) sensorValue.Accel.X,
    					(long int) sensorValue.Accel.Y,
    					(long int) sensorValue.Accel.Z,
    					(double) sensorValue.Noise,
    					(unsigned long int) timer,
    					(unsigned long int) j);
    
    //				strcpy(ptr,msg);
    //				ptr+=sz;
                    memset(&sensorValue, 0x00, sizeof(sensorValue));
                    memset(&timer,0x00, sizeof(timer));
                    vTaskDelay(pdMS_TO_TICKS(0));
            	}
            	printf("%s\n", msg);
    //        	memset(&s,0x00,sizeof(s));
    //        	ptr = s;
            }
            if (RETCODE_OK != retcode)
            {
                Retcode_RaiseError(retcode);
            }
            //vTaskDelay(pdMS_TO_TICKS(APP_CONTROLLER_TX_DELAY));
        }
    }

    Output:

    .....
     INFO | XDK DEVICE 1: 17,-95,1043,0.001291;15194x1644
     INFO | XDK DEVICE 1: 11,-105,1044,0.000556;15203x1645
     INFO | XDK DEVICE 1: 13,-118,1040,0.000442;15212x1646
     INFO | XDK DEVICE 1: 15,-109,1040,0.000845;15221x1647
     INFO | XDK DEVICE 1: *******Light sensor application callback received*******
     INFO | XDK DEVICE 1: 1,-97,1041,0.002672;15231x1648
     INFO | XDK DEVICE 1: 19,-98,1044,0.001244;15239x1649
     INFO | XDK DEVICE 1: -10,-96,1047,0.000438;15248x1650
     INFO | Port 'COM13' has been disconnected

    Here are some additional suggestions,

    • it is better to clear the msg buffer before getting sensor data rather than performing memset after printing.
    • Not sure with your exact sue case, but the tick counts (timer variable), need not be reset to 0 everytime. The incremental tick count might be of better usage to know analyse the time taken for transmitting data via USB.

    With Regards,

    Shanmathi P

     

    wqrelkkmaf
    New Poster

    Re: Improve USB write performance

    Hi Shanmathi,

    thanks for your reply. The reason I'm using a buffer (and bufsize to scale it) is because I want to minimize the calls of the (what I assume to be) costly printf function that writes data to usb. I call it every 100 messages and write big chunks instead of small ones. As far as I can see, your code writes data after every Get_SensorData and subsequently on average achieves ca 108Hz (as seen by dividing count by ticks*1000 from the log).

    Thanks for the note that I don't need to reset the timer but clear the msg-variable. I'll change that.

    Question: What's the difference between memset at the end of the loop and calling it before getting sensor data?

    Cheers

    Arno

    Shanmathi
    Established Member

    Re: Improve USB write performance

    Hi wqrelkkmaf ,

    Sorry for the late reply, somehow missed to post the reply.

    Regarding  Ques - What's the difference between memset at the end of the loop and calling it before getting sensor data?

    There is no functional difference, but only recommended to have a clean buffer just before data acquisition.

    There might be possiblities that the buffer can get corrupted or filled with junk data due to some other memory corruptions/ pointer mishandlings.

    Thus it is recommended to have it the following way, clean buffer -> get sensor data -> do post processing/ other activities -> again clean buffer -> get data....

    instead of get sensor data -> clean buffer ->  do post processing/ other activities -> get data...

     

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist