1// Copyright 2014, VIXL authors
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7//   * Redistributions of source code must retain the above copyright notice,
8//     this list of conditions and the following disclaimer.
9//   * Redistributions in binary form must reproduce the above copyright notice,
10//     this list of conditions and the following disclaimer in the documentation
11//     and/or other materials provided with the distribution.
12//   * Neither the name of ARM Limited nor the names of its contributors may be
13//     used to endorse or promote products derived from this software without
14//     specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#ifndef VIXL_AARCH64_INSTRUMENT_AARCH64_H_
28#define VIXL_AARCH64_INSTRUMENT_AARCH64_H_
29
30#include "../globals-vixl.h"
31#include "../utils-vixl.h"
32
33#include "constants-aarch64.h"
34#include "decoder-aarch64.h"
35#include "instrument-aarch64.h"
36
37namespace vixl {
38namespace aarch64 {
39
40const int kCounterNameMaxLength = 256;
41const uint64_t kDefaultInstrumentationSamplingPeriod = 1 << 22;
42
43
44enum InstrumentState { InstrumentStateDisable = 0, InstrumentStateEnable = 1 };
45
46
47enum CounterType {
48  Gauge = 0,      // Gauge counters reset themselves after reading.
49  Cumulative = 1  // Cumulative counters keep their value after reading.
50};
51
52
53class Counter {
54 public:
55  explicit Counter(const char* name, CounterType type = Gauge);
56
57  void Increment();
58  void Enable();
59  void Disable();
60  bool IsEnabled();
61  uint64_t GetCount();
62  VIXL_DEPRECATED("GetCount", uint64_t count()) { return GetCount(); }
63
64  const char* GetName();
65  VIXL_DEPRECATED("GetName", const char* name()) { return GetName(); }
66
67  CounterType GetType();
68  VIXL_DEPRECATED("GetType", CounterType type()) { return GetType(); }
69
70 private:
71  char name_[kCounterNameMaxLength];
72  uint64_t count_;
73  bool enabled_;
74  CounterType type_;
75};
76
77
78class Instrument : public DecoderVisitor {
79 public:
80  explicit Instrument(
81      const char* datafile = NULL,
82      uint64_t sample_period = kDefaultInstrumentationSamplingPeriod);
83  ~Instrument();
84
85  void Enable();
86  void Disable();
87
88// Declare all Visitor functions.
89#define DECLARE(A) void Visit##A(const Instruction* instr) VIXL_OVERRIDE;
90  VISITOR_LIST(DECLARE)
91#undef DECLARE
92
93 private:
94  void Update();
95  void DumpCounters();
96  void DumpCounterNames();
97  void DumpEventMarker(unsigned marker);
98  void HandleInstrumentationEvent(unsigned event);
99  Counter* GetCounter(const char* name);
100
101  void InstrumentLoadStore(const Instruction* instr);
102  void InstrumentLoadStorePair(const Instruction* instr);
103
104  std::list<Counter*> counters_;
105
106  FILE* output_stream_;
107
108  // Counter information is dumped every sample_period_ instructions decoded.
109  // For a sample_period_ = 0 a final counter value is only produced when the
110  // Instrumentation class is destroyed.
111  uint64_t sample_period_;
112};
113
114}  // namespace aarch64
115}  // namespace vixl
116
117#endif  // VIXL_AARCH64_INSTRUMENT_AARCH64_H_
118