A test program for evaluating the performance implications of memory cache clearing/invalidation on the Electrosmith Daisy Seed

Home   »   A test program for evaluating the performance implications of memory cache clearing/invalidation on the Electrosmith Daisy Seed

#include "daisy_seed.h"
#include "dev/oled_ssd130x.h"
#include "sys/dma.h"
#include 

#define BUFFER_SIZE 128
#define ITERATIONS 100

#define PIN_OLED_DC 9
#define PIN_OLED_RESET 30

using namespace daisy;

DaisySeed seed;

OledDisplay display;

uint8_t cacheable_buffer[BUFFER_SIZE];

uint8_t cacheclear_buffer[BUFFER_SIZE];

uint8_t cacheinvalidate_buffer[BUFFER_SIZE];

DMA_BUFFER_MEM_SECTION uint8_t uncacheable_buffer[BUFFER_SIZE];


int main(void) {

  seed.Configure();
  seed.Init();

  OledDisplay::Config display_config;
  display_config.driver_config.transport_config.pin_config.dc =
      seed.GetPin(PIN_OLED_DC);
  display_config.driver_config.transport_config.pin_config.reset =
      seed.GetPin(PIN_OLED_RESET);
  display.Init(display_config);

  for (;;) {
    // Cacheable
    uint32_t delta_cacheable = 0;
    uint32_t start_cacheable = System::GetTick();
    for (size_t i = 0; i < ITERATIONS; i++) {
      uint8_t v = 0;
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        cacheable_buffer[k] = v;
      }
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        assert(cacheable_buffer[k] == v);
      }
      v++;
    }
    delta_cacheable = System::GetTick() - start_cacheable;

    // Uncachcheable
    uint32_t delta_uncacheable = 0;
    uint32_t start_uncacheable = System::GetTick();
    for (size_t i = 0; i < ITERATIONS; i++) {
      uint8_t v = 0;
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        uncacheable_buffer[k] = v;
      }
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        assert(uncacheable_buffer[k] == v);
      }
      v++;
    }
    delta_uncacheable = System::GetTick() - start_uncacheable;

    // Cache clear
    uint32_t delta_cacheclear = 0;
    uint32_t start_cacheclear = System::GetTick();
    for (size_t i = 0; i < ITERATIONS; i++) {
      uint8_t v = 0;
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        cacheclear_buffer[k] = v;
        // dsy_dma_clear_cache_for_buffer(cacheclear_buffer, BUFFER_SIZE);
      }
      dsy_dma_clear_cache_for_buffer(cacheclear_buffer, BUFFER_SIZE);
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        assert(cacheclear_buffer[k] == v);
      }
      v++;
    }
    delta_cacheclear = System::GetTick() - start_cacheclear;

    // Cache invalidate
    uint32_t delta_cacheinvalidate = 0;
    uint32_t start_cacheinvalidate = System::GetTick();
    for (size_t i = 0; i < ITERATIONS; i++) {
      uint8_t v = 0;
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        cacheinvalidate_buffer[k] = v;
        // dsy_dma_invalidate_cache_for_buffer(cacheinalidate_buffer,
        // BUFFER_SIZE);
      }
      dsy_dma_invalidate_cache_for_buffer(cacheinvalidate_buffer, BUFFER_SIZE);
      for (size_t k = 0; k < BUFFER_SIZE; k++) {
        assert(cacheinvalidate_buffer[k] == v);
      }
      v++;
    }
    delta_cacheinvalidate = System::GetTick() - start_cacheinvalidate;

    // Write the results out to the OLED...
    display.Fill(false);
    display.SetCursor(0, 0);
    std::string str = "Cacheable  : ";
    char *cstr = &str[0];
    display.WriteString(cstr, Font_6x8, true);

    str = std::to_string(delta_cacheable);
    display.SetCursor(80, 0);
    display.WriteString(cstr, Font_6x8, true);

    str = "Uncacheable: ";
    display.SetCursor(0, 8);
    display.WriteString(cstr, Font_6x8, true);

    str = std::to_string(delta_uncacheable);
    display.SetCursor(80, 8);
    display.WriteString(cstr, Font_6x8, true);

    str = "Cacheflush : ";
    display.SetCursor(0, 16);
    display.WriteString(cstr, Font_6x8, true);

    str = std::to_string(delta_cacheclear);
    display.SetCursor(80, 16);
    display.WriteString(cstr, Font_6x8, true);

    str = "Cacheinval : ";
    display.SetCursor(0, 24);
    display.WriteString(cstr, Font_6x8, true);

    str = std::to_string(delta_cacheinvalidate);
    display.SetCursor(80, 24);
    display.WriteString(cstr, Font_6x8, true);

    display.Update();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *