1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_TRACE_PROFILE_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_TRACE_PROFILE_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include <unordered_set> 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker #include "base/locks.h" 23*795d594fSAndroid Build Coastguard Worker #include "base/macros.h" 24*795d594fSAndroid Build Coastguard Worker #include "base/os.h" 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN { 27*795d594fSAndroid Build Coastguard Worker 28*795d594fSAndroid Build Coastguard Worker class ArtMethod; 29*795d594fSAndroid Build Coastguard Worker 30*795d594fSAndroid Build Coastguard Worker // TODO(mythria): A randomly chosen value. Tune it later based on the number of 31*795d594fSAndroid Build Coastguard Worker // entries required in the buffer. 32*795d594fSAndroid Build Coastguard Worker static constexpr size_t kAlwaysOnTraceBufSize = 2048; 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Worker // This class implements low-overhead tracing. This feature is available only when 35*795d594fSAndroid Build Coastguard Worker // always_enable_profile_code is enabled which is a build time flag defined in 36*795d594fSAndroid Build Coastguard Worker // build/flags/art-flags.aconfig. When this flag is enabled, AOT and JITed code can record events 37*795d594fSAndroid Build Coastguard Worker // on each method execution. When a profile is started, method entry / exit events are recorded in 38*795d594fSAndroid Build Coastguard Worker // a per-thread circular buffer. When requested the recorded events in the buffer are dumped into a 39*795d594fSAndroid Build Coastguard Worker // file. The buffers are released when the profile is stopped. 40*795d594fSAndroid Build Coastguard Worker class TraceProfiler { 41*795d594fSAndroid Build Coastguard Worker public: 42*795d594fSAndroid Build Coastguard Worker // Starts profiling by allocating a per-thread buffer for all the threads. 43*795d594fSAndroid Build Coastguard Worker static void Start(); 44*795d594fSAndroid Build Coastguard Worker 45*795d594fSAndroid Build Coastguard Worker // Releases all the buffers. 46*795d594fSAndroid Build Coastguard Worker static void Stop(); 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker // Dumps the recorded events in the buffer from all threads in the specified file. 49*795d594fSAndroid Build Coastguard Worker static void Dump(int fd); 50*795d594fSAndroid Build Coastguard Worker static void Dump(const char* trace_filename); 51*795d594fSAndroid Build Coastguard Worker 52*795d594fSAndroid Build Coastguard Worker // Called when thread is exiting to release the allocated buffer. 53*795d594fSAndroid Build Coastguard Worker static void ReleaseThreadBuffer(Thread* self) REQUIRES(Locks::trace_lock_); 54*795d594fSAndroid Build Coastguard Worker 55*795d594fSAndroid Build Coastguard Worker static bool IsTraceProfileInProgress() REQUIRES(Locks::trace_lock_); 56*795d594fSAndroid Build Coastguard Worker 57*795d594fSAndroid Build Coastguard Worker // Allocates a buffer for the specified thread. 58*795d594fSAndroid Build Coastguard Worker static void AllocateBuffer(Thread* thread); 59*795d594fSAndroid Build Coastguard Worker 60*795d594fSAndroid Build Coastguard Worker private: 61*795d594fSAndroid Build Coastguard Worker // Dumps the events from all threads into the trace_file. 62*795d594fSAndroid Build Coastguard Worker static void Dump(std::unique_ptr<File>&& trace_file); 63*795d594fSAndroid Build Coastguard Worker 64*795d594fSAndroid Build Coastguard Worker // This method goes over all the events in the thread_buffer and stores the encoded event in the 65*795d594fSAndroid Build Coastguard Worker // buffer. It returns the pointer to the next free entry in the buffer. 66*795d594fSAndroid Build Coastguard Worker // This also records the ArtMethods from the events in the thread_buffer in a set. This set is 67*795d594fSAndroid Build Coastguard Worker // used to dump the information about the methods once buffers from all threads have been 68*795d594fSAndroid Build Coastguard Worker // processed. 69*795d594fSAndroid Build Coastguard Worker static uint8_t* DumpBuffer(uint32_t thread_id, 70*795d594fSAndroid Build Coastguard Worker uintptr_t* thread_buffer, 71*795d594fSAndroid Build Coastguard Worker uint8_t* buffer /* out */, 72*795d594fSAndroid Build Coastguard Worker std::unordered_set<ArtMethod*>& methods /* out */); 73*795d594fSAndroid Build Coastguard Worker 74*795d594fSAndroid Build Coastguard Worker static std::string GetMethodInfoLine(ArtMethod* method) REQUIRES(Locks::mutator_lock_); 75*795d594fSAndroid Build Coastguard Worker 76*795d594fSAndroid Build Coastguard Worker static bool profile_in_progress_ GUARDED_BY(Locks::trace_lock_); 77*795d594fSAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(TraceProfiler); 78*795d594fSAndroid Build Coastguard Worker }; 79*795d594fSAndroid Build Coastguard Worker 80*795d594fSAndroid Build Coastguard Worker } // namespace art 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_TRACE_PROFILE_H_ 83