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.

18
#include <iostream>
Y
Yu Kun 已提交
19
#include "../Utils.h"
X
xj.lin 已提交
20 21 22 23 24
#include "Resource.h"


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

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

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

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

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

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

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

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

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

114
TaskTableItemPtr Resource::pick_task_execute() {
115
    auto indexes = task_table_.PickToExecute(3);
X
xj.lin 已提交
116 117 118
    for (auto index : indexes) {
        // try to set one task executing, then return
        if (task_table_.Execute(index))
119
            return task_table_.Get(index);
X
xj.lin 已提交
120 121 122 123 124 125 126 127 128
        // 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 已提交
129
        load_flag_ = false;
130
        lock.unlock();
131 132 133 134 135
        while (true) {
            auto task_item = pick_task_load();
            if (task_item == nullptr) {
                break;
            }
136
            LoadFile(task_item->task);
137
            task_item->Loaded();
W
wxyu 已提交
138
            if (subscriber_) {
139
                auto event = std::make_shared<LoadCompletedEvent>(shared_from_this(), task_item);
140
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
141
            }
X
xj.lin 已提交
142
        }
143

X
xj.lin 已提交
144 145 146 147
    }
}

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

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

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

X
xj.lin 已提交
176 177 178 179 180 181
    }
}

}
}
}