fireworks 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0fe5cd9e7bcef7e37ce48b908cc335e79ca4a140
4
+ data.tar.gz: 1efda50386a57ba7c90043984464130bd7213e8f
5
+ SHA512:
6
+ metadata.gz: 2dccce2cdb849a06ae0b322e5917a3f5aeab40fc7b0e4bc990dd7c592b41ea1b0ff14ac79e7871c207da1b98808a242972113c1cfe7be9421ecb389215156735
7
+ data.tar.gz: 3a4313d7012f1a3b7bc2ca4d3609401e1b1cc98e6bd3e439c46eb36d2fc35e68d4a7f2fa3f866ce548eeeb70bad3baf57205f6b1298089a360d264c8647a2fd9
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://www.rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Kongregate
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '/../lib'))
4
+ require 'fireworks'
5
+
6
+ require 'optparse'
7
+
8
+ options = {}
9
+ optparse = OptionParser.new do |opts|
10
+ opts.banner = 'Usage: ebs-firweorks.rb [options]'
11
+
12
+ opts.on('-d', '--device DEVICE', 'Device to prewarm') do |device|
13
+ options[:device] = device
14
+ end
15
+
16
+ opts.on('-t', '--threads THREADS', Integer, 'Number of threads to run') do |threads|
17
+ options[:threads] = threads
18
+ end
19
+
20
+ opts.on('-i', '--interval INTERVAL', Integer, 'Interval, in seconds, for updates') do |interval|
21
+ options[:interval] = interval
22
+ end
23
+ end
24
+
25
+ begin
26
+ optparse.parse!
27
+ mandatory = [:device]
28
+ missing = mandatory.select { |param| options[param].nil? }
29
+ unless missing.empty?
30
+ puts "Missing options: #{missing.join(', ')}"
31
+ puts optparse
32
+ exit
33
+ end
34
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
35
+ puts e
36
+ puts optparse
37
+ exit
38
+ end
39
+
40
+ Fireworks::BlockDevice.new(options).prewarm
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'fireworks'
3
+ s.version = '0.0.1'
4
+ s.date = '2015-08-06'
5
+ s.summary = 'Faster EBS prewarming'
6
+ s.description = 'Faster EBS prewarming via running multiple dds in parallel'
7
+ s.authors = ['Andrew Grim']
8
+ s.email = 'stopdropandrew@gmail.com'
9
+ s.files = `git ls-files -z`.split("\x0")
10
+ s.executables = ['fireworks']
11
+ s.homepage = 'https://github.com/kongregate/fireworks'
12
+ s.license = 'MIT'
13
+
14
+ s.add_runtime_dependency 'filesize'
15
+ s.add_runtime_dependency 'chronic_duration'
16
+ end
@@ -0,0 +1 @@
1
+ require_relative 'fireworks/block_device'
@@ -0,0 +1,133 @@
1
+ require 'open3'
2
+ require 'filesize'
3
+ require 'chronic_duration'
4
+
5
+ module Fireworks
6
+ class BlockDevice
7
+ attr_reader :device, :threads, :interval
8
+
9
+ def initialize(device:, threads: 1, interval: 5)
10
+ @threadpool = []
11
+
12
+ @device = device
13
+ @threads = threads
14
+ @interval = interval
15
+ raise ArgumentError, "Device #{device} does not exist" unless File.exist?(device)
16
+ raise ArgumentError, "Device #{device} is empty" unless device_size > 0
17
+ raise ArgumentError, "Device #{device} is already mounted" if already_mounted?
18
+ end
19
+
20
+ def device_size
21
+ @device_size ||= `blockdev --getsize64 #{device}`.to_i
22
+ end
23
+
24
+ def already_mounted?
25
+ !`mount | grep #{device}`.empty?
26
+ end
27
+
28
+ def prewarm
29
+ start_threads
30
+
31
+ until @threadpool.all?(&:complete?)
32
+ @threadpool.each(&:update_status)
33
+
34
+ output_stats
35
+ sleep interval
36
+ end
37
+ end
38
+
39
+ def current_stats
40
+ {
41
+ bytes_completed: @threadpool.map(&:bytes_completed).inject(:+),
42
+ total_rate: @threadpool.map(&:rate).inject(:+),
43
+ num_up_to_date: @threadpool.select(&:up_to_date?).size,
44
+ num_complete: @threadpool.select(&:complete?).size
45
+ }
46
+ end
47
+
48
+ def output_stats
49
+ stats = current_stats
50
+ bytes_completed = Filesize.new(stats[:bytes_completed])
51
+ total_rate = Filesize.new(stats[:total_rate])
52
+ device_file_size = Filesize.new(device_size)
53
+ time_left = ((device_file_size - bytes_completed) / total_rate)
54
+ puts format(
55
+ '%s / %s (%.2f%%) [%s/s] %d UpToDate - %d Complete - ETA %s',
56
+ bytes_completed.pretty,
57
+ device_file_size.pretty,
58
+ 100.0 * bytes_completed / device_file_size,
59
+ total_rate.pretty,
60
+ stats[:num_up_to_date],
61
+ stats[:num_complete],
62
+ total_rate > 0 ? ChronicDuration.output(time_left, format: :short) : 'Infinity'
63
+ )
64
+ end
65
+
66
+ private
67
+
68
+ def start_threads
69
+ total_chunks = Filesize.new(device_size) / Filesize.from('1MiB')
70
+ chunks_per_thread = (total_chunks / threads).to_i
71
+
72
+ 1.upto(threads).each do |thread_num|
73
+ start_point = chunks_per_thread * (thread_num - 1)
74
+
75
+ stdin, stdout, stderr, thread = Open3.popen3("dd if=#{device} of=#{device} bs=1M skip=#{start_point} seek=#{start_point} count=#{chunks_per_thread} conv=notrunc")
76
+ stdin.close
77
+ stdout.close
78
+
79
+ @threadpool.push(Fireworks::DDThread.new(thread: thread, stderr: stderr))
80
+ end
81
+ end
82
+ end
83
+
84
+ class DDThread
85
+ attr_reader :bytes_completed, :rate
86
+
87
+ def initialize(thread:, stderr:)
88
+ @thread = thread
89
+ @stderr = stderr
90
+ @eof = false
91
+ @bytes_completed = 0
92
+ @rate = 0
93
+ @updated_at = nil
94
+ end
95
+
96
+ def complete?
97
+ !@thread.alive? && @eof
98
+ end
99
+
100
+ def update_status
101
+ usr1! # send signal to dump stderr
102
+
103
+ sleep 0.1 # need a short sleep to let IO threads run
104
+
105
+ buffer = attempt_read
106
+ most_recent_status = buffer.to_s.split("\n").last
107
+ return unless most_recent_status && (match = most_recent_status.match(/(\d+) bytes.*, (\d+(?:\.\d+)? \w+)\/s/))
108
+
109
+ @bytes_completed = match[1].to_i
110
+ @rate = Filesize.from(match[2]).to_i
111
+ @updated_at = Time.now
112
+ end
113
+
114
+ def up_to_date?
115
+ return false if @updated_at.nil?
116
+ Time.now - @updated_at < 120
117
+ end
118
+
119
+ private
120
+
121
+ def attempt_read
122
+ @stderr.read_nonblock(8196)
123
+ rescue Errno::EAGAIN, Errno::EWOULDBLOCK
124
+ return # No data!
125
+ rescue EOFError
126
+ @eof = true
127
+ end
128
+
129
+ def usr1!
130
+ Process.kill('USR1', @thread.pid) if @thread.alive?
131
+ end
132
+ end
133
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fireworks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Grim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: filesize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: chronic_duration
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Faster EBS prewarming via running multiple dds in parallel
42
+ email: stopdropandrew@gmail.com
43
+ executables:
44
+ - fireworks
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - Rakefile
52
+ - bin/fireworks
53
+ - fireworks.gemspec
54
+ - lib/fireworks.rb
55
+ - lib/fireworks/block_device.rb
56
+ homepage: https://github.com/kongregate/fireworks
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Faster EBS prewarming
80
+ test_files: []