batch_experiment 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6db331d43fbfee1847a2385bc1c90616433451b
4
- data.tar.gz: 0b5b00e526fe8ff5d78d32991ee79e2655d24c5c
3
+ metadata.gz: b01a82b2fe96315d3ce7f39ee99e40719a56ee7e
4
+ data.tar.gz: 94f0f3f697a48958353f66f521e6b462806ee044
5
5
  SHA512:
6
- metadata.gz: 41b4f4c3866c68c86677630466e88eb574ca0ccd417a55c94eaa378d55c368673f07c5bd6dfb4018ac69d1519c730e8d6b41cc8d0c9e6ac58e51e12684435322
7
- data.tar.gz: babb03ca7415332843d231e21e30a9ecea32f3da5792fc48ccc5023c4c31eaf374c48091869e9a42c45e83b01f8d6647ea70e384d14fdecd511cdac9fc580be3
6
+ metadata.gz: 33806f836956132aa78b8efe7a2cb5972e353159ad848e156f7bde542d44d6d5e8d6fce85cdbf23e70308f367dc6bf81ba44c1645d22f65437cc4cedba2b2af7
7
+ data.tar.gz: b7b17a54beca0742a6679dccb3db6e45a49aeb1389b99421c95e5414f222d85645d985f59a42e888833e3957b4c426e99a552093caf3be795d7806f5fb76390b
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # batch_experiment
2
+
3
+ Things you could want to do, and this tool does for you:
4
+ * You want to run a batch of sh commands, but only one of those per core/cpu.
5
+ * You want to maximize the core use, the moment a core/cpu becomes free from one of your commands, you want the next command to take its place.
6
+ * You want the output of those commands to be saved by command/file (you want to have a file with what exactly the command outputted).
7
+ * You want to specify timeouts for those commands to be killed.
8
+ * You want the power to resume the batch from an interruption (i.e. system crashes, or energy goes down) without losing any work.
9
+
10
+ What conditions you need to use this tool:
11
+ * You use linux.
12
+ * You have installed: sh (the shell); ruby (obvioulsy); time (NOT the bash/sh internal command, but the [package one](https://www.archlinux.org/packages/extra/x86_64/time/)); timeout (from coreutils); taskset (from [util-linux](https://www.archlinux.org/packages/core/x86_64/util-linux/)).
13
+
14
+ What is not needed:
15
+ * To know how to program in ruby. Only taking less than 5 minutes to learn some basic syntax will suffice to run commands on multiple cores and save the results to files. However, there's an exception, if you want not only to execute the commands but want to extract and group some information from their output to a CVS, you will need to tell ruby how to do the extracting part.
16
+
17
+ ## How to use it
18
+
19
+ You will need to create a ruby script (copy, past and adapt one of the provided examples), give it execution permissions ("chmod +x script.rb"), and execute it ("./script.rb"). It's good to remember that this will probably flood the folder from were you called the command with files containing the output of the commands. Also good to remember that the commands will be called from the folder where you called the script, so they probably will expect that any relative paths/filenames given to them to be relative to the current folder.
20
+
21
+ ## Examples
22
+
23
+ After installing the gem, you will have a examples folder (/home/YOUR_USER/.gem/ruby/RUBY_VERSION/gems/batch_experiment-GEM_VERSION/examples). The sample_batch.rb gives you a good ideia of how to use #batch (no csv creation). The example_batch.rb together with the lib/batch_experiment/sample_extractors.rb gives a good ideia of how to use #experiment with multiple commands and extractors (csv creation).
24
+
25
+ ```ruby
26
+ #!/bin/ruby
27
+
28
+ require 'batch_experiment'
29
+
30
+ commands = [
31
+ 'sleep 2 && echo orange',
32
+ 'sleep 4 && echo banana',
33
+ 'sleep 100 && echo "never gonna happen"',
34
+ ]
35
+
36
+ conf = {
37
+ # IDs of the CPU cores that can be used for executing tests.
38
+ cpus_available: [0, 1],
39
+ # Maximum number of seconds that a command can run. After this a kill command
40
+ # (TERM signal) will be issued.
41
+ timeout: 5,
42
+ # Maximum number of seconds that a command can run after a kill command was
43
+ # issued. After this a kill -9 command (KILL signal) will be issued.
44
+ post_timeout: 1,
45
+ }
46
+
47
+ BatchExperiment::batch(commands, conf)
48
+ ```
49
+
50
+ This code was born in [this repository](https://github.com/henriquebecker91/masters/tree/master/codes/rb/batch_experiment).
51
+
@@ -0,0 +1,15 @@
1
+ #!/bin/ruby
2
+
3
+ require_relative '../lib/batch_experiment.rb'
4
+
5
+ commands = []
6
+ 10000.times { | n | commands << "sleep 1 && echo #{n}" }
7
+
8
+ conf = {
9
+ cpus_available: [1, 2, 3],
10
+ timeout: 5,
11
+ post_timeout: 1,
12
+ }
13
+
14
+ BatchExperiment::batch(commands, conf)
15
+
@@ -23,12 +23,16 @@ module BatchExperiment
23
23
  # terminated commands on comms_executed.
24
24
  def self.update_finished(free_cpus, comms_running, comms_executed)
25
25
  comms_running.delete_if do | job |
26
- if job[:proc].exited?
26
+ # Don't call '#exited?' twice, store value at variable. If you call
27
+ # it twice it's possible to remove it from the list of running commands
28
+ # without freeing a cpu, what will end locking all cpus forever.
29
+ exited = job[:proc].exited?
30
+ if exited
27
31
  free_cpus.push(job[:cpu])
28
32
  File.delete(job[:lockfname])
29
33
  comms_executed << job[:command]
30
34
  end
31
- job[:proc].exited? # bool returned to delete_if
35
+ exited # bool returned to delete_if
32
36
  end
33
37
  end
34
38
 
@@ -42,7 +46,8 @@ module BatchExperiment
42
46
  # After the command ends its execution this file is removed. If the command
43
47
  # ends its execution by means of a timeout the file is also removed. The file
44
48
  # only remains if the batch procedure is interrupted (script was killed,
45
- # or system crashed).
49
+ # or system crashed). This '.unfinished' file will contain the process pid,
50
+ # if the corresponding process started with success.
46
51
  #
47
52
  # @param commands [Array<String>] The shell commands.
48
53
  # @param conf [Hash] The configurations, as follows:
@@ -53,8 +58,8 @@ module BatchExperiment
53
58
  # parameter. Is the same for all the commands.
54
59
  # -- time_fmt [String] A string in the time (external command) format. See
55
60
  # http://linux.die.net/man/1/time. Default: 'ext_time: %e\next_mem: %M\n'.
56
- # busy_loop_sleep [Number] How many seconds to wait before checking if a
57
- # command ended execution. This is max time a cpu will be vacant between
61
+ # -- busy_loop_sleep [Number] How many seconds to wait before checking if
62
+ # a command ended execution. This is max time a cpu will be vacant between
58
63
  # two commands. Default: 0.1.
59
64
  # -- post_timeout [Number] A command isn't guaranteed to end after
60
65
  # receiving a TERM signal. If the command hasn't stopped, waits
@@ -168,6 +173,9 @@ module BatchExperiment
168
173
  command: command
169
174
  }
170
175
 
176
+ # The lock file now stores the process pid for debug reasons.
177
+ File.open(lockfname, 'w') { | f | f.write cproc.pid }
178
+
171
179
  puts "command assigned to cpu#{cpu}"
172
180
  STDOUT.flush
173
181
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch_experiment
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique Becker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-23 00:00:00.000000000 Z
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: childprocess
@@ -30,7 +30,9 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - README.md
33
34
  - examples/bible.txt
35
+ - examples/debug_batch.rb
34
36
  - examples/example_batch.rb
35
37
  - examples/sample_batch.rb
36
38
  - examples/taoteching.txt