tick-sample.h 4.87 KB
Newer Older
lpy's avatar
lpy committed
1 2 3 4 5 6 7
// Copyright 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_PROFILER_TICK_SAMPLE_H_
#define V8_PROFILER_TICK_SAMPLE_H_

8
#include "include/v8-unwinder.h"
lpy's avatar
lpy committed
9
#include "src/base/platform/time.h"
10
#include "src/common/globals.h"
lpy's avatar
lpy committed
11 12 13 14 15 16

namespace v8 {
namespace internal {

class Isolate;

17 18 19 20 21 22 23 24 25
// TickSample captures the information collected for each sample.
struct V8_EXPORT TickSample {
  // Internal profiling (with --prof + tools/$OS-tick-processor) wants to
  // include the runtime function we're calling. Externally exposed tick
  // samples don't care.
  enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame };

  TickSample()
      : state(OTHER),
26
        embedder_state(EmbedderStateTag::EMPTY),
27 28 29 30
        pc(nullptr),
        external_callback_entry(nullptr),
        frames_count(0),
        has_external_callback(false),
31
        update_stats_(true) {}
32 33 34 35 36 37 38 39 40 41 42 43 44

  /**
   * Initialize a tick sample from the isolate.
   * \param isolate The isolate.
   * \param state Execution state.
   * \param record_c_entry_frame Include or skip the runtime function.
   * \param update_stats Whether update the sample to the aggregated stats.
   * \param use_simulator_reg_state When set to true and V8 is running under a
   *                                simulator, the method will use the simulator
   *                                register state rather than the one provided
   *                                with |state| argument. Otherwise the method
   *                                will use provided register |state| as is.
   */
lpy's avatar
lpy committed
45
  void Init(Isolate* isolate, const v8::RegisterState& state,
46
            RecordCEntryFrame record_c_entry_frame, bool update_stats,
47 48
            bool use_simulator_reg_state = true,
            base::TimeDelta sampling_interval = base::TimeDelta());
49 50 51 52 53 54 55 56 57 58 59
  /**
   * Get a call stack sample from the isolate.
   * \param isolate The isolate.
   * \param state Register state.
   * \param record_c_entry_frame Include or skip the runtime function.
   * \param frames Caller allocated buffer to store stack frames.
   * \param frames_limit Maximum number of frames to capture. The buffer must
   *                     be large enough to hold the number of frames.
   * \param sample_info The sample info is filled up by the function
   *                    provides number of actual captured stack frames and
   *                    the current VM state.
60 61 62 63 64 65 66
   * \param out_state Output parameter. If non-nullptr pointer is provided,
   *                  and the execution is currently in a fast API call,
   *                  records StateTag::EXTERNAL to it. The caller could then
   *                  use this as a marker to not take into account the actual
   *                  VM state recorded in |sample_info|. In the case of fast
   *                  API calls, the VM state must be EXTERNAL, as the callback
   *                  is always an external C++ function.
67 68 69 70 71 72 73 74 75 76 77 78 79
   * \param use_simulator_reg_state When set to true and V8 is running under a
   *                                simulator, the method will use the simulator
   *                                register state rather than the one provided
   *                                with |state| argument. Otherwise the method
   *                                will use provided register |state| as is.
   * \note GetStackSample is thread and signal safe and should only be called
   *                      when the JS thread is paused or interrupted.
   *                      Otherwise the behavior is undefined.
   */
  static bool GetStackSample(Isolate* isolate, v8::RegisterState* state,
                             RecordCEntryFrame record_c_entry_frame,
                             void** frames, size_t frames_limit,
                             v8::SampleInfo* sample_info,
80
                             StateTag* out_state = nullptr,
81
                             bool use_simulator_reg_state = true);
82 83

  void print() const;
84 85

  StateTag state;  // The state of the VM.
86
  EmbedderStateTag embedder_state;
87 88 89 90 91 92 93 94
  void* pc;        // Instruction pointer.
  union {
    void* tos;  // Top stack value (*sp).
    void* external_callback_entry;
  };
  static const unsigned kMaxFramesCountLog2 = 8;
  static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1;
  void* stack[kMaxFramesCount];     // Call stack.
95
  void* context = nullptr;          // Address of the incumbent native context.
96
  void* embedder_context = nullptr;  // Address of the embedder native context.
97 98
  unsigned frames_count : kMaxFramesCountLog2;  // Number of captured frames.
  bool has_external_callback : 1;
99
  bool update_stats_ : 1;  // Whether the sample should update aggregated stats.
100 101

  base::TimeTicks timestamp;
102
  base::TimeDelta sampling_interval_;  // Sampling interval used to capture.
lpy's avatar
lpy committed
103 104 105 106 107 108
};

}  // namespace internal
}  // namespace v8

#endif  // V8_PROFILER_TICK_SAMPLE_H_