Files
palemoon27/toolkit/devtools/server/HeapSnapshot.h
T
roytam1 14412dba88 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1200795, part 1 - Eliminate excessive detail from cycle collector graph memory reporting. r=njn (8775704ad4)
- Bug 1200795, part 2 - Add memory reporting for CCGraph::mPtrToNodeMap. r=njn (154912392e)
- Bug 1202670 - Move most of nsCycleCollector::RemoveObjectFromGraph into CCGraph. r=smaug (78b449602d)
- Bug 1163006, part 1 - Make the cycle collector use the concrete logger class. r=smaug (a23f4adfd3)
- Bug 1163657 - Clean up setting of mFlags in CCGraphBuilder's ctor. r=smaug (c49fa66d20)
- Bug 1163006, part 2 - Rename various cycle collector listener variables to logger. r=smaug (c655503d1b)
- Bug 1163006, part 3 - Clean up some cycle collector logger set up code. r=smaug (9825ec8e80)
- Bug 1163006, part 4 - De-COM the nsICycleCollectorListener methods we only call from C++. r=smaug (39e579782f)
- Bug 1163006, part 5 - Add a less COM-y getter for mWantAllTraces. r=smaug (70c424197e)
- Bug 1152505 - CCGraphBuilder::NoteChild should pass aEdgeName by reference. r=smaug (25ffabb54c)
- Bug 1189122 - Assert when we Suspect() when a CC scan is in progress. r=smaug (ae997c0fec)
- Bug 1189829 - fix quadratic behavior in nsConsoleService; r=erahm (2b37337085)
- Bug 1189231 - Impl operator->* to nsAutoPtr. r=nfroyd (a491e0e775)
- Bug 1196050 - Replace NSCAP_Zero usage with decltype(nullptr). r=froydnj (4b4d411103)
- Bug 1196925 - Remove the non-existing relocatable store buffer counts from about:memory; r=njn (d030d12a81)
- Bug 1123237 - Part 2. MemoryProfiler hooks in js engine. r=terrence (e3495c3520)
- Bug 1196847 - Part 1: Allow storage of a unique id for a cell independent of address; r=jonco (53b94846f1)
- Bug 1207730 - Update comments in Barrier.h and sort classes in same order; r=sfink (ba3abf54cb)
- Bug 1196847 - Part 2: Implement a cell hasher that uses unique id based hashes; r=jonco (3811c58b7a)
- Bug 1196848 - Add Zone::uniqueIds to about:memory reports; r=njn (bff35eb98c)
- Bug 1123237 - Part 3. Monitoring allocation and gc events in nursery and tenured heaps. r=terrence (47cc20bb98)
- Bug 1123237 - Part 4. Monitoring allocations and frees for ArrayBuffer. r=terrence,sfink (3495e46571)
- Bug 1123237 - Part 5. Don't emit inline allocation when memory profiler enabled. r=terrence (b4b44652d0)
- Bug 1123237 - Part 6. A new API to get backtrace without allocating memory in profiler. r=mstange (eff2d42cbd)
- Bug 1123237 - Part 7. XPCOM interface for memory profiler. r=smaug (f3a85f8181)
- Bug 1123237 - Part 8. Tracking the memory events. r=BenWa,terrence (8694fa4ad4)
- Bug 1123237 - Part 9. Interface to memory-profiler add-ons. r=jimb (1f8ed472ad)
- Bug 1123237 - Part 10. Expose SwapElements from nsBaseHashtable. r=nfroyd (a2e06425ad)
- Bug 1123237 - Part 11. Don't use STL in memory-profiler. r=BenWa,cervantes (c13096634c)
- Bug 1123237 - Part 12. Fix GC hazards. r=terrence (64776fc60e)
- Bug 1194424 - Part 0: Extend the core dump protobuf message format to accomodate allocation stacks; r=sfink (d25ca13aec)
- Bug 1167292 - Part 1: Add a telemetry probe to time how long it takes to save a heap snapshot; r=ejpbruel (25a9047d18)
- Bug 1167292 - Part 2: Add a telemetry probe to time how long it takes to read a heap snapshot; r=ejpbruel (71e3bb5fe3)
- Bug 1167292 - Part 3: Add telemetry probes for node and edge counts when saving heap snapshots; r=ejpbruel (7985b9edf6)
- Bug 1187062 - Part 0: Make js::Debugger::getObjectAllocationSite return a SavedFrame* rather than a JSObject*; r=sfink (0ec4175b9c)
- Bug 1204613 - Add a static_assert that ProfileEntry::{Flags,Category} do not overlap; r=djvj (a2d3672676)
- Bug 1204168 - Add the js::ProfileEntry::setCategory method; r=djvj (dce07b7eb4)
2022-01-17 09:17:44 +08:00

184 lines
5.9 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_devtools_HeapSnapshot__
#define mozilla_devtools_HeapSnapshot__
#include "js/HashTable.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/devtools/DeserializedNode.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/Maybe.h"
#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtr.h"
#include "CoreDump.pb.h"
#include "nsCOMPtr.h"
#include "nsCRTGlue.h"
#include "nsCycleCollectionParticipant.h"
#include "nsISupports.h"
#include "nsWrapperCache.h"
#include "nsXPCOM.h"
namespace mozilla {
namespace devtools {
struct NSFreePolicy {
void operator()(void* ptr) {
NS_Free(ptr);
}
};
using UniqueString = UniquePtr<char16_t[], NSFreePolicy>;
struct UniqueStringHashPolicy {
struct Lookup {
const char16_t* str;
size_t length;
Lookup(const char16_t* str, size_t length)
: str(str)
, length(length)
{ }
};
static js::HashNumber hash(const Lookup& lookup) {
MOZ_ASSERT(lookup.str);
return HashString(lookup.str, lookup.length);
}
static bool match(const UniqueString& existing, const Lookup& lookup) {
MOZ_ASSERT(lookup.str);
return NS_strncmp(existing.get(), lookup.str, lookup.length) == 0;
}
};
class HeapSnapshot final : public nsISupports
, public nsWrapperCache
{
friend struct DeserializedNode;
explicit HeapSnapshot(JSContext* cx, nsISupports* aParent)
: timestamp(Nothing())
, rootId(0)
, nodes(cx)
, strings(cx)
, mParent(aParent)
{
MOZ_ASSERT(aParent);
};
// Initialize this HeapSnapshot from the given buffer that contains a
// serialized core dump. Do NOT take ownership of the buffer, only borrow it
// for the duration of the call. Return false on failure.
bool init(const uint8_t* buffer, uint32_t size);
// Save the given `protobuf::Node` message in this `HeapSnapshot` as a
// `DeserializedNode`.
bool saveNode(const protobuf::Node& node);
// If present, a timestamp in the same units that `PR_Now` gives.
Maybe<uint64_t> timestamp;
// The id of the root node for this deserialized heap graph.
NodeId rootId;
// The set of nodes in this deserialized heap graph, keyed by id.
using NodeMap = js::HashMap<NodeId, UniquePtr<DeserializedNode>>;
NodeMap nodes;
// Core dump files have many duplicate strings: type names are repeated for
// each node, and although in theory edge names are highly customizable for
// specific edges, in practice they are also highly duplicated. Rather than
// make each Deserialized{Node,Edge} malloc their own copy of their edge and
// type names, we de-duplicate the strings here and Deserialized{Node,Edge}
// get borrowed pointers into this set.
using UniqueStringSet = js::HashSet<UniqueString, UniqueStringHashPolicy>;
UniqueStringSet strings;
protected:
nsCOMPtr<nsISupports> mParent;
virtual ~HeapSnapshot() { }
public:
// Create a `HeapSnapshot` from the given buffer that contains a serialized
// core dump. Do NOT take ownership of the buffer, only borrow it for the
// duration of the call.
static already_AddRefed<HeapSnapshot> Create(JSContext* cx,
dom::GlobalObject& global,
const uint8_t* buffer,
uint32_t size,
ErrorResult& rv);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(HeapSnapshot)
MOZ_DECLARE_REFCOUNTED_TYPENAME(HeapSnapshot)
nsISupports* GetParentObject() const { return mParent; }
virtual JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
const char16_t* borrowUniqueString(const char16_t* duplicateString,
size_t length);
};
// A `CoreDumpWriter` is given the data we wish to save in a core dump and
// serializes it to disk, or memory, or a socket, etc.
class CoreDumpWriter
{
public:
virtual ~CoreDumpWriter() { };
// Write the given bits of metadata we would like to associate with this core
// dump.
virtual bool writeMetadata(uint64_t timestamp) = 0;
enum EdgePolicy : bool {
INCLUDE_EDGES = true,
EXCLUDE_EDGES = false
};
// Write the given `JS::ubi::Node` to the core dump. The given `EdgePolicy`
// dictates whether its outgoing edges should also be written to the core
// dump, or excluded.
virtual bool writeNode(const JS::ubi::Node& node,
EdgePolicy includeEdges) = 0;
};
// Serialize the heap graph as seen from `node` with the given `CoreDumpWriter`.
// If `wantNames` is true, capture edge names. If `zones` is non-null, only
// capture the sub-graph within the zone set, otherwise capture the whole heap
// graph. Returns false on failure.
bool
WriteHeapGraph(JSContext* cx,
const JS::ubi::Node& node,
CoreDumpWriter& writer,
bool wantNames,
JS::ZoneSet* zones,
JS::AutoCheckCannotGC& noGC,
uint32_t& outNodeCount,
uint32_t& outEdgeCount);
inline bool
WriteHeapGraph(JSContext* cx,
const JS::ubi::Node& node,
CoreDumpWriter& writer,
bool wantNames,
JS::ZoneSet* zones,
JS::AutoCheckCannotGC& noGC)
{
uint32_t ignoreNodeCount;
uint32_t ignoreEdgeCount;
return WriteHeapGraph(cx, node, writer, wantNames, zones, noGC,
ignoreNodeCount, ignoreEdgeCount);
}
} // namespace devtools
} // namespace mozilla
#endif // mozilla_devtools_HeapSnapshot__