1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TRACE_EVENT_THREAD_INSTRUCTION_COUNT_H_
6 #define BASE_TRACE_EVENT_THREAD_INSTRUCTION_COUNT_H_
7 
8 #include <stdint.h>
9 
10 #include "base/base_export.h"
11 
12 namespace base {
13 namespace trace_event {
14 
15 // Represents the number of instructions that were retired between two samples
16 // of a thread's performance counters.
17 class BASE_EXPORT ThreadInstructionDelta {
18  public:
ThreadInstructionDelta()19   constexpr ThreadInstructionDelta() : delta_(0) {}
ThreadInstructionDelta(int64_t delta)20   explicit constexpr ThreadInstructionDelta(int64_t delta) : delta_(delta) {}
21 
ToInternalValue()22   constexpr int64_t ToInternalValue() const { return delta_; }
23 
24  private:
25   int64_t delta_;
26 };
27 
28 // Uses the system's performance counters in order to measure the number of
29 // instructions that have been retired on the current thread.
30 class BASE_EXPORT ThreadInstructionCount {
31  public:
32   // Returns true if the platform supports hardware retired instruction
33   // counters.
34   static bool IsSupported();
35 
36   // Returns the number of retired instructions relative to some epoch count,
37   // or -1 if getting the current instruction count failed / is disabled.
38   static ThreadInstructionCount Now();
39 
ThreadInstructionCount()40   constexpr ThreadInstructionCount() : value_(-1) {}
ThreadInstructionCount(int64_t value)41   explicit constexpr ThreadInstructionCount(int64_t value) : value_(value) {}
42 
is_null()43   constexpr bool is_null() const { return value_ == -1; }
44 
45   constexpr ThreadInstructionDelta operator-(
46       ThreadInstructionCount other) const {
47     return ThreadInstructionDelta(value_ - other.value_);
48   }
49 
ToInternalValue()50   constexpr int64_t ToInternalValue() const { return value_; }
51 
52  private:
53   int64_t value_;
54 };
55 
56 }  // namespace trace_event
57 }  // namespace base
58 
59 #endif  // BASE_TRACE_EVENT_THREAD_INSTRUCTION_COUNT_H_
60