Skip to content

Commit

Permalink
[perf] delete status current path.
Browse files Browse the repository at this point in the history
  • Loading branch information
jun01.feng committed Aug 17, 2024
1 parent 88bb058 commit aec86c1
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 53 deletions.
3 changes: 1 addition & 2 deletions src/CBasic/CFuncType.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ enum class CFunctionType {
* 这里这样实现,是为了符合 CStatus 类似写法
* */
#define CErrStatus(info) \
CStatus(info, CGRAPH_GET_LOCATE) \
CStatus(info) \

/** 返回异常信息和状态 */
#define CGRAPH_RETURN_ERROR_STATUS(info) \
Expand Down Expand Up @@ -88,7 +88,6 @@ enum class CFunctionType {
#define CGRAPH_THROW_EXCEPTION_BY_CONDITION(cond, info) \
if (unlikely(cond)) { CGRAPH_THROW_EXCEPTION(info); } \


CGRAPH_NAMESPACE_END

#endif //CGRAPH_CFUNCTYPE_H
22 changes: 2 additions & 20 deletions src/CBasic/CStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ class CSTATUS {
public:
explicit CSTATUS() = default;

explicit CSTATUS(const std::string &errorInfo,
const std::string &locateInfo = CGRAPH_EMPTY) {
explicit CSTATUS(const std::string &errorInfo) {
this->error_code_ = STATUS_ERR; // 默认的error code信息
this->error_info_ = errorInfo;
this->error_locate_ = locateInfo;
}

explicit CSTATUS(int errorCode, const std::string &errorInfo,
const std::string &locateInfo = CGRAPH_EMPTY) {
explicit CSTATUS(int errorCode, const std::string &errorInfo) {
this->error_code_ = errorCode;
this->error_info_ = errorInfo;
this->error_locate_ = locateInfo;
}

CSTATUS(const CSTATUS &status) {
Expand All @@ -52,7 +48,6 @@ class CSTATUS {

this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}

CSTATUS(const CSTATUS &&status) noexcept {
Expand All @@ -62,15 +57,13 @@ class CSTATUS {

this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}

CSTATUS& operator=(const CSTATUS& status) {
if (this->error_code_ != status.error_code_) {
// 如果status是正常的话,则所有数据保持不变
this->error_code_ = status.error_code_;
this->error_info_ = status.error_info_;
this->error_locate_ = status.error_locate_;
}
return (*this);
}
Expand All @@ -83,7 +76,6 @@ class CSTATUS {
if (!this->isErr() && cur.isErr()) {
this->error_code_ = cur.error_code_;
this->error_info_ = cur.error_info_;
this->error_locate_ = cur.error_locate_;
}

return (*this);
Expand All @@ -96,7 +88,6 @@ class CSTATUS {
if (this->error_code_ != STATUS_OK) {
this->error_code_ = STATUS_OK;
this->error_info_.clear();
this->error_locate_.clear();
}
}

Expand All @@ -116,14 +107,6 @@ class CSTATUS {
return this->error_info_;
}

/**
* 获取报错位置
* @return
*/
const std::string& getLocate() const {
return this->error_locate_;
}

/**
* 判断当前状态是否可行
* @return
Expand Down Expand Up @@ -174,7 +157,6 @@ class CSTATUS {
private:
int error_code_ = STATUS_OK; // 错误码信息
std::string error_info_; // 错误信息描述
std::string error_locate_; // 错误发生的具体位置,形如:file|function|line
};

CGRAPH_INTERNAL_NAMESPACE_END
Expand Down
9 changes: 7 additions & 2 deletions src/GraphCtrl/GraphElement/GElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ CBool GElement::isMacro() const {


CStatus GElement::crashed(const CException& ex) {
return CStatus(internal::STATUS_CRASH, ex.what(), CGRAPH_GET_LOCATE);
return CStatus(internal::STATUS_CRASH, ex.what());
}


CIndex GElement::getThreadIndex() {
CGRAPH_THROW_EXCEPTION_BY_CONDITION((nullptr == thread_pool_), \
this->getName() + " getThreadIndex with no threadpool") // 理论不可能出现的情况
this->getName() + " getThreadIndex with no thread pool") // 理论不可能出现的情况

auto tid = (CSize)std::hash<std::thread::id>{}(std::this_thread::get_id());
return thread_pool_->getThreadIndex(tid);
Expand Down Expand Up @@ -561,4 +561,9 @@ GElementPtrArr GElement::getDeepPath(CBool reverse) const {
return path;
}


CBool GElement::isDefaultBinding() const {
return CGRAPH_DEFAULT_BINDING_INDEX == binding_index_;
}

CGRAPH_NAMESPACE_END
6 changes: 6 additions & 0 deletions src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@ class GElement : public GElementObject,
*/
std::vector<GElement *> getDeepPath(CBool reverse) const;

/**
* 判断是否是默认绑定策略
* @return
*/
CBool isDefaultBinding() const;

private:
/** 状态相关信息 */
CBool done_ { false }; // 判定被执行结束
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ CStatus GMultiCondition<type>::parallelRun() {
continue; // 不满足条件,则不执行
}

futures.emplace_back(this->thread_pool_->commit([cur] {
futures.emplace_back(std::move(this->thread_pool_->commit([cur] {
return cur->fatProcessor(CFunctionType::RUN);
}, cur->getBindingIndex()));
}, cur->binding_index_)));
}

for (auto& fut: futures) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ CVoid GDynamicEngine::process(GElementPtr element, CBool affinity) {
afterElementRun(element);
};

if (affinity
&& CGRAPH_DEFAULT_BINDING_INDEX == element->getBindingIndex()) {
if (affinity && element->isDefaultBinding()) {
// 如果 affinity=true,表示用当前的线程,执行这个逻辑。以便增加亲和性
exec();
} else {
Expand Down Expand Up @@ -234,9 +233,9 @@ CVoid GDynamicEngine::parallelRunAll() {
std::vector<std::future<CStatus>> futures;
futures.reserve(total_end_size_);
for (int i = 0; i < total_end_size_; i++) {
futures.emplace_back(thread_pool_->commit([this, i] {
futures.emplace_back(std::move(thread_pool_->commit([this, i] {
return total_element_arr_[i]->fatProcessor(CFunctionType::RUN);
}, calcIndex(total_element_arr_[i])));
}, calcIndex(total_element_arr_[i]))));
}

for (auto& fut : futures) {
Expand Down
9 changes: 4 additions & 5 deletions src/GraphCtrl/GraphElement/_GEngine/GEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ class GEngine : public GElementObject {
* @param element
* @return
*/
inline CIndex calcIndex(GElementPtr element) const {
CIndex calcIndex(GElementCPtr element) const {
/**
* 如果没有设定绑定线程的话,就用默认调度策略
* 否则的话,会走绑定的thread。
* 如果设定的 binding_index_ >= thread 总数,会在 threadpool 层做统一判定
*/
auto bindingIndex = element->getBindingIndex();
return CGRAPH_DEFAULT_BINDING_INDEX == bindingIndex
? schedule_strategy_ : bindingIndex;
return element->isDefaultBinding()
? schedule_strategy_ : element->binding_index_;
}

/**
Expand All @@ -68,7 +67,7 @@ class GEngine : public GElementObject {
element->linkable_ = false; // 防止出现之前的留存逻辑。确保只有当前链接关系下,需要设置 linkable的,才会设置为 true
if (1 == element->dependence_.size()
&& 1 == (*element->dependence_.begin())->run_before_.size()
&& element->getBindingIndex() == (*(element->dependence_.begin()))->getBindingIndex()) {
&& element->binding_index_ == (*(element->dependence_.begin()))->binding_index_) {
element->linkable_ = true;
linked_size_++;
}
Expand Down
27 changes: 9 additions & 18 deletions src/UtilsCtrl/UtilsDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ CGRAPH_NAMESPACE_BEGIN
using CGRAPH_LOCK_GUARD = std::lock_guard<std::mutex>;
using CGRAPH_UNIQUE_LOCK = std::unique_lock<std::mutex>;

/* 判断函数流程是否可以继续 */
CGRAPH_INTERNAL_NAMESPACE_BEGIN
static std::mutex g_check_status_mtx;
static std::mutex g_echo_mtx;
CGRAPH_INTERNAL_NAMESPACE_END

#if __cplusplus >= 201703L
using CGRAPH_READ_LOCK = std::shared_lock<std::shared_mutex>;
using CGRAPH_WRITE_LOCK = std::unique_lock<std::shared_mutex>;
Expand All @@ -49,7 +43,6 @@ CGRAPH_INTERNAL_NAMESPACE_END
using CGRAPH_WRITE_LOCK = CGRAPH_LOCK_GUARD;
#endif


template<typename T>
CStatus __ASSERT_NOT_NULL(T t) {
return (unlikely(nullptr == t))
Expand Down Expand Up @@ -82,15 +75,13 @@ CVoid __ASSERT_NOT_NULL_THROW_EXCEPTION(T t, Args... args) {
__ASSERT_NOT_NULL_THROW_EXCEPTION(args...);
}


/** 判断传入的多个指针信息,是否为空 */
#define CGRAPH_ASSERT_NOT_NULL(ptr, ...) \
{ \
const CStatus& __cur_status__ = __ASSERT_NOT_NULL(ptr, ##__VA_ARGS__); \
if (unlikely(__cur_status__.isErr())) { return __cur_status__; } \
} \


/** 判断传入的多个指针,是否为空。如果为空,则抛出异常信息 */
#define CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(ptr, ...) \
__ASSERT_NOT_NULL_THROW_EXCEPTION(ptr, ##__VA_ARGS__); \
Expand Down Expand Up @@ -128,14 +119,13 @@ CVoid __ASSERT_NOT_NULL_THROW_EXCEPTION(T t, Args... args) {
#define CGRAPH_SLEEP_MILLISECOND(ms) \
std::this_thread::sleep_for(std::chrono::milliseconds(ms)); \

#define CGRAPH_FUNCTION_CHECK_STATUS \
if (unlikely(status.isErr())) { \
if (status.isCrash()) { throw CException(status.getInfo()); } \
CGRAPH_LOCK_GUARD lock{ internal::g_check_status_mtx }; \
CGRAPH_ECHO("%s, errorCode = [%d], errorInfo = [%s].", \
status.getLocate().c_str(), status.getCode(), status.getInfo().c_str()); \
return status; \
} \
#define CGRAPH_FUNCTION_CHECK_STATUS \
if (unlikely(status.isErr())) { \
if (status.isCrash()) { throw CException(status.getInfo()); } \
CGRAPH_ECHO("errorCode = [%d], errorInfo = [%s].", \
status.getCode(), status.getInfo().c_str()); \
return status; \
} \

/**
* 定制化输出
Expand All @@ -148,7 +138,8 @@ inline CVoid CGRAPH_ECHO(const char *cmd, ...) {
return;
#endif

std::lock_guard<std::mutex> lock{ internal::g_echo_mtx };
static std::mutex echo_mtx;
std::lock_guard<std::mutex> lock{ echo_mtx };
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() % 1000;
Expand Down

0 comments on commit aec86c1

Please sign in to comment.