-
Notifications
You must be signed in to change notification settings - Fork 66
Tracing GC for Backends #24
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
Open
dgrnbrg
wants to merge
4
commits into
master
Choose a base branch
from
tracing-gc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| (ns hitchhiker.tracing-gc | ||
| (:require [hitchhiker.tree.core :as hh])) | ||
|
|
||
| ;; Note: this implementation is single-threaded, and could be made parallel without too much effort | ||
|
|
||
| ;; We might need to trace millions or billions of keys. That might not fit in memory, so this could be backed | ||
| ;; by leveldb or hsql so that we can spill to disk when necessary. We don't need a functional datastructure here. | ||
| (defprotocol IGCScratch | ||
| (add-to-work-queue! [this addr] "Adds the given address to the work queue to be processed") | ||
| (pop-from-work-queue! [this] "Pops the next element off of the work queue, or returns nil if we're done") | ||
| (observe-addr! [this addr] "Marks the given addr as being currently active") | ||
| (observed? [this addr] "Returns true if the given addr was observed")) | ||
| ; | ||
| ;;; The workq is a ref containing a collection of addresses we still need to scan. | ||
| ;;; The observed-set is a ref containing the set of addresses we know are active | ||
| ;;; For simplicity, adding an addr to the workq automatically observes it as well | ||
| ;;; ^^ this allows us to only add new addrs to the workq, without a separate set of "in workq" | ||
| (defrecord InMemScratch [workq observed-set] | ||
| IGCScratch | ||
| (add-to-work-queue! [_ addr] | ||
| (dosync | ||
| (when-not (contains? @observed-set addr) | ||
| (alter workq conj addr) | ||
| (alter observed-set conj addr)))) | ||
| (pop-from-work-queue! [_] | ||
| (dosync | ||
| (when (seq @workq) | ||
| (let [head (peek @workq)] | ||
| (alter workq pop) | ||
| head)))) | ||
| (observe-addr! [_ addr] | ||
| (dosync | ||
| (alter conj observed-set addr))) | ||
| (observed? [_ addr] | ||
| (contains? @observed-set addr))) | ||
|
|
||
| (defn in-mem-scratch | ||
| "Creates an instance of in memory GC scratch" | ||
| [] | ||
| (->InMemScratch [] #{})) | ||
|
|
||
| (defn trace-gc! | ||
| "Does a tracing GC and frees up all unused keys. | ||
| This is a simple mark-sweep algorithm. | ||
|
|
||
| gc-scratch should be an instance of IGCScratch | ||
| gc-roots should be a list of the roots, which should implement IResolve. These are generated by calls to anchor-root. | ||
| all-keys should be a lazy sequence that will contain every key in storage. This algorithm will not hold the whole sequence in memory | ||
| delete-fn will be called on every key that should be deleted during the sweep phase" | ||
| [gc-scratch gc-roots all-keys delete-fn] | ||
| ;; First, we'll initialize the work queue | ||
| (doseq [root gc-roots] | ||
| (add-to-work-queue! gc-scratch root)) | ||
| ;; Now, we'll do the mark phase | ||
| (loop [] | ||
| (when-let [addr (pop-from-work-queue! gc-scratch)] | ||
| (observe-addr! gc-scratch addr) | ||
| (let [node (hh/resolve addr)] | ||
| (when (hh/index-node? node) | ||
| (doseq [c (:children node)] | ||
| (add-to-work-queue! gc-scratch c)))) | ||
| (recur))) | ||
| ;; Next, we do the sweep | ||
| (loop [ks all-keys] | ||
| (when (seq ks) | ||
| (let [head (first ks)] | ||
| (when-not (observed? gc-scratch head) | ||
| (delete-fn head))) | ||
| (recur (next ks))))) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
resolveshould only be necessary if the current node is an index node. However, we currently can't tell from an address if we're looking at an index or data node, and so we also resolve all data nodes, which is terrible for perf.IResolveshould have a methodindex?or something, so that we can dramatically reduce the IO by never reading data nodes into memory.