> For the complete documentation index, see [llms.txt](https://sliu583.gitbook.io/blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sliu583.gitbook.io/blog/specific-work/seminar-and-talk/berkeley-system-seminar/immortal-threads-multithreaded-event-driven-intermittent-computing-on-ultra-low-power-microcontroll.md).

# Immortal Threads: Multithreaded Event-driven Intermittent Computing on Ultra-Low-Power Microcontroll

### Presentation&#x20;

#### Low-power computing&#x20;

* LoWPANs&#x20;
  * Volcano monitoring, structural monitoring, smart home and IoT&#x20;
* Types of wireless network&#x20;
  * High cost, high power <-- -->Low cost, low power <-- --> ultra-low cost, ultra-low power&#x20;
  * E.g. wifi, bluetooth, LoWPAN (embedded mesh network)&#x20;
* Low-power embedded devices
  * &#x20;Hamilton sensor platform&#x20;
    * 10 dollars excluding the battery (manufacture)&#x20;
  * 32 KiB RAM, 250 kb/s IEEE 802.15.3 radio&#x20;
    * All SRAM, no DRAM&#x20;
  * RAM limited: power consumption (reason)&#x20;
  * This is an example of embedded programming&#x20;
  * No OS-supported features into it&#x20;
* Two questions&#x20;
  * How should we program such devices?&#x20;
  * How can we compose them into larger embedded networked sensor system?&#x20;
* Early thoughts (late 1990s and early 2000s)&#x20;
  * Requirements that shape the design of network sensor system&#x20;
    * Low power and small size: constrain the processing, storage, and interconnect capability&#x20;
    * Highly concurrent: primary mode of operation ... is to flow information from place to place&#x20;
    * Little physical parallelism: number of independent controllers, the capabilities of the controllers&#x20;
      * More burden put on the CPU&#x20;
    * Diverse designs&#x20;
      * Application-specific&#x20;
    * Unusually robust&#x20;
      * No human in the loop
* Concurrency model&#x20;
  * Threads v.s events debate&#x20;
    * Threads are much easier to program, but tricky to sync&#x20;
    * Events allow for faster task switching and scale to more concurrent tasks&#x20;
* Writing event-driven code is cumbersome&#x20;
  * I2C: bus
  * State machines&#x20;
  * Generate state machines underneath the hood (some of the existing techs)&#x20;
* Making event-driven programming easier&#x20;
  * TinyOS&#x20;
    * Stack-based cooperative multithreading&#x20;
  * Contiki&#x20;
    * Preemptive multithreading as a library&#x20;
* Portothreads example&#x20;
  * Minimizing the thread states, but complex code in run&#x20;
  * pt -> lc (2 bytes)&#x20;
  * Stackless: no stack, lose the variables&#x20;

#### Paper&#x20;

* Energy harvesting battery-less devices&#x20;
  * Future sensing devices are tiny, sustainable and run forever&#x20;
  * Capacitor on the device to store energy&#x20;
* A typical battery-less sensor architecture&#x20;
  * Q: FRAM (map to physical address space and it's non-volatile)&#x20;
* Program failures: intermittent execution&#x20;
  * Existing techniques&#x20;
    * Checkpoint: back up entire state
      * Costly&#x20;
      * Consistent snapshot&#x20;
    * Event-driven tasks&#x20;
      * Globals: Non-v ram (FRAM)
      * Task: atomic; non-preemptive and stackless&#x20;
      * Checkpoint easily&#x20;
* Stackful concurrency&#x20;
  * Programming explicitness&#x20;
* Problem statement&#x20;
  * Programming model that&#x20;
    * No cognitive load and lightweight as task-based model&#x20;
    * Stackful concurrency (preemption + multithreading)&#x20;
    * Minimal wasted progress&#x20;
  * Pseudo-stackful preemptive multithreading&#x20;
* Immortal threads
  * Think only event-driven aspects&#x20;
    * Identify the events
    * Threads as event handler&#x20;
    * Manage state management and transitions&#x20;
  * Compiler frontend&#x20;
    * Almost free checkpoints: save only the program counter rather than all registers and memory (just 2 bytes)&#x20;
      * Counter of the switch statements of where you were&#x20;
      * Stack is always on Non-V memory&#x20;
    * Just-in-time privatization&#x20;
      * Creates private copies of variables dynamically to keep non-V memory consistent&#x20;

Questions&#x20;

* Immortal threads
  * break up the execution into idempotent reads and writes&#x20;
  * checkpoint after each such memory operation&#x20;
* Why is this faster than prior SOTA&#x20;
* Is intermittent computing really necessary&#x20;
  * IPSN 2019: capacity over capacitance for reliable energy&#x20;
  * Concurrency is less important now&#x20;
* Depends on the idea: lose power frequently&#x20;
  * Add a second power-backup&#x20;
* Reasonable: power budget --> decrease the cost of checkpointing&#x20;
  * No dynamic power measurements to do the prediction&#x20;
* Does it support loop and if statement?&#x20;
  * Dynamic loop
    * Statically check each branch in loop body&#x20;
