106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include <iostream>
1606f6ba60Sopenharmony_ci#include <sstream>
1706f6ba60Sopenharmony_ci#include <thread>
1806f6ba60Sopenharmony_ci#include "unistd.h"
1906f6ba60Sopenharmony_ci#include "include/sp_utils.h"
2006f6ba60Sopenharmony_ci#include "include/ByTrace.h"
2106f6ba60Sopenharmony_ci#include "include/sp_log.h"
2206f6ba60Sopenharmony_ci#include "include/common.h"
2306f6ba60Sopenharmony_cinamespace OHOS {
2406f6ba60Sopenharmony_cinamespace SmartPerf {
2506f6ba60Sopenharmony_civoid ByTrace::SetTraceConfig(int mSum, int mInterval, long long mThreshold, int mLowfps, int mCurNum) const
2606f6ba60Sopenharmony_ci{
2706f6ba60Sopenharmony_ci    sum = mSum;
2806f6ba60Sopenharmony_ci    interval = mInterval;
2906f6ba60Sopenharmony_ci    threshold = mThreshold;
3006f6ba60Sopenharmony_ci    lowfps = mLowfps;
3106f6ba60Sopenharmony_ci    curNum = mCurNum;
3206f6ba60Sopenharmony_ci    LOGI("ByTrace::SetTraceConfig mSum(%d) mInterval(%d) mThreshold(%lld) mLowfps(%d) mCurNum(%d)", mSum, mInterval,
3306f6ba60Sopenharmony_ci        mThreshold, mLowfps, mCurNum);
3406f6ba60Sopenharmony_ci}
3506f6ba60Sopenharmony_civoid ByTrace::ThreadGetTrace() const
3606f6ba60Sopenharmony_ci{
3706f6ba60Sopenharmony_ci    std::string result;
3806f6ba60Sopenharmony_ci    std::string cmdString;
3906f6ba60Sopenharmony_ci    if (SPUtils::IsHmKernel()) {
4006f6ba60Sopenharmony_ci        cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_1024);
4106f6ba60Sopenharmony_ci    } else {
4206f6ba60Sopenharmony_ci        cmdString = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_2048);
4306f6ba60Sopenharmony_ci    }
4406f6ba60Sopenharmony_ci    std::string time = std::to_string(SPUtils::GetCurTime());
4506f6ba60Sopenharmony_ci    std::string traceFile = "/data/local/tmp/sptrace_" + time + ".ftrace";
4606f6ba60Sopenharmony_ci    std::string traceCmdExe = cmdString + traceFile;
4706f6ba60Sopenharmony_ci    SPUtils::LoadCmd(traceCmdExe, result);
4806f6ba60Sopenharmony_ci    LOGI("TRACE threadGetTrace  CMD(%s)", traceCmdExe.c_str());
4906f6ba60Sopenharmony_ci}
5006f6ba60Sopenharmony_ciTraceStatus ByTrace::CheckFpsJitters(std::vector<long long> jitters, int cfps) const
5106f6ba60Sopenharmony_ci{
5206f6ba60Sopenharmony_ci    times++;
5306f6ba60Sopenharmony_ci    int two = 2;
5406f6ba60Sopenharmony_ci    long long curTime = SPUtils::GetCurTime();
5506f6ba60Sopenharmony_ci    if (curNum <= sum && currentTrigger < 0 && times > two) {
5606f6ba60Sopenharmony_ci        for (size_t i = 0; i < jitters.size(); i++) {
5706f6ba60Sopenharmony_ci            long long normalJitter = jitters[i] / 1e6;
5806f6ba60Sopenharmony_ci            if (normalJitter > threshold || cfps < lowfps) {
5906f6ba60Sopenharmony_ci                TriggerCatch(curTime);
6006f6ba60Sopenharmony_ci            }
6106f6ba60Sopenharmony_ci        }
6206f6ba60Sopenharmony_ci    }
6306f6ba60Sopenharmony_ci    if ((curTime - lastTriggerTime) / 1e3 > interval && currentTrigger == 1) {
6406f6ba60Sopenharmony_ci        currentTrigger = -1;
6506f6ba60Sopenharmony_ci    }
6606f6ba60Sopenharmony_ci    return TraceStatus::TRACE_FINISH;
6706f6ba60Sopenharmony_ci}
6806f6ba60Sopenharmony_civoid ByTrace::TriggerCatch(long long curTime) const
6906f6ba60Sopenharmony_ci{
7006f6ba60Sopenharmony_ci    if ((curTime - lastTriggerTime) / 1e3 > interval && !CheckHitraceId()) {
7106f6ba60Sopenharmony_ci        std::thread tStart([this] { this->ThreadGetTrace(); });
7206f6ba60Sopenharmony_ci        currentTrigger = 1;
7306f6ba60Sopenharmony_ci        lastTriggerTime = curTime;
7406f6ba60Sopenharmony_ci        curNum++;
7506f6ba60Sopenharmony_ci        tStart.detach();
7606f6ba60Sopenharmony_ci    }
7706f6ba60Sopenharmony_ci}
7806f6ba60Sopenharmony_ci
7906f6ba60Sopenharmony_cibool ByTrace::CheckHitraceId() const
8006f6ba60Sopenharmony_ci{
8106f6ba60Sopenharmony_ci    std::string result;
8206f6ba60Sopenharmony_ci    std::string hitrace = CMD_COMMAND_MAP.at(CmdCommand::HITRACE_CMD);
8306f6ba60Sopenharmony_ci    SPUtils::LoadCmd(hitrace, result);
8406f6ba60Sopenharmony_ci    if (result.empty()) {
8506f6ba60Sopenharmony_ci        return false;
8606f6ba60Sopenharmony_ci    }
8706f6ba60Sopenharmony_ci    if (result.find("-t") != std::string::npos) {
8806f6ba60Sopenharmony_ci        return true;
8906f6ba60Sopenharmony_ci    }
9006f6ba60Sopenharmony_ci    return false;
9106f6ba60Sopenharmony_ci}
9206f6ba60Sopenharmony_ci}
9306f6ba60Sopenharmony_ci}
94