forked from facebookarchive/treadmill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scheduler.h
88 lines (66 loc) · 2 KB
/
Scheduler.h
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
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
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include <memory>
#include <thread>
#include <folly/futures/Future.h>
#include <folly/futures/Promise.h>
#include <folly/io/async/NotificationQueue.h>
#include "treadmill/Event.h"
DECLARE_bool(wait_for_runner_ready);
namespace facebook {
namespace windtunnel {
namespace treadmill {
class Scheduler {
public:
Scheduler(uint32_t rps, uint32_t number_of_workers,
uint32_t logging_threshold);
~Scheduler();
folly::Future<folly::Unit> run();
// Transition from running to paused (no-op if not running).
void pause();
// Transition from paused to running (no-op if not paused).
void resume();
// Set the phase of the test
void setPhase(const std::string& phase_name);
// It is safe to call stop() multiple times.
void stop();
// The scheduler _must_ be stopped first.
void join();
folly::NotificationQueue<Event>& getWorkerQueue(uint32_t id);
void setRps(int32_t rps);
private:
enum RunState { RUNNING, PAUSED, STOPPING };
/**
* Draws from an exponential distribution with the given mean.
*/
static double randomExponentialInterval(double mean);
/**
* Waits until given amount of nanosecond pases, for precise timing it uses
* spin-loop.
*/
static void waitNs(int64_t ns);
/**
* Puts given message on each worker's queue.
*/
void messageAllWorkers(Event event);
void loop();
uint32_t logging_threshold_;
uint32_t next_{0};
uint32_t rps_;
std::vector<uint64_t> logged_;
std::vector<folly::NotificationQueue<Event>> queues_;
std::atomic<RunState> state_;
std::unique_ptr<std::thread> thread_;
folly::Promise<folly::Unit> promise_;
};
} // namespace treadmill
} // namespace windtunnel
} // namespace facebook