* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* 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 FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
#include "gtest/gtest.h"
#include "mockcpp/mockcpp.hpp"
#include "device_simulator_manager.h"
#include "errno/error_code.h"
#include "msprof_start.h"
#include "../stub/cli_stub.h"
#include "mmpa_api.h"
#include "data_manager.h"
#include "aicpu_report_hdc.h"
#include "devprof_drv_aicpu.h"
#include "input_parser.h"
using namespace analysis::dvvp::common::error;
using namespace Cann::Dvvp::Test;
static const char DC_RM_RF[] = "rm -rf ./cliDcstest_workspace";
static const char DC_MKDIR[] = "mkdir ./cliDcstest_workspace";
static const char DC_OUTPUT_DIR[] = "--output=./cliDcstest_workspace/output";
class CliDcStest: public testing::Test {
protected:
virtual void SetUp()
{
DlStub();
const ::testing::TestInfo* curTest = ::testing::UnitTest::GetInstance()->current_test_info();
DataMgr().Init("", "acljson");
optind = 1;
system(DC_MKDIR);
system("touch ./cli");
MOCKER_CPP(&AicpuReportHdc::Init).stubs().will(returnValue(-1));
EXPECT_EQ(2, SimulatorMgr().CreateDeviceSimulator(2, StPlatformType::DC_TYPE));
SimulatorMgr().SetSocSide(SocType::HOST);
}
virtual void TearDown()
{
GlobalMockObject::verify();
DevprofDrvAicpu::instance()->isRegister_ = false;
EXPECT_EQ(2, SimulatorMgr().DelDeviceSimulator(2, StPlatformType::DC_TYPE));
system(DC_RM_RF);
system("rm -rf ./cli");
DataMgr().UnInit();
MsprofMgr().UnInit();
}
void DlStub()
{
MOCKER(dlopen).stubs().will(invoke(mmDlopen));
MOCKER(dlsym).stubs().will(invoke(mmDlsym));
MOCKER(dlclose).stubs().will(invoke(mmDlclose));
MOCKER(dlerror).stubs().will(invoke(mmDlerror));
}
};
TEST_F(CliDcStest, CliDefault)
{
const char* argv[] = {DC_OUTPUT_DIR,};
std::vector<std::string> deviceDataList = {"hwts.data", "ts_track.data", "aicore.data"};
MsprofMgr().SetDeviceCheckList(deviceDataList);
std::vector<std::string> hostDataList = {
"unaging.api_event.data", "unaging.compact.node_basic_info", "unaging.compact.task_track", "unaging.additional.context_id_info"
};
MsprofMgr().SetHostCheckList(hostDataList);
std::vector<uint64_t> bitList = {PROF_ACL_API, PROF_TASK_TIME_L1, PROF_AICORE_METRICS};
MsprofMgr().SetBitSwitchCheckList(bitList);
EXPECT_EQ(PROFILING_SUCCESS, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
}
TEST_F(CliDcStest, CliTx)
{
const char* argv[] = {DC_OUTPUT_DIR, "--msproftx=on"};
std::vector<std::string> hostDataList = {"aging.additional.msproftx"};
MsprofMgr().SetHostCheckList(hostDataList);
MsprofMgr().SetMsprofTx(true);
EXPECT_EQ(PROFILING_SUCCESS, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
MsprofMgr().SetMsprofTx(false);
}
TEST_F(CliDcStest, CliDataAicpuReportDataMC2)
{
const char* argv[] = {DC_OUTPUT_DIR, "--task-time=l1",};
std::vector<std::string> dataList = {"aicpu.data"};
MsprofMgr().SetDeviceCheckList(dataList);
MsprofMgr().SetSleepTime(100);
EXPECT_EQ(PROFILING_SUCCESS, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
GlobalMockObject::verify();
}
TEST_F(CliDcStest, CliDynamicError)
{
const char* argv[] = {DC_OUTPUT_DIR, "--dynamic=on",};
MOCKER_CPP(&Analysis::Dvvp::Msprof::InputParser::CheckDynProfValid)
.stubs()
.will(returnValue(-1));
MsprofMgr().SetSleepTime(100);
EXPECT_EQ(PROFILING_FAILED, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
GlobalMockObject::verify();
}
TEST_F(CliDcStest, CliDynamicSysDevice)
{
const char* argv[] = {DC_OUTPUT_DIR, "--dynamic=on", "--sys-devices=0"};
EXPECT_EQ(PROFILING_FAILED, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
GlobalMockObject::verify();
}
TEST_F(CliDcStest, CliDynamicSysPeriod)
{
const char* argv[] = {DC_OUTPUT_DIR, "--dynamic=on", "--sys-period=10"};
EXPECT_EQ(PROFILING_FAILED, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
GlobalMockObject::verify();
}
TEST_F(CliDcStest, CliDynamicSysCpuProfiling)
{
const char* argv[] = {DC_OUTPUT_DIR, "--dynamic=on", "--sys-cpu-profiling=on"};
EXPECT_EQ(PROFILING_FAILED, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
GlobalMockObject::verify();
}
TEST_F(CliDcStest, CliL2)
{
const char* argv[] = {DC_OUTPUT_DIR, "--l2=on"};
std::vector<std::string> dataList = {"l2_cache.data"};
std::vector<std::string> blackDataList = {"socpmu.data"};
MsprofMgr().SetDeviceCheckList(dataList, blackDataList);
EXPECT_EQ(PROFILING_SUCCESS, MsprofMgr().MsprofStartByAppMode(sizeof(argv) / sizeof(char *), argv));
GlobalMockObject::verify();
}