-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add getMemoryStats endpoint to registry
#486
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a1521b8
Add 'getMemoryStats' endpoint to registry
rvanasa 54f5b0f
Address Copilot review feedback on getMemoryStats endpoint
rvanasa 135a9fa
perf: limit getMemoryStats overhead via systematic sampling
rvanasa cbe8bc3
revert: restore getMemoryStats as a query call
rvanasa c144cf3
Remove getMemoryStats tests for now (not possible with WASI / interpr…
rvanasa c574844
Fix sampling
rvanasa c3a6740
Account for Copilot review suggestions
rvanasa fcf2c29
Don't bump API version
rvanasa 7d4bddf
Lower sample size to 1000
rvanasa b7a9f49
Add comment
rvanasa c0674f9
Merge branch 'main' into registry-memory-stats
rvanasa 9de56a9
refactor: extract shared sampling helpers into MemoryStats.mo
rvanasa d4a43bb
Merge branch 'registry-memory-stats' of https://github.com/dfinity/mo…
rvanasa 3029b30
Simplify MemoryStats: add statsFor* helpers, deduplicate sampling loop
rvanasa 9907fd3
Fix unrelated behavior change
rvanasa d2d3641
Address reviewer feedback on getMemoryStats
rvanasa deb1c77
Remove dead code and fix warnings
rvanasa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import Nat "mo:base/Nat"; | ||
| import Buffer "mo:base/Buffer"; | ||
| import TrieMap "mo:base/TrieMap"; | ||
| import Iter "mo:base/Iter"; | ||
|
|
||
| module { | ||
| public type StructureStats = { | ||
| count : Nat; | ||
| bytes : Nat; | ||
| }; | ||
|
|
||
| public type MemoryStats = { | ||
| rtsHeapSize : Nat; | ||
| rtsMemorySize : Nat; | ||
|
|
||
| packageVersions : StructureStats; | ||
| packageConfigs : StructureStats; | ||
| highestConfigs : StructureStats; | ||
| packagePublications : StructureStats; | ||
| ownersByPackage : StructureStats; | ||
| maintainersByPackage : StructureStats; | ||
| fileIdsByPackage : StructureStats; | ||
| hashByFileId : StructureStats; | ||
| packageFileStats : StructureStats; | ||
| packageTestStats : StructureStats; | ||
| packageBenchmarks : StructureStats; | ||
| packageNotes : StructureStats; | ||
| packageDocsCoverage : StructureStats; | ||
|
|
||
| downloadsByPackageName : StructureStats; | ||
| downloadsByPackageId : StructureStats; | ||
| dailySnapshots : StructureStats; | ||
| weeklySnapshots : StructureStats; | ||
| dailySnapshotsByPackageName : StructureStats; | ||
| dailySnapshotsByPackageId : StructureStats; | ||
| weeklySnapshotsByPackageName : StructureStats; | ||
| weeklySnapshotsByPackageId : StructureStats; | ||
| dailyTempRecords : StructureStats; | ||
| weeklyTempRecords : StructureStats; | ||
|
|
||
| storages : StructureStats; | ||
| storageByFileId : StructureStats; | ||
|
|
||
| users : StructureStats; | ||
| names : StructureStats; | ||
| }; | ||
|
|
||
| public let SAMPLE_SIZE : Nat = 1_000; | ||
|
|
||
| // Serializes a systematic sample of up to SAMPLE_SIZE elements from an | ||
| // iterator and extrapolates the total byte count, keeping to_candid | ||
| // allocations bounded. | ||
| public func sampleIterBytes<V>( | ||
| iter : Iter.Iter<V>, | ||
| total : Nat, | ||
| serialize : V -> Blob, | ||
| ) : Nat { | ||
| if (total == 0) return 0; | ||
| let stride = if (total <= SAMPLE_SIZE) 1 else (total + SAMPLE_SIZE - 1 : Nat) / SAMPLE_SIZE; | ||
| var sum = 0; | ||
| var i = 0; | ||
| var sampled = 0; | ||
| label sampleLoop for (v in iter) { | ||
| if (sampled >= SAMPLE_SIZE) break sampleLoop; | ||
| if (i % stride == 0) { | ||
| sum += serialize(v).size(); | ||
| sampled += 1; | ||
| }; | ||
| i += 1; | ||
| }; | ||
| if (sampled == 0) 0 else sum * total / sampled; | ||
| }; | ||
|
|
||
| // Two-level sampling for TrieMaps whose values are Buffers. | ||
| // Budgets to_candid calls across both keys and their inner elements. | ||
| public func sampleMapOfBuffersBytes<K, V>( | ||
| map : TrieMap.TrieMap<K, Buffer.Buffer<V>>, | ||
| serialize : V -> Blob, | ||
| ) : Nat { | ||
| let total = map.size(); | ||
| if (total == 0) return 0; | ||
| let numSampledKeys = Nat.min(total, SAMPLE_SIZE); | ||
| let perKeyBudget = Nat.max(1, SAMPLE_SIZE / numSampledKeys); | ||
| let stride = if (total <= SAMPLE_SIZE) 1 else (total + SAMPLE_SIZE - 1 : Nat) / SAMPLE_SIZE; | ||
| var sum = 0; | ||
| var i = 0; | ||
| var sampled = 0; | ||
| label sampleLoop for ((_, buf) in map.entries()) { | ||
| if (sampled >= SAMPLE_SIZE) break sampleLoop; | ||
| if (i % stride == 0) { | ||
| let bufTotal = buf.size(); | ||
| if (bufTotal > 0) { | ||
| let bufStride = if (bufTotal <= perKeyBudget) 1 else (bufTotal + perKeyBudget - 1 : Nat) / perKeyBudget; | ||
| var j = 0; | ||
| var bufSum = 0; | ||
| var bufSampled = 0; | ||
| while (j < bufTotal and bufSampled < perKeyBudget) { | ||
| bufSum += serialize(buf.get(j)).size(); | ||
| bufSampled += 1; | ||
| j += bufStride; | ||
| }; | ||
| sum += bufSum * bufTotal / bufSampled; | ||
| }; | ||
| sampled += 1; | ||
| }; | ||
| i += 1; | ||
| }; | ||
| if (sampled == 0) return 0; | ||
| sum * total / sampled; | ||
| }; | ||
|
|
||
| // Convenience: returns { count; bytes } for a TrieMap. | ||
| public func statsForMap<K, V>( | ||
| map : TrieMap.TrieMap<K, V>, | ||
| serialize : (K, V) -> Blob, | ||
| ) : StructureStats { | ||
| let total = map.size(); | ||
| { | ||
| count = total; | ||
| bytes = sampleIterBytes( | ||
| map.entries(), | ||
| total, | ||
| func((k, v) : (K, V)) : Blob = serialize(k, v), | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
| // Convenience: returns { count; bytes } for a Buffer. | ||
| public func statsForBuffer<V>( | ||
| buf : Buffer.Buffer<V>, | ||
| serialize : V -> Blob, | ||
| ) : StructureStats { | ||
| { | ||
| count = buf.size(); | ||
| bytes = sampleIterBytes(buf.vals(), buf.size(), serialize); | ||
| }; | ||
| }; | ||
|
|
||
| // Convenience: returns { count; bytes } for an arbitrary Iter + known size. | ||
| public func statsForIter<V>( | ||
| iter : Iter.Iter<V>, | ||
| total : Nat, | ||
| serialize : V -> Blob, | ||
| ) : StructureStats { | ||
| { count = total; bytes = sampleIterBytes(iter, total, serialize) }; | ||
| }; | ||
|
|
||
| // Convenience: returns { count; bytes } for a TrieMap<K, Buffer<V>>. | ||
| public func statsForMapOfBuffers<K, V>( | ||
| map : TrieMap.TrieMap<K, Buffer.Buffer<V>>, | ||
| serialize : V -> Blob, | ||
| ) : StructureStats { | ||
| { count = map.size(); bytes = sampleMapOfBuffersBytes(map, serialize) }; | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.