Skip to content

Instantly share code, notes, and snippets.

@kakra kakra/defrag-preloaded.rb

Last active Dec 17, 2015
Embed
What would you like to do?
A very basic ruby script for defragmenting files on btrfs loaded by preload daemon
#!/bin/env ruby
#
# basic concept script for defragmenting files on btrfs loaded by preload daemon
#
# Actually, this idea is working in the spirit of systemd's readahead function which
# can defrag (and relocate?) files on btrfs during boot. Preload takes on where
# readahead stops working, so it should be a great place to continue integrating that
# idea there. This script is actually here, to test the impact of such an idea and
# find first caveats and show-stoppers.
#
# Author: Kai Krakow <hurikhan77@gmail.com>
# License: GPL3
file_already_queued = {}
IO.popen('xargs --no-run-if-empty --max-args=20 --max-procs=3 --null btrfs filesystem defragment -v -clzo', 'w') do |xargs|
File.foreach('/var/lib/preload/preload.state') do |line|
next unless %r{^(EXE|MAP)\s.*?file://(.*)} =~ line
next if file_already_queued[name = $2]
if File.exists?(name)
xargs.print "#{name}\0"
file_already_queued[name] = true
end
end
xargs.flush
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.