Skip to content

Instantly share code, notes, and snippets.

@wataash
Last active December 29, 2022 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wataash/aba98b18f5fc86d604bfe3ef087fb63e to your computer and use it in GitHub Desktop.
Save wataash/aba98b18f5fc86d604bfe3ef087fb63e to your computer and use it in GitHub Desktop.
systemd-analyze dot (only the top 30 units with the most appearances)
require "set"
unless ARGV.length == 1
warn("usage: systemd-analyze dot | systemd-analyze-dot-only-n N")
exit(1)
end
n = ARGV.shift.to_i(10)
txt = ARGF.read
pairs_unit_count = Hash.new(0)
txt.scan(/^\t"(.+?)"->"(.+?)" \[color="\w+"\];$/) do |left, right|
pairs_unit_count[left] += 1
pairs_unit_count[right] += 1
end
n_units = pairs_unit_count.sort_by { |_unit, count| -count }.first(n).map { |unit, _count| unit }.to_set
# n_units.add("postfix.service")
# n_units.delete("shutdown.target")
txt.split(/\r?\n/) do |line|
unless line.start_with?("\t")
puts(line)
next
end
match_data = /^\t"(.+?)"->"(.+?)" \[color="\w+"\];$/.match(line)
if match_data.nil?
warn("unexpected line: #{line}")
next
end
next unless n_units.include?(match_data[1]) && n_units.include?(match_data[2])
puts(line)
end
@wataash
Copy link
Author

wataash commented Dec 29, 2022

systemd-analyze dot | ruby systemd-analyze-dot-only-n.rb 30 | dot -Tsvg > /tmp/systemd.svg
# with:
# n_units.add("postfix.service")
# n_units.delete("shutdown.target")

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment