fix error

Signed-off-by: zyxzyx <zhangyixin19@huawei.com>
This commit is contained in:
zyxzyx 2024-11-15 17:38:10 +08:00
parent e1f11bfc73
commit 38680882ee
7 changed files with 48 additions and 18 deletions

View File

@ -135,6 +135,7 @@ public:
private:
int pid_ = -1; // for SA mode
bool printMallocNmd_ = false;
bool nmdOnly_ = false;
bool saMode_ = false;
bool CheckProcess();
bool CheckProcessName();

View File

@ -65,6 +65,10 @@ public:
{
pid_ = pid;
}
inline void SetNmdOnly(bool nmdOnly)
{
nmdOnly_ = nmdOnly;
}
inline void InitStatisticsTime()
{
lastStatisticsTime_ = std::chrono::steady_clock::now();
@ -241,6 +245,7 @@ private:
std::atomic<uint64_t> napiIndex_{1};
ScheduleTaskManager scheduleTaskManager_;
uint32_t nmdFd_ = 0;
bool nmdOnly_ = false;
};
#endif // STACK_PREPROCESS_H

View File

@ -24,6 +24,7 @@ namespace {
constexpr uint32_t LIB_SMS = 4096;
constexpr uint32_t CALL_STACK_SMS = 16384;
constexpr uint32_t NMD_DURATION = 5;
constexpr uint32_t NMD_SHMEM = 300;
constexpr int ONLY_NMD = 2;
}
@ -128,8 +129,6 @@ int32_t NativeMemoryProfilerSaClientManager::GetMallocStats(int fd, int pid, int
CHECK_TRUE(type == 0 || type == 1 || type == ONLY_NMD, RET_ERR,
"NativeMemoryProfilerSaClientManager: type is invalid");
std::shared_ptr<NativeMemoryProfilerSaConfig> config = std::make_shared<NativeMemoryProfilerSaConfig>();
CHECK_NOTNULL(config, RET_ERR, "NativeMemoryProfilerSaClientManager: config is nullptr");
CHECK_TRUE(CheckConfig(config), RET_ERR, "CheckConfig failed");
config->printNmd_ = true;
config->printNmdOnly_ = printNmdOnly;
if (config->printNmdOnly_) {
@ -137,9 +136,12 @@ int32_t NativeMemoryProfilerSaClientManager::GetMallocStats(int fd, int pid, int
config->duration_ = NMD_DURATION;
config->mallocDisable_ = true;
config->mmapDisable_ = true;
config->shareMemorySize_ = NMD_SHMEM;
}
config->nmdPid_ = static_cast<uint32_t>(pid);
config->nmdType_ = static_cast<uint32_t>(type);
CHECK_NOTNULL(config, RET_ERR, "NativeMemoryProfilerSaClientManager: config is nullptr");
CHECK_TRUE(CheckConfig(config), RET_ERR, "CheckConfig failed");
auto service = GetRemoteService();
if (service == nullptr) {
PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaClientManager: stop GetRemoteService failed");

View File

@ -225,24 +225,27 @@ int32_t NativeMemoryProfilerSaService::StartHook(std::shared_ptr<NativeMemoryPro
"check config failed");
return RET_ERR;
}
std::shared_ptr<TraceFileWriter> writeFile = nullptr;
if (!config->printNmdOnly_) {
if (fd == 0) {
auto retFile = COMMON::CheckNotExistsFilePath(config->filePath_);
if (!retFile.first) {
PROFILER_LOG_INFO(LOG_CORE, "%s:check file path %s fail", __func__, config->filePath_.c_str());
COMMON::PluginWriteToHisysevent("native_hook_plugin", "hiview", args,
COMMON::ErrorType::RET_IVALID_PATH, "check file path failed");
return RET_ERR;
}
int fdTemp = open(retFile.second.c_str(), O_RDWR | O_CREAT, FILE_MODE);
CHECK_TRUE(fdTemp >= 0, RET_ERR, "Failed to open file(%s)", config->filePath_.c_str());
fd = static_cast<uint32_t>(fdTemp);
if (fd == 0) {
auto retFile = COMMON::CheckNotExistsFilePath(config->filePath_);
if (!retFile.first) {
PROFILER_LOG_INFO(LOG_CORE, "%s:check file path %s fail", __func__, config->filePath_.c_str());
COMMON::PluginWriteToHisysevent("native_hook_plugin", "hiview", args, COMMON::ErrorType::RET_IVALID_PATH,
"check file path failed");
return RET_ERR;
}
writeFile = std::make_shared<TraceFileWriter>(fd);
CHECK_NOTNULL(writeFile, RET_ERR, "Failed to create TraceFileWriter");
writeFile->SetTimeSource();
int fdTemp = open(retFile.second.c_str(), O_RDWR | O_CREAT, FILE_MODE);
CHECK_TRUE(fdTemp >= 0, RET_ERR, "Failed to open file(%s)", config->filePath_.c_str());
fd = static_cast<uint32_t>(fdTemp);
}
std::shared_ptr<TraceFileWriter> writeFile = nullptr;
if (config->printNmdOnly_) {
std::shared_ptr<TraceFileWriter> writeFile = std::make_shared<TraceFileWriter>(0);
} else {
std::shared_ptr<TraceFileWriter> writeFile = std::make_shared<TraceFileWriter>(fd);
}
CHECK_NOTNULL(writeFile, RET_ERR, "Failed to create TraceFileWriter");
writeFile->SetTimeSource();
std::shared_ptr<HookManager> hook = std::make_shared<HookManager>();
CHECK_NOTNULL(hook, RET_ERR, "Failed to create HookManager");

View File

@ -308,6 +308,7 @@ bool HookManager::HandleHookContext(const std::shared_ptr<HookManagerCtx>& ctx)
} else {
ctx->stackPreprocess->SetWriter(const_cast<WriterStructPtr>(writerAdapter_->GetStruct()));
}
ctx->stackPreprocess->SetNmdOnly(nmdOnly_)
return true;
}
@ -730,6 +731,7 @@ void HookManager::SetHookConfig(const std::shared_ptr<NativeMemoryProfilerSaConf
hookConfig_.set_max_js_stack_depth(config->maxJsStackDepth_);
hookConfig_.set_filter_napi_name(config->filterNapiName_);
printMallocNmd_ = config->printNmd_;
nmdOnly_ = config->printNmdOnly_;
}
int32_t HookManager::CreatePluginSession()

View File

@ -1482,6 +1482,9 @@ size_t StackPreprocess::ComputeAlign(size_t size)
void StackPreprocess::WriteHookConfig()
{
if (nmdOnly_) {
return;
}
const size_t configSize = hookConfig_.ByteSizeLong();
auto buffer = std::make_unique<uint8_t[]>(configSize);
hookConfig_.SerializeToArray(buffer.get(), configSize);

View File

@ -46,6 +46,7 @@ TraceFileWriter::TraceFileWriter(const std::string& path) : TraceFileWriter(path
TraceFileWriter::TraceFileWriter(int32_t fd) : fd_(fd)
{
CHECK_TRUE(fd_ != 0, NO_RETVAL, "only-nmd mode, no need to use TraceFileWriter");
if (write(fd_, &header_, sizeof(header_)) != sizeof(header_)) {
PROFILER_LOG_ERROR(LOG_CORE, "write initial header failed!, error: %s", strerror(errno));
}
@ -93,6 +94,7 @@ TraceFileWriter::TraceFileWriter(const std::string& path, bool splitFile, uint32
TraceFileWriter::~TraceFileWriter()
{
CHECK_TRUE(fd_ != 0, NO_RETVAL, "only-nmd mode, no need to use TraceFileWriter");
(void)FlushStream();
if (stream_.is_open()) {
stream_.close();
@ -109,6 +111,7 @@ std::string TraceFileWriter::Path() const
bool TraceFileWriter::SetPluginConfig(const void* data, size_t size)
{
CHECK_TRUE(fd_ != 0, false, "SetPluginConfig, nmd mode no need to use TraceFileWriter");
if (isSplitFile_) {
std::vector<char> configVec;
auto configData = reinterpret_cast<ConstCharPtr>(data);
@ -125,6 +128,7 @@ void TraceFileWriter::WriteStandalonePluginData(
const std::string &pluginName, const std::string &data,
const std::string &pluginVersion)
{
CHECK_TRUE(fd_ != 0, NO_RETVAL, "WriteStandalonePluginData, nmd mode no need to use TraceFileWriter");
LITE::ProfilerPluginData pluginData;
pluginData.set_name(pluginName);
pluginData.set_data(data);
@ -150,6 +154,7 @@ void TraceFileWriter::WriteStandalonePluginData(
void TraceFileWriter::SetTimeStamp()
{
CHECK_TRUE(fd_ != 0, NO_RETVAL, "SetTimeStamp, nmd mode no need to use TraceFileWriter");
header_.data_.boottime = headerDataTime_.boottime;
header_.data_.realtime = headerDataTime_.realtime;
header_.data_.realtimeCoarse = headerDataTime_.realtimeCoarse;
@ -161,6 +166,7 @@ void TraceFileWriter::SetTimeStamp()
void TraceFileWriter::SetTimeSource()
{
CHECK_TRUE(fd_ != 0, NO_RETVAL, "SetTimeSource, nmd mode no need to use TraceFileWriter");
constexpr uint64_t nanoSeconds = 1000000000;
struct timespec ts;
clock_gettime(CLOCK_BOOTTIME, &ts);
@ -185,6 +191,7 @@ void TraceFileWriter::SetTimeSource()
void TraceFileWriter::SetDurationTime()
{
CHECK_TRUE(fd_ != 0, NO_RETVAL, "SetDurationTime, nmd mode no need to use TraceFileWriter");
struct timespec ts;
clock_gettime(CLOCK_BOOTTIME, &ts);
constexpr uint64_t nanoSeconds = 1000000000;
@ -194,6 +201,7 @@ void TraceFileWriter::SetDurationTime()
bool TraceFileWriter::WriteHeader()
{
CHECK_TRUE(fd_ != 0, false, "WriteHeader, nmd-only mode no need to use TraceFileWriter");
LogDiskUsage();
if (isSplitFile_) {
std::string timeStr = COMMON::GetTimeStr();
@ -237,6 +245,7 @@ void TraceFileWriter::DeleteOldSplitFile()
long TraceFileWriter::Write(const void* data, size_t size)
{
CHECK_TRUE(fd_ != 0, 0, "Write, nmd-only mode no need to use TraceFileWriter");
if (isSplitFile_ && !isStop_) {
if (IsSplitFile(size)) {
return -1;
@ -270,6 +279,7 @@ long TraceFileWriter::WriteStandalonePluginFile(const std::string &file,
const std::string &version,
DataType type)
{
CHECK_TRUE(fd_ != 0, 0, "WriteStandalonePluginFile, no need to use TraceFileWriter");
CHECK_TRUE(stream_.is_open(), 0, "binary file %s not open or open failed!", path_.c_str());
auto retFile = COMMON::CheckNotExistsFilePath(file);
if (!retFile.first) {
@ -326,6 +336,7 @@ long TraceFileWriter::WriteStandalonePluginFile(const std::string &file,
bool TraceFileWriter::IsSplitFile(uint32_t size)
{
CHECK_TRUE(fd_ != 0, false, "IsSplitFile, nmd-only mode no need to use TraceFileWriter");
dataSize_ += sizeof(uint32_t) + size;
if (dataSize_ >= splitFileMaxSize_) {
PROFILER_LOG_INFO(LOG_CORE, "need to split the file(%s), data size:%d, size: %d, splitFileMaxSize_:%d",
@ -357,6 +368,7 @@ bool TraceFileWriter::IsSplitFile(uint32_t size)
long TraceFileWriter::Write(const MessageLite& message)
{
CHECK_TRUE(fd_ != 0, 0, "Write, nmd-only mode no need to use TraceFileWriter");
auto size = message.ByteSizeLong();
if (isSplitFile_ && !isStop_) {
if (IsSplitFile(size)) {
@ -373,6 +385,7 @@ long TraceFileWriter::Write(const MessageLite& message)
bool TraceFileWriter::Finish()
{
CHECK_TRUE(fd_ != 0, false, "Finish(), nmd-only mode no need to use TraceFileWriter");
// update header info
helper_.Update(header_);
SetTimeStamp(); // add timestamp in header
@ -408,6 +421,7 @@ bool TraceFileWriter::Finish()
bool TraceFileWriter::Flush()
{
CHECK_TRUE(fd_ != 0, false, "Finish(), nmd-only mode no need to use TraceFileWriter");
return FlushStream();
}