blob: 1be1cc41424568d8f887bd4effd862bc6a34a658 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhaoe343b762011-12-05 16:36:44 -080016
17#ifndef ART_SRC_TRACE_H_
18#define ART_SRC_TRACE_H_
19
jeffhaoa9ef3fd2011-12-13 18:33:43 -080020#include <ostream>
21#include <set>
22#include <string>
jeffhaoe343b762011-12-05 16:36:44 -080023
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
jeffhaoe343b762011-12-05 16:36:44 -080025#include "globals.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070027#include "safe_map.h"
jeffhao2692b572011-12-16 15:42:28 -080028#include "UniquePtr.h"
jeffhaoe343b762011-12-05 16:36:44 -080029
30namespace art {
31
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace mirror {
Mathieu Chartier66f19252012-09-18 08:57:04 -070033class AbstractMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034} // namespace mirror
jeffhaoa9ef3fd2011-12-13 18:33:43 -080035class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080036
Elliott Hughescfbe73d2012-05-22 17:37:06 -070037enum ProfilerClockSource {
38 kProfilerClockSourceThreadCpu,
39 kProfilerClockSourceWall,
40 kProfilerClockSourceDual,
41};
42
jeffhaoe343b762011-12-05 16:36:44 -080043class Trace {
44 public:
jeffhaoa9ef3fd2011-12-13 18:33:43 -080045 enum TraceEvent {
46 kMethodTraceEnter = 0,
47 kMethodTraceExit = 1,
48 kMethodTraceUnwind = 2,
49 };
50
jeffhao0791adc2012-04-04 11:14:32 -070051 enum TraceFlag {
52 kTraceCountAllocs = 1,
53 };
54
Elliott Hughescfbe73d2012-05-22 17:37:06 -070055 static void SetDefaultClockSource(ProfilerClockSource clock_source);
56
jeffhaoe343b762011-12-05 16:36:44 -080057 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms);
58 static void Stop();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 static void Shutdown() NO_THREAD_SAFETY_ANALYSIS; // TODO: implement appropriate locking.
jeffhaoe343b762011-12-05 16:36:44 -080060
Elliott Hughescfbe73d2012-05-22 17:37:06 -070061 bool UseWallClock();
62 bool UseThreadCpuClock();
63
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 void LogMethodTraceEvent(Thread* self, const mirror::AbstractMethod* method, TraceEvent event);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080065
jeffhaoe343b762011-12-05 16:36:44 -080066 private:
Elliott Hughescfbe73d2012-05-22 17:37:06 -070067 explicit Trace(File* trace_file, int buffer_size, int flags);
jeffhao2692b572011-12-16 15:42:28 -080068
69 void BeginTracing();
Ian Rogersb726dcb2012-09-05 08:57:23 -070070 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -080071
jeffhaoa9ef3fd2011-12-13 18:33:43 -080072 // Methods to output traced methods and threads.
jeffhao2692b572011-12-16 15:42:28 -080073 void GetVisitedMethods(size_t end_offset);
Ian Rogersb726dcb2012-09-05 08:57:23 -070074 void DumpMethodList(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
75 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080076
jeffhao2692b572011-12-16 15:42:28 -080077 // Set of methods visited by the profiler.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 std::set<const mirror::AbstractMethod*> visited_methods_;
jeffhaoe343b762011-12-05 16:36:44 -080079
jeffhao2692b572011-12-16 15:42:28 -080080 // Maps a thread to its clock base.
Elliott Hughesa0e18062012-04-13 15:59:59 -070081 SafeMap<Thread*, uint64_t> thread_clock_base_map_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080082
jeffhao2692b572011-12-16 15:42:28 -080083 // File to write trace data out to, NULL if direct to ddms.
84 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080085
jeffhao2692b572011-12-16 15:42:28 -080086 // Buffer to store trace data.
87 UniquePtr<uint8_t> buf_;
88
jeffhao0791adc2012-04-04 11:14:32 -070089 // Flags enabling extra tracing of things such as alloc counts.
90 int flags_;
91
Elliott Hughescfbe73d2012-05-22 17:37:06 -070092 ProfilerClockSource clock_source_;
93
jeffhao2692b572011-12-16 15:42:28 -080094 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_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800101
jeffhaoe343b762011-12-05 16:36:44 -0800102 DISALLOW_COPY_AND_ASSIGN(Trace);
103};
104
105} // namespace art
106
107#endif // ART_SRC_TRACE_H_