common.h 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Kenton Varda's avatar
Kenton Varda committed
24 25
#ifndef CAPNP_BENCHMARK_COMMON_H_
#define CAPNP_BENCHMARK_COMMON_H_
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

#include <unistd.h>
#include <limits>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <semaphore.h>
#include <algorithm>
#include <stdexcept>
#include <stdio.h>
#include <string.h>

41
namespace capnp {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
namespace benchmark {

static inline uint32_t nextFastRand() {
  static constexpr uint32_t A = 1664525;
  static constexpr uint32_t C = 1013904223;
  static uint32_t state = C;
  state = A * state + C;
  return state;
}

static inline uint32_t fastRand(uint32_t range) {
  return nextFastRand() % range;
}

static inline double fastRandDouble(double range) {
  return nextFastRand() * range / std::numeric_limits<uint32_t>::max();
}

inline int32_t div(int32_t a, int32_t b) {
  if (b == 0) return std::numeric_limits<int32_t>::max();
  // INT_MIN / -1 => SIGFPE.  Who knew?
  if (a == std::numeric_limits<int32_t>::min() && b == -1) {
    return std::numeric_limits<int32_t>::max();
  }
  return a / b;
}

inline int32_t mod(int32_t a, int32_t b) {
  if (b == 0) return std::numeric_limits<int32_t>::max();
  // INT_MIN % -1 => SIGFPE.  Who knew?
  if (a == std::numeric_limits<int32_t>::min() && b == -1) {
    return std::numeric_limits<int32_t>::max();
  }
  return a % b;
}

static const char* const WORDS[] = {
    "foo ", "bar ", "baz ", "qux ", "quux ", "corge ", "grault ", "garply ", "waldo ", "fred ",
    "plugh ", "xyzzy ", "thud "
};
constexpr size_t WORDS_COUNT = sizeof(WORDS) / sizeof(WORDS[0]);

template <typename T>
class ProducerConsumerQueue {
public:
  ProducerConsumerQueue() {
    front = new Node;
    back = front;
    sem_init(&semaphore, 0, 0);
  }

93
  ~ProducerConsumerQueue() noexcept(false) {
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 123 124 125 126 127 128 129
    while (front != nullptr) {
      Node* oldFront = front;
      front = front->next;
      delete oldFront;
    }
    sem_destroy(&semaphore);
  }

  void post(T t) {
    back->next = new Node(t);
    back = back->next;
    sem_post(&semaphore);
  }

  T next() {
    sem_wait(&semaphore);
    Node* oldFront = front;
    front = front->next;
    delete oldFront;
    return front->value;
  }

private:
  struct Node {
    T value;
    Node* next;

    Node(): next(nullptr) {}
    Node(T value): value(value), next(nullptr) {}
  };

  Node* front;  // Last node that has been consumed.
  Node* back;   // Last node in list.
  sem_t semaphore;
};

130
// TODO(cleanup):  Use SYSCALL(), get rid of this exception class.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
class OsException: public std::exception {
public:
  OsException(int error): error(error) {}
  ~OsException() noexcept {}

