scheduling_tests.cpp 3.61 KB
Newer Older
1 2
#include <catch.hpp>

3
#include <vector>
4
#include <atomic>
5

6 7 8
#include "pls/internal/scheduling/traded_cas_field.h"
#include "pls/internal/scheduling/task.h"
#include "pls/internal/scheduling/external_trading_deque.h"
9
#include "pls/internal/scheduling/scheduler.h"
10 11 12

using namespace pls::internal::scheduling;

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
constexpr int MAX_NUM_THREADS = 8;
constexpr int MAX_NUM_TASKS = 32;
constexpr int MAX_STACK_SIZE = 1024 * 8;

TEST_CASE("tasks distributed over workers (do not block)", "[internal/scheduling/scheduler.h]") {
  scheduler scheduler{3, MAX_NUM_TASKS, MAX_STACK_SIZE};

  std::atomic<int> num_run{0};
  scheduler.perform_work([&] {
    scheduler::spawn([&] {
      num_run++;
      while (num_run < 3);
    });
    scheduler::spawn([&] {
      while (num_run < 1);
      num_run++;
      while (num_run < 3);
    });
    scheduler::spawn([&] {
      while (num_run < 2);
      num_run++;
    });
    scheduler::sync();
  });
  REQUIRE(true);
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
TEST_CASE("traded cas field bitmaps correctly", "[internal/scheduling/traded_cas_field.h]") {
  traded_cas_field empty_field;
  REQUIRE(empty_field.is_empty());
  REQUIRE(!empty_field.is_filled_with_stamp());
  REQUIRE(!empty_field.is_filled_with_object());

  const int stamp = 42;
  const int ID = 10;
  traded_cas_field tag_field;
  tag_field.fill_with_stamp(stamp, ID);
  REQUIRE(tag_field.is_filled_with_stamp());
  REQUIRE(!tag_field.is_empty());
  REQUIRE(!tag_field.is_filled_with_object());
  REQUIRE(tag_field.get_stamp() == stamp);
  REQUIRE(tag_field.get_deque_id() == ID);

56
  alignas(64) task obj{nullptr, 0, 0, 0};
57 58 59 60 61 62 63 64
  traded_cas_field obj_field;
  obj_field.fill_with_trade_object(&obj);
  REQUIRE(obj_field.is_filled_with_object());
  REQUIRE(!obj_field.is_empty());
  REQUIRE(!obj_field.is_filled_with_stamp());
}

TEST_CASE("external trading deque", "[internal/scheduling/external_trading_deque]") {
65 66
  external_trading_deque deque_1{1, 16};
  external_trading_deque deque_2{2, 16};
67

68 69 70 71
  task tasks[4] = {{nullptr, 0, 0, 0},
                   {nullptr, 0, 1, 0},
                   {nullptr, 0, 2, 0},
                   {nullptr, 0, 3, 0}};
72 73 74 75 76 77 78 79 80 81 82 83 84 85

  SECTION("basic operations") {
    // Must start empty
    REQUIRE(!deque_1.pop_bot());
    REQUIRE(!deque_2.pop_bot());

    // Local push/pop
    deque_1.push_bot(&tasks[0]);
    REQUIRE(*deque_1.pop_bot() == &tasks[0]);
    REQUIRE(!deque_1.pop_bot());

    // Local push, external pop
    deque_1.push_bot(&tasks[0]);
    auto peek = deque_1.peek_top();
86
    REQUIRE(*deque_1.pop_top(&tasks[1], peek) == &tasks[0]);
87
    REQUIRE(*external_trading_deque::get_trade_object(&tasks[0]) == &tasks[1]);
88
    REQUIRE(!deque_1.pop_top(&tasks[1], peek));
89 90 91 92 93 94 95 96 97 98 99 100
    REQUIRE(!deque_1.pop_bot());

    // Keeps push/pop order
    deque_1.push_bot(&tasks[0]);
    deque_1.push_bot(&tasks[1]);
    REQUIRE(*deque_1.pop_bot() == &tasks[1]);
    REQUIRE(*deque_1.pop_bot() == &tasks[0]);
    REQUIRE(!deque_1.pop_bot());

    deque_1.push_bot(&tasks[0]);
    deque_1.push_bot(&tasks[1]);
    auto peek1 = deque_1.peek_top();
101
    REQUIRE(*deque_1.pop_top(&tasks[2], peek1) == &tasks[0]);
102
    auto peek2 = deque_1.peek_top();
103
    REQUIRE(*deque_1.pop_top(&tasks[3], peek2) == &tasks[1]);
104 105 106 107 108 109 110
  }

  SECTION("Interwined execution #1") {
    // Two top poppers
    deque_1.push_bot(&tasks[0]);
    auto peek1 = deque_1.peek_top();
    auto peek2 = deque_1.peek_top();
111 112
    REQUIRE(*deque_1.pop_top(&tasks[1], peek1) == &tasks[0]);
    REQUIRE(!deque_1.pop_top(&tasks[2], peek2));
113 114 115 116 117 118 119
  }

  SECTION("Interwined execution #2") {
    // Top and bottom access
    deque_1.push_bot(&tasks[0]);
    auto peek1 = deque_1.peek_top();
    REQUIRE(*deque_1.pop_bot() == &tasks[0]);
120
    REQUIRE(!deque_1.pop_top(&tasks[2], peek1));
121
  }
122
}