* -------------------------------------------------------------------------
* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*/
#ifndef PROFILER_SERVER_MEMSNAPSHOTPARSER_H
#define PROFILER_SERVER_MEMSNAPSHOTPARSER_H
#include "pch.h"
#include "TimelineProtocolEvent.h"
#include "MemScopeProtocolEvent.h"
#include "MemSnapshotProtocolEvent.h"
#include "ThreadPool.h"
namespace Dic::Module {
enum class ParserState {
INIT = -1,
Loading = 0,
Processing = 1,
FINISH_SUCCESS = 2,
FINISH_FAILURE = 3,
UP_TO_DATE = 4,
};
struct MemSnapshotParserContext {
public:
MemSnapshotParserContext() = default;
void Reset(std::string nPicklePath = "", std::string nLogPath = "", std::string nOutputPath = "");
bool IsFinished() const;
bool IsReadyToParse() const;
std::string GetPicklePath() const;
std::string GetLogPath() const;
std::string GetOutputDbPath() const;
ParserState GetState() const;
void SetState(const ParserState &newState);
uint8_t GetProgress() const;
void SetProgress(uint8_t newProgress);
std::string GetWorkDir() const;
static void SendParseProgressEvent(int progress);
private:
std::string picklePath{};
std::string logPath{};
std::string outputDbPath{};
ParserState state{ParserState::INIT};
std::string workDir{};
uint8_t progress{0};
mutable std::shared_mutex _mutex{};
};
class MemSnapshotParser {
public:
MemSnapshotParser(const MemSnapshotParser &) = delete;
MemSnapshotParser &operator=(const MemSnapshotParser &) = delete;
static MemSnapshotParser &Instance();
void Reset();
void AsyncParseMemSnapshotPickle(const std::string &pickleFilePath);
MemSnapshotParserContext &GetParseContext();
static bool CheckIfParsingNeed(const MemSnapshotParserContext &context);
private:
MemSnapshotParser();
~MemSnapshotParser();
static void ParseMemSnapshotTask();
static void ParseDaemonTask();
static void ParseCallBack();
bool TryOpenParsingResultDbAndSetVersion() const;
std::unique_ptr<MemScopeParseSuccessEvent> BuildParseSuccessEventFromContext() const;
std::unique_ptr<ParseFailEvent> BuildParseFailEventFromContext(const std::string &errMsg) const;
std::unique_ptr<ThreadPool> _threadPool;
MemSnapshotParserContext parseContext = MemSnapshotParserContext();
};
}
#endif