Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API for getting memory allocation statistics #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions malloc_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,19 @@ func getChunk() []byte {
func putChunk(chunk []byte) {
// No-op.
}

// GetOffHeapMemoryStats returns the memory allocation statistics from off-heap
// memory. This function returns two numbers:
//
// - The first number represents the total memory allocated from the off-heap,
// including both used and free memory.
//
// - The second number represents the memory size which is allocated from
// the off-heap but currently not in use (free memory).
//
// Note that these statistics provide insights into the off-heap memory usage,
// which is memory managed directly by the application and not subject to golang
// garbage collection.
func GetOffHeapMemoryStats() (uint64, uint64) {
return 0, 0
}
27 changes: 27 additions & 0 deletions malloc_mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const chunksPerAlloc = 1024
var (
freeChunks []*[chunkSize]byte
freeChunksLock sync.Mutex

// Statistics of memory allocated via off-heap, guarded by lock.
allocatedSize uint64 // The total memory size allocated from the offheap
freeChunkSize uint64 // The total memory size allocated but not used
)

func getChunk() []byte {
Expand All @@ -31,12 +35,15 @@ func getChunk() []byte {
p := (*[chunkSize]byte)(unsafe.Pointer(&data[0]))
freeChunks = append(freeChunks, p)
data = data[chunkSize:]
allocatedSize += chunkSize
freeChunkSize += chunkSize
}
}
n := len(freeChunks) - 1
p := freeChunks[n]
freeChunks[n] = nil
freeChunks = freeChunks[:n]
freeChunkSize -= chunkSize
freeChunksLock.Unlock()
return p[:]
}
Expand All @@ -50,5 +57,25 @@ func putChunk(chunk []byte) {

freeChunksLock.Lock()
freeChunks = append(freeChunks, p)
freeChunkSize += chunkSize
freeChunksLock.Unlock()
}

// GetOffHeapMemoryStats returns the memory allocation statistics from off-heap
// memory. This function returns two numbers:
//
// - The first number represents the total memory allocated from the off-heap,
// including both used and free memory.
//
// - The second number represents the memory size which is allocated from
// the off-heap but currently not in use (free memory).
//
// Note that these statistics provide insights into the off-heap memory usage,
// which is memory managed directly by the application and not subject to golang
// garbage collection.
func GetOffHeapMemoryStats() (uint64, uint64) {
freeChunksLock.Lock()
defer freeChunksLock.Unlock()

return allocatedSize, freeChunkSize
}