• 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 #ifndef ECMASCRIPT_CPU_PROFILER_H
17 #define ECMASCRIPT_CPU_PROFILER_H
18 
19 #include <semaphore.h>
20 
21 #include "ecmascript/cpu_profiler/profile_generator.h"
22 #include "ecmascript/cpu_profiler/profile_processor.h"
23 #include "os/mutex.h"
24 
25 namespace panda::ecmascript {
26 struct CurrentProcessInfo {
27     uint64_t nowTimeStamp = 0;
28     uint64_t tts = 0;
29     pid_t pid = 0;
30     pthread_t tid = 0;
31 };
32 
33 class GcStateScope {
34 public:
GcStateScope(JSThread * thread)35     inline explicit GcStateScope(JSThread *thread)
36     {
37         thread_ = thread;
38         thread_->SetGcState(true);
39     }
40 
~GcStateScope()41     inline ~GcStateScope()
42     {
43         thread_->SetGcState(false);
44     }
45 private:
46     JSThread *thread_ = nullptr;
47 };
48 
49 class CpuProfiler {
50 public:
51     static CpuProfiler *GetInstance();
52     static void ParseMethodInfo(JSMethod *method, JSThread *thread, InterpretedFrameHandler frameHandler);
53     static void GetFrameStack(JSThread *thread);
54     static void IsNeedAndGetStack(JSThread *thread);
55     static void GetStackSignalHandler(int signal);
56 
57     static CMap<JSMethod *, struct StackInfo> staticStackInfo_;
58     static sem_t sem_;
59     static CVector<JSMethod *> staticFrameStack_;
60 
61     void StartCpuProfiler(const EcmaVM *vm, const std::string &fileName);
62     void StopCpuProfiler();
63     std::string GetProfileName() const;
64     virtual ~CpuProfiler();
65 private:
66     static CMap<std::string, int> scriptIdMap_;
67     static std::atomic<CpuProfiler*> singleton_;
68     static os::memory::Mutex synchronizationMutex_;
69 
70     explicit CpuProfiler();
71     void SetProfileStart(uint64_t nowTimeStamp);
72     void GetCurrentProcessInfo(struct CurrentProcessInfo &currentProcessInfo);
73     bool CheckFileName(const std::string &fileName, std::string &absoluteFilePath) const;
74 
75     bool isOnly_ = false;
76     int interval_ = 500; // 500:Sampling interval 500 microseconds
77     std::string fileName_ = "";
78     ProfileGenerator *generator_ = nullptr;
79     pthread_t tid_ = 0;
80 };
81 } // namespace panda::ecmascript
82 #endif // ECMASCRIPT_CPU_PROFILE_H