dart-sdk/runtime/vm/random.h
Ben Konyi 8caeaf7a6c [ VM ] Add support for heap sampling profiler
This CL introduces new embedding APIs for supporting heap sample
profiling. A registered sampling callback is invoked approximately every
N bytes based on an exponential distribution, providing information
about the isolate group the allocation occurred in, the user visible
name of the allocated object type, a weak persistent handle to the
allocated object, and the size of the allocation.

Sampling is triggered using artificial TLAB boundaries to cause
allocations to be sampled to take the allocation slow path where the
registered callback can be invoked with the allocation information.

Only new space allocations are currently traced, with old space
allocation support to be added in a future CL.

TEST=Dart_HeapSampling

Change-Id: I22bcdeec6e823bc1ab44898d4c596fbed7169fa1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/264520
Commit-Queue: Ben Konyi <bkonyi@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
2022-11-22 18:05:24 +00:00

50 lines
1 KiB
C++

// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_RANDOM_H_
#define RUNTIME_VM_RANDOM_H_
#include <atomic>
#include "vm/allocation.h"
#include "vm/flags.h"
#include "vm/globals.h"
namespace dart {
DECLARE_FLAG(uint64_t, random_seed);
class Random {
public:
Random();
// Seed must be non-zero.
explicit Random(uint64_t seed);
~Random();
uint32_t NextUInt32();
uint64_t NextUInt64() {
return (static_cast<uint64_t>(NextUInt32()) << 32) |
static_cast<uint64_t>(NextUInt32());
}
static uint64_t GlobalNextUInt64();
static void Init();
static void Cleanup();
// Generates a uniform random variable in the range [0,1].
double NextDouble();
private:
uint64_t NextState();
void Initialize(uint64_t seed);
std::atomic<uint64_t> _state;
DISALLOW_COPY_AND_ASSIGN(Random);
};
} // namespace dart
#endif // RUNTIME_VM_RANDOM_H_