1 /* 2 * Copyright (c) 2023 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_DFX_TRACING_TRACING_H 17 #define ECMASCRIPT_DFX_TRACING_TRACING_H 18 19 #include "ecmascript/dfx/cpu_profiler/samples_record.h" 20 #include "ecmascript/method.h" 21 #include "ecmascript/debugger/notification_manager.h" 22 23 namespace panda::ecmascript { 24 25 struct TraceEventCpuProfileArg { 26 std::vector<int> nodes; 27 std::vector<int> samples; 28 std::vector<int> timeDeltas; 29 }; 30 31 struct TraceEvent { TraceEventTraceEvent32 TraceEvent(std::string cat, std::string name, std::string ph, int64_t pid, int64_t tid) 33 : cat_(cat), name_(name), ph_(ph), pid_(pid), tid_(tid) 34 { 35 } 36 ~TraceEvent() = default; 37 SetTsTraceEvent38 void SetTs(int64_t ts) 39 { 40 ts_ = ts; 41 } 42 SetDurTraceEvent43 void SetDur(int64_t dur) 44 { 45 dur_ = dur; 46 } 47 SetTdurTraceEvent48 void SetTdur(int64_t tdur) 49 { 50 tdur_ = tdur; 51 } 52 SetTtsTraceEvent53 void SetTts(int64_t tts) 54 { 55 tts_ = tts; 56 } 57 SetSTraceEvent58 void SetS(const std::string& s) 59 { 60 s_ = s; 61 } 62 SetIdTraceEvent63 void SetId(const std::string& Id) 64 { 65 id_ = Id; 66 } 67 SetArgsTraceEvent68 void SetArgs(const std::string& args) 69 { 70 args_ = args; 71 } 72 SetCpuProfileArgsTraceEvent73 void SetCpuProfileArgs(TraceEventCpuProfileArg& args) 74 { 75 cpuProfileArgs_ = args; 76 } 77 78 uint64_t GetEventBufferSize(); 79 80 std::string cat_; 81 std::string name_; 82 std::string ph_; 83 [[maybe_unused]] int64_t pid_ {0}; 84 [[maybe_unused]] int64_t tid_ {0}; 85 int64_t ts_ {0}; 86 std::string s_; 87 std::string id_; 88 int64_t dur_ {0}; 89 int64_t tdur_ {0}; 90 int64_t tts_ {0}; 91 std::string args_; 92 std::optional<struct TraceEventCpuProfileArg> cpuProfileArgs_; 93 }; 94 95 class Tracing : public tooling::RuntimeListener { 96 public: 97 explicit Tracing(const EcmaVM *vm); 98 virtual ~Tracing(); 99 100 void StartTracing(std::string &categories); 101 std::unique_ptr<std::vector<TraceEvent>> StopTracing(); 102 void TraceEventRecordMemory(); 103 void TraceEventRecordCpuProfilerStart(struct ProfileInfo* profileInfo); 104 void TraceEventRecordCpuProfiler(struct ProfileInfo* profileInfo, int &nodePos, uint32_t &samplePos); 105 void TraceEventRecordCpuProfilerEnd(struct ProfileInfo* profileInfo); 106 void GetBufferUseage(double &percentFull, uint32_t &eventCount, double &value); 107 void TraceEventRecordTracingStart(); 108 void TraceEventUpdateCpuProfiler(struct ProfileInfo *profileInfo); 109 110 void LoadModule(std::string_view name, std::string_view) override; 111 void BytecodePcChanged(JSThread *thread, JSHandle<Method> method, uint32_t bcOffset) override; 112 bool HandleDebuggerStmt(JSHandle<Method> method, uint32_t bcOffset) override; 113 void VmStart() override; 114 void VmDeath() override; 115 void NativeCalling(const void *nativeAddress) override; 116 void NativeReturn(const void *nativeAddress) override; 117 void MethodEntry(JSHandle<Method> method, JSHandle<JSTaggedValue> envHandle) override; 118 void MethodExit(JSHandle<Method> method) override; 119 IsTracing()120 bool IsTracing() 121 { 122 return isTracing_; 123 } 124 125 private: 126 void RecordTraceEvent(TraceEvent &event); 127 uint64_t GetTimeStamp(); 128 129 const EcmaVM *vm_; 130 [[maybe_unused]] std::string categories_; 131 bool isTracing_ {false}; 132 static constexpr uint64_t MAX_BUFFER_SIZE_DEFAULT = 200 * 1024 * 1024; 133 uint64_t maxBufferSize_ {MAX_BUFFER_SIZE_DEFAULT}; 134 uint64_t bufferSize_ {0}; 135 pthread_t tid_ {0}; 136 137 std::unique_ptr<std::vector<TraceEvent>> traceEvents_; 138 std::unique_ptr<std::vector<TraceEvent>> traceEventsCpuProfiler_; 139 std::mutex lock_; 140 }; 141 } // namespace panda::ecmascript 142 #endif // ECMASCRIPT_DFX_TRACING_TRACING_H