112e714ceSopenharmony_ci/*
212e714ceSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
312e714ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
412e714ceSopenharmony_ci * you may not use this file except in compliance with the License.
512e714ceSopenharmony_ci * You may obtain a copy of the License at
612e714ceSopenharmony_ci *
712e714ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
812e714ceSopenharmony_ci *
912e714ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1012e714ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1112e714ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212e714ceSopenharmony_ci * See the License for the specific language governing permissions and
1312e714ceSopenharmony_ci * limitations under the License.
1412e714ceSopenharmony_ci */
1512e714ceSopenharmony_ci
1612e714ceSopenharmony_ci#ifndef NEURAL_NETWORK_RUNTIME_SCOPED_TRACE_H
1712e714ceSopenharmony_ci#define NEURAL_NETWORK_RUNTIME_SCOPED_TRACE_H
1812e714ceSopenharmony_ci
1912e714ceSopenharmony_ci#include <string>
2012e714ceSopenharmony_ci#include "hitrace/trace.h"
2112e714ceSopenharmony_ci
2212e714ceSopenharmony_ci#define NNRT_TRACE_NAME(name) ScopedTrace ___tracer(name)
2312e714ceSopenharmony_cinamespace OHOS {
2412e714ceSopenharmony_cinamespace NeuralNetworkRuntime {
2512e714ceSopenharmony_ciclass ScopedTrace {
2612e714ceSopenharmony_cipublic:
2712e714ceSopenharmony_ci    inline ScopedTrace(const std::string& name)
2812e714ceSopenharmony_ci    {
2912e714ceSopenharmony_ci        m_name = name;
3012e714ceSopenharmony_ci        HiviewDFX::HiTraceId traceId = HiviewDFX::HiTraceChain::GetId();
3112e714ceSopenharmony_ci        if (traceId.IsValid()) {
3212e714ceSopenharmony_ci            HiviewDFX::HiTraceChain::Tracepoint(HITRACE_TP_GENERAL, traceId, "NNRt Trace start: %s", name.c_str());
3312e714ceSopenharmony_ci        }
3412e714ceSopenharmony_ci    }
3512e714ceSopenharmony_ci
3612e714ceSopenharmony_ci    inline ~ScopedTrace()
3712e714ceSopenharmony_ci    {
3812e714ceSopenharmony_ci        HiviewDFX::HiTraceId traceId = HiviewDFX::HiTraceChain::GetId();
3912e714ceSopenharmony_ci        if (traceId.IsValid()) {
4012e714ceSopenharmony_ci            HiviewDFX::HiTraceChain::Tracepoint(HITRACE_TP_GENERAL, traceId, "NNRt Trace end: %s", m_name.c_str());
4112e714ceSopenharmony_ci        }
4212e714ceSopenharmony_ci    }
4312e714ceSopenharmony_ci
4412e714ceSopenharmony_ciprivate:
4512e714ceSopenharmony_ci    std::string m_name {};
4612e714ceSopenharmony_ci};
4712e714ceSopenharmony_ci} // namespace NeuralNetworkRuntime
4812e714ceSopenharmony_ci} // namespace OHOS
4912e714ceSopenharmony_ci
5012e714ceSopenharmony_ci#endif // NEURAL_NETWORK_RUNTIME_SCOPED_TRACE_H
51