1// Copyright 2020 the V8 project 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 V8_BUILTINS_PROFILE_DATA_READER_H_
6#define V8_BUILTINS_PROFILE_DATA_READER_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <vector>
11
12namespace v8 {
13namespace internal {
14
15class ProfileDataFromFile {
16 public:
17  // A hash of the function's Graph before scheduling. Allows us to avoid using
18  // profiling data if the function has been changed.
19  int hash() const { return hash_; }
20
21  // Returns how many times the block with the given ID was executed during
22  // profiling.
23  double GetCounter(size_t block_id) const {
24    // The profile data is allowed to omit blocks which were never hit, so be
25    // careful to avoid out-of-bounds access.
26    return block_id < block_counts_by_id_.size() ? block_counts_by_id_[block_id]
27                                                 : 0;
28  }
29
30  // Load basic block profiling data for the builtin with the given name, if
31  // such data exists. The returned vector is indexed by block ID, and its
32  // values are the number of times each block was executed while profiling.
33  static const ProfileDataFromFile* TryRead(const char* name);
34
35 protected:
36  int hash_ = 0;
37
38  // How many times each block was executed, indexed by block ID. This vector
39  // may be shorter than the total number of blocks; any omitted block should be
40  // treated as a zero.
41  std::vector<double> block_counts_by_id_;
42};
43
44// The following strings can't be static members of ProfileDataFromFile until
45// C++ 17; see https://stackoverflow.com/q/8016780/839379 . So for now we use a
46// namespace.
47namespace ProfileDataFromFileConstants {
48
49// Any line in a v8.log beginning with this string represents a basic block
50// counter.
51static constexpr char kBlockCounterMarker[] = "block";
52
53// Any line in a v8.log beginning with this string represents the hash of the
54// function Graph for a builtin.
55static constexpr char kBuiltinHashMarker[] = "builtin_hash";
56
57}  // namespace ProfileDataFromFileConstants
58
59}  // namespace internal
60}  // namespace v8
61
62#endif  // V8_BUILTINS_PROFILE_DATA_READER_H_
63