  const char* what() const noexcept override {
    return strerror(error);
  }

private:
  int error;
};

static void writeAll(int fd, const void* buffer, size_t size) {
  const char* pos = reinterpret_cast<const char*>(buffer);
  while (size > 0) {
    ssize_t n = write(fd, pos, size);
    if (n <= 0) {
      throw OsException(errno);
    }
    pos += n;
    size -= n;
  }
}

static void readAll(int fd, void* buffer, size_t size) {
  char* pos = reinterpret_cast<char*>(buffer);
  while (size > 0) {
    ssize_t n = read(fd, pos, size);
    if (n <= 0) {
      throw OsException(errno);
    }
    pos += n;
    size -= n;
  }
}

template <typename BenchmarkMethods, typename Func>
uint64_t passByPipe(Func&& clientFunc, uint64_t iters) {
  int clientToServer[2];
  int serverToClient[2];
  if (pipe(clientToServer) < 0) throw OsException(errno);
  if (pipe(serverToClient) < 0) throw OsException(errno);

  pid_t child = fork();
  if (child == 0) {
    // Client.
    close(clientToServer[0]);
    close(serverToClient[1]);

    uint64_t throughput = clientFunc(serverToClient[0], clientToServer[1], iters);
    writeAll(clientToServer[1], &throughput, sizeof(throughput));

    exit(0);
  } else {
    // Server.
    close(clientToServer[1]);
    close(serverToClient[0]);

    uint64_t throughput = BenchmarkMethods::server(clientToServer[0], serverToClient[1], iters);

    uint64_t clientThroughput = 0;
    readAll(clientToServer[0], &clientThroughput, sizeof(clientThroughput));
    throughput += clientThroughput;

    int status;
    if (waitpid(child, &status, 0) != child) {
      throw OsException(errno);
    }
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
      throw std::logic_error("Child exited abnormally.");
    }

    return throughput;
  }
}

template <typename BenchmarkTypes, typename TestCase, typename Reuse, typename Compression>
uint64_t doBenchmark(const std::string& mode, uint64_t iters) {
  typedef typename BenchmarkTypes::template BenchmarkMethods<TestCase, Reuse, Compression>
      BenchmarkMethods;
  if (mode == "client") {
    return BenchmarkMethods::syncClient(STDIN_FILENO, STDOUT_FILENO, iters);
  } else if (mode == "server") {
    return BenchmarkMethods::server(STDIN_FILENO, STDOUT_FILENO, iters);
  } else if (mode == "object") {
    return BenchmarkMethods::passByObject(iters, false);
  } else if (mode == "object-size") {
    return BenchmarkMethods::passByObject(iters, true);
  } else if (mode == "bytes") {
    return BenchmarkMethods::passByBytes(iters);
  } else if (mode == "pipe") {
    return passByPipe<BenchmarkMethods>(BenchmarkMethods::syncClient, iters);
  } else if (mode == "pipe-async") {
    return passByPipe<BenchmarkMethods>(BenchmarkMethods::asyncClient, iters);
  } else {
    fprintf(stderr, "Unknown mode: %s\n", mode.c_str());
    exit(1);
  }
}

template <typename BenchmarkTypes, typename TestCase, typename Compression>
uint64_t doBenchmark2(const std::string& mode, const std::string& reuse, uint64_t iters) {
  if (reuse == "reuse") {
    return doBenchmark<
        BenchmarkTypes, TestCase, typename BenchmarkTypes::ReusableResources, Compression>(
            mode, iters);
  } else if (reuse == "no-reuse") {
    return doBenchmark<
        BenchmarkTypes, TestCase, typename BenchmarkTypes::SingleUseResources, Compression>(
            mode, iters);
  } else {
    fprintf(stderr, "Unknown reuse mode: %s\n", reuse.c_str());
    exit(1);
  }
}

template <typename BenchmarkTypes, typename TestCase>
uint64_t doBenchmark3(const std::string& mode, const std::string& reuse,
                      const std::string& compression, uint64_t iters) {
  if (compression == "none") {
    return doBenchmark2<BenchmarkTypes, TestCase, typename BenchmarkTypes::Uncompressed>(
        mode, reuse, iters);
254 255 256
  } else if (compression == "packed") {
    return doBenchmark2<BenchmarkTypes, TestCase, typename BenchmarkTypes::Packed>(
        mode, reuse, iters);
257
#if HAVE_SNAPPY
258 259 260
  } else if (compression == "snappy") {
    return doBenchmark2<BenchmarkTypes, TestCase, typename BenchmarkTypes::SnappyCompressed>(
        mode, reuse, iters);
261
#endif  // HAVE_SNAPPY
262 263 264 265 266 267
  } else {
    fprintf(stderr, "Unknown compression mode: %s\n", compression.c_str());
    exit(1);
  }
}

268
template <typename BenchmarkTypes, typename TestCase>
269
int benchmarkMain(int argc, char* argv[]) {
270 271
  if (argc != 5) {
    fprintf(stderr, "USAGE:  %s MODE REUSE COMPRESSION ITERATION_COUNT\n", argv[0]);
272 273 274
    return 1;
  }

275 276 277
  uint64_t iters = strtoull(argv[4], nullptr, 0);
  uint64_t throughput = doBenchmark3<BenchmarkTypes, TestCase>(argv[1], argv[2], argv[3], iters);
  fprintf(stdout, "%llu\n", (long long unsigned int)throughput);
278 279 280 281

  return 0;
}

282
}  // namespace capnp
283 284
}  // namespace benchmark

Kenton Varda's avatar
Kenton Varda committed
285
#endif  // CAPNP_BENCHMARK_COMMON_H_