-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
suicide immediately when resource hard limit is met #1649
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ void LogtailMonitor::Stop() { | |
|
||
void LogtailMonitor::Monitor() { | ||
LOG_INFO(sLogger, ("profiling", "started")); | ||
int32_t lastMonitorTime = time(NULL); | ||
int32_t lastMonitorTime = time(NULL), lastCheckHardLimitTime = time(nullptr); | ||
CpuStat curCpuStat; | ||
{ | ||
unique_lock<mutex> lock(mThreadRunningMux); | ||
|
@@ -172,40 +172,54 @@ void LogtailMonitor::Monitor() { | |
} | ||
#endif | ||
|
||
// Update statistics and send to logtail_status_profile regularly. | ||
// If CPU or memory limit triggered, send to logtail_suicide_profile. | ||
if ((monitorTime - lastMonitorTime) < INT32_FLAG(monitor_interval)) | ||
continue; | ||
lastMonitorTime = monitorTime; | ||
|
||
// Memory usage has exceeded limit, try to free some timeout objects. | ||
if (1 == mMemStat.mViolateNum) { | ||
LOG_DEBUG(sLogger, ("Memory is upper limit", "run gabbage collection.")); | ||
LogInput::GetInstance()->SetForceClearFlag(true); | ||
} | ||
GetMemStat(); | ||
CalCpuStat(curCpuStat, mCpuStat); | ||
// CalCpuLimit and CalMemLimit will check if the number of violation (CPU | ||
// or memory exceeds limit) // is greater or equal than limits ( | ||
// flag(cpu_limit_num) and flag(mem_limit_num)). | ||
// Returning true means too much violations, so we have to prepare to restart | ||
// logtail to release resource. | ||
// Mainly for controlling memory because we have no idea to descrease memory usage. | ||
if (CheckCpuLimit() || CheckMemLimit()) { | ||
LOG_ERROR(sLogger, | ||
("Resource used by program exceeds upper limit", | ||
"prepare restart Logtail")("cpu_usage", mCpuStat.mCpuUsage)("mem_rss", mMemStat.mRss)); | ||
Suicide(); | ||
static uint32_t checkHardLimitInterval | ||
= INT32_FLAG(monitor_interval) > 30 ? INT32_FLAG(monitor_interval) / 6 : 5; | ||
if ((monitorTime - lastCheckHardLimitTime) >= checkHardLimitInterval) { | ||
lastCheckHardLimitTime = monitorTime; | ||
|
||
GetMemStat(); | ||
CalCpuStat(curCpuStat, mCpuStat); | ||
if (CheckHardCpuLimit() || CheckHardMemLimit()) { | ||
LOG_ERROR(sLogger, | ||
("Resource used by program exceeds hard limit", | ||
"prepare restart Logtail")("cpu_usage", mCpuStat.mCpuUsage)("mem_rss", mMemStat.mRss)); | ||
Suicide(); | ||
} | ||
} | ||
|
||
if (IsHostIpChanged()) { | ||
Suicide(); | ||
} | ||
|
||
SendStatusProfile(false); | ||
if (BOOL_FLAG(logtail_dump_monitor_info)) { | ||
if (!DumpMonitorInfo(monitorTime)) | ||
LOG_ERROR(sLogger, ("Fail to dump monitor info", "")); | ||
// Update statistics and send to logtail_status_profile regularly. | ||
// If CPU or memory limit triggered, send to logtail_suicide_profile. | ||
if ((monitorTime - lastMonitorTime) >= INT32_FLAG(monitor_interval)) { | ||
lastMonitorTime = monitorTime; | ||
|
||
// Memory usage has exceeded limit, try to free some timeout objects. | ||
if (1 == mMemStat.mViolateNum) { | ||
LOG_DEBUG(sLogger, ("Memory is upper limit", "run gabbage collection.")); | ||
LogInput::GetInstance()->SetForceClearFlag(true); | ||
} | ||
// CalCpuLimit and CalMemLimit will check if the number of violation (CPU | ||
// or memory exceeds limit) // is greater or equal than limits ( | ||
// flag(cpu_limit_num) and flag(mem_limit_num)). | ||
// Returning true means too much violations, so we have to prepare to restart | ||
// logtail to release resource. | ||
// Mainly for controlling memory because we have no idea to descrease memory usage. | ||
if (CheckSoftCpuLimit() || CheckSoftMemLimit()) { | ||
LOG_ERROR(sLogger, | ||
("Resource used by program exceeds upper limit for some time", | ||
"prepare restart Logtail")("cpu_usage", mCpuStat.mCpuUsage)("mem_rss", mMemStat.mRss)); | ||
Suicide(); | ||
} | ||
|
||
if (IsHostIpChanged()) { | ||
Suicide(); | ||
} | ||
|
||
SendStatusProfile(false); | ||
if (BOOL_FLAG(logtail_dump_monitor_info)) { | ||
if (!DumpMonitorInfo(monitorTime)) | ||
LOG_ERROR(sLogger, ("Fail to dump monitor info", "")); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -439,7 +453,7 @@ void LogtailMonitor::CalCpuStat(const CpuStat& curCpu, CpuStat& savedCpu) { | |
#endif | ||
} | ||
|
||
bool LogtailMonitor::CheckCpuLimit() { | ||
bool LogtailMonitor::CheckSoftCpuLimit() { | ||
float cpuUsageLimit = AppConfig::GetInstance()->IsResourceAutoScale() | ||
? AppConfig::GetInstance()->GetScaledCpuUsageUpLimit() | ||
: AppConfig::GetInstance()->GetCpuUsageUpLimit(); | ||
|
@@ -451,7 +465,7 @@ bool LogtailMonitor::CheckCpuLimit() { | |
return false; | ||
} | ||
|
||
bool LogtailMonitor::CheckMemLimit() { | ||
bool LogtailMonitor::CheckSoftMemLimit() { | ||
if (mMemStat.mRss > AppConfig::GetInstance()->GetMemUsageUpLimit()) { | ||
if (++mMemStat.mViolateNum > INT32_FLAG(mem_limit_num)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 间隔变了,这里次数没变? |
||
return true; | ||
|
@@ -460,6 +474,17 @@ bool LogtailMonitor::CheckMemLimit() { | |
return false; | ||
} | ||
|
||
bool LogtailMonitor::CheckHardCpuLimit() { | ||
float cpuUsageLimit = AppConfig::GetInstance()->IsResourceAutoScale() | ||
? AppConfig::GetInstance()->GetScaledCpuUsageUpLimit() | ||
: AppConfig::GetInstance()->GetCpuUsageUpLimit(); | ||
return mCpuStat.mCpuUsage > 10 * cpuUsageLimit; | ||
} | ||
|
||
bool LogtailMonitor::CheckHardMemLimit() { | ||
return mMemStat.mRss > 10 * AppConfig::GetInstance()->GetMemUsageUpLimit(); | ||
} | ||
|
||
void LogtailMonitor::DumpToLocal(const sls_logs::LogGroup& logGroup) { | ||
string dumpStr = "\n####logtail status####\n"; | ||
for (int32_t logIdx = 0; logIdx < logGroup.logs_size(); ++logIdx) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
间隔变了,这里次数没变?