提交 a591e965 编写于 作者: W wxyu

MS-373 Add resource test


Former-commit-id: 64c86f4852c4d0cb6d31a679e63ad7555e03d088
上级 4cb3e103
......@@ -24,6 +24,7 @@ Please mark all change in change log and use the ticket from JIRA.
- MS-366 - Implement TaskTable
- MS-368 - Implement cost.cpp
- MS-371 - Add TaskTableUpdatedEvent
- MS-373 - Add resource test
## New Feature
- MS-343 - Implement ResourceMgr
......
......@@ -18,7 +18,7 @@ void
TaskTable::Put(TaskPtr task) {
auto item = std::make_shared<TaskTableItem>();
item->task = std::move(task);
item->state = TaskTableItemState::LOADED;
item->state = TaskTableItemState::START;
table_.push_back(item);
if (subscriber_) {
subscriber_();
......@@ -30,7 +30,7 @@ TaskTable::Put(std::vector<TaskPtr> &tasks) {
for (auto &task : tasks) {
auto item = std::make_shared<TaskTableItem>();
item->task = std::move(task);
item->state = TaskTableItemState::LOADED;
item->state = TaskTableItemState::START;
table_.push_back(item);
}
if (subscriber_) {
......@@ -59,8 +59,8 @@ TaskTable::Move(uint64_t index) {
auto &task = table_[index];
std::lock_guard<std::mutex> lock(task->mutex);
if (task->state == TaskTableItemState::START) {
task->state = TaskTableItemState::LOADING;
if (task->state == TaskTableItemState::LOADED) {
task->state = TaskTableItemState::MOVING;
return true;
}
return false;
......
......@@ -16,6 +16,7 @@ CpuResource::CpuResource(std::string name)
: Resource(std::move(name), ResourceType::CPU) {}
void CpuResource::LoadFile(TaskPtr task) {
task->Load(LoadType::DISK2CPU, 0);
//if (src.type == DISK) {
// fd = open(filename);
// content = fd.read();
......@@ -30,7 +31,7 @@ void CpuResource::LoadFile(TaskPtr task) {
}
void CpuResource::Process(TaskPtr task) {
task->Execute();
}
}
......
......@@ -16,11 +16,11 @@ GpuResource::GpuResource(std::string name)
: Resource(std::move(name), ResourceType::GPU) {}
void GpuResource::LoadFile(TaskPtr task) {
task->Load(LoadType::CPU2GPU, 0);
}
void GpuResource::Process(TaskPtr task) {
task->Execute();
}
}
......
......@@ -25,6 +25,7 @@ Resource::Resource(std::string name, ResourceType type)
}
void Resource::Start() {
running_ = true;
loader_thread_ = std::thread(&Resource::loader_function, this);
executor_thread_ = std::thread(&Resource::executor_function, this);
}
......@@ -33,20 +34,26 @@ void Resource::Stop() {
running_ = false;
WakeupLoader();
WakeupExecutor();
loader_thread_.join();
executor_thread_.join();
}
TaskTable &Resource::task_table() {
return task_table_;
}
void Resource::WakeupExecutor() {
exec_cv_.notify_one();
}
void Resource::WakeupLoader() {
std::lock_guard<std::mutex> lock(load_mutex_);
load_flag_ = true;
load_cv_.notify_one();
}
void Resource::WakeupExecutor() {
std::lock_guard<std::mutex> lock(exec_mutex_);
exec_flag_ = true;
exec_cv_.notify_one();
}
TaskTableItemPtr Resource::pick_task_load() {
auto indexes = PickToLoad(task_table_, 3);
for (auto index : indexes) {
......@@ -73,9 +80,12 @@ void Resource::loader_function() {
while (running_) {
std::unique_lock<std::mutex> lock(load_mutex_);
load_cv_.wait(lock, [&] { return load_flag_; });
load_flag_ = false;
auto task_item = pick_task_load();
if (task_item) {
LoadFile(task_item->task);
// TODO: wrapper loaded
task_item->state = TaskTableItemState::LOADED;
if (subscriber_) {
auto event = std::make_shared<CopyCompletedEvent>(shared_from_this(), task_item);
subscriber_(std::static_pointer_cast<Event>(event));
......@@ -85,7 +95,6 @@ void Resource::loader_function() {
}
void Resource::executor_function() {
GetRegisterFunc(RegisterType::START_UP)->Exec();
if (subscriber_) {
auto event = std::make_shared<StartUpEvent>(shared_from_this());
subscriber_(std::static_pointer_cast<Event>(event));
......@@ -93,6 +102,7 @@ void Resource::executor_function() {
while (running_) {
std::unique_lock<std::mutex> lock(exec_mutex_);
exec_cv_.wait(lock, [&] { return exec_flag_; });
exec_flag_ = false;
auto task_item = pick_task_execute();
if (task_item) {
Process(task_item->task);
......
......@@ -76,16 +76,16 @@ public:
public:
/*
* wake up executor;
* wake up loader;
*/
void
WakeupExecutor();
WakeupLoader();
/*
* wake up loader;
/*
* wake up executor;
*/
void
WakeupLoader();
WakeupExecutor();
protected:
Resource(std::string name, ResourceType type);
......@@ -138,7 +138,6 @@ private:
void
executor_function();
private:
std::string name_;
ResourceType type_;
......
......@@ -9,7 +9,7 @@ class CostTest : public ::testing::Test {
protected:
void
SetUp() override {
for (uint64_t i = 0; i < 7; ++i) {
for (uint64_t i = 0; i < 8; ++i) {
auto task = std::make_shared<XSearchTask>();
table_.Put(task);
}
......
......@@ -10,6 +10,8 @@ protected:
SetUp() override {
node1_ = std::make_shared<Node>();
node2_ = std::make_shared<Node>();
node3_ = std::make_shared<Node>();
node4_ = std::make_shared<Node>();
auto pcie = Connection("PCIe", 11.0);
......
......@@ -27,15 +27,28 @@ protected:
gpu_resource_ = ResourceFactory::Create("gpu");
flag_ = false;
auto subscriber = [&](EventPtr) {
auto subscriber = [&](EventPtr event) {
std::unique_lock<std::mutex> lock(mutex_);
flag_ = true;
cv_.notify_one();
if (event->Type() == EventType::COPY_COMPLETED || event->Type() == EventType::FINISH_TASK) {
flag_ = true;
cv_.notify_one();
}
};
disk_resource_->RegisterSubscriber(subscriber);
cpu_resource_->RegisterSubscriber(subscriber);
gpu_resource_->RegisterSubscriber(subscriber);
disk_resource_->Start();
cpu_resource_->Start();
gpu_resource_->Start();
}
void
TearDown() override {
disk_resource_->Stop();
cpu_resource_->Stop();
gpu_resource_->Stop();
}
void
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册