File: workload.cpp

package info (click to toggle)
xenium 0.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,152 kB
  • sloc: cpp: 12,299; makefile: 20
file content (57 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "workload.hpp"

#include <cstdint>

using config_t = tao::config::value;

namespace {

struct dummy_workload_simulator : workload_simulator{
  dummy_workload_simulator(std::uint32_t iterations) :
    _iterations(iterations)
  {}
  dummy_workload_simulator(const config_t& config) :
    _iterations(config.as<std::uint32_t>("iterations"))
  {}

  void simulate() override {
    // use volatile here to prevent the compiler from eliminating the empty loop
    for (volatile std::uint32_t i = 0; i < _iterations; ++i) {
      if (i % 64) {
        // TODO - pause?
      }
    }
  }
private:
  std::uint32_t _iterations;
};

template <class T>
workload_factory::builder make_builder() {
  return [](const config_t& config) -> std::shared_ptr<workload_simulator> {
    return std::make_shared<T>(config);
  };
}

}

workload_factory::workload_factory() {
  register_workload("dummy", make_builder<dummy_workload_simulator>());
}

void workload_factory::register_workload(std::string type, builder func) {
  _builders.emplace(std::move(type), std::move(func));
}

std::shared_ptr<workload_simulator> workload_factory::operator()(const config_t& config) {
  if (config.is_integer()) {
    auto iterations = config.get_unsigned();
    return std::make_shared<dummy_workload_simulator>(iterations);
  }

  auto type = config.as<std::string>("type");
  auto it = _builders.find(type);
  if (it == _builders.end())
    throw std::runtime_error("Invalid workload type " + type);
  return it->second(config);
}