pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Profiler

This sample times two ways of sorting with profiler nodes. Each node, defined with PBL_PROFILER_NODE_DEFINE(), accumulates the CPU cycles spent between its start and stop, however many times it runs; the sample shuffles the values before each sort under a node of its own.

pbl_profiler_print_stats() logs a line per node, the busiest first, and the PBL_PROFILER_NODE_* macros read a node’s totals.

Run:

pbl samples samples/subsys/profiler/nodes

Boards:

every board

Sources

src/main.c
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <pbl/profiler/profiler.h>

#define VALUES 128U
#define ROUNDS 20U

/* Each node accumulates the cycles spent between its start and its stop. */
PBL_PROFILER_NODE_DEFINE(bubble_sort);
PBL_PROFILER_NODE_DEFINE(insertion_sort);
PBL_PROFILER_NODE_DEFINE(shuffle);

static uint16_t s_values[VALUES];

static void prv_shuffle(uint32_t seed) {
  for (unsigned int i = 0; i < VALUES; i++) {
    seed = seed * 1664525U + 1013904223U;
    s_values[i] = (uint16_t)(seed >> 16);
  }
}

static void prv_bubble_sort(void) {
  for (unsigned int i = 0; i < VALUES - 1U; i++) {
    for (unsigned int j = 0; j < VALUES - 1U - i; j++) {
      if (s_values[j] > s_values[j + 1U]) {
        uint16_t tmp = s_values[j];

        s_values[j] = s_values[j + 1U];
        s_values[j + 1U] = tmp;
      }
    }
  }
}

static void prv_insertion_sort(void) {
  for (unsigned int i = 1; i < VALUES; i++) {
    uint16_t val = s_values[i];
    unsigned int j = i;

    while (j > 0 && s_values[j - 1U] > val) {
      s_values[j] = s_values[j - 1U];
      j--;
    }
    s_values[j] = val;
  }
}

int main(void) {
  pbl_profiler_reset();
  pbl_profiler_start();

  for (unsigned int round = 0; round < ROUNDS; round++) {
    PBL_PROFILER_NODE_START(shuffle);
    prv_shuffle(round);
    PBL_PROFILER_NODE_STOP(shuffle);

    PBL_PROFILER_NODE_START(bubble_sort);
    prv_bubble_sort();
    PBL_PROFILER_NODE_STOP(bubble_sort);

    PBL_PROFILER_NODE_START(shuffle);
    prv_shuffle(round);
    PBL_PROFILER_NODE_STOP(shuffle);

    PBL_PROFILER_NODE_START(insertion_sort);
    prv_insertion_sort();
    PBL_PROFILER_NODE_STOP(insertion_sort);
  }

  /* Stops the run and logs a line per node, the busiest first. */
  pbl_profiler_print_stats();

  printf("profiler: %u sorts of %u values each way\n",
         (unsigned int)PBL_PROFILER_NODE_COUNT(bubble_sort), VALUES);
  printf("profiler: bubble sort %u us, insertion sort %u us\n",
         (unsigned int)PBL_PROFILER_NODE_TOTAL_US(bubble_sort),
         (unsigned int)PBL_PROFILER_NODE_TOTAL_US(insertion_sort));

  uint32_t bubble = PBL_PROFILER_NODE_TOTAL_CYCLES(bubble_sort);
  uint32_t insertion = PBL_PROFILER_NODE_TOTAL_CYCLES(insertion_sort);

  printf("profiler: insertion sort took fewer cycles: %s\n", insertion < bubble ? "yes" : "no");

  printf("Sample done\n");

  return 0;
}

Output

Ran for \d+ cycles \(\d+ us\) at \d+ Hz
Name +Count +Cycles +Time \(us\) +Avg \(us\) +% CPU
bubble_sort +20 
(insertion_sort +20|shuffle +40) 
(insertion_sort +20|shuffle +40) 
profiler: 20 sorts of 128 values each way
profiler: bubble sort \d+ us, insertion sort \d+ us
profiler: insertion sort took fewer cycles: yes
Sample done

Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/subsys/profiler/nodes.md.

Overview

  • Sources
  • Output