Skip to content

Commit

Permalink
shared: Add takeHeapSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMrBonnie committed Oct 2, 2023
1 parent eae43a4 commit 5dc78f2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion shared/src/modules/SharedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void SyncedMetaEnumerator(js::DynamicPropertyEnumeratorContext& ctx)
extern js::Class baseObjectClass, worldObjectClass, entityClass, resourceClass, bufferClass;
// TODO (xLuxy): bind colshape classes except colShapeClass
extern js::Class colShapeClass, colShapeCircleClass, colShapeCuboidClass, colShapeCylinderClass, colShapePolyClass, colShapeRectClass, colShapeSphereClass;
extern js::Namespace enumsNamespace, sharedEventsNamespace, fileNamespace;
extern js::Namespace enumsNamespace, sharedEventsNamespace, fileNamespace, profilerNamespace;
static js::Module sharedModule("@altv/shared", "", { &baseObjectClass, &worldObjectClass, &entityClass, &colShapeClass, &resourceClass, &bufferClass }, [](js::ModuleTemplate& module)
{
module.StaticProperty("isDebug", alt::ICore::Instance().IsDebug());
Expand All @@ -126,6 +126,7 @@ static js::Module sharedModule("@altv/shared", "", { &baseObjectClass, &worldObj
module.Namespace("RPC");
module.Namespace(enumsNamespace);
module.Namespace(fileNamespace);
module.Namespace(profilerNamespace);
// Blip namespaces
module.Namespace("PointBlip");
module.Namespace("AreaBlip");
Expand Down
45 changes: 45 additions & 0 deletions shared/src/namespaces/ProfilerNamespace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "Namespace.h"
#include "interfaces/IAltResource.h"
#include "v8-profiler.h"

#include <sstream>

class StringOutputStream : public v8::OutputStream
{
std::stringstream stream;
std::function<void(const std::string&)> callback;
js::IAltResource* resource;

public:
StringOutputStream(js::IAltResource* _resource, std::function<void(const std::string&)>&& _callback) : resource(_resource), callback(_callback) {}

virtual void EndOfStream() override
{
resource->PushNextTickCallback([this]() { this->callback(this->stream.str()); });
}
virtual WriteResult WriteAsciiChunk(char* data, int size) override
{
stream << data;
return WriteResult::kContinue;
}
};

static void TakeHeapSnapshot(js::FunctionContext& ctx)
{
const v8::HeapSnapshot* snapshot = ctx.GetIsolate()->GetHeapProfiler()->TakeHeapSnapshot();
js::Promise* promise = new js::Promise;
StringOutputStream* stream = new StringOutputStream(ctx.GetResource<js::IAltResource>(),
[=](const std::string& resultStr)
{
promise->Resolve(resultStr);
delete promise;
delete stream;
});
snapshot->Serialize(stream, v8::HeapSnapshot::SerializationFormat::kJSON);
ctx.Return(promise->Get());
}

// clang-format off
extern js::Namespace profilerNamespace("Profiler", [](js::NamespaceTemplate& tpl) {
tpl.StaticFunction("takeHeapSnapshot", TakeHeapSnapshot);
});
7 changes: 7 additions & 0 deletions types/shared/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,13 @@ declare module "@altv/shared" {
export class GenericEventHandler extends EventHandler {}
}

export namespace Profiler {
/**
* Resolves to a JSON string that can be loaded by e.g. Chrome DevTools to investigate the current heap objects.
*/
export function takeHeapSnapshot(): Promise<string>;
}

export namespace Enums {
export enum AmmoSpecialType {
NONE,
Expand Down

0 comments on commit 5dc78f2

Please sign in to comment.