• 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_PROFILE_GENERSTOR_H
17 #define ECMASCRIPT_PROFILE_GENERSTOR_H
18 
19 #include <ctime>
20 #include <fstream>
21 #include <string>
22 #include <unistd.h>
23 #include <sys/syscall.h>
24 #include "ecmascript/js_method.h"
25 #include "ecmascript/mem/c_containers.h"
26 namespace panda::ecmascript {
27 const long long TIME_CHANGE = 10000000000000; // 10000000000000:Discard the first 3 bits of the current nanosecond time
28 struct StackInfo {
29     std::string codeType = "";
30     std::string functionName = "";
31     int columnNumber = 0;
32     int lineNumber = 0;
33     int scriptId = 0;
34     std::string url = "";
35 };
36 struct MethodNode {
37     int id = 0;
38     int parentId = 0;
39     struct StackInfo codeEntry;
40 };
41 struct SampleInfo {
42     int id = 0;
43     int line = 0;
44     uint64_t timeStamp = 0;
45 };
46 struct MethodKey {
47     JSMethod *method = nullptr;
48     int parentId = 0;
49     bool operator< (const MethodKey &methodKey) const
50     {
51         return parentId < methodKey.parentId || (parentId == methodKey.parentId && method < methodKey.method);
52     }
53 };
54 
55 class ProfileGenerator {
56 public:
57     explicit ProfileGenerator();
58     virtual ~ProfileGenerator();
59 
60     void AddSample(CVector<JSMethod *> sample, uint64_t sampleTimeStamp);
61     void WriteMethodsAndSampleInfo(bool timeEnd);
62     CVector<struct MethodNode> GetMethodNodes() const;
63     CDeque<struct SampleInfo> GetSamples() const;
64     std::string GetSampleData() const;
65     void SetThreadStartTime(uint64_t threadStartTime);
66     void SetStartsampleData(std::string sampleData);
67     void SetFileName(std::string &fileName);
68     const std::string GetFileName() const;
69     void ClearSampleData();
70 
71     static bool staticGcState_;
72     std::ofstream fileHandle_;
73 private:
74     void WriteAddNodes();
75     void WriteAddSamples();
76     struct StackInfo GetMethodInfo(JSMethod *method);
77     struct StackInfo GetGcInfo();
78 
79     CVector<struct MethodNode> methodNodes_;
80     CVector<int> stackTopLines_;
81     CMap<struct MethodKey, int> methodMap_;
82     CDeque<struct SampleInfo> samples_;
83     std::string sampleData_ = "";
84     uint64_t threadStartTime_ = 0;
85     std::string fileName_ = "";
86 };
87 } // namespace panda::ecmascript
88 #endif // ECMASCRIPT_PROFILE_GENERSTOR_H