2021-02-18 17:21:25 -05:00
|
|
|
|
2022-02-23 23:08:28 -05:00
|
|
|
#include "wrap-sstream.hpp"
|
|
|
|
|
|
2021-02-18 17:21:25 -05:00
|
|
|
#include "sched.hpp"
|
2021-07-30 13:57:03 -04:00
|
|
|
#include "streambuffer.hpp"
|
|
|
|
|
#include "luastack.hpp"
|
2021-02-18 17:21:25 -05:00
|
|
|
|
2022-02-25 19:57:23 -05:00
|
|
|
#include <ostream>
|
|
|
|
|
|
2021-02-18 17:21:25 -05:00
|
|
|
bool SchedEntry::operator < (const SchedEntry &other) const {
|
|
|
|
|
if (clock_ < other.clock_) return true;
|
|
|
|
|
if (clock_ > other.clock_) return false;
|
|
|
|
|
if (thread_id_ < other.thread_id_) return true;
|
|
|
|
|
if (thread_id_ > other.thread_id_) return false;
|
|
|
|
|
if (place_id_ < other.place_id_) return true;
|
|
|
|
|
if (place_id_ > other.place_id_) return false;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string SchedEntry::debug_string() const {
|
|
|
|
|
eng::ostringstream oss;
|
2021-07-30 13:57:03 -04:00
|
|
|
oss << "(" << clock_ << "," << thread_id_ << "," << place_id_ << ")";
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:21:25 -05:00
|
|
|
void Schedule::add(int64_t clk, int64_t thid, int64_t plid) {
|
2022-04-25 17:17:41 -04:00
|
|
|
assert(plid != 0);
|
|
|
|
|
assert(thid != 0);
|
2021-02-18 17:21:25 -05:00
|
|
|
schedule_.insert(SchedEntry(clk, thid, plid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Schedule::ready(int64_t clk) const {
|
|
|
|
|
return (!schedule_.empty()) && (schedule_.begin()->clock() <= clk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SchedEntry Schedule::pop() {
|
2021-07-30 13:57:03 -04:00
|
|
|
assert(!schedule_.empty());
|
2021-02-18 17:21:25 -05:00
|
|
|
SchedEntry result = *schedule_.begin();
|
|
|
|
|
schedule_.erase(schedule_.begin());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-03-16 12:19:37 -04:00
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
eng::string Schedule::debug_string() {
|
|
|
|
|
eng::ostringstream oss;
|
2021-07-30 13:57:03 -04:00
|
|
|
for (const SchedEntry &se : schedule_) {
|
|
|
|
|
oss << se.debug_string();
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 12:19:37 -04:00
|
|
|
void Schedule::serialize(StreamBuffer *sb) {
|
2021-07-19 17:32:24 -04:00
|
|
|
sb->write_uint32(schedule_.size());
|
2021-03-16 12:19:37 -04:00
|
|
|
for (const SchedEntry &entry : schedule_) {
|
|
|
|
|
sb->write_int64(entry.clock_);
|
|
|
|
|
sb->write_int64(entry.thread_id_);
|
|
|
|
|
sb->write_int64(entry.place_id_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Schedule::deserialize(StreamBuffer *sb) {
|
|
|
|
|
schedule_.clear();
|
2021-07-19 17:32:24 -04:00
|
|
|
size_t nentry = sb->read_uint32();
|
2021-03-16 12:19:37 -04:00
|
|
|
for (size_t i = 0; i < nentry; i++) {
|
|
|
|
|
int64_t clock = sb->read_int64();
|
|
|
|
|
int64_t thread_id = sb->read_int64();
|
|
|
|
|
int64_t place_id = sb->read_int64();
|
|
|
|
|
add(clock, thread_id, place_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-30 13:57:03 -04:00
|
|
|
|
2021-12-15 23:03:43 -05:00
|
|
|
LuaDefine(unittests_scheduler, "", "some unit tests") {
|
2021-07-30 13:57:03 -04:00
|
|
|
Schedule s, xs;
|
|
|
|
|
StreamBuffer sb;
|
|
|
|
|
|
|
|
|
|
// Put some stuff into a scheduler.
|
|
|
|
|
s.add(1, 5, 3);
|
|
|
|
|
s.add(4, 3, 2);
|
|
|
|
|
s.add(1, 3, 2);
|
|
|
|
|
s.add(4, 2, 3);
|
|
|
|
|
|
|
|
|
|
// Test serialize and deserialize.
|
|
|
|
|
LuaAssert(L, s.debug_string() == "(1,3,2)(1,5,3)(4,2,3)(4,3,2)");
|
|
|
|
|
s.serialize(&sb);
|
|
|
|
|
xs.deserialize(&sb);
|
|
|
|
|
LuaAssert(L, xs.debug_string() == "(1,3,2)(1,5,3)(4,2,3)(4,3,2)");
|
|
|
|
|
|
|
|
|
|
// Verify that pop, ready, and empty do the right thing.
|
|
|
|
|
LuaAssert(L, s.ready(0) == false)
|
|
|
|
|
LuaAssert(L, s.ready(1) == true)
|
|
|
|
|
LuaAssert(L, s.pop().debug_string() == "(1,3,2)");
|
|
|
|
|
LuaAssert(L, s.ready(0) == false)
|
|
|
|
|
LuaAssert(L, s.ready(1) == true)
|
|
|
|
|
LuaAssert(L, s.pop().debug_string() == "(1,5,3)");
|
|
|
|
|
LuaAssert(L, s.debug_string() == "(4,2,3)(4,3,2)");
|
|
|
|
|
LuaAssert(L, s.pop().debug_string() == "(4,2,3)");
|
|
|
|
|
LuaAssert(L, s.pop().debug_string() == "(4,3,2)");
|
|
|
|
|
LuaAssert(L, s.ready(10000) == false);
|
|
|
|
|
LuaAssert(L, s.empty());
|
|
|
|
|
LuaAssert(L, s.debug_string() == "");
|
|
|
|
|
|
|
|
|
|
// Test serialization of an empty streambuffer.
|
|
|
|
|
s.serialize(&sb);
|
|
|
|
|
xs.deserialize(&sb);
|
|
|
|
|
LuaAssert(L, xs.debug_string() == "");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|