|
1 | 1 | /* |
2 | 2 |
|
3 | 3 | * This file is part of the RonDB REST API Server |
4 | | - * Copyright (c) 2023 Hopsworks AB |
| 4 | + * Copyright (c) 2025 Hopsworks AB |
5 | 5 | * |
6 | 6 | * This program is free software: you can redistribute it and/or modify |
7 | 7 | * it under the terms of the GNU General Public License as published by |
@@ -37,7 +37,6 @@ type Heap struct { |
37 | 37 | buffers []*NativeBuffer |
38 | 38 | buffersStats MemoryStats |
39 | 39 | mutex *sync.Mutex |
40 | | - minAllocatedBuffers int64 |
41 | 40 | allocatedBuffers int64 |
42 | 41 | maxAllocatedBuffers int64 |
43 | 42 | } |
@@ -68,7 +67,6 @@ func New() (heap *Heap, releaseBuffers func(), err error) { |
68 | 67 | heap = &Heap{ |
69 | 68 | buffers: []*NativeBuffer{}, |
70 | 69 | mutex: &sync.Mutex{}, |
71 | | - minAllocatedBuffers: preAllocatedBuffers, |
72 | 70 | allocatedBuffers: preAllocatedBuffers, |
73 | 71 | maxAllocatedBuffers: maxAllocatedBuffers, |
74 | 72 | buffersStats: MemoryStats{ |
@@ -101,9 +99,10 @@ func (heap *Heap) releaseAllBuffers() { |
101 | 99 | heap.mutex.Lock() |
102 | 100 | defer heap.mutex.Unlock() |
103 | 101 |
|
104 | | - if heap.buffersStats.BuffersCount != int64(len(heap.buffers)) { |
| 102 | + stats := heap.getNativeBuffersStatsInt() |
| 103 | + if stats.BuffersCount != int64(len(heap.buffers)) { |
105 | 104 | log.Warnf("Shutting down heap. Number of free buffers do not match. Expecting: %d, Got: %d.", |
106 | | - heap.buffersStats.BuffersCount, int64(len(heap.buffers))) |
| 105 | + stats.BuffersCount, int64(len(heap.buffers))) |
107 | 106 | } |
108 | 107 |
|
109 | 108 | for _, buffer := range heap.buffers { |
@@ -136,21 +135,19 @@ func (heap *Heap) GetBuffer() (buff *NativeBuffer, returnBuff func(), err error) |
136 | 135 | func (heap *Heap) returnBuffer(buffer *NativeBuffer) { |
137 | 136 | heap.mutex.Lock() |
138 | 137 | defer heap.mutex.Unlock() |
139 | | - |
140 | | - if int64(len(heap.buffers)) < heap.minAllocatedBuffers { |
141 | | - heap.buffers = append(heap.buffers, buffer) |
142 | | - } else { |
143 | | - C.free(buffer.Buffer) |
144 | | - heap.allocatedBuffers-- |
145 | | - heap.buffersStats.DeallocationsCount++ |
146 | | - } |
| 138 | + heap.buffers = append(heap.buffers, buffer) |
147 | 139 | } |
148 | 140 |
|
149 | 141 | func (heap *Heap) GetNativeBuffersStats() MemoryStats { |
150 | 142 | heap.mutex.Lock() |
151 | 143 | defer heap.mutex.Unlock() |
| 144 | + return heap.getNativeBuffersStatsInt() |
| 145 | +} |
152 | 146 |
|
153 | | - // Only (De)AllocationsCount are updated continuously. Update the others now. |
| 147 | +/* |
| 148 | +* with out locks |
| 149 | + */ |
| 150 | +func (heap *Heap) getNativeBuffersStatsInt() MemoryStats { |
154 | 151 | heap.buffersStats.BuffersCount = heap.allocatedBuffers |
155 | 152 | heap.buffersStats.FreeBuffers = int64(len(heap.buffers)) |
156 | 153 | return heap.buffersStats |
|
0 commit comments