# Measure SPI throughput

Source: [https://docs.qualcomm.com/doc/80-88500-1/topic/33_Measure_SPI_throughput.html](https://docs.qualcomm.com/doc/80-88500-1/topic/33_Measure_SPI_throughput.html)

## Before you begin

The following files are required to perform the SPI throughput test: 

| File type | Location |
| --- | --- |
| `spidev` driver | system/lib/modules/spidev.ko |
| `spidev` source code | /vendor/qcom/proprietary/kernel-tests/spi |
| `spidevtest` compiled binary | /data/kernel-tests/spidevtest |

## About this task

To perform the SPI throughput test, do the following:

## Procedure

1. Find the number of SPI masters configured in the system.
        
adb rootCopy to clipboard

        adb wait-for-deviceCopy to clipboard

        adb shell Copy to clipboard

        cd /sys/class/spi_masterCopy to clipboard

        lsCopy to clipboard

The `ls` command lists each `spi master` as a folder under `sysfs`.
2. Load the `spidev` driver module.
        
cd /system/lib/modulesCopy to clipboard

        insmod spidev.ko busnum=0 chipselect=1 maxspeed=50000000Copy to clipboard

Where,
    - `busnum=0` means `spi0` is selected
    - `chipselect=1` means `cs 1` is selected
    - `maxspeed=50000000` limits the maximum SPI transfer speed to 50 MHz 
Note: The maximum SPI transfer
                    speed must be less than the maximum speed defined in the device tree of the SPI
                    master. Otherwise the following error occurs:

            ----------------- kernel log -------- 
            spi_qsd 7575000.spi: Invalid transfer: 50000000 Hz, 32 bpw
             tx=ffffffc042600000, rx=ffffffc03e280000
            --------------------------------------- Copy to clipboard

    If an error message is displayed after executing the `insmod` command,
            it indicates that different builds are used to compile the system image and boot image.
            To resolve this issue, recompile the `spidev.ko` file using the same
            build that is used to compile the boot image and push the new build to
              /system/lib/modules.
3. Enable the necessary ftrace mask of the SPI driver.
        
echo "" > /d/tracing/set_eventCopy to clipboard

        echo "" > /d/tracing/traceCopy to clipboard

        echo 1 > /d/tracing/events/spi/spi_message_submit/enableCopy to clipboard

        echo 1 > /d/tracing/events/spi/spi_message_start/enableCopy to clipboard

        echo 1 > /d/tracing/events/spi/spi_message_done/enableCopy to clipboard

        echo 1 > /d/tracing/events/spi/spi_transfer_start/enableCopy to clipboard

        echo 1 > /d/tracing/events/spi/spi_transfer_stop/enableCopy to clipboard

To get the transfer time, use the `delta (spi_message_start, spi_message_done)` method.
4. Execute the SPI transfer:
        
cd /data/kernel-tests/spiCopy to clipboard

        chmod 777 spidevtestCopy to clipboard

Sample output:

<samp class="ph systemoutput">./spidevtest spidev0.1 -l -t 4 -z 50000000 </samp>

<samp class="ph systemoutput">./spidevtest spidev0.1 -l -t 4 -z 50000000 </samp>

<samp class="ph systemoutput">Test passed</samp>
5. Collect the `ftrace` log.
        
cat /d/tracing/traceCopy to clipboard

<samp class="ph systemoutput"> ---- Trace log ------ </samp>

<samp class="ph systemoutput">spidevtest-3902 [001] d..3 134.138392: spi_message_submit: spi0.1
              ffffffc0b9d77da8</samp>

<samp class="ph systemoutput">spi0-243 [001] ...1 134.138512: spi_transfer_start: spi0.1
              ffffffc0b9d4a480 len=32808</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.147282: spi_transfer_stop: spi0.1
              ffffffc0b9d4a480 len=32808</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.147357: spi_message_done: spi0.1 ffffffc0b9d77da8
              len=32808/32808</samp>

<samp class="ph systemoutput">spidevtest-3902 [001] d..3 134.147985: spi_message_submit: spi0.1
              ffffffc0b9d77da8</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.148275: spi_message_start: spi0.1
              ffffffc0b9d77da8</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.148326: spi_transfer_start: spi0.1
              ffffffc0b9d4a480 len=32808</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.156654: spi_transfer_stop: spi0.1
              ffffffc0b9d4a480 len=32808</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.156715: spi_message_done: spi0.1 ffffffc0b9d77da8
              len=32808/32808</samp>

<samp class="ph systemoutput">spidevtest-3902 [000] d..3 134.157526: spi_message_submit: spi0.1
              ffffffc0b9d77da8</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.157643: spi_message_start: spi0.1
              ffffffc0b9d77da8</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.157685: spi_transfer_start: spi0.1
              ffffffc049119580 len=32808</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.166033: spi_transfer_stop: spi0.1
              ffffffc049119580 len=32808</samp>

<samp class="ph systemoutput">spi0-243 [000] ...1 134.166095: spi_message_done: spi0.1
            ffffffc0b9d77da8 len=32808/32808</samp>
6. Calculate the throughput, using the formula: Package\_size \*
            8/(1000\*Delta\_time) .
        
Where, `Delta time = spi_message_done - spi_message_start`. The
              `Delta_time` is the duration measured in milliseconds. The SPI
            throughput is calculated in Mbps.

Delta\_time\_1: 134.147357 - 134.138474 = 8.883 ms

Delta\_time\_2: 134.156715 - 134.148275 = 8.44 ms

Delta\_time\_3: 134.166095 - 134.157643 = 8.452 ms

Average\_Delta = (8.883 + 8.44 + 8.452)/3 = 8.5916 ms

Throughput = 32808 \* 8/(8.5916 \* 1000) = 30.55 Mbps

Note:To achieve the best SPI
            throughput, do the following:
    1. Set the SPI transfer parameter setting as N = 32 bits per word, transfer speed =
                50 MHz, large payload size.
    2. Set the Linux OS governor to the Performance mode and disable the idle power
                collapse.

**Parent Topic:** [SPI in QUP](https://docs.qualcomm.com/doc/80-88500-1/topic/30_SPI_in_QUP_v3.html)

Last Published: Aug 18, 2023

[Previous Topic
Add delay between SPI\_CS asserting pin and SPI\_CLK](https://docs.qualcomm.com/bundle/publicresource/80-88500-1/topics/32_Add_delay_time_between_SPI_CS_asserting_pin_and_SPI_CLK.md) [Next Topic
Configure SPI in sensor core](https://docs.qualcomm.com/bundle/publicresource/80-88500-1/topics/34_Configure_SPI_in_sensor_core_.md)