OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 class Isolate; | 37 class Isolate; |
38 | 38 |
39 // ---------------------------------------------------------------------------- | 39 // ---------------------------------------------------------------------------- |
40 // Sampler | 40 // Sampler |
41 // | 41 // |
42 // A sampler periodically samples the state of the VM and optionally | 42 // A sampler periodically samples the state of the VM and optionally |
43 // (if used for profiling) the program counter and stack pointer for | 43 // (if used for profiling) the program counter and stack pointer for |
44 // the thread that created it. | 44 // the thread that created it. |
45 | 45 |
| 46 struct RegisterState { |
| 47 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {} |
| 48 Address pc; // Instruction pointer. |
| 49 Address sp; // Stack pointer. |
| 50 Address fp; // Frame pointer. |
| 51 }; |
| 52 |
46 // TickSample captures the information collected for each sample. | 53 // TickSample captures the information collected for each sample. |
47 struct TickSample { | 54 struct TickSample { |
48 TickSample() | 55 TickSample() |
49 : state(OTHER), | 56 : state(OTHER), |
50 pc(NULL), | 57 pc(NULL), |
51 sp(NULL), | |
52 fp(NULL), | |
53 external_callback(NULL), | 58 external_callback(NULL), |
54 frames_count(0), | 59 frames_count(0), |
55 has_external_callback(false), | 60 has_external_callback(false), |
56 top_frame_type(StackFrame::NONE) {} | 61 top_frame_type(StackFrame::NONE) {} |
57 void Trace(Isolate* isolate); | 62 void Init(Isolate* isolate, const RegisterState& state); |
58 StateTag state; // The state of the VM. | 63 StateTag state; // The state of the VM. |
59 Address pc; // Instruction pointer. | 64 Address pc; // Instruction pointer. |
60 Address sp; // Stack pointer. | |
61 Address fp; // Frame pointer. | |
62 union { | 65 union { |
63 Address tos; // Top stack value (*sp). | 66 Address tos; // Top stack value (*sp). |
64 Address external_callback; | 67 Address external_callback; |
65 }; | 68 }; |
66 static const int kMaxFramesCount = 64; | 69 static const int kMaxFramesCount = 64; |
67 Address stack[kMaxFramesCount]; // Call stack. | 70 Address stack[kMaxFramesCount]; // Call stack. |
68 int frames_count : 8; // Number of captured frames. | 71 int frames_count : 8; // Number of captured frames. |
69 bool has_external_callback : 1; | 72 bool has_external_callback : 1; |
70 StackFrame::Type top_frame_type : 4; | 73 StackFrame::Type top_frame_type : 4; |
71 }; | 74 }; |
72 | 75 |
73 class Sampler { | 76 class Sampler { |
74 public: | 77 public: |
75 // Initializes the Sampler support. Called once at VM startup. | 78 // Initializes the Sampler support. Called once at VM startup. |
76 static void SetUp(); | 79 static void SetUp(); |
77 static void TearDown(); | 80 static void TearDown(); |
78 | 81 |
79 // Initialize sampler. | 82 // Initialize sampler. |
80 Sampler(Isolate* isolate, int interval); | 83 Sampler(Isolate* isolate, int interval); |
81 virtual ~Sampler(); | 84 virtual ~Sampler(); |
82 | 85 |
83 Isolate* isolate() const { return isolate_; } | 86 Isolate* isolate() const { return isolate_; } |
84 int interval() const { return interval_; } | 87 int interval() const { return interval_; } |
85 | 88 |
86 // Performs stack sampling. | 89 // Performs stack sampling. |
87 void SampleStack(TickSample* sample); | 90 void SampleStack(const RegisterState& regs); |
88 | |
89 // This method is called for each sampling period with the current | |
90 // program counter. | |
91 virtual void Tick(TickSample* sample) = 0; | |
92 | 91 |
93 // Start and stop sampler. | 92 // Start and stop sampler. |
94 void Start(); | 93 void Start(); |
95 void Stop(); | 94 void Stop(); |
96 | 95 |
97 // Is the sampler used for profiling? | 96 // Is the sampler used for profiling? |
98 bool IsProfiling() const { return NoBarrier_Load(&profiling_) > 0; } | 97 bool IsProfiling() const { return NoBarrier_Load(&profiling_) > 0; } |
99 void IncreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, 1); } | 98 void IncreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, 1); } |
100 void DecreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, -1); } | 99 void DecreaseProfilingDepth() { NoBarrier_AtomicIncrement(&profiling_, -1); } |
101 | 100 |
102 // Whether the sampler is running (that is, consumes resources). | 101 // Whether the sampler is running (that is, consumes resources). |
103 bool IsActive() const { return NoBarrier_Load(&active_); } | 102 bool IsActive() const { return NoBarrier_Load(&active_); } |
104 | 103 |
105 // Used in tests to make sure that stack sampling is performed. | 104 // Used in tests to make sure that stack sampling is performed. |
106 int samples_taken() const { return samples_taken_; } | 105 int samples_taken() const { return samples_taken_; } |
107 void ResetSamplesTaken() { samples_taken_ = 0; } | 106 void ResetSamplesTaken() { samples_taken_ = 0; } |
108 | 107 |
109 class PlatformData; | 108 class PlatformData; |
110 PlatformData* platform_data() const { return data_; } | 109 PlatformData* platform_data() const { return data_; } |
111 | 110 |
| 111 protected: |
| 112 // This method is called for each sampling period with the current |
| 113 // program counter. |
| 114 virtual void Tick(TickSample* sample) = 0; |
| 115 |
112 private: | 116 private: |
113 void SetActive(bool value) { NoBarrier_Store(&active_, value); } | 117 void SetActive(bool value) { NoBarrier_Store(&active_, value); } |
114 | 118 |
115 Isolate* isolate_; | 119 Isolate* isolate_; |
116 const int interval_; | 120 const int interval_; |
117 Atomic32 profiling_; | 121 Atomic32 profiling_; |
118 Atomic32 active_; | 122 Atomic32 active_; |
119 PlatformData* data_; // Platform specific data. | 123 PlatformData* data_; // Platform specific data. |
120 int samples_taken_; // Counts stack samples taken. | 124 int samples_taken_; // Counts stack samples taken. |
121 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); | 125 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
122 }; | 126 }; |
123 | 127 |
124 | 128 |
125 } } // namespace v8::internal | 129 } } // namespace v8::internal |
126 | 130 |
127 #endif // V8_SAMPLER_H_ | 131 #endif // V8_SAMPLER_H_ |
OLD | NEW |