!2796 combine table, extract loop method from unified collector

Merge pull request !2796 from 尹之帆/loopCache
This commit is contained in:
openharmony_ci 2025-01-24 10:18:23 +00:00 committed by Gitee
commit 2d26ff19dd
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 216 additions and 144 deletions

View File

@ -272,7 +272,7 @@ ohos_source_set("ucollection_source") {
# for plugin unified_collector
sources += [
"collector/impl/trace/utils/app_event_task_storage.cpp",
"collector/impl/trace/utils/trace_behavior_controller.cpp",
"collector/impl/trace/utils/trace_behavior_db_helper.cpp",
"collector/impl/trace/utils/trace_cache_monitor.cpp",
"collector/impl/trace/utils/trace_flow_controller.cpp",
"collector/impl/trace/utils/trace_manager.cpp",

View File

@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_BEHAVIOR_CONTROLLER_H
#define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_BEHAVIOR_CONTROLLER_H
#ifndef FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_BEHAVIOR_DB_HELPER_H
#define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_BEHAVIOR_DB_HELPER_H
#include "rdb_store.h"
@ -30,10 +30,10 @@ struct BehaviorRecord {
int32_t usedQuota = 0;
};
class TraceBehaviorController {
class TraceBehaviorDbHelper {
public:
TraceBehaviorController();
~TraceBehaviorController() = default;
TraceBehaviorDbHelper();
~TraceBehaviorDbHelper() = default;
public:
bool GetRecord(BehaviorRecord &behaviorRecord);
@ -44,4 +44,4 @@ private:
};
} // namespace HiviewDFX
} // namespace OHOS
#endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_BEHAVIOR_CONTROLLER_H
#endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_BEHAVIOR_DB_HELPER_H

View File

@ -17,30 +17,42 @@
#define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_CACHE_MONITOR_H
#include "memory_collector.h"
#include "trace_behavior_controller.h"
#include "trace_behavior_db_helper.h"
namespace OHOS {
namespace HiviewDFX {
namespace {
enum TraceCacheMonitorState {
EXIT = 0,
RUNNING = 1,
INTERRUPT = 2,
};
}
class TraceCacheMonitor {
public:
TraceCacheMonitor(int32_t lowMemThreshold);
TraceCacheMonitor();
~TraceCacheMonitor();
void RunMonitorCycle(int32_t interval);
bool UseCacheTimeQuota(int32_t usedQuota);
void SetLowMemThreshold(int32_t threshold);
void RunMonitorLoop();
void ExitMonitorLoop();
private:
void MonitorFfrtTask();
void RunMonitorCycle(int32_t interval);
bool IsLowMemState();
void SetCacheOn();
void SetCacheOff();
void CountDownCacheOff();
bool IsLowMemState();
void SleepandUpdateCacheStatus(int32_t interval);
bool UseCacheTimeQuota(int32_t usedQuota);
private:
TraceBehaviorController behaviorController_;
std::mutex stateMutex_;
TraceCacheMonitorState monitorState_ = EXIT;
std::shared_ptr<UCollectUtil::MemoryCollector> collector_;
int32_t lowMemThreshold_ = 0;
bool isCacheOn_ = false;
bool isWaitingForNormal_ = false;
bool isWaitingForRecovery_ = false;
int32_t lowMemThreshold_ = 0;
int32_t cacheDuration_ = 0;
int32_t cacheOffCountdown_ = 0;
};

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "trace_behavior_controller.h"
#include "trace_behavior_db_helper.h"
#include "file_util.h"
#include "hiview_logger.h"
@ -26,12 +26,12 @@
namespace OHOS {
namespace HiviewDFX {
DEFINE_LOG_TAG("TraceBehaviorController");
DEFINE_LOG_TAG("TraceBehaviorDbHelper");
namespace {
const std::string DB_PATH = "/data/log/hiview/unified_collection/trace/";
const std::string DB_NAME = "trace_flow_control";
constexpr int32_t DB_VERSION = 1;
const std::string TABLE_NAME_BEHAVIOR = "trace_behavior_controller";
const std::string TABLE_NAME_BEHAVIOR = "trace_behavior_db_helper";
const std::string COLUMN_ID = "id";
const std::string COLUMN_BEHAVIOR_ID = "behavior_id ";
const std::string COLUMN_DATE = "task_date";
@ -39,14 +39,14 @@ const std::string COLUMN_USED_QUOTA = "used_quota";
class TraceBehaviorDbStoreCallback : public NativeRdb::RdbOpenCallback {
public:
int OnCreate(NativeRdb::RdbStore &rdbStore) override;
int OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
};
int32_t CreateTraceBehaviorControlTable(NativeRdb::RdbStore& rdbStore)
int32_t CreateTraceBehaviorDbHelperTable(NativeRdb::RdbStore& rdbStore)
{
/**
* table: trace_behavior_controller
* table: trace_behavior_db_helper
*
* describe: store trace behavior quota
* |-----|-------------|-----------|------------|
@ -68,21 +68,21 @@ int32_t CreateTraceBehaviorControlTable(NativeRdb::RdbStore& rdbStore)
return 0;
}
int TraceBehaviorDbStoreCallback::OnCreate(NativeRdb::RdbStore& rdbStore)
int32_t TraceBehaviorDbStoreCallback::OnCreate(NativeRdb::RdbStore& rdbStore)
{
HIVIEW_LOGD("create dbStore");
if (auto ret = CreateTraceBehaviorControlTable(rdbStore); ret != NativeRdb::E_OK) {
HIVIEW_LOGE("failed to create table trace_flow_control");
if (auto ret = CreateTraceBehaviorDbHelperTable(rdbStore); ret != NativeRdb::E_OK) {
HIVIEW_LOGE("failed to create table %{public}s", TABLE_NAME_BEHAVIOR.c_str());
return ret;
}
return NativeRdb::E_OK;
}
int TraceBehaviorDbStoreCallback::OnUpgrade(NativeRdb::RdbStore& rdbStore, int oldVersion, int newVersion)
int32_t TraceBehaviorDbStoreCallback::OnUpgrade(NativeRdb::RdbStore& rdbStore, int32_t oldVersion, int32_t newVersion)
{
HIVIEW_LOGD("oldVersion=%{public}d, newVersion=%{public}d", oldVersion, newVersion);
std::string sql = SqlUtil::GenerateDropSql(TABLE_NAME_BEHAVIOR);
if (int ret = rdbStore.ExecuteSql(sql); ret != NativeRdb::E_OK) {
if (int32_t ret = rdbStore.ExecuteSql(sql); ret != NativeRdb::E_OK) {
HIVIEW_LOGE("failed to drop table %{public}s, ret=%{public}d", TABLE_NAME_BEHAVIOR.c_str(), ret);
return -1;
}
@ -99,7 +99,7 @@ NativeRdb::ValuesBucket InnerGetBucket(const BehaviorRecord &behaviorRecord)
}
}
TraceBehaviorController::TraceBehaviorController()
TraceBehaviorDbHelper::TraceBehaviorDbHelper()
{
std::string dbStorePath = DB_PATH + DB_NAME + ".db";
NativeRdb::RdbStoreConfig config(dbStorePath);
@ -108,15 +108,19 @@ TraceBehaviorController::TraceBehaviorController()
auto ret = NativeRdb::E_OK;
dbStore_ = NativeRdb::RdbHelper::GetRdbStore(config, DB_VERSION, callback, ret);
if (ret != NativeRdb::E_OK) {
HIVIEW_LOGW("failed to init db store, db store path=%{public}s", dbStorePath.c_str());
HIVIEW_LOGE("failed to init db store, db store path=%{public}s", dbStorePath.c_str());
dbStore_ = nullptr;
}
ret = CreateTraceBehaviorDbHelperTable(*dbStore_);
if (ret != NativeRdb::E_OK) {
HIVIEW_LOGE("create table %{public}s failed", TABLE_NAME_BEHAVIOR.c_str());
}
}
bool TraceBehaviorController::GetRecord(BehaviorRecord &behaviorRecord)
bool TraceBehaviorDbHelper::GetRecord(BehaviorRecord &behaviorRecord)
{
if (dbStore_ == nullptr) {
HIVIEW_LOGW("db store is null, path=%{public}s", DB_PATH.c_str());
HIVIEW_LOGE("db store is null, path=%{public}s", DB_PATH.c_str());
return false;
}
NativeRdb::AbsRdbPredicates predicates(TABLE_NAME_BEHAVIOR);
@ -124,14 +128,14 @@ bool TraceBehaviorController::GetRecord(BehaviorRecord &behaviorRecord)
predicates.EqualTo(COLUMN_DATE, behaviorRecord.dateNum);
auto resultSet = dbStore_->Query(predicates, {COLUMN_USED_QUOTA});
if (resultSet == nullptr) {
HIVIEW_LOGW("failed to query from table %{public}s, db is null", TABLE_NAME_BEHAVIOR.c_str());
HIVIEW_LOGE("failed to query from table %{public}s", TABLE_NAME_BEHAVIOR.c_str());
return false;
}
if (resultSet->GoToNextRow() == NativeRdb::E_OK) {
resultSet->GetInt(0, behaviorRecord.usedQuota); // 0 means used_quota field
} else {
HIVIEW_LOGW("Fail to get record for date %{public}s, set usedQuota to 0", behaviorRecord.dateNum.c_str());
HIVIEW_LOGW("fail to get record for date %{public}s, set usedQuota to 0", behaviorRecord.dateNum.c_str());
behaviorRecord.usedQuota = 0;
resultSet->Close();
return false;
@ -140,10 +144,10 @@ bool TraceBehaviorController::GetRecord(BehaviorRecord &behaviorRecord)
return true;
}
bool TraceBehaviorController::InsertRecord(BehaviorRecord &behaviorRecord)
bool TraceBehaviorDbHelper::InsertRecord(BehaviorRecord &behaviorRecord)
{
if (dbStore_ == nullptr) {
HIVIEW_LOGW("db store is null, path=%{public}s", DB_PATH.c_str());
HIVIEW_LOGE("db store is null, path=%{public}s", DB_PATH.c_str());
return false;
}
NativeRdb::ValuesBucket bucket;
@ -158,7 +162,7 @@ bool TraceBehaviorController::InsertRecord(BehaviorRecord &behaviorRecord)
return true;
}
bool TraceBehaviorController::UpdateRecord(BehaviorRecord &behaviorRecord)
bool TraceBehaviorDbHelper::UpdateRecord(BehaviorRecord &behaviorRecord)
{
if (dbStore_ == nullptr) {
HIVIEW_LOGE("db store is null, path=%{public}s", DB_PATH.c_str());
@ -168,7 +172,7 @@ bool TraceBehaviorController::UpdateRecord(BehaviorRecord &behaviorRecord)
NativeRdb::AbsRdbPredicates predicates(TABLE_NAME_BEHAVIOR);
predicates.EqualTo(COLUMN_BEHAVIOR_ID, behaviorRecord.behaviorId);
predicates.EqualTo(COLUMN_DATE, behaviorRecord.dateNum);
int changedRows = 0;
int32_t changedRows = 0;
if (dbStore_->Update(changedRows, bucket, predicates) != NativeRdb::E_OK) {
HIVIEW_LOGW("failed to update table");
return false;

View File

@ -20,18 +20,24 @@
#include "ffrt.h"
#include "hitrace_dump.h"
#include "hiview_logger.h"
#include "parameter_ex.h"
#include "string_util.h"
#include "time_util.h"
namespace OHOS {
namespace HiviewDFX {
DEFINE_LOG_TAG("TraceCacheMonitor");
#if defined(HIVIEW_LOW_MEM_THRESHOLD)
namespace {
constexpr int32_t HITRACE_CACHE_DURATION_LIMIT_DAILY_TOTAL = 10 * 60;
constexpr int32_t HITRACE_CACHE_DURATION_LIMIT_PER_EVENT = 2 * 60;
constexpr int32_t HITRACE_CACHE_FILE_SIZE_LIMIT = 800;
constexpr int32_t HITRACE_CACHE_FILE_SLICE_SPAN = 10;
constexpr int32_t HITRACE_CACHE_DURATION_LIMIT_DAILY_TOTAL = 10 * 60; // 10 minutes
constexpr int32_t HITRACE_CACHE_DURATION_LIMIT_PER_EVENT = 2 * 60; // 2 minutes
constexpr int32_t HITRACE_CACHE_FILE_SIZE_LIMIT = 800; // 800MB
constexpr int32_t HITRACE_CACHE_FILE_SLICE_SPAN = 10; // 10 seconds
constexpr uint64_t S_TO_NS = 1000000000;
constexpr int32_t CACHE_OFF_CONDITION_COUNTDOWN = 2;
constexpr int32_t MONITOR_INTERVAL = 5;
constexpr char PARAM_KEY_CACHE_LOW_MEM_THRESHOLD[] = "hiviewdfx.ucollection.memthreshold";
constexpr int32_t HIVIEW_CACHE_LOW_MEM_THRESHOLD = HIVIEW_LOW_MEM_THRESHOLD;
std::chrono::system_clock::time_point GetNextDay()
{
@ -50,83 +56,100 @@ std::chrono::system_clock::time_point GetNextDay()
std::time_t nextDayTime = std::mktime(&nowTm);
return std::chrono::system_clock::from_time_t(nextDayTime);
}
void OnLowMemThresholdChange(const char *key, const char *value, void *context)
{
if (context == nullptr || key == nullptr || value == nullptr) {
HIVIEW_LOGW("invalid input");
return;
}
if (strncmp(key, PARAM_KEY_CACHE_LOW_MEM_THRESHOLD, strlen(PARAM_KEY_CACHE_LOW_MEM_THRESHOLD)) != 0) {
HIVIEW_LOGE("key error");
return;
}
int32_t threshold = StringUtil::StrToInt(value);
if (threshold < 0) {
HIVIEW_LOGW("invalid threshold: %{public}s", value);
return;
}
TraceCacheMonitor *monitor = static_cast<TraceCacheMonitor *>(context);
monitor->SetLowMemThreshold(threshold);
HIVIEW_LOGI("set low mem threshold to %{public}d", threshold);
}
} // namespace
TraceCacheMonitor::TraceCacheMonitor(int32_t lowMemThreshold)
TraceCacheMonitor::TraceCacheMonitor()
{
collector_ = UCollectUtil::MemoryCollector::Create();
CollectResult<SysMemory> data = collector_->CollectSysMemory();
if (lowMemThreshold > data.data.memTotal) {
lowMemThreshold_ = data.data.memTotal;
} else {
lowMemThreshold_ = lowMemThreshold;
}
lowMemThreshold_ = HIVIEW_CACHE_LOW_MEM_THRESHOLD;
int32_t ret = Parameter::WatchParamChange(PARAM_KEY_CACHE_LOW_MEM_THRESHOLD, OnLowMemThresholdChange, this);
HIVIEW_LOGI("watchParamChange ret: %{public}d", ret);
}
TraceCacheMonitor::~TraceCacheMonitor()
{
if (isCacheOn_) {
OHOS::HiviewDFX::Hitrace::CacheTraceOff();
SetCacheOff();
}
}
void TraceCacheMonitor::SetCacheOn()
void TraceCacheMonitor::SetLowMemThreshold(int32_t threshold)
{
OHOS::HiviewDFX::Hitrace::TraceErrorCode ret = OHOS::HiviewDFX::Hitrace::CacheTraceOn(HITRACE_CACHE_FILE_SIZE_LIMIT,
HITRACE_CACHE_FILE_SLICE_SPAN);
isCacheOn_ = (ret == OHOS::HiviewDFX::Hitrace::TraceErrorCode::SUCCESS);
cacheDuration_ = 0;
cacheOffCountdown_ = CACHE_OFF_CONDITION_COUNTDOWN;
lowMemThreshold_ = threshold;
}
void TraceCacheMonitor::SetCacheOff()
{
isCacheOn_ = false;
OHOS::HiviewDFX::Hitrace::CacheTraceOff();
}
void TraceCacheMonitor::CountDownCacheOff()
{
cacheOffCountdown_--;
if (cacheOffCountdown_ <= 0) {
isCacheOn_ = false;
OHOS::HiviewDFX::Hitrace::CacheTraceOff();
}
}
bool TraceCacheMonitor::IsLowMemState()
void TraceCacheMonitor::RunMonitorLoop()
{
CollectResult<SysMemory> data = collector_->CollectSysMemory();
return data.data.memAvailable < lowMemThreshold_;
if (data.retCode != UCollect::UcError::SUCCESS || data.data.memTotal < lowMemThreshold_) {
HIVIEW_LOGW("monitor task prerequisites not met, memory collection ret: %{public}d, "
"threshold: %{public}d, total memory: %{public}d",
data.retCode, lowMemThreshold_, data.data.memTotal);
return;
}
std::lock_guard<std::mutex> lock(stateMutex_);
if (monitorState_ != EXIT) {
HIVIEW_LOGW("monitorLoop is already running");
return;
}
HIVIEW_LOGI("start hiview monitor task");
monitorState_ = RUNNING;
auto task = [this] { this->MonitorFfrtTask(); };
ffrt::submit(task, {}, {}, ffrt::task_attr().name("dft_uc_Monitor"));
}
bool TraceCacheMonitor::UseCacheTimeQuota(int32_t interval)
void TraceCacheMonitor::ExitMonitorLoop()
{
BehaviorRecord record;
record.behaviorId = CACHE_LOW_MEM;
record.dateNum = TimeUtil::TimestampFormatToDate(TimeUtil::GetSeconds(), "%Y%m%d");
if (!behaviorController_.GetRecord(record) && !behaviorController_.InsertRecord(record)) {
HIVIEW_LOGW("Failed to get and insert record");
return false;
std::lock_guard<std::mutex> lock(stateMutex_);
if (monitorState_ == RUNNING) {
HIVIEW_LOGI("interrupting monitor running state.");
monitorState_ = INTERRUPT;
} else {
HIVIEW_LOGW("monitor is not in running state, exit.");
}
if (record.usedQuota >= HITRACE_CACHE_DURATION_LIMIT_DAILY_TOTAL) {
HIVIEW_LOGW("UsedQuota exceeds daily limit.");
return false;
}
void TraceCacheMonitor::MonitorFfrtTask()
{
while (monitorState_ == RUNNING) {
RunMonitorCycle(MONITOR_INTERVAL);
}
record.usedQuota += interval;
behaviorController_.UpdateRecord(record);
return true;
std::lock_guard<std::mutex> lock(stateMutex_);
if (monitorState_ == INTERRUPT) {
monitorState_ = EXIT;
}
HIVIEW_LOGI("exit hiview monitor task");
}
void TraceCacheMonitor::RunMonitorCycle(int32_t interval)
{
bool isTargetCacheOn = IsLowMemState();
if (isWaitingForNormal_) {
if (isWaitingForRecovery_) {
if (isTargetCacheOn) {
ffrt::this_task::sleep_for(std::chrono::seconds(interval));
return;
} else {
isWaitingForNormal_ = false;
isWaitingForRecovery_ = false;
}
}
@ -154,17 +177,86 @@ void TraceCacheMonitor::RunMonitorCycle(int32_t interval)
int32_t timeDiff = static_cast<int32_t>((endTime - startTime) / S_TO_NS);
if (!UseCacheTimeQuota(timeDiff)) {
SetCacheOff();
HIVIEW_LOGW("Quota exceeded, sleep until the next day");
HIVIEW_LOGW("quota exceeded, sleep until the next day");
ffrt::this_task::sleep_until(GetNextDay());
} else {
cacheDuration_ += timeDiff;
if (cacheDuration_ >= HITRACE_CACHE_DURATION_LIMIT_PER_EVENT) {
SetCacheOff();
HIVIEW_LOGW("Reach cache duration limit, wait until returning to normal state");
isWaitingForNormal_ = true;
HIVIEW_LOGW("reach cache duration limit, wait for system to recover from low mem state");
isWaitingForRecovery_ = true;
}
}
}
bool TraceCacheMonitor::IsLowMemState()
{
CollectResult<SysMemory> data = collector_->CollectSysMemory();
return (data.retCode == UCollect::UcError::SUCCESS) && (data.data.memAvailable < lowMemThreshold_);
}
void TraceCacheMonitor::SetCacheOn()
{
OHOS::HiviewDFX::Hitrace::TraceErrorCode ret = OHOS::HiviewDFX::Hitrace::CacheTraceOn(
HITRACE_CACHE_FILE_SIZE_LIMIT, HITRACE_CACHE_FILE_SLICE_SPAN);
isCacheOn_ = (ret == OHOS::HiviewDFX::Hitrace::TraceErrorCode::SUCCESS);
cacheDuration_ = 0;
cacheOffCountdown_ = CACHE_OFF_CONDITION_COUNTDOWN;
}
void TraceCacheMonitor::SetCacheOff()
{
isCacheOn_ = false;
OHOS::HiviewDFX::Hitrace::CacheTraceOff();
}
void TraceCacheMonitor::CountDownCacheOff()
{
cacheOffCountdown_--;
if (cacheOffCountdown_ <= 0) { // two cycles above threshold to turn off cache
isCacheOn_ = false;
OHOS::HiviewDFX::Hitrace::CacheTraceOff();
}
}
bool TraceCacheMonitor::UseCacheTimeQuota(int32_t interval)
{
auto behaviorDbHelper = std::make_shared<TraceBehaviorDbHelper>();
BehaviorRecord record;
record.behaviorId = CACHE_LOW_MEM;
record.dateNum = TimeUtil::TimestampFormatToDate(TimeUtil::GetSeconds(), "%Y%m%d");
if (!behaviorDbHelper->GetRecord(record) && !behaviorDbHelper->InsertRecord(record)) {
HIVIEW_LOGE("failed to get and insert record, close task");
ExitMonitorLoop();
return false;
}
if (record.usedQuota >= HITRACE_CACHE_DURATION_LIMIT_DAILY_TOTAL) {
HIVIEW_LOGW("usedQuota exceeds daily limit.");
return false;
}
record.usedQuota += interval;
behaviorDbHelper->UpdateRecord(record);
return true;
}
#else
TraceCacheMonitor::TraceCacheMonitor()
{
}
TraceCacheMonitor::~TraceCacheMonitor()
{
}
void TraceCacheMonitor::RunMonitorLoop()
{
HIVIEW_LOGW("monitor feature Not Enabled.");
}
void TraceCacheMonitor::ExitMonitorLoop()
{
HIVIEW_LOGW("monitor feature Not Enabled.");
}
#endif // HIVIEW_LOW_MEM_THRESHOLD
} // namespace HiviewDFX
} // namespace OHOS

View File

@ -24,7 +24,6 @@
#include "sql_util.h"
#include "string_util.h"
#include "trace_storage.h"
#include "trace_behavior_controller.h"
using namespace OHOS::HiviewDFX::UCollectUtil;

View File

@ -16,18 +16,14 @@
"OHOS::HiviewDFX::UCollectUtil::ProcessCollector::Create()";
"OHOS::HiviewDFX::UCollectUtil::ThreadCpuCollector::Create(int, bool)";
"OHOS::HiviewDFX::UCollectUtil::GraphicMemoryCollector::Create()";
"OHOS::HiviewDFX::TraceBehaviorController::TraceBehaviorController()";
"OHOS::HiviewDFX::TraceBehaviorController::GetRecord(OHOS::HiviewDFX::BehaviorRecord&)";
"OHOS::HiviewDFX::TraceBehaviorController::InsertRecord(OHOS::HiviewDFX::BehaviorRecord&)";
"OHOS::HiviewDFX::TraceBehaviorController::UpdateRecord(OHOS::HiviewDFX::BehaviorRecord&)";
"OHOS::HiviewDFX::TraceFlowController::AddNewFinishTask(std::__h::shared_ptr<OHOS::HiviewDFX::AppCallerEvent>)";
"OHOS::HiviewDFX::TraceFlowController::CleanAppTrace()";
"OHOS::HiviewDFX::TraceFlowController::HasCallOnceToday(int, unsigned long)";
"OHOS::HiviewDFX::TraceFlowController::TraceFlowController()";
"OHOS::HiviewDFX::TraceCacheMonitor::TraceCacheMonitor(int)";
"OHOS::HiviewDFX::TraceCacheMonitor::TraceCacheMonitor()";
"OHOS::HiviewDFX::TraceCacheMonitor::~TraceCacheMonitor()";
"OHOS::HiviewDFX::TraceCacheMonitor::RunMonitorCycle(int)";
"OHOS::HiviewDFX::TraceCacheMonitor::UseCacheTimeQuota(int)";
"OHOS::HiviewDFX::TraceCacheMonitor::RunMonitorLoop()";
"OHOS::HiviewDFX::TraceCacheMonitor::ExitMonitorLoop()";
"OHOS::HiviewDFX::TraceWorker::GetInstance()";
"OHOS::HiviewDFX::TraceWorker::HandleUcollectionTask(std::__h::function<void ()>)";
"OHOS::HiviewDFX::UCollectUtil::ProcessStatus::GetProcessLastForegroundTime(int)";

View File

@ -23,6 +23,7 @@
#include "cpu_collection_task.h"
#include "plugin.h"
#include "sys_event.h"
#include "trace_cache_monitor.h"
#include "unified_collection_stat.h"
namespace OHOS {
@ -48,9 +49,8 @@ private:
void RunUCollectionStatTask();
void IoCollectionTask();
void UCollectionStatTask();
void RunHiviewMonitorTask();
void ExitHiviewMonitorTask();
void HiviewPerfMonitorFfrtTask();
void RunCacheMonitorLoop();
void ExitCacheMonitorLoop();
void CleanDataFiles();
void OnMainThreadJank(SysEvent& sysEvent);
bool OnStartAppTrace(std::shared_ptr<AppCallerEvent> appCallerEvent);
@ -62,10 +62,9 @@ private:
private:
std::string workPath_;
std::shared_ptr<CpuCollectionTask> cpuCollectionTask_;
std::shared_ptr<TraceCacheMonitor> traceCacheMonitor_;
std::list<uint64_t> taskList_;
volatile bool isCpuTaskRunning_;
std::atomic<bool> isHiviewPerfMonitorRunning_ = false;
std::atomic<bool> isHiviewPerfMonitorExit_ = true;
static bool isEnableRecordTrace_;
std::shared_ptr<AppTraceContext> appTraceContext_;
}; // UnifiedCollector

View File

@ -32,19 +32,11 @@
#include "process_status.h"
#include "sys_event.h"
#include "time_util.h"
#include "trace_behavior_controller.h"
#include "trace_cache_monitor.h"
#include "trace_flow_controller.h"
#include "trace_manager.h"
#include "uc_observer_mgr.h"
#include "unified_collection_stat.h"
#if defined(HIVIEW_LOW_MEM_THRESHOLD) && (HIVIEW_LOW_MEM_THRESHOLD > 0)
constexpr int32_t HIVIEW_CACHE_LOW_MEM_THRESHOLD = HIVIEW_LOW_MEM_THRESHOLD;
#else
constexpr int32_t HIVIEW_CACHE_LOW_MEM_THRESHOLD = 0;
#endif
namespace OHOS {
namespace HiviewDFX {
REGISTER(UnifiedCollector);
@ -68,7 +60,6 @@ const std::string DEVELOP_TRACE_RECORDER_TRUE = "true";
const std::string DEVELOP_TRACE_RECORDER_FALSE = "false";
const std::string HIVIEW_UCOLLECTION_TEST_APP_TRACE_STATE_TRUE = "true";
constexpr char KEY_FREEZE_DETECTOR_STATE[] = "persist.hiview.freeze_detector";
constexpr int32_t HIVEW_PERF_MONITOR_INTERVAL = 5;
const int8_t STATE_COUNT = 2;
const int8_t COML_STATE = 0;
@ -343,9 +334,7 @@ void UnifiedCollector::Init()
RunIoCollectionTask();
RunUCollectionStatTask();
LoadHitraceService();
#if defined(HIVIEW_LOW_MEM_THRESHOLD) && (HIVIEW_LOW_MEM_THRESHOLD > 0)
RunHiviewMonitorTask();
#endif
RunCacheMonitorLoop();
}
if (isAllowCollect || Parameter::IsDeveloperMode()) {
RunCpuCollectionTask();
@ -401,9 +390,7 @@ void UnifiedCollector::OnSwitchStateChanged(const char* key, const char* value,
unifiedCollectorPtr->RunCpuCollectionTask();
unifiedCollectorPtr->RunIoCollectionTask();
unifiedCollectorPtr->RunUCollectionStatTask();
#if defined(HIVIEW_LOW_MEM_THRESHOLD) && (HIVIEW_LOW_MEM_THRESHOLD > 0)
unifiedCollectorPtr->RunHiviewMonitorTask();
#endif
unifiedCollectorPtr->RunCacheMonitorLoop();
LoadHitraceService();
} else {
isUCollectionSwitchOn = false;
@ -414,9 +401,7 @@ void UnifiedCollector::OnSwitchStateChanged(const char* key, const char* value,
unifiedCollectorPtr->workLoop_->RemoveEvent(it);
}
unifiedCollectorPtr->taskList_.clear();
#if defined(HIVIEW_LOW_MEM_THRESHOLD) && (HIVIEW_LOW_MEM_THRESHOLD > 0)
unifiedCollectorPtr->ExitHiviewMonitorTask();
#endif
unifiedCollectorPtr->ExitCacheMonitorLoop();
ExitHitraceService();
unifiedCollectorPtr->CleanDataFiles();
}
@ -527,34 +512,19 @@ void UnifiedCollector::RunRecordTraceTask()
HIVIEW_LOGI("add ucollection trace switch param watcher ret: %{public}d", ret);
}
#if defined(HIVIEW_LOW_MEM_THRESHOLD) && (HIVIEW_LOW_MEM_THRESHOLD > 0)
void UnifiedCollector::RunHiviewMonitorTask()
void UnifiedCollector::RunCacheMonitorLoop()
{
if (workPath_.empty() || !isHiviewPerfMonitorExit_.load() || HIVIEW_CACHE_LOW_MEM_THRESHOLD == 0) {
HIVIEW_LOGW("Hiview Monitor Task prerequisites are not met.");
return;
if (traceCacheMonitor_ == nullptr) {
traceCacheMonitor_ = std::make_shared<TraceCacheMonitor>();
}
isHiviewPerfMonitorRunning_.store(true);
auto task = [this] { this->HiviewPerfMonitorFfrtTask(); };
isHiviewPerfMonitorExit_.store(false);
ffrt::submit(task, {}, {}, ffrt::task_attr().name("dft_uc_Monitor"));
traceCacheMonitor_->RunMonitorLoop();
}
void UnifiedCollector::ExitHiviewMonitorTask()
void UnifiedCollector::ExitCacheMonitorLoop()
{
isHiviewPerfMonitorRunning_.store(false);
}
void UnifiedCollector::HiviewPerfMonitorFfrtTask()
{
std::shared_ptr<TraceCacheMonitor> traceCacheMonitor =
std::make_shared<TraceCacheMonitor>(HIVIEW_CACHE_LOW_MEM_THRESHOLD);
while (isHiviewPerfMonitorRunning_.load()) {
traceCacheMonitor->RunMonitorCycle(HIVEW_PERF_MONITOR_INTERVAL);
if (traceCacheMonitor_ != nullptr) {
traceCacheMonitor_->ExitMonitorLoop();
}
isHiviewPerfMonitorExit_.store(true);
HIVIEW_LOGW("exit hiview monitor task");
}
#endif // HIVIEW_LOW_MEM_THRESHOLD
} // namespace HiviewDFX
} // namespace OHOS