Module: Pwrake::Parallelism

Defined in:
lib/pwrake/report/parallelism.rb

Class Method Summary collapse

Class Method Details

.count_start_end_by_pattern(csvtable, pattern) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/pwrake/report/parallelism.rb', line 233

def count_start_end_by_pattern(csvtable, pattern)
  h = Hash.new
  start_time = nil

  csvtable.each do |row|
    command = row['command']
    if command == 'pwrake_profile_start'
      start_time = Time.parse(row['start_time'])
    elsif command == 'pwrake_profile_end'
      t = Time.parse(row['start_time']) - start_time
      h.values.each do |a|
        a << [t,0]
      end
    elsif start_time
      cmd = get_command_key(command, pattern)
      h[cmd] ||= []
      t = Time.parse(row['start_time']) - start_time
      h[cmd] << [t,+1]
      t = Time.parse(row['end_time']) - start_time
      h[cmd] << [t,-1]
    end
  end

  h.each do |k,a|
    a.sort!{|x,y| x[0]<=>y[0]}
  end
  h
end

.count_start_end_from_csv(file) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pwrake/report/parallelism.rb', line 6

def count_start_end_from_csv(file)
  a = []
  start_time = nil

  CSV.foreach(file,:headers=>true) do |row|
    if row['command'] == 'pwrake_profile_start'
      start_time = Time.parse(row['start_time'])
    elsif row['command'] == 'pwrake_profile_end'
      t = Time.parse(row['start_time']) - start_time
      a << [t,0]
    elsif start_time
      t = Time.parse(row['start_time']) - start_time
      a << [t,+1]
      t = Time.parse(row['end_time']) - start_time
      a << [t,-1]
    end
  end

  a.sort{|x,y| x[0]<=>y[0]}
end

.count_start_end_from_csv_table(csvtable) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pwrake/report/parallelism.rb', line 27

def count_start_end_from_csv_table(csvtable)
  a = []
  start_time = nil

  csvtable.each do |row|
    if row['command'] == 'pwrake_profile_start'
      start_time = Time.parse(row['start_time'])
    elsif row['command'] == 'pwrake_profile_end'
      t = Time.parse(row['start_time']) - start_time
      a << [t,0]
    elsif start_time
      t = Time.parse(row['start_time']) - start_time
      a << [t,+1]
      t = Time.parse(row['end_time']) - start_time
      a << [t,-1]
    end
  end

  a.sort{|x,y| x[0]<=>y[0]}
end

.exec_density(a) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pwrake/report/parallelism.rb', line 48

def exec_density(a)
  reso = 0.1
  delta = 1/reso
  t_end = (a.last)[0]
  n = (t_end/reso).to_i + 1
  i = 0
  t_next = reso
  d = (n+1).times.map{|i| [reso*i,0]}
  a.each do |x|
    while d[i+1][0] <= x[0]
      i += 1
    end
    if x[1] == 1
      d[i][1] += delta
    end
  end
  d
end

.get_command_key(s, pattern) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/pwrake/report/parallelism.rb', line 213

def get_command_key(s, pattern)
  pattern.each do |cmd,regex|
    if regex =~ s
      return cmd
    end
  end
  if /\(([^()]+)\)/ =~ s
    s = $1
  end
  a = s.split(/;/)
  a.each do |x|
    if /^\s*(\S+)/ =~ x
      k = $1
      next if k=='cd'
      return k
    end
  end
  nil
end

