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