• 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/base/config.h"
23 #include "ecmascript/dfx/cpu_profiler/cpu_profiler.h"
24 #include "ecmascript/dfx/cpu_profiler/samples_record.h"
25 #include "ecmascript/ecma_vm.h"
26 #include "ecmascript/log_wrapper.h"
27 
28 namespace panda::ecmascript {
29 const int USEC_PER_SEC = 1000 * 1000;
30 const int NSEC_PER_USEC = 1000;
~SamplingProcessor()31 SamplingProcessor::~SamplingProcessor() {}
32 
Run(void * arg)33 void *SamplingProcessor::Run(void *arg)
34 {
35     RunParams params = *reinterpret_cast<RunParams *>(arg);
36     SamplesRecord *generator = params.generatorParam;
37     uint32_t interval = params.intervalParam;
38     pthread_t pid = params.pidParam;
39     pthread_t tid = pthread_self();
40     pthread_setname_np(tid, "SamplingThread");
41     uint64_t startTime = GetMicrosecondsTimeStamp();
42     uint64_t endTime = startTime;
43     generator->SetThreadStartTime(startTime);
44     generator->AddStartTraceEvent();
45     while (generator->GetIsStart()) {
46         if (pthread_kill(pid, SIGPROF) != 0) {
47             LOG(ERROR, RUNTIME) << "pthread_kill signal failed";
48             return nullptr;
49         }
50         if (generator->SemWait(0) != 0) {
51             LOG_ECMA(ERROR) << "sem_[0] wait failed";
52             return nullptr;
53         }
54         startTime = GetMicrosecondsTimeStamp();
55         int64_t ts = static_cast<int64_t>(interval) - static_cast<int64_t>(startTime - endTime);
56         endTime = startTime;
57         if (ts > 0) {
58             usleep(ts);
59             endTime = GetMicrosecondsTimeStamp();
60         }
61         if (generator->GetMethodNodeCount() + generator->GetframeStackLength() >= MAX_NODE_COUNT) {
62             break;
63         }
64         if (generator->samplesQueue_->IsEmpty()) {
65             uint64_t sampleTimeStamp = SamplingProcessor::GetMicrosecondsTimeStamp();
66             generator->AddEmptyStackSample(sampleTimeStamp);
67         } else {
68             while (!generator->samplesQueue_->IsEmpty()) {
69                 FrameStackAndInfo *frame = generator->samplesQueue_->PopFrame();
70                 generator->AddSample(frame);
71             }
72         }
73         generator->AddTraceEvent(false);
74     }
75     generator->SetThreadStopTime();
76     generator->AddTraceEvent(true);
77     pthread_setname_np(tid, "OS_GC_Thread");
78     if (generator->SemPost(1) != 0) {
79         LOG_ECMA(ERROR) << "sem_[1] post failed";
80         return nullptr;
81     }
82     return nullptr;
83 }
84 
GetMicrosecondsTimeStamp()85 uint64_t SamplingProcessor::GetMicrosecondsTimeStamp()
86 {
87     struct timespec time;
88     clock_gettime(CLOCK_MONOTONIC, &time);
89     return time.tv_sec * USEC_PER_SEC + time.tv_nsec / NSEC_PER_USEC;
90 }
91 } // namespace panda::ecmascript
92