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 #include "ecmascript/cpu_profiler/profile_processor.h"
16
17 #include <csignal>
18 #include <sys/time.h>
19 #include "ecmascript/cpu_profiler/cpu_profiler.h"
20 #include "ecmascript/interpreter/interpreter.h"
21
22 namespace panda::ecmascript {
23 JSThread *ProfileProcessor::thread_ = nullptr;
24 bool ProfileProcessor::isStart_ = true;
ProfileProcessor(ProfileGenerator * generator,const EcmaVM * vm,int interval)25 ProfileProcessor::ProfileProcessor(ProfileGenerator *generator, const EcmaVM *vm, int interval)
26 {
27 generator_ = generator;
28 interval_ = interval;
29 pid_ = pthread_self();
30 thread_ = vm->GetAssociatedJSThread();
31 }
~ProfileProcessor()32 ProfileProcessor::~ProfileProcessor() {}
33
Run(uint32_t threadIndex)34 bool ProfileProcessor::Run(uint32_t threadIndex)
35 {
36 uint64_t startTime = 0;
37 uint64_t endTime = 0;
38 startTime = GetMicrosecondsTimeStamp();
39 generator_->SetThreadStartTime(startTime);
40 while (isStart_) {
41 static int collectCount = 1;
42 #if ECMASCRIPT_ENABLE_ACTIVE_CPUPROFILER
43 JSThread *thread = GetJSThread();
44 ProfileGenerator::staticGcState_ = thread->GetGcState();
45 if (!ProfileGenerator::staticGcState_) {
46 thread->SetGetStackSignal(true);
47 if (sem_wait(&CpuProfiler::sem_) != 0) {
48 LOG(ERROR, RUNTIME) << "sem_ wait failed";
49 }
50 }
51 #else
52 if (pthread_kill(pid_, SIGINT) != 0) {
53 LOG(ERROR, RUNTIME) << "pthread_kill signal failed";
54 return false;
55 }
56 if (sem_wait(&CpuProfiler::sem_) != 0) {
57 LOG(ERROR, RUNTIME) << "sem_ wait failed";
58 return false;
59 }
60 #endif
61 endTime = GetMicrosecondsTimeStamp();
62 generator_->AddSample(CpuProfiler::staticFrameStack_, endTime);
63 if (generator_->GetMethodNodes().size() >= 10 || // 10:Number of nodes currently stored
64 generator_->GetSamples().size() == 100) { // 100:Number of Samples currently stored
65 generator_->WriteMethodsAndSampleInfo(false);
66 }
67 int64_t ts = interval_ - (endTime - startTime);
68 if (ts > 0) {
69 usleep(ts);
70 }
71 startTime = GetMicrosecondsTimeStamp();
72 if (collectCount % 50 == 0) { // 50:The sampling times reached 50 times.
73 WriteSampleDataToFile();
74 }
75 collectCount++;
76 }
77 if (sem_post(&CpuProfiler::sem_) != 0) {
78 LOG(ERROR, RUNTIME) << "sem_ post failed";
79 return false;
80 }
81 return true;
82 }
83
GetMicrosecondsTimeStamp()84 uint64_t ProfileProcessor::GetMicrosecondsTimeStamp()
85 {
86 struct timeval time;
87 gettimeofday(&time, NULL);
88 return (time.tv_sec * 1000000 + time.tv_usec); // 1000000:Second to subtle
89 }
90
GetJSThread()91 JSThread *ProfileProcessor::GetJSThread()
92 {
93 return thread_;
94 }
95
SetIsStart(bool isStart)96 void ProfileProcessor::SetIsStart(bool isStart)
97 {
98 isStart_ = isStart;
99 }
100
WriteSampleDataToFile()101 void ProfileProcessor::WriteSampleDataToFile()
102 {
103 static int flag = 0;
104 if (!generator_->fileHandle_.is_open() || generator_->GetSampleData().size() < 1024 * 1024) { // 1024 * 1024:1M
105 return;
106 }
107 if (flag == 0) {
108 generator_->fileHandle_ << generator_->GetSampleData();
109 generator_->ClearSampleData();
110 generator_->fileHandle_.close();
111 generator_->fileHandle_.open(generator_->GetFileName().c_str(), std::ios::app);
112 flag++;
113 return;
114 }
115 generator_->fileHandle_ << generator_->GetSampleData();
116 generator_->ClearSampleData();
117 }
118 } // namespace panda::ecmascript