Resource.cpp 4.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18 19
#include "scheduler/resource/Resource.h"
#include "scheduler/Utils.h"
X
xj.lin 已提交
20

S
starlord 已提交
21 22
#include <iostream>
#include <utility>
X
xj.lin 已提交
23 24 25

namespace zilliz {
namespace milvus {
W
wxyu 已提交
26
namespace scheduler {
X
xj.lin 已提交
27

S
starlord 已提交
28 29
std::ostream&
operator<<(std::ostream& out, const Resource& resource) {
30 31 32 33
    out << resource.Dump();
    return out;
}

S
starlord 已提交
34
Resource::Resource(std::string name, ResourceType type, uint64_t device_id, bool enable_loader, bool enable_executor)
X
xj.lin 已提交
35 36
    : name_(std::move(name)),
      type_(type),
37
      device_id_(device_id),
38
      enable_loader_(enable_loader),
W
wxyu 已提交
39 40
      enable_executor_(enable_executor) {
    // register subscriber in tasktable
W
wxyu 已提交
41 42 43 44 45 46
    task_table_.RegisterSubscriber([&] {
        if (subscriber_) {
            auto event = std::make_shared<TaskTableUpdatedEvent>(shared_from_this());
            subscriber_(std::static_pointer_cast<Event>(event));
        }
    });
X
xj.lin 已提交
47 48
}

W
wxyu 已提交
49 50
void
Resource::Start() {
W
wxyu 已提交
51
    running_ = true;
52 53 54 55 56 57
    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 已提交
58 59
}

W
wxyu 已提交
60 61
void
Resource::Stop() {
X
xj.lin 已提交
62
    running_ = false;
63 64 65 66 67 68 69 70
    if (enable_loader_) {
        WakeupLoader();
        loader_thread_.join();
    }
    if (enable_executor_) {
        WakeupExecutor();
        executor_thread_.join();
    }
X
xj.lin 已提交
71 72
}

W
wxyu 已提交
73 74
void
Resource::WakeupLoader() {
75 76 77 78
    {
        std::lock_guard<std::mutex> lock(load_mutex_);
        load_flag_ = true;
    }
X
xj.lin 已提交
79 80 81
    load_cv_.notify_one();
}

W
wxyu 已提交
82 83
void
Resource::WakeupExecutor() {
84 85 86 87
    {
        std::lock_guard<std::mutex> lock(exec_mutex_);
        exec_flag_ = true;
    }
W
wxyu 已提交
88 89 90
    exec_cv_.notify_one();
}

W
wxyu 已提交
91 92 93
uint64_t
Resource::NumOfTaskToExec() {
    uint64_t count = 0;
S
starlord 已提交
94
    for (auto& task : task_table_) {
W
wxyu 已提交
95 96 97 98 99
        if (task->state == TaskTableItemState::LOADED) ++count;
    }
    return count;
}

S
starlord 已提交
100 101
TaskTableItemPtr
Resource::pick_task_load() {
102
    auto indexes = task_table_.PickToLoad(10);
X
xj.lin 已提交
103 104
    for (auto index : indexes) {
        // try to set one task loading, then return
S
starlord 已提交
105
        if (task_table_.Load(index)) return task_table_.Get(index);
X
xj.lin 已提交
106 107 108 109 110
        // else try next
    }
    return nullptr;
}

S
starlord 已提交
111 112
TaskTableItemPtr
Resource::pick_task_execute() {
113
    auto indexes = task_table_.PickToExecute(3);
X
xj.lin 已提交
114 115
    for (auto index : indexes) {
        // try to set one task executing, then return
S
starlord 已提交
116
        if (task_table_.Execute(index)) return task_table_.Get(index);
X
xj.lin 已提交
117 118 119 120 121
        // else try next
    }
    return nullptr;
}

S
starlord 已提交
122 123
void
Resource::loader_function() {
X
xj.lin 已提交
124 125
    while (running_) {
        std::unique_lock<std::mutex> lock(load_mutex_);
S
starlord 已提交
126
        load_cv_.wait(lock, [&] { return load_flag_; });
W
wxyu 已提交
127
        load_flag_ = false;
128
        lock.unlock();
129 130 131 132 133
        while (true) {
            auto task_item = pick_task_load();
            if (task_item == nullptr) {
                break;
            }
134
            LoadFile(task_item->task);
135
            task_item->Loaded();
W
wxyu 已提交
136
            if (subscriber_) {
137
                auto event = std::make_shared<LoadCompletedEvent>(shared_from_this(), task_item);
138
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
139
            }
X
xj.lin 已提交
140 141 142 143
        }
    }
}

S
starlord 已提交
144 145
void
Resource::executor_function() {
W
wxyu 已提交
146
    if (subscriber_) {
147 148
        auto event = std::make_shared<StartUpEvent>(shared_from_this());
        subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
149
    }
X
xj.lin 已提交
150 151
    while (running_) {
        std::unique_lock<std::mutex> lock(exec_mutex_);
S
starlord 已提交
152
        exec_cv_.wait(lock, [&] { return exec_flag_; });
W
wxyu 已提交
153
        exec_flag_ = false;
154
        lock.unlock();
155 156 157 158 159
        while (true) {
            auto task_item = pick_task_execute();
            if (task_item == nullptr) {
                break;
            }
Y
Yu Kun 已提交
160

161
            auto start = get_current_timestamp();
162
            Process(task_item->task);
163
            auto finish = get_current_timestamp();
Y
Yu Kun 已提交
164 165 166
            ++total_task_;
            total_cost_ += finish - start;

167
            task_item->Executed();
W
wxyu 已提交
168
            if (subscriber_) {
169 170
                auto event = std::make_shared<FinishTaskEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
171
            }
X
xj.lin 已提交
172 173 174 175
        }
    }
}

S
starlord 已提交
176 177 178
}  // namespace scheduler
}  // namespace milvus
}  // namespace zilliz