.plot_parallelism(file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pwrake/report/parallelism.rb', line 67

def plot_parallelism(file)
  a = count_start_end_from_csv(file)
  return if a.size < 4

  density = exec_density(a)

  base = file.sub(/\.csv$/,"")
  fpara = base+"_para.dat"

  n = a.size
  i = 0
  y = 0
  y_max = 0

  File.open(fpara,"w") do |f|
    begin
      t = 0
      y_pre = 0
      n.times do |i|
        if a[i][0]-t > 0.001
          f.printf "%.3f %d\n", t, y_pre
          t = a[i][0]
          f.printf "%.3f %d\n", t, y
        end
        y += a[i][1]
        y_pre = y
        y_max = y if y > y_max
      end
    rescue
      p a[i]
    end
  end

  t_end = (a.last)[0]

  IO.popen("gnuplot","r+") do |f|
    f.puts "
set terminal png
set output '#{base}.png'
#set rmargin 10
set title '#{base}'
set xlabel 'time (sec)'
set ylabel 'parallelism'

set arrow 1 from #{t_end},#{y_max*0.5} to #{t_end},0 linecolor rgb 'blue'
set label 1 at first #{t_end},#{y_max*0.5} right \"#{t_end}\\nsec\" textcolor rgb 'blue'

plot '#{fpara}' w l axis x1y1 title 'parallelism'
"
  end

  #puts "Parallelism plot: #{base}.png"
end

.plot_parallelism2(csvtable, base) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pwrake/report/parallelism.rb', line 122

def plot_parallelism2(csvtable, base)
  a = count_start_end_from_csv_table(csvtable)
  return if a.size < 4

  density = exec_density(a)

  fimg = base+'/parallelism.png'

  n = a.size
  i = 0
  y = 0
  y_max = 0

  para = []
  begin
    t = 0
    y_pre = 0
    n.times do |i|
      if a[i][0]-t > 0.001
        para.push "%.3f %d" % [t, y_pre]
        t = a[i][0]
        para.push "%.3f %d" % [t, y]
      end
      y += a[i][1]
      y_pre = y
      y_max = y if y > y_max
    end
  rescue
    p a[i]
  end

  t_end = (a.last)[0]

  if system("which gnuplot >/dev/null 2>&1")
  IO.popen("gnuplot","r+") do |f|
    f.print "
set terminal png
set output '#{fimg}'
#set rmargin 10
set title '#{base}'
set xlabel 'time (sec)'
set ylabel 'parallelism'
set y2tics
set ytics nomirror
set y2label 'exec/sec'

set arrow 1 from #{t_end},#{y_max*0.5} to #{t_end},0 linecolor rgb 'blue'
set label 1 \"#{t_end}\\nsec\" at first #{t_end},#{y_max*0.5} right front textcolor rgb 'blue'

plot '-' w l axis x1y1 title 'parallelism', '-' w l axis x1y2 title 'exec/sec'
"
    para.each do |x|
      f.puts x
    end
    f.puts "e"

    density.each do |t,d|
      f.puts "#{t} #{d}"
    end
  end
  end

  #puts "Parallelism plot: #{fimg}"
  fimg
end

.plot_parallelism_by_host(csvtable, base) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/pwrake/report/parallelism.rb', line 341

def plot_parallelism_by_host(csvtable,base)
  fpng = base+"/para_host.png"
  data = read_time_by_host_from_csv(csvtable)
  return fpng if data.size == 0

  grid = []
  hosts = data.keys.sort
  hosts.each do |h|
    a = timeline_to_grid(data[h])
    grid << a
  end

  if system("which gnuplot >/dev/null 2>&1")
  IO.popen("gnuplot","r+") do |f|
    f.puts "
set terminal png
set output '#{fpng}'
#set rmargin 7
set lmargin 16
set pm3d map
set pm3d corners2color c1
set xlabel 'time (sec)'
set ytics nomirror
set ticslevel 0
set format y ''
"
    hosts.each_with_index do |h,i|
      if /^([^.]+)\./ =~ h
        h = $1
      end
      f.puts "set ytics add ('#{h}' #{i+0.5})"
    end
    f.puts "splot '-' using 2:1:3 with pm3d title ''"

    grid.each_with_index do |a,j|
      a.each do |x|
        f.printf "%g %g %d\n", j, x[0], x[1]
      end
      f.printf "\n"
    end
    j = grid.size
    grid.last.each do |x|
      f.printf "%g %g %d\n", j, x[0], x[1]
    end
    f.printf "e\n"
  end
  end
  fpng
end

.plot_parallelism_by_pattern(csvtable, base, pattern) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/pwrake/report/parallelism.rb', line 262

def plot_parallelism_by_pattern(csvtable, base, pattern)
  y_max = 0
  t_end = 0
  para = {}
  t_pat = count_start_end_by_pattern(csvtable, pattern)
  t_pat.each do |cmd,a|
    n = a.size
    i = 0
    y = 0
    para[cmd] = dat = []
    begin
      t = 0
      y_pre = 0
      n.times do |i|
        if a[i][0]-t > 0.001
          dat.push "%.3f %d" % [t, y_pre]
          t = a[i][0]
          dat.push "%.3f %d" % [t, y]
        end
        y += a[i][1]
        y_pre = y
        y_max = y if y > y_max
      end
    rescue
      p a[i]
    end
    if (a.last)[0] > t_end
      t_end = (a.last)[0]
    end
  end

  fimg = base+'/para_cmd.png'

  if system("which gnuplot >/dev/null 2>&1")
  IO.popen("gnuplot","r+") do |f|
    #begin f = $stdout
    f.print "
set terminal png
set output '#{fimg}'
set title '#{base}'
set xlabel 'time (sec)'
set ylabel 'parallelism'
"
    f.print "plot "
    f.puts para.map{|cmd,re| "'-' w l title '#{cmd}'"}.join(",")
    para.each do |cmd,dat|
      dat.each do |x|
        f.puts x
      end
      f.puts "e"
    end
  end
  end

  #puts "Parallelism plot: #{fimg}"
  fimg
end

.read_time_by_host_from_csv(csvtable) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/pwrake/report/parallelism.rb', line 188

def read_time_by_host_from_csv(csvtable)
  a = {}
  start_time = nil

  csvtable.each do |row|
    host = row['host']
    if row['command'] == 'pwrake_profile_start'
      start_time = Time.parse(row['start_time'])
    elsif row['command'] == 'pwrake_profile_end'
      t = Time.parse(row['start_time']) - start_time
      a.each do |h,v|
        v << [t,0]
      end
    elsif start_time
      a[host] ||= []
      t = Time.parse(row['start_time']) - start_time
      a[host] << [t,+1]
      t = Time.parse(row['end_time']) - start_time
      a[host] << [t,-1]
    end
  end
  a
end

.timeline_to_grid(a) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/pwrake/report/parallelism.rb', line 320

def timeline_to_grid(a)
  resolution = Rational(1,10)

  a = a.sort{|x,y| x[0]<=>y[0]}
  t_end = (a.last)[0]

  ngrid = (t_end/resolution).floor
  grid = [[0,0]]

  j = 0
  a.each do |x|
    i = (x[0]/resolution).floor
    while j < i
      grid[j+1] = [j*resolution,grid[j][1]]
      j += 1
    end
    grid[i][1] += x[1]
  end
  return grid
end