Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_TRACE_H_ |
| 18 | #define ART_SRC_TRACE_H_ |
| 19 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 20 | #include <ostream> |
| 21 | #include <set> |
| 22 | #include <string> |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 23 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 24 | #include "file.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 25 | #include "globals.h" |
| 26 | #include "macros.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 27 | #include "safe_map.h" |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 28 | #include "UniquePtr.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 32 | class AbstractMethod; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 33 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 34 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 35 | uint32_t TraceMethodUnwindFromCode(Thread* self); |
| 36 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 37 | struct TraceStackFrame { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 38 | TraceStackFrame(AbstractMethod* method, uintptr_t return_pc) |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 39 | : method_(method), return_pc_(return_pc) { |
| 40 | } |
| 41 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 42 | AbstractMethod* method_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 43 | uintptr_t return_pc_; |
| 44 | }; |
| 45 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 46 | enum ProfilerClockSource { |
| 47 | kProfilerClockSourceThreadCpu, |
| 48 | kProfilerClockSourceWall, |
| 49 | kProfilerClockSourceDual, |
| 50 | }; |
| 51 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 52 | class Trace { |
| 53 | public: |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 54 | enum TraceEvent { |
| 55 | kMethodTraceEnter = 0, |
| 56 | kMethodTraceExit = 1, |
| 57 | kMethodTraceUnwind = 2, |
| 58 | }; |
| 59 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 60 | enum TraceFlag { |
| 61 | kTraceCountAllocs = 1, |
| 62 | }; |
| 63 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 64 | static void SetDefaultClockSource(ProfilerClockSource clock_source); |
| 65 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 66 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms); |
| 67 | static void Stop(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 68 | static void Shutdown() NO_THREAD_SAFETY_ANALYSIS; // TODO: implement appropriate locking. |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 69 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 70 | bool UseWallClock(); |
| 71 | bool UseThreadCpuClock(); |
| 72 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 73 | void LogMethodTraceEvent(Thread* self, const AbstractMethod* method, TraceEvent event); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 74 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 75 | void AddSavedCodeToMap(const AbstractMethod* method, const void* code); |
| 76 | void RemoveSavedCodeFromMap(const AbstractMethod* method); |
| 77 | const void* GetSavedCodeFromMap(const AbstractMethod* method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 78 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 79 | void SaveAndUpdateCode(AbstractMethod* method); |
| 80 | void ResetSavedCode(AbstractMethod* method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 81 | |
| 82 | private: |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 83 | explicit Trace(File* trace_file, int buffer_size, int flags); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 84 | |
| 85 | void BeginTracing(); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 86 | void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 87 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 88 | // Replaces code of each method with a pointer to a stub for method tracing. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 89 | void InstallStubs(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 90 | |
| 91 | // Restores original code for each method and fixes the return values of each thread's stack. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 92 | void UninstallStubs() LOCKS_EXCLUDED(Locks::thread_list_lock_); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 93 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 94 | // Methods to output traced methods and threads. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 95 | void GetVisitedMethods(size_t end_offset); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 96 | void DumpMethodList(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 97 | void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 98 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 99 | // Maps a method to its original code pointer. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 100 | SafeMap<const AbstractMethod*, const void*> saved_code_map_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 101 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 102 | // Set of methods visited by the profiler. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 103 | std::set<const AbstractMethod*> visited_methods_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 104 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 105 | // Maps a thread to its clock base. |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 106 | SafeMap<Thread*, uint64_t> thread_clock_base_map_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 107 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 108 | // File to write trace data out to, NULL if direct to ddms. |
| 109 | UniquePtr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 110 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 111 | // Buffer to store trace data. |
| 112 | UniquePtr<uint8_t> buf_; |
| 113 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 114 | // Flags enabling extra tracing of things such as alloc counts. |
| 115 | int flags_; |
| 116 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 117 | ProfilerClockSource clock_source_; |
| 118 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 119 | bool overflow_; |
| 120 | int buffer_size_; |
| 121 | uint64_t start_time_; |
| 122 | uint16_t trace_version_; |
| 123 | uint16_t record_size_; |
| 124 | |
| 125 | volatile int32_t cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 126 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 127 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 128 | }; |
| 129 | |
| 130 | } // namespace art |
| 131 | |
| 132 | #endif // ART_SRC_TRACE_H_ |