Home | History | Annotate | Download | only in sensors
      1 page.title=Batching
      2 @jd:body
      3 
      4 <!--
      5     Copyright 2014 The Android Open Source Project
      6 
      7     Licensed under the Apache License, Version 2.0 (the "License");
      8     you may not use this file except in compliance with the License.
      9     You may obtain a copy of the License at
     10 
     11         http://www.apache.org/licenses/LICENSE-2.0
     12 
     13     Unless required by applicable law or agreed to in writing, software
     14     distributed under the License is distributed on an "AS IS" BASIS,
     15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16     See the License for the specific language governing permissions and
     17     limitations under the License.
     18 -->
     19 <div id="qv-wrapper">
     20   <div id="qv">
     21     <h2>In this document</h2>
     22     <ol id="auto-toc">
     23     </ol>
     24   </div>
     25 </div>
     26 
     27 <h2 id="what_is_batching">What is batching?</h2>
     28 <p>Batching refers to storing sensor events in a hardware FIFO before reporting
     29   them through the <a href="hal-interface.html">HAL</a> instead of reporting them immediately.</p>
     30 <p>Batching can enable significant power savings by preventing the SoC from waking
     31   up to receive each event. Instead, the events can be grouped and processed
     32   together. </p>
     33 <p>The bigger the FIFOs, the more power can be saved. Implementing batching is an
     34   exercise of trading off hardware memory for reduced power consumption.</p>
     35 <p>Batching happens when a sensor possesses a hardware FIFO
     36   (<code>sensor_t.fifoMaxEventCount &gt; 0</code>) and we are in one of two situations:</p>
     37 <ul>
     38   <li> <code>max_report_latency &gt; 0</code>, meaning the sensor events for this specific sensor can
     39     be delayed up to <code>max_report_latency</code> before being reported through the HAL. </li>
     40   <li> or the SoC is in suspend mode and the sensor is a non-wake-up sensor, meaning
     41     events must be stored while waiting for the SoC to wake up. </li>
     42 </ul>
     43 <p>See the paragraph on the <a
     44   href="hal-interface.html#batch_sensor_flags_sampling_period_maximum_report_latency">HAL
     45   batch function</a> for more details.</p>
     46 <p>The opposite of batching is the continuous operation, where events are not
     47   buffered, meaning they are reported immediately. Continuous operation
     48   corresponds to:</p>
     49 <ul>
     50   <li> when <code>max_report_latency = 0</code> and the events can be delivered to the application,
     51     meaning
     52     <ul>
     53       <li> the SoC is awake </li>
     54       <li> or the sensor is a wake-up sensor </li>
     55     </ul>
     56   </li>
     57   <li> or when the sensor doesnt have a hardware FIFO (<code>sensor_t.fifoMaxEventCount =
     58     0</code>), in which case
     59     <ul>
     60       <li> the events are reported if the SoC is awake or the sensor is a wake-up sensor </li>
     61       <li> the events are lost when the SoC is asleep and the sensor is not a wake-up
     62         sensor </li>
     63     </ul>
     64   </li>
     65 </ul>
     66 <h2 id="wake-up_fifos_and_non-wake-up_fifos">Wake-up FIFOs and non-wake-up FIFOs</h2>
     67 <p>Sensor events from <a href="suspend-mode.html#wake-up_sensors">wake-up
     68   sensors</a> must be stored into a wake-up FIFO. There can be one wake-up FIFO
     69   per sensor, or, more commonly, one big shared wake-up FIFO where events from all wake-up
     70   sensors are interleaved. Other options are also possible, with for example some
     71   wake-up sensors having a dedicated FIFO, and the rest of the wake-up sensors
     72   all sharing the same one.</p>
     73 <p>Similarly, sensor events from <a
     74   href="suspend-mode.html#non-wake-up_sensors">non-wake-up sensors</a> must be
     75   stored into a non-wake-up FIFOs, and there can be one or several
     76   non-wake-up FIFOs.</p>
     77 <p>In all cases, wake-up sensor events and non-wake-up sensor events cannot be
     78   interleaved into the same FIFO. Wake-up events go in wake-up FIFOs, and
     79   non-wake-up events go in non-wake-up FIFOs.</p>
     80 <p>For the wake-up FIFO, the one big shared FIFO design provides the best power
     81   benefits. For the non-wake-up FIFO, there is no preference between the one big
     82   shared FIFO and several small reserved FIFOs. See <a
     83   href="#fifo_allocation_priority">FIFO allocation priority</a> for suggestions
     84   on how to dimension each FIFO.</p>
     85 <h2 id="behavior_outside_of_suspend_mode">Behavior outside of suspend mode</h2>
     86 <p>When the SoC is awake (not in suspend mode), the events can be stored
     87   temporarily in their FIFO, as long as they are not delayed by more than
     88   <code>max_report_latency</code>.</p>
     89 <p>As long as the SoC doesnt enter the suspend mode, no event shall be dropped or
     90   lost. If internal hardware FIFOs is getting full before <code>max_report_latency</code>
     91   elapsed, then events are reported at that point to ensure that no event is
     92   lost.</p>
     93 <p>If several sensors share the same FIFO and the <code>max_report_latency</code> of one of
     94   them elapses, all events from the FIFO are reported, even if the
     95   <code>max_report_latency</code> of the other sensors didnt elapse yet. The general goal is
     96   to reduce the number of times batches of events must be reported, so as soon as
     97   one event must be reported, all events from all sensors can be reported.</p>
     98 <p>For example, if the following sensors are activated:</p>
     99 <ul>
    100   <li> accelerometer batched with <code>max_report_latency</code> = 20s </li>
    101   <li> gyroscope batched with <code>max_report_latency</code> = 5s </li>
    102 </ul>
    103 <p>Then the accelerometer batches can be reported at the same time the gyroscope
    104   batches are reported (every 5 seconds), even if the accelerometer and the
    105   gyroscope do not share the same FIFO.</p>
    106 <h2 id="behavior_in_suspend_mode">Behavior in suspend mode</h2>
    107 <p>Batching is particularly beneficial when wanting to collect sensor data in the
    108   background without keeping the SoC awake. Because the sensor drivers and HAL
    109   implementation are not allowed to hold a wake-lock*, the SoC can enter the
    110   suspend mode even while sensor data is being collected.</p>
    111 <p>The behavior of sensors while the SoC is suspended depends on whether the
    112   sensor is a wake-up sensor. See <a
    113 href="suspend-mode.html#wake-up_sensors">Wake-up sensors</a> for some
    114 details.</p>
    115 <p>When a non-wake-up FIFO fills up, it must wrap around and behave like a
    116   circular buffer, overwriting older events: the new events replace the old ones.
    117   <code>max_report_latency</code> has no impact on non-wake-up FIFOs while in suspend mode.</p>
    118 <p>When a wake-up FIFO fills up, or when the <code>max_report_latency</code> of one of the
    119   wake-up sensor elapsed, the hardware must wake up the SoC and report the data.</p>
    120 <p>In both cases (wake-up and non-wake-up), as soon as the SoC comes out of
    121   suspend mode, a batch is produced with the content of all FIFOs, even if
    122   <code>max_report_latency</code> of some sensors didnt elapse yet. This minimizes the risk
    123   of having to wake-up the SoC again soon if it goes back to suspend. Hence, it
    124   minimizes power consumption.</p>
    125 <p>*One notable exception of drivers not being allowed to hold a wake lock is when
    126   a wake-up sensor with <a href="report-modes.html#continuous">continuous
    127   reporting mode</a> is activated with <code>max_report_latency</code> &lt; 1
    128   second. In that case, the driver can hold a wake lock because the SoC would
    129   anyway not have the time to enter the suspend mode, as it would be awoken by
    130   a wake-up event before reaching the suspend mode.</p>
    131 <h2 id="precautions_to_take_when_batching_wake-up_sensors">Precautions to take when batching wake-up sensors</h2>
    132 <p>Depending on the device, it might take a few milliseconds for the SoC to
    133   entirely come out of suspend and start flushing the FIFO. Enough head room must
    134   be allocated in the FIFO to allow the device to entirely come out of suspend
    135   without the wake-up FIFO overflowing. No events shall be lost, and the
    136   <code>max_report_latency</code> must be respected.</p>
    137 <h2 id="precautions_to_take_when_batching_non-wake-up_on-change_sensors">Precautions to take when batching non-wake-up on-change sensors</h2>
    138 <p>On-change sensors only generate events when the value they are measuring is
    139   changing. If the measured value changes while the SoC is in suspend mode,
    140   applications expect to receive an event as soon as the SoC wakes up. Because of
    141   this, batching of <a href="suspend-mode.html#non-wake-up_sensors">non-wake-up</a> on-change sensor events must be performed carefully if the sensor shares its
    142   FIFO with other sensors. The last event generated by each on-change sensor must
    143   always be saved outside of the shared FIFO so it can never be overwritten by
    144   other events. When the SoC wakes up, after all events from the FIFO have been
    145   reported, the last on-change sensor event must be reported.</p>
    146 <p>Here is a situation we want to avoid:</p>
    147 <ol>
    148   <li> An application registers to the non-wake-up step counter (on-change) and the
    149     non-wake-up accelerometer (continuous), both sharing the same FIFO </li>
    150   <li> The application receives a step counter event step_count=1000 steps </li>
    151   <li> The SoC goes to suspend </li>
    152   <li> The user walks 20 steps, causing step counter and accelerometer events to be
    153     interleaved, the last step counter event being step_count = 1020 steps </li>
    154   <li> The user doesnt move for a long time, causing accelerometer events to continue
    155     accumulating in the FIFO, eventually overwriting every step_count event in the
    156     shared FIFO </li>
    157   <li> SoC wakes up and all events from the FIFO are sent to the application </li>
    158   <li> The application receives only accelerometer events and thinks that the user
    159     didnt walk (bad!) </li>
    160 </ol>
    161 <p>By saving the last step counter event outside of the FIFO, the HAL can report
    162   this event when the SoC wakes up, even if all other step counter events were
    163   overwritten by accelerometer events. This way, the application receives
    164   step_count = 1020 steps when the SoC wakes up.</p>
    165 <h2 id="implementing_batching">Implementing batching</h2>
    166 <p>Batching cannot be emulated in software. It must be implemented entirely in
    167   hardware, with hardware FIFOs. In particular, it cannot be implemented on the
    168   SoC, for example in the HAL implementation, as this would be
    169   counter-productive. The goal here is to save significant amounts of power.
    170   Batching must be implemented without the aid of the SoC, which should be
    171   allowed to be in suspend mode during batching.</p>
    172 <p><code>max_report_latency</code> can be modified at any time, in particular while the
    173   specified sensor is already enabled; and this shall not result in the loss of
    174   events.</p>
    175 <h2 id="fifo_allocation_priority">FIFO allocation priority</h2>
    176 <p>On platforms in which hardware FIFO size is limited, the system designers may
    177   have to choose how much FIFO to reserve for each sensor. To help with this
    178   choice, here is a list of applications made possible when batching is
    179   implemented on the different sensors.</p>
    180 <h3 id="high_value_low_power_pedestrian_dead_reckoning">High value: Low power pedestrian dead reckoning</h3>
    181 <p>Target batching time: 1 to 10 minutes</p>
    182 <p>Sensors to batch:</p>
    183 <ul>
    184   <li> Wake-up Step detector </li>
    185   <li> Wake-up Game rotation vector at 5Hz </li>
    186   <li> Wake-up Barometer at 5Hz </li>
    187   <li> Wake-up Uncalibrated Magnetometer at 5Hz </li>
    188 </ul>
    189 <p>Batching this data allows performing pedestrian dead reckoning while letting
    190   the SoC go to suspend.</p>
    191 <h3 id="high_value_medium_power_intermittent_activity_gesture_recognition">High value: Medium power intermittent activity/gesture recognition</h3>
    192 <p>Target batching time: 3 seconds</p>
    193 <p>Sensors to batch: Non-wake-up Accelerometer at 50Hz</p>
    194 <p>Batching this data allows periodically recognizing arbitrary activities and
    195   gestures without having to keep the SoC awake while the data is collected.</p>
    196 <h3 id="medium_value_medium_power_continuous_activity_gesture_recognition">Medium value: Medium power continuous activity/gesture recognition</h3>
    197 <p>Target batching time: 1 to 3 minutes</p>
    198 <p>Sensors to batch: Wake-up Accelerometer at 50Hz</p>
    199 <p>Batching this data allows continuously recognizing arbitrary activities and
    200   gestures without having to keep the SoC awake while the data is collected.</p>
    201 <h3 id="medium-high_value_interrupt_load_reduction">Medium-high value: Interrupt load reduction</h3>
    202 <p>Target batching time: &lt; 1 second</p>
    203 <p>Sensors to batch: any high frequency sensor, usually non-wake-up.</p>
    204 <p>If the gyroscope is set at 240Hz, even batching just 10 gyro events can reduce
    205   the number of interrupts from 240/second to 24/second.</p>
    206 <h3 id="medium_value_continuous_low_frequency_data_collection">Medium value: Continuous low frequency data collection</h3>
    207 <p>Target batching time: 1 to 10 minutes</p>
    208 <p>Sensors to batch:</p>
    209 <ul>
    210   <li> Wake-up barometer at 1Hz, </li>
    211   <li> Wake-up humidity sensor at 1Hz </li>
    212   <li> Other low frequency wake-up sensors at similar rates </li>
    213 </ul>
    214 <p>Allows creating monitoring applications at low power.</p>
    215 <h3 id="medium-low_value_continuous_full-sensors_collection">Medium-low value: Continuous full-sensors collection</h3>
    216 <p>Target batching time: 1 to 10 minutes</p>
    217 <p>Sensors to batch: all wake-up sensors, at high frequencies</p>
    218 <p>Allows full collection of sensor data while leaving the SoC in suspend mode.
    219   Only to consider if FIFO space is not an issue.</p>
    220