1b78f13911bfe6eda303e91ef215c87a165aae8aeAlexandre Rames// Copyright 2014, VIXL authors
2578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// All rights reserved.
3578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
4578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// Redistribution and use in source and binary forms, with or without
5578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// modification, are permitted provided that the following conditions are met:
6578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
7578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//   * Redistributions of source code must retain the above copyright notice,
8578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     this list of conditions and the following disclaimer.
9578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//   * Redistributions in binary form must reproduce the above copyright notice,
10578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     this list of conditions and the following disclaimer in the documentation
11578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     and/or other materials provided with the distribution.
12578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//   * Neither the name of ARM Limited nor the names of its contributors may be
13578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     used to endorse or promote products derived from this software without
14578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//     specific prior written permission.
15578645f14e122d2b87d907e298cda7e7d0babf1farmvixl//
16578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25578645f14e122d2b87d907e298cda7e7d0babf1farmvixl// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
27d3832965c62a8ad461b9ea9eb0994ca6b0a3da2cAlexandre Rames#ifndef VIXL_AARCH64_INSTRUMENT_AARCH64_H_
28d3832965c62a8ad461b9ea9eb0994ca6b0a3da2cAlexandre Rames#define VIXL_AARCH64_INSTRUMENT_AARCH64_H_
29578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
30b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "../globals-vixl.h"
31b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "../utils-vixl.h"
32b68bacb75c1ab265fc539afa93964c7f51f35589Alexandre Rames
33b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "constants-aarch64.h"
34b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "decoder-aarch64.h"
35b49bdb7996e603555eba4c8b56c7325e3e737ab6Alexandre Rames#include "instrument-aarch64.h"
36578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
37578645f14e122d2b87d907e298cda7e7d0babf1farmvixlnamespace vixl {
3888c46b84df005638546de5e4e965bdcc31352f48Pierre Langloisnamespace aarch64 {
39578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
40578645f14e122d2b87d907e298cda7e7d0babf1farmvixlconst int kCounterNameMaxLength = 256;
41578645f14e122d2b87d907e298cda7e7d0babf1farmvixlconst uint64_t kDefaultInstrumentationSamplingPeriod = 1 << 22;
42578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
43578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
440f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixlenum InstrumentState { InstrumentStateDisable = 0, InstrumentStateEnable = 1 };
45578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
46578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
47578645f14e122d2b87d907e298cda7e7d0babf1farmvixlenum CounterType {
48578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Gauge = 0,      // Gauge counters reset themselves after reading.
49578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Cumulative = 1  // Cumulative counters keep their value after reading.
50578645f14e122d2b87d907e298cda7e7d0babf1farmvixl};
51578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
52578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
53578645f14e122d2b87d907e298cda7e7d0babf1farmvixlclass Counter {
54578645f14e122d2b87d907e298cda7e7d0babf1farmvixl public:
55c68cb64496485710cdb5b8480f8fee287058c93farmvixl  explicit Counter(const char* name, CounterType type = Gauge);
56578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
57578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void Increment();
58578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void Enable();
59578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void Disable();
60578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  bool IsEnabled();
6188c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  uint64_t GetCount();
6288c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  VIXL_DEPRECATED("GetCount", uint64_t count()) { return GetCount(); }
6388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois
6488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  const char* GetName();
6588c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  VIXL_DEPRECATED("GetName", const char* name()) { return GetName(); }
6688c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois
6788c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  CounterType GetType();
6888c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  VIXL_DEPRECATED("GetType", CounterType type()) { return GetType(); }
69578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
70578645f14e122d2b87d907e298cda7e7d0babf1farmvixl private:
71578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  char name_[kCounterNameMaxLength];
72578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  uint64_t count_;
73578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  bool enabled_;
74578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  CounterType type_;
75578645f14e122d2b87d907e298cda7e7d0babf1farmvixl};
76578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
77578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
780f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixlclass Instrument : public DecoderVisitor {
79578645f14e122d2b87d907e298cda7e7d0babf1farmvixl public:
800f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixl  explicit Instrument(
810f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixl      const char* datafile = NULL,
820f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixl      uint64_t sample_period = kDefaultInstrumentationSamplingPeriod);
83578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  ~Instrument();
84578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
85578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void Enable();
86578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void Disable();
87578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
880f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixl// Declare all Visitor functions.
893fac43c1a101f98f116e752b80abc122d32b83acPierre Langlois#define DECLARE(A) void Visit##A(const Instruction* instr) VIXL_OVERRIDE;
90578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  VISITOR_LIST(DECLARE)
910f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixl#undef DECLARE
92578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
93578645f14e122d2b87d907e298cda7e7d0babf1farmvixl private:
94578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void Update();
95578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void DumpCounters();
96578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void DumpCounterNames();
97578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void DumpEventMarker(unsigned marker);
98578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  void HandleInstrumentationEvent(unsigned event);
99578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  Counter* GetCounter(const char* name);
100578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
101c68cb64496485710cdb5b8480f8fee287058c93farmvixl  void InstrumentLoadStore(const Instruction* instr);
102c68cb64496485710cdb5b8480f8fee287058c93farmvixl  void InstrumentLoadStorePair(const Instruction* instr);
103578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
104578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  std::list<Counter*> counters_;
105578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
1060f35e36b7f5d1d2f4d95989b418447e1a4bcc8cdarmvixl  FILE* output_stream_;
107684cd2a7f5845539b58d0da7e012e39df49ceff0armvixl
108684cd2a7f5845539b58d0da7e012e39df49ceff0armvixl  // Counter information is dumped every sample_period_ instructions decoded.
109684cd2a7f5845539b58d0da7e012e39df49ceff0armvixl  // For a sample_period_ = 0 a final counter value is only produced when the
110684cd2a7f5845539b58d0da7e012e39df49ceff0armvixl  // Instrumentation class is destroyed.
111578645f14e122d2b87d907e298cda7e7d0babf1farmvixl  uint64_t sample_period_;
112578645f14e122d2b87d907e298cda7e7d0babf1farmvixl};
113578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
11488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois}  // namespace aarch64
115578645f14e122d2b87d907e298cda7e7d0babf1farmvixl}  // namespace vixl
116578645f14e122d2b87d907e298cda7e7d0babf1farmvixl
117d3832965c62a8ad461b9ea9eb0994ca6b0a3da2cAlexandre Rames#endif  // VIXL_AARCH64_INSTRUMENT_AARCH64_H_
118