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 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 25 | #include "globals.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "os.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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | namespace mirror { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 33 | class AbstractMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | } // namespace mirror |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 35 | class Thread; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 36 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 37 | enum ProfilerClockSource { |
| 38 | kProfilerClockSourceThreadCpu, |
| 39 | kProfilerClockSourceWall, |
| 40 | kProfilerClockSourceDual, |
| 41 | }; |
| 42 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 43 | class Trace { |
| 44 | public: |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 45 | enum TraceEvent { |
| 46 | kMethodTraceEnter = 0, |
| 47 | kMethodTraceExit = 1, |
| 48 | kMethodTraceUnwind = 2, |
| 49 | }; |
| 50 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 51 | enum TraceFlag { |
| 52 | kTraceCountAllocs = 1, |
| 53 | }; |
| 54 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 55 | static void SetDefaultClockSource(ProfilerClockSource clock_source); |
| 56 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 57 | static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms); |
| 58 | static void Stop(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | static void Shutdown() NO_THREAD_SAFETY_ANALYSIS; // TODO: implement appropriate locking. |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 61 | bool UseWallClock(); |
| 62 | bool UseThreadCpuClock(); |
| 63 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | void LogMethodTraceEvent(Thread* self, const mirror::AbstractMethod* method, TraceEvent event); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 65 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 66 | private: |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 67 | explicit Trace(File* trace_file, int buffer_size, int flags); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 68 | |
| 69 | void BeginTracing(); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 70 | void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 71 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 72 | // Methods to output traced methods and threads. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 73 | void GetVisitedMethods(size_t end_offset); |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 74 | void DumpMethodList(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 75 | void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 76 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 77 | // Set of methods visited by the profiler. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | std::set<const mirror::AbstractMethod*> visited_methods_; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 79 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 80 | // Maps a thread to its clock base. |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 81 | SafeMap<Thread*, uint64_t> thread_clock_base_map_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 82 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 83 | // File to write trace data out to, NULL if direct to ddms. |
| 84 | UniquePtr<File> trace_file_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 85 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 86 | // Buffer to store trace data. |
| 87 | UniquePtr<uint8_t> buf_; |
| 88 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 89 | // Flags enabling extra tracing of things such as alloc counts. |
| 90 | int flags_; |
| 91 | |
Elliott Hughes | cfbe73d | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 92 | ProfilerClockSource clock_source_; |
| 93 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 94 | bool overflow_; |
| 95 | int buffer_size_; |
| 96 | uint64_t start_time_; |
| 97 | uint16_t trace_version_; |
| 98 | uint16_t record_size_; |
| 99 | |
| 100 | volatile int32_t cur_offset_; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 101 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(Trace); |
| 103 | }; |
| 104 | |
| 105 | } // namespace art |
| 106 | |
| 107 | #endif // ART_SRC_TRACE_H_ |