1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "request_tracer.h"
17 #include <atomic>
18 #if HAS_NETMANAGER_BASE
19 #include "hitrace_meter.h"
20 #endif
21
22 OHOS::NetStack::RequestTracer::Trace::~Trace() = default;
23
24
Trace(const std::string & className,int32_t idNum)25 OHOS::NetStack::RequestTracer::Trace::Trace(const std::string &className, int32_t idNum)
26 : className_(className),
27 stageName_(std::nullopt),
28 idNum_(idNum)
29 {
30 }
31
GenerateId()32 int32_t OHOS::NetStack::RequestTracer::Trace::GenerateId()
33 {
34 static std::atomic_int32_t idNum = 0;
35 return ++idNum;
36 }
Trace(const std::string & className)37 OHOS::NetStack::RequestTracer::Trace::Trace(const std::string &className) : Trace(className, GenerateId())
38 {
39 }
40
Tracepoint(const std::string & stage)41 void OHOS::NetStack::RequestTracer::Trace::Tracepoint(const std::string &stage)
42 {
43 if (stage == stageName_) {
44 return;
45 }
46 Finish();
47 stageName_ = stage;
48 #if HAS_NETMANAGER_BASE
49 StartAsyncTrace(HITRACE_TAG_NET, className_ + "$$" + *stageName_, idNum_);
50 #endif
51 }
52
Finish()53 void OHOS::NetStack::RequestTracer::Trace::Finish()
54 {
55 #if HAS_NETMANAGER_BASE
56 if (stageName_) {
57 FinishAsyncTrace(HITRACE_TAG_NET, className_ + "$$" + *stageName_, idNum_);
58 }
59 #endif
60 }