Resource.cpp 4.5 KB
Newer Older
X
xj.lin 已提交
1 2 3 4 5
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
6
#include <iostream>
Y
Yu Kun 已提交
7
#include "../Utils.h"
X
xj.lin 已提交
8 9 10 11 12 13 14
#include "Resource.h"


namespace zilliz {
namespace milvus {
namespace engine {

15 16 17 18 19
std::ostream &operator<<(std::ostream &out, const Resource &resource) {
    out << resource.Dump();
    return out;
}

20 21
Resource::Resource(std::string name,
                   ResourceType type,
22
                   uint64_t device_id,
23 24
                   bool enable_loader,
                   bool enable_executor)
X
xj.lin 已提交
25 26
    : name_(std::move(name)),
      type_(type),
27
      device_id_(device_id),
X
xj.lin 已提交
28
      running_(false),
29 30
      enable_loader_(enable_loader),
      enable_executor_(enable_executor),
X
xj.lin 已提交
31 32
      load_flag_(false),
      exec_flag_(false) {
W
wxyu 已提交
33 34 35 36 37 38
    task_table_.RegisterSubscriber([&] {
        if (subscriber_) {
            auto event = std::make_shared<TaskTableUpdatedEvent>(shared_from_this());
            subscriber_(std::static_pointer_cast<Event>(event));
        }
    });
X
xj.lin 已提交
39 40 41
}

void Resource::Start() {
W
wxyu 已提交
42
    running_ = true;
43 44 45 46 47 48
    if (enable_loader_) {
        loader_thread_ = std::thread(&Resource::loader_function, this);
    }
    if (enable_executor_) {
        executor_thread_ = std::thread(&Resource::executor_function, this);
    }
X
xj.lin 已提交
49 50 51 52
}

void Resource::Stop() {
    running_ = false;
53 54 55 56 57 58 59 60
    if (enable_loader_) {
        WakeupLoader();
        loader_thread_.join();
    }
    if (enable_executor_) {
        WakeupExecutor();
        executor_thread_.join();
    }
X
xj.lin 已提交
61 62 63 64 65 66 67
}

TaskTable &Resource::task_table() {
    return task_table_;
}

void Resource::WakeupLoader() {
68 69 70 71
    {
        std::lock_guard<std::mutex> lock(load_mutex_);
        load_flag_ = true;
    }
X
xj.lin 已提交
72 73 74
    load_cv_.notify_one();
}

W
wxyu 已提交
75
void Resource::WakeupExecutor() {
76 77 78 79
    {
        std::lock_guard<std::mutex> lock(exec_mutex_);
        exec_flag_ = true;
    }
W
wxyu 已提交
80 81 82
    exec_cv_.notify_one();
}

83
TaskTableItemPtr Resource::pick_task_load() {
84
    auto indexes = PickToLoad(task_table_, 10);
X
xj.lin 已提交
85 86 87
    for (auto index : indexes) {
        // try to set one task loading, then return
        if (task_table_.Load(index))
88
            return task_table_.Get(index);
X
xj.lin 已提交
89 90 91 92 93
        // else try next
    }
    return nullptr;
}

94
TaskTableItemPtr Resource::pick_task_execute() {
X
xj.lin 已提交
95 96 97 98
    auto indexes = PickToExecute(task_table_, 3);
    for (auto index : indexes) {
        // try to set one task executing, then return
        if (task_table_.Execute(index))
99
            return task_table_.Get(index);
X
xj.lin 已提交
100 101 102 103 104 105 106 107 108
        // else try next
    }
    return nullptr;
}

void Resource::loader_function() {
    while (running_) {
        std::unique_lock<std::mutex> lock(load_mutex_);
        load_cv_.wait(lock, [&] { return load_flag_; });
W
wxyu 已提交
109
        load_flag_ = false;
110
        lock.unlock();
111 112 113 114 115
        while (true) {
            auto task_item = pick_task_load();
            if (task_item == nullptr) {
                break;
            }
116
            LoadFile(task_item->task);
117
            task_item->Loaded();
W
wxyu 已提交
118
            if (subscriber_) {
119
                auto event = std::make_shared<LoadCompletedEvent>(shared_from_this(), task_item);
120
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
121
            }
X
xj.lin 已提交
122
        }
123

X
xj.lin 已提交
124 125 126 127
    }
}

void Resource::executor_function() {
W
wxyu 已提交
128
    if (subscriber_) {
129 130
        auto event = std::make_shared<StartUpEvent>(shared_from_this());
        subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
131
    }
X
xj.lin 已提交
132 133 134
    while (running_) {
        std::unique_lock<std::mutex> lock(exec_mutex_);
        exec_cv_.wait(lock, [&] { return exec_flag_; });
W
wxyu 已提交
135
        exec_flag_ = false;
136
        lock.unlock();
137 138 139 140 141
        while (true) {
            auto task_item = pick_task_execute();
            if (task_item == nullptr) {
                break;
            }
Y
Yu Kun 已提交
142 143

            auto start = get_now_timestamp();
144
            Process(task_item->task);
Y
Yu Kun 已提交
145 146 147 148
            auto finish = get_now_timestamp();
            ++total_task_;
            total_cost_ += finish - start;

149
            task_item->Executed();
W
wxyu 已提交
150
            if (subscriber_) {
151 152
                auto event = std::make_shared<FinishTaskEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
153
            }
X
xj.lin 已提交
154
        }
155

X
xj.lin 已提交
156 157 158 159 160 161 162 163 164 165 166
    }
}

RegisterHandlerPtr Resource::GetRegisterFunc(const RegisterType &type) {
    // construct object each time.
    return register_table_[type]();
}

}
}
}