1 /*
2 * Copyright (c) 2021 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/dfx/cpu_profiler/sampling_processor.h"
17
18
19 #include "ecmascript/dfx/cpu_profiler/samples_record.h"
20 #if defined(ENABLE_FFRT_INTERFACES)
21 #include "c/executor_task.h"
22 #endif
23
24 namespace panda::ecmascript {
25 const int USEC_PER_SEC = 1000 * 1000;
26 const int NSEC_PER_USEC = 1000;
~SamplingProcessor()27 SamplingProcessor::~SamplingProcessor() {}
28
Run(void * arg)29 void *SamplingProcessor::Run(void *arg)
30 {
31 LOG_ECMA(INFO) << "SamplingProcessor::Run start";
32 RunParams params = *reinterpret_cast<RunParams *>(arg);
33 SamplesRecord *generator = params.generator_;
34 uint32_t interval = params.interval_;
35 pthread_t jsThreadId = params.tid_;
36 pthread_t samplingThreadId = pthread_self();
37 pthread_setname_np(samplingThreadId, "SamplingThread");
38 uint64_t startTime = generator->GetThreadStartTime();
39 uint64_t endTime = startTime;
40 generator->AddStartTraceEvent();
41 while (generator->GetIsStart()) {
42 startTime = GetMicrosecondsTimeStamp();
43 int64_t ts = static_cast<int64_t>(interval) - static_cast<int64_t>(startTime - endTime);
44 endTime = startTime;
45 if (ts > 0) {
46 usleep(ts);
47 endTime = GetMicrosecondsTimeStamp();
48 }
49 #if defined(ENABLE_FFRT_INTERFACES)
50 // When the ffrt is disabled for js thread, including main thread and worker thread,
51 // then the taskHandle is a nullptr
52 if (params.taskHandle_ != nullptr) {
53 // When the ffrt task is not running on any threads (hang),
54 // the tid returned by ffrt_task_get_tid will be zero
55 pthread_t tid = ffrt_task_get_tid(params.taskHandle_);
56 if (tid != 0 && jsThreadId != tid) {
57 jsThreadId = tid;
58 }
59 if (tid == 0) {
60 // The samplesQueue_ may not be empty due to the call of PostNapiFrame
61 SamplingProcessor::AddSample(generator);
62 generator->AddTraceEvent(false);
63 continue;
64 }
65 }
66 #endif // defined(ENABLE_FFRT_INTERFACES)
67 if (pthread_kill(jsThreadId, SIGPROF) != 0) {
68 LOG(ERROR, RUNTIME) << "pthread_kill signal failed";
69 return nullptr;
70 }
71 if (generator->SemWait(0) != 0) {
72 LOG_ECMA(ERROR) << "sem_[0] wait failed";
73 return nullptr;
74 }
75 if (generator->GetMethodNodeCount() + generator->GetframeStackLength() >= MAX_NODE_COUNT) {
76 LOG_ECMA(ERROR) << "SamplingProcessor::Run, exceed MAX_NODE_COUNT";
77 break;
78 }
79 SamplingProcessor::AddSample(generator);
80 generator->AddTraceEvent(false);
81 }
82 generator->SetThreadStopTime();
83 generator->AddTraceEvent(true);
84 return PostSemAndLogEnd(generator, samplingThreadId);
85 }
86
AddSample(SamplesRecord * generator)87 void SamplingProcessor::AddSample(SamplesRecord *generator)
88 {
89 if (generator->samplesQueue_->IsEmpty()) {
90 uint64_t sampleTimeStamp = SamplingProcessor::GetMicrosecondsTimeStamp();
91 generator->AddEmptyStackSample(sampleTimeStamp);
92 } else {
93 while (!generator->samplesQueue_->IsEmpty()) {
94 FrameStackAndInfo *frame = generator->samplesQueue_->PopFrame();
95 generator->AddSample(frame);
96 }
97 }
98 }
99
PostSemAndLogEnd(SamplesRecord * generator,pthread_t tid)100 void *SamplingProcessor::PostSemAndLogEnd(SamplesRecord *generator, pthread_t tid)
101 {
102 pthread_setname_np(tid, "OS_GC_Thread");
103 if (generator->SemPost(1) != 0) {
104 LOG_ECMA(ERROR) << "sem_[1] post failed, errno = " << errno;
105 return nullptr;
106 }
107 LOG_ECMA(INFO) << "SamplingProcessor::Run end";
108 return nullptr;
109 }
110
GetMicrosecondsTimeStamp()111 uint64_t SamplingProcessor::GetMicrosecondsTimeStamp()
112 {
113 struct timespec time;
114 clock_gettime(CLOCK_MONOTONIC, &time);
115 return time.tv_sec * USEC_PER_SEC + time.tv_nsec / NSEC_PER_USEC;
116 }
117 } // namespace panda::ecmascript
118