1 /*
2 * Copyright (c) 2021-2022 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 "ecmascript/jobs/hitrace_scope.h"
17
18 #include "ecmascript/jobs/pending_job.h"
19 #include "hitrace/trace.h"
20
21 namespace panda::ecmascript::job {
EnqueueJobScope(const JSHandle<PendingJob> & pendingJob,QueueType queueType)22 EnqueueJobScope::EnqueueJobScope(const JSHandle<PendingJob> &pendingJob, QueueType queueType)
23 {
24 HiTraceId id = HiTraceChain::GetId();
25 if (id.IsValid() && id.IsFlagEnabled(HITRACE_FLAG_INCLUDE_ASYNC)) {
26 HiTraceId childId = HiTraceChain::CreateSpan();
27
28 pendingJob->SetChainId(childId.GetChainId());
29 pendingJob->SetSpanId(childId.GetSpanId());
30 pendingJob->SetParentSpanId(childId.GetParentSpanId());
31 pendingJob->SetFlags(childId.GetFlags());
32
33 if (id.IsFlagEnabled(HITRACE_FLAG_TP_INFO)) {
34 if (queueType == QueueType::QUEUE_PROMISE) {
35 HiTraceChain::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_CS,
36 childId, "Queue type:%s", "Promise queue");
37 } else if (queueType == QueueType::QUEUE_SCRIPT) {
38 HiTraceChain::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_CS,
39 childId, "Queue type:%s", "Script queue");
40 } else {
41 HiTraceChain::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_CS,
42 childId, "Queue type:%s", "Other queue");
43 }
44 }
45 }
46 }
47
~EnqueueJobScope()48 EnqueueJobScope::~EnqueueJobScope()
49 {
50 }
51
ExecuteJobScope(const JSHandle<PendingJob> & pendingJob)52 ExecuteJobScope::ExecuteJobScope(const JSHandle<PendingJob> &pendingJob)
53 {
54 saveId_ = HiTraceChain::GetId();
55 if (saveId_.IsValid()) {
56 HiTraceChain::ClearId();
57 }
58 if (pendingJob->GetChainId() != 0) {
59 hitraceId_.SetChainId(pendingJob->GetChainId());
60 hitraceId_.SetSpanId(pendingJob->GetSpanId());
61 hitraceId_.SetParentSpanId(pendingJob->GetParentSpanId());
62 hitraceId_.SetFlags(pendingJob->GetFlags());
63
64 if (hitraceId_.IsValid()) {
65 HiTraceChain::SetId(hitraceId_);
66 if (hitraceId_.IsFlagEnabled(HITRACE_FLAG_TP_INFO)) {
67 HiTraceChain::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_SR,
68 hitraceId_, "Before %s pending job execute", "Promise");
69 }
70 }
71 }
72 }
73
~ExecuteJobScope()74 ExecuteJobScope::~ExecuteJobScope()
75 {
76 if (hitraceId_.IsValid()) {
77 if (hitraceId_.IsFlagEnabled(HITRACE_FLAG_TP_INFO)) {
78 HiTraceChain::Tracepoint(HITRACE_CM_THREAD, HITRACE_TP_SS,
79 hitraceId_, "After %s pending job execute", "Promise");
80 }
81 HiTraceChain::ClearId();
82 }
83 if (saveId_.IsValid()) {
84 HiTraceChain::SetId(saveId_);
85 }
86 }
87 } // namespace panda::ecmascript::job