main.cpp 5.52 KB
Newer Older
1 2 3 4 5
#include <cstdio>
#include <csetjmp>
#include <cstring>
#include <chrono>

6 7 8 9
#include "fcontext/fcontext.h"

#include "fiber_call.h"

10 11 12
using namespace std;

// Settings for stack and benchmark
13 14 15
const size_t NUM_RUNS = 1000000;
const size_t STACK_SIZE = 512 * 1;
const char MAGIC_NUMBER = (unsigned char) 0xAB;
16 17

// Memory for custom stack and continuation semantics
18
char custom_stack[STACK_SIZE] = {0};
19 20 21 22 23 24 25 26 27 28 29 30
jmp_buf buffer;

// Example callback function and declaration of our assembly stack switching routine
extern "C" {
void custom_stack_callback(void *);

void __attribute__ ((noinline)) callback() {
  static volatile int tmp;
  tmp = 0; // Force at least a single memory write
}
}

31
long measure_function_call() {
32 33 34 35 36 37 38 39
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
    callback();
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

40
long measure_stack_switch() {
41 42
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
43
    custom_stack_callback(&custom_stack[STACK_SIZE]);
44 45 46 47 48
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

49
long measure_continuation() {
50 51 52
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
    if (setjmp(buffer) == 0) {
53
      custom_stack_callback(&custom_stack[STACK_SIZE]);
54 55 56 57 58 59
    }
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

60
long measure_continuation_and_jump() {
61 62 63
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
    if (setjmp(buffer) == 0) {
64
      custom_stack_callback(&custom_stack[STACK_SIZE]);
65 66 67 68 69 70 71
      longjmp(buffer, 1);
    }
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
void fcontext_callback_fast(fcontext_transfer_t transfer) {
  for (;;) {
    callback();
    jump_fcontext(transfer.ctx, nullptr);
  }
}

long measure_fcontext_fast() {
  fcontext_t context = make_fcontext(&custom_stack[STACK_SIZE], STACK_SIZE, &fcontext_callback_fast);

  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
    context = jump_fcontext(context, nullptr).ctx;
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

void fcontext_callback_clean(fcontext_transfer_t transfer) {
  callback();
  jump_fcontext(transfer.ctx, nullptr);
}

long measure_fcontext_clean() {
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
    fcontext_t context = make_fcontext(&custom_stack[STACK_SIZE], STACK_SIZE, &fcontext_callback_clean);
    jump_fcontext(context, nullptr);
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

void fcontext_callcc(fcontext_transfer_t transfer) {
  callback();
  jump_fcontext(jump_fcontext(transfer.ctx, nullptr).ctx, nullptr);
}

long measure_fcontext_callcc() {
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
    fcontext_t context = make_fcontext(&custom_stack[STACK_SIZE], STACK_SIZE, &fcontext_callcc);
    jump_fcontext(jump_fcontext(context, nullptr).ctx, nullptr);
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

long measure_custom() {
  using namespace pls::internal::base;

123 124
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
125 126 127 128
    fiber_call(custom_stack, STACK_SIZE, [](continuation_t continuation) {
      callback();
      return continuation;
    });
129 130 131 132 133
  }
  auto end_time = chrono::steady_clock::now();
  return chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count();
}

134 135 136 137 138 139 140
int main() {
  memset(custom_stack, MAGIC_NUMBER, STACK_SIZE);

  auto time_cont_jump = measure_continuation_and_jump();
  auto time_cont = measure_continuation();
  auto time_stack = measure_stack_switch();
  auto time_func = measure_function_call();
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  auto time_fcontext_fast = measure_fcontext_fast();
  auto time_fcontext_clean = measure_fcontext_clean();
  auto time_fcontext_calcc = measure_fcontext_callcc();
  auto time_custom = measure_custom();

  printf("Base\n");
  printf("Function Call    : %10ld, %5.5f\n", time_func, ((float) time_func / NUM_RUNS));
  printf("Longjmp\n");
  printf("Stack Switching  : %10ld, %5.5f\n", time_stack, ((float) time_stack / NUM_RUNS));
  printf("Full Continuation: %10ld, %5.5f\n", time_cont, ((float) time_cont / NUM_RUNS));
  printf("Jump Continuation: %10ld, %5.5f\n", time_cont_jump, ((float) time_cont_jump / NUM_RUNS));
  printf("Boost\n");
  printf("FContext Fast    : %10ld, %5.5f\n", time_fcontext_fast, ((float) time_fcontext_fast / NUM_RUNS));
  printf("FContext Clean   : %10ld, %5.5f\n", time_fcontext_clean, ((float) time_fcontext_clean / NUM_RUNS));
  printf("FContext CallCC  : %10ld, %5.5f\n", time_fcontext_calcc, ((float) time_fcontext_calcc / NUM_RUNS));
  printf("Custom\n");
  printf("Custom Fast Call : %10ld, %5.5f\n", time_custom, ((float) time_custom / NUM_RUNS));
158 159 160

  for (unsigned int i = 0; i < STACK_SIZE; i++) {
    if (custom_stack[i] != MAGIC_NUMBER) {
161
      printf("\n\nUsed stack size about %u bytes.\n", (STACK_SIZE - i));
162 163 164 165 166 167
      break;
    }
  }

  return 0;
}