Skip to content

Commit

Permalink
added GetHeapStatistics - monitor isolate heap statistics (#24)
Browse files Browse the repository at this point in the history
* fixed comment

* added isoalte heap statistics

* added TestGetHeapStatistics test
  • Loading branch information
mehrdadrad authored and rogchap committed Nov 2, 2019
1 parent fb5bf53 commit ed27ac7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
38 changes: 36 additions & 2 deletions isolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ type Isolate struct {
ptr C.IsolatePtr
}

// HeapStatistics represents V8 isolate heap statistics
type HeapStatistics struct {
TotalHeapSize uint64
TotalHeapSizeExecutable uint64
TotalPhysicalSize uint64
TotalAvailableSize uint64
UsedHeapSize uint64
HeapSizeLimit uint64
MallocedMemory uint64
ExternalMemory uint64
PeakMallocedMemory uint64
NumberOfNativeContexts uint64
NumberOfDetachedContexts uint64
}

// NewIsolate creates a new V8 isolate. Only one thread may access
// a given isolate at a time, but different threads may access
// different isolates simultaneously.
Expand All @@ -30,12 +45,31 @@ func NewIsolate() (*Isolate, error) {
return iso, nil
}

// Forcefully terminate the current thread of JavaScript execution
// in the given isolate.
// TerminateExecution terminates forcefully the current thread
// of JavaScript execution in the given isolate.
func (i *Isolate) TerminateExecution() {
C.IsolateTerminateExecution(i.ptr)
}

// GetHeapStatistics returns heap statistics for an isolate.
func (i *Isolate) GetHeapStatistics() HeapStatistics {
hs := C.IsolationGetHeapStatistics(i.ptr)

return HeapStatistics{
TotalHeapSize: uint64(hs.total_heap_size),
TotalHeapSizeExecutable: uint64(hs.total_heap_size_executable),
TotalPhysicalSize: uint64(hs.total_physical_size),
TotalAvailableSize: uint64(hs.total_available_size),
UsedHeapSize: uint64(hs.used_heap_size),
HeapSizeLimit: uint64(hs.heap_size_limit),
MallocedMemory: uint64(hs.malloced_memory),
ExternalMemory: uint64(hs.external_memory),
PeakMallocedMemory: uint64(hs.peak_malloced_memory),
NumberOfNativeContexts: uint64(hs.number_of_native_contexts),
NumberOfDetachedContexts: uint64(hs.number_of_detached_contexts),
}
}

func (i *Isolate) finalizer() {
C.IsolateDispose(i.ptr)
i.ptr = nil
Expand Down
17 changes: 17 additions & 0 deletions isolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ func TestIsolateTermination(t *testing.T) {
t.Errorf("unexpected error: %v", e)
}
}

func TestGetHeapStatistics(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
v8go.NewContext(iso)
v8go.NewContext(iso)

hs := iso.GetHeapStatistics()

if hs.NumberOfNativeContexts != 2 {
t.Error("expect NumberOfNativeContexts return 2, got", hs.NumberOfNativeContexts)
}

if hs.NumberOfDetachedContexts != 0 {
t.Error("expect NumberOfDetachedContexts return 0, got", hs.NumberOfDetachedContexts)
}
}
23 changes: 23 additions & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ void IsolateTerminateExecution(IsolatePtr ptr) {
iso->TerminateExecution();
}

IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr) {
if (ptr == nullptr) {
return IsolateHStatistics{0};
}
Isolate* iso = static_cast<Isolate*>(ptr);
v8::HeapStatistics hs;
iso->GetHeapStatistics(&hs);

return IsolateHStatistics{
hs.total_heap_size(),
hs.total_heap_size_executable(),
hs.total_physical_size(),
hs.total_available_size(),
hs.used_heap_size(),
hs.heap_size_limit(),
hs.malloced_memory(),
hs.external_memory(),
hs.peak_malloced_memory(),
hs.number_of_native_contexts(),
hs.number_of_detached_contexts()
};
}

/********** Context **********/

ContextPtr NewContext(IsolatePtr ptr) {
Expand Down
17 changes: 17 additions & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
extern "C" {
#endif

#include <stddef.h>

typedef void* IsolatePtr;
typedef void* ContextPtr;
typedef void* ValuePtr;
Expand All @@ -19,10 +21,25 @@ typedef struct {
RtnError error;
} RtnValue;

typedef struct {
size_t total_heap_size;
size_t total_heap_size_executable;
size_t total_physical_size;
size_t total_available_size;
size_t used_heap_size;
size_t heap_size_limit;
size_t malloced_memory;
size_t external_memory;
size_t peak_malloced_memory;
size_t number_of_native_contexts;
size_t number_of_detached_contexts;
} IsolateHStatistics;

extern void Init();
extern IsolatePtr NewIsolate();
extern void IsolateDispose(IsolatePtr ptr);
extern void IsolateTerminateExecution(IsolatePtr ptr);
extern IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr ptr);

extern ContextPtr NewContext(IsolatePtr prt);
extern void ContextDispose(ContextPtr ptr);
Expand Down

0 comments on commit ed27ac7

Please sign in to comment.