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