性能优化1,修复编译bug
This commit is contained in:
parent
943362937b
commit
2a14f765c4
@ -11,6 +11,7 @@
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <utility>
|
||||
|
||||
#include "node.h"
|
||||
#include "plugin_loader.h"
|
||||
@ -103,6 +104,32 @@ private:
|
||||
};
|
||||
|
||||
struct NodeEntry {
|
||||
NodeEntry() = default;
|
||||
NodeEntry(const NodeEntry&) = delete;
|
||||
NodeEntry& operator=(const NodeEntry&) = delete;
|
||||
|
||||
NodeEntry(NodeEntry&& other) noexcept {
|
||||
*this = std::move(other);
|
||||
}
|
||||
|
||||
NodeEntry& operator=(NodeEntry&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
id = std::move(other.id);
|
||||
type = std::move(other.type);
|
||||
role = std::move(other.role);
|
||||
enabled = other.enabled;
|
||||
config = std::move(other.config);
|
||||
context = std::move(other.context);
|
||||
node = std::move(other.node);
|
||||
cpu_affinity = std::move(other.cpu_affinity);
|
||||
|
||||
sched_state.store(other.sched_state.load(std::memory_order_relaxed), std::memory_order_relaxed);
|
||||
pool_index = other.pool_index;
|
||||
|
||||
metrics = std::move(other.metrics);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string id;
|
||||
std::string type;
|
||||
std::string role;
|
||||
|
||||
@ -209,10 +209,10 @@ public:
|
||||
key_to_pool_.clear();
|
||||
|
||||
auto add_pool = [&](const std::string& key, std::vector<int> cpus, int threads) -> uint32_t {
|
||||
Pool p;
|
||||
p.key = key;
|
||||
p.cpus = std::move(cpus);
|
||||
p.thread_count = std::max(1, threads);
|
||||
auto p = std::make_unique<Pool>();
|
||||
p->key = key;
|
||||
p->cpus = std::move(cpus);
|
||||
p->thread_count = std::max(1, threads);
|
||||
pools_.push_back(std::move(p));
|
||||
const uint32_t idx = static_cast<uint32_t>(pools_.size() - 1);
|
||||
key_to_pool_[key] = idx;
|
||||
@ -244,9 +244,11 @@ public:
|
||||
}
|
||||
|
||||
// Start pool threads.
|
||||
for (auto& p : pools_) {
|
||||
for (int i = 0; i < p.thread_count; ++i) {
|
||||
p.threads.emplace_back([this, &p]() { WorkerLoop(p); });
|
||||
for (auto& up : pools_) {
|
||||
Pool* p = up.get();
|
||||
if (!p) continue;
|
||||
for (int i = 0; i < p->thread_count; ++i) {
|
||||
p->threads.emplace_back([this, p]() { WorkerLoop(*p); });
|
||||
}
|
||||
}
|
||||
|
||||
@ -258,7 +260,9 @@ public:
|
||||
if (!started_) return;
|
||||
stop_.store(true);
|
||||
|
||||
for (auto& p : pools_) {
|
||||
for (auto& up : pools_) {
|
||||
if (!up) continue;
|
||||
Pool& p = *up;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(p.mu);
|
||||
p.ready.clear();
|
||||
@ -266,7 +270,9 @@ public:
|
||||
p.cv.notify_all();
|
||||
}
|
||||
|
||||
for (auto& p : pools_) {
|
||||
for (auto& up : pools_) {
|
||||
if (!up) continue;
|
||||
Pool& p = *up;
|
||||
for (auto& t : p.threads) {
|
||||
if (t.joinable()) t.join();
|
||||
}
|
||||
@ -310,7 +316,8 @@ public:
|
||||
if (prev & kRunning) return;
|
||||
|
||||
const uint32_t idx = (n->pool_index < pools_.size()) ? n->pool_index : default_pool_index_;
|
||||
Pool& p = pools_[idx];
|
||||
if (idx >= pools_.size() || !pools_[idx]) return;
|
||||
Pool& p = *pools_[idx];
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(p.mu);
|
||||
p.ready.push_back(n);
|
||||
@ -433,7 +440,7 @@ private:
|
||||
std::atomic<bool> stop_{false};
|
||||
bool started_ = false;
|
||||
uint32_t default_pool_index_ = 0;
|
||||
std::vector<Pool> pools_;
|
||||
std::vector<std::unique_ptr<Pool>> pools_;
|
||||
std::unordered_map<std::string, uint32_t> key_to_pool_;
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user