Pulse.cpp 5.01 KB
Newer Older
1
2
3
4
5
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2022 Florian Fischer
#include <boost/program_options.hpp>
#include <chrono>
#include <compare>
6
#include <cstdint>
7
#include <cstdlib>
8
#include <ctime>
9
10
11
#include <exception>
#include <fstream>	// IWYU pragma: keep
#include <iostream>
12
#include <sstream>
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>

#include "CountingPrivateSemaphore.hpp"
#include "Runtime.hpp"
#include "RuntimeBuilder.hpp"
#include "emper-common.h"
#include "emper.hpp"

namespace po = boost::program_options;

using Clock = std::chrono::high_resolution_clock;
using TimePoint = std::chrono::time_point<Clock>;

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
enum class PulseMode {
	Burst, /*!< Introduce all work as fast as possible */
	Even,	 /*!< Introduce work evenly during the pulse */
};

auto operator<<(std::ostream& out, const PulseMode& mode) -> std::ostream& {
	switch (mode) {
		case PulseMode::Burst:
			out << "burst";
			break;
		case PulseMode::Even:
			out << "even";
			break;
	}
	return out;
}

auto operator>>(std::istream& in, PulseMode& mode) -> std::istream& {
	std::string token;

	in >> token;
	if (token == "burst") {
		mode = PulseMode::Burst;
	} else if (token == "even") {
		mode = PulseMode::Even;
	} else {
		in.setstate(std::ios_base::failbit);
	}
	return in;
}

static PulseMode pulseMode;

59
60
// Pulse of new work in microseconds
static int64_t pulse = 1L * 1000 * 1000;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Number of pulses
static unsigned iterations = 30;
// Utilization of the runtime in percent
static unsigned utilization = 80;

class Work {
 public:
	static Work** doneWork;
	static thread_local Work** localDoneWork;

	TimePoint start;
	TimePoint end;
	Work* next = nullptr;

	Work() : start(Clock::now()) {}

	void run() {
78
		const auto deadline = Clock::now() + std::chrono::microseconds(pulse);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
		while (Clock::now() < deadline) {
		}

		finish();
	}

	void finish() {
		this->end = std::chrono::high_resolution_clock::now();
		this->next = *localDoneWork;
		*localDoneWork = this;
	}
};

Work** Work::doneWork;
thread_local Work** Work::localDoneWork;

static void pulser(Runtime& runtime) {
	const workerid_t workerCount = runtime.getWorkerCount();
	const workerid_t workItems = (workerCount * utilization) / 100;

99
100
101
102
103
104
105
106
107
	struct timespec ts {
		0, 0
	};
	// TODO: better calculate the time until the next burst
	if (pulseMode == PulseMode::Burst)
		ts.tv_nsec = pulse * 1000;
	else
		ts.tv_nsec = (pulse * 1000) / workItems;

108
109
110
111
112
	CPS cps;
	for (unsigned i = 0; i < iterations; ++i) {
		for (unsigned w = 0; w < workItems; ++w) {
			Work* work = new Work();
			spawn([=] { work->run(); }, cps);
113
			if (pulseMode == PulseMode::Even) emper::nanosleep(&ts);
114
115
		}

116
117
		// TODO: Think about busy looping for small times
		if (pulseMode == PulseMode::Burst) emper::nanosleep(&ts);
118
119
120
121
122
123
124
125
126
127
128
129
130
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
	}
	cps.wait();
	runtime.initiateTermination();
}

static auto eval(const po::variables_map& vm) -> int {
	std::cerr << "Starting pulse evaluation with pulse=" << pulse;
	std::cerr << ", iterations=" << iterations;
	std::cerr << " and utilization=" << utilization;
	std::cerr << std::endl;

	const workerid_t workerCount = Runtime::getDefaultWorkerCount();
	Work::doneWork = new Work*[workerCount];

	RuntimeBuilder runtimeBuilder;
	Runtime runtime = runtimeBuilder.withWorkerCount(workerCount)
												.newWorkerHook([](workerid_t id) {
													Work::localDoneWork = &Work::doneWork[id];
													*Work::localDoneWork = nullptr;
												})
												.build();

	runtime.executeAndWait([&] { pulser(runtime); });

	std::fstream fout;
	std::ostream* out = &std::cout;
	if (vm.count("latencies-file")) {
		out = &fout;
		fout.open(vm["latencies-file"].as<std::string>(), std::fstream::out);
	}

	for (unsigned i = 0; i < workerCount; ++i) {
		auto* localDoneWork = Work::doneWork[i];
		while (localDoneWork) {
			auto* work = localDoneWork;
			*out << i << ","
					 << std::chrono::duration_cast<std::chrono::nanoseconds>(work->end - work->start).count()
					 << std::endl;
			localDoneWork = work->next;
			delete work;
		}
	}
	delete[] Work::doneWork;

	if (fout.is_open()) fout.close();

	return EXIT_SUCCESS;
}

auto main(int argc, char* argv[]) -> int {
	po::options_description desc("Allowed options");
	// clang-format off
	desc.add_options()
		("help", "Show help")
172
173
174
175
176
		("pulse", po::value<int64_t>(&pulse)->default_value(pulse), "The pulse of new work")
		("iterations", po::value<unsigned>(&iterations)->default_value(iterations),
									 "The number of pulses")
		("utilization", po::value<unsigned>(&utilization)->default_value(utilization),
										"The target utilization of the runtime")
177
		("latencies-file", po::value<std::string>(), "File to store all collected latencies")
178
		("pulse-mode", po::value<PulseMode>(&pulseMode)->default_value(PulseMode::Burst), "How to introducing the work")
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
		;
	// clang-format on

	auto parse_result = po::command_line_parser(argc, argv).options(desc).run();

	po::variables_map vm;
	po::store(parse_result, vm);
	po::notify(vm);

	if (vm.count("help")) {
		std::cout << desc << "\n";
		return EXIT_SUCCESS;
	}

	try {
		return eval(vm);
	} catch (std::exception& e) {
		std::cerr << e.what() << std::endl;
		return EXIT_FAILURE;
	}
}