thread_instruction_count.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef BASE_TRACE_EVENT_THREAD_INSTRUCTION_COUNT_H_
  5. #define BASE_TRACE_EVENT_THREAD_INSTRUCTION_COUNT_H_
  6. #include <stdint.h>
  7. #include "base/base_export.h"
  8. namespace base {
  9. namespace trace_event {
  10. // Represents the number of instructions that were retired between two samples
  11. // of a thread's performance counters.
  12. class BASE_EXPORT ThreadInstructionDelta {
  13. public:
  14. constexpr ThreadInstructionDelta() : delta_(0) {}
  15. explicit constexpr ThreadInstructionDelta(int64_t delta) : delta_(delta) {}
  16. constexpr int64_t ToInternalValue() const { return delta_; }
  17. private:
  18. int64_t delta_;
  19. };
  20. // Uses the system's performance counters in order to measure the number of
  21. // instructions that have been retired on the current thread.
  22. class BASE_EXPORT ThreadInstructionCount {
  23. public:
  24. // Returns true if the platform supports hardware retired instruction
  25. // counters.
  26. static bool IsSupported();
  27. // Returns the number of retired instructions relative to some epoch count,
  28. // or -1 if getting the current instruction count failed / is disabled.
  29. static ThreadInstructionCount Now();
  30. constexpr ThreadInstructionCount() : value_(-1) {}
  31. explicit constexpr ThreadInstructionCount(int64_t value) : value_(value) {}
  32. constexpr bool is_null() const { return value_ == -1; }
  33. constexpr ThreadInstructionDelta operator-(
  34. ThreadInstructionCount other) const {
  35. return ThreadInstructionDelta(value_ - other.value_);
  36. }
  37. constexpr int64_t ToInternalValue() const { return value_; }
  38. private:
  39. int64_t value_;
  40. };
  41. } // namespace trace_event
  42. } // namespace base
  43. #endif // BASE_TRACE_EVENT_THREAD_INSTRUCTION_COUNT_H_