blob: e3f254ee7a3641656bd941496f0c8b97c29e53bf [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
Mathieu Chartier66f19252012-09-18 08:57:04 -070032class AbstractMethod;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080033class Thread;
jeffhaoe343b762011-12-05 16:36:44 -080034
Elliott Hughescfbe73d2012-05-22 17:37:06 -070035enum ProfilerClockSource {
36 kProfilerClockSourceThreadCpu,
37 kProfilerClockSourceWall,
38 kProfilerClockSourceDual,
39};
40
jeffhaoe343b762011-12-05 16:36:44 -080041class Trace {
42 public:
jeffhaoa9ef3fd2011-12-13 18:33:43 -080043 enum TraceEvent {
44 kMethodTraceEnter = 0,
45 kMethodTraceExit = 1,
46 kMethodTraceUnwind = 2,
47 };
48
jeffhao0791adc2012-04-04 11:14:32 -070049 enum TraceFlag {
50 kTraceCountAllocs = 1,
51 };
52
Elliott Hughescfbe73d2012-05-22 17:37:06 -070053 static void SetDefaultClockSource(ProfilerClockSource clock_source);
54
jeffhaoe343b762011-12-05 16:36:44 -080055 static void Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms);
56 static void Stop();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057 static void Shutdown() NO_THREAD_SAFETY_ANALYSIS; // TODO: implement appropriate locking.
jeffhaoe343b762011-12-05 16:36:44 -080058
Elliott Hughescfbe73d2012-05-22 17:37:06 -070059 bool UseWallClock();
60 bool UseThreadCpuClock();
61
Mathieu Chartier66f19252012-09-18 08:57:04 -070062 void LogMethodTraceEvent(Thread* self, const AbstractMethod* method, TraceEvent event);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080063
jeffhaoe343b762011-12-05 16:36:44 -080064 private:
Elliott Hughescfbe73d2012-05-22 17:37:06 -070065 explicit Trace(File* trace_file, int buffer_size, int flags);
jeffhao2692b572011-12-16 15:42:28 -080066
67 void BeginTracing();
Ian Rogersb726dcb2012-09-05 08:57:23 -070068 void FinishTracing() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao2692b572011-12-16 15:42:28 -080069
jeffhaoa9ef3fd2011-12-13 18:33:43 -080070 // Methods to output traced methods and threads.
jeffhao2692b572011-12-16 15:42:28 -080071 void GetVisitedMethods(size_t end_offset);
Ian Rogersb726dcb2012-09-05 08:57:23 -070072 void DumpMethodList(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
73 void DumpThreadList(std::ostream& os) LOCKS_EXCLUDED(Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -080074
jeffhao2692b572011-12-16 15:42:28 -080075 // Set of methods visited by the profiler.
Mathieu Chartier66f19252012-09-18 08:57:04 -070076 std::set<const AbstractMethod*> visited_methods_;
jeffhaoe343b762011-12-05 16:36:44 -080077
jeffhao2692b572011-12-16 15:42:28 -080078 // Maps a thread to its clock base.
Elliott Hughesa0e18062012-04-13 15:59:59 -070079 SafeMap<Thread*, uint64_t> thread_clock_base_map_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080080
jeffhao2692b572011-12-16 15:42:28 -080081 // File to write trace data out to, NULL if direct to ddms.
82 UniquePtr<File> trace_file_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080083
jeffhao2692b572011-12-16 15:42:28 -080084 // Buffer to store trace data.
85 UniquePtr<uint8_t> buf_;
86
jeffhao0791adc2012-04-04 11:14:32 -070087 // Flags enabling extra tracing of things such as alloc counts.
88 int flags_;
89
Elliott Hughescfbe73d2012-05-22 17:37:06 -070090 ProfilerClockSource clock_source_;
91
jeffhao2692b572011-12-16 15:42:28 -080092 bool overflow_;
93 int buffer_size_;
94 uint64_t start_time_;
95 uint16_t trace_version_;
96 uint16_t record_size_;
97
98 volatile int32_t cur_offset_;
jeffhaoa9ef3fd2011-12-13 18:33:43 -080099
jeffhaoe343b762011-12-05 16:36:44 -0800100 DISALLOW_COPY_AND_ASSIGN(Trace);
101};
102
103} // namespace art
104
105#endif // ART_SRC_TRACE_H_