Top Level Namespace

Defined Under Namespace

Modules: Jkr Classes: Array, Barrier, Cluster1D, Dir, Float, Integer

Instance Method Summary collapse

Instance Method Details

#gnuplot_label_escape(str) ⇒ Object



10
11
12
13
# File 'lib/jkr/plot.rb', line 10

def gnuplot_label_escape(str)
  ret = str.gsub(/_/, "\\\\\\_").gsub(/\{/, "\\{").gsub(/\}/, "\\}")
  ret
end

#kmeans1d(data, k, delta = 0.001) ⇒ Object

kmeans algorithm



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/jkr/stat/kmeans-1d.rb', line 38

def kmeans1d(data, k, delta=0.001)
  clusters = []

  # Assign intial values for all clusters
  (1..k).each do |point|
    index = (data.length * rand).to_i

    rand_point = data[index]
    c = Cluster1D.new(rand_point)

    clusters.push c
  end

  # Loop
  while true
    # Assign points to clusters
    data.each do |point|
      min_dist = +Float::INFINITY
      min_cluster = nil

      # Find the closest cluster
      clusters.each do |cluster|
        dist = (point - cluster.center).abs

        if dist < min_dist
          min_dist = dist
          min_cluster = cluster
        end
      end

      # Add to closest cluster
      min_cluster.points.push point
    end

    # Check deltas
    max_delta = -Float::INFINITY

    clusters.each do |cluster|
      dist_moved = cluster.recenter!

      # Get largest delta
      if dist_moved > max_delta
        max_delta = dist_moved
      end
    end

    # Check exit condition
    if max_delta < delta
      return clusters
    end

    # Reset points for the next iteration
    clusters.each do |cluster|
      cluster.points = []
    end
  end
end

#plot_bar(config) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
261
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/jkr/plot.rb', line 205

def plot_bar(config)
  [:output, :title, :series_labels, :item_labels, :data].each do |key|
    unless config[key]
      raise StandardError.new("key '#{key.to_s}' is required for bar graph")
    end
  end

  config[:size] ||= "0.9,0.6"

  data = config[:data]

  if ! data.all?{|series| series.all?{|datum| datum[:value].is_a? Numeric}}
    raise StandardError.new("All datum must have :value key")
  end

  if config[:datafile]
    datafile = File.open(config[:datafile], "w")
  else
    datafile = Tempfile.new("plot_bar")
  end
  if config[:gpfile]
    gpfile = File.open(config[:gpfile], "w")
  else
    gpfile = Tempfile.new("plot_bar")
  end
  datafile_path = rel_path(gpfile.path, datafile.path)

  config[:item_label_angle] ||= -30

  plot_data = []
  plot_stdev_data = []

  if data.all?{|series| series.all?{|datum| datum[:stdev].is_a? Numeric}}
    draw_stdev = true
  else
    draw_stdev = false
  end

  item_barwidth = 0.8 # 2.0 / 3.0
  num_serieses = data.size
  data_idx = 0
  config[:data].each_with_index do |series, series_idx|
    series.each_with_index do |datum, item_idx|
      xpos = item_idx - item_barwidth / 2.0 + item_barwidth / num_serieses.to_f / 2.0 + item_barwidth / num_serieses.to_f * series_idx
      barwidth = item_barwidth / num_serieses.to_f
      datafile.puts([xpos, datum[:value], barwidth, (datum[:stdev] || "")].join("\t"))
    end

    plot_data.push({
                     :title => config[:series_labels][series_idx],
                     :using => "1:2:3",
                     :index => "#{data_idx}:#{data_idx}",
                     :with => "with boxes fs solid 0.7",
                   })

    if series.every?{|datum| datum[:stdev]}
      plot_data.push({
                       :title => nil,
                       :using => "1:2:4",
                       :index => "#{data_idx}:#{data_idx}",
                       :with => "with yerrorbars lc 1 lt 1 pt 0",
                     })
    end

    datafile.puts("\n\n")
    data_idx += 1
  end
  datafile.fsync

  ylabel = if config[:ylabel]
             "set ylabel '#{config[:ylabel]}'"
           else
             ""
           end

  yrange = if config[:yrange]
             "set yrange #{config[:yrange]}"
           else
             ""
           end

  plot_stmt = "plot " + plot_data.map do |plot_datum|
    [:using].each do |key|
      unless plot_datum[key]
        raise StandardError.new("key '#{key.to_s}' is required for a plot_datum of bar graph")
        return
      end
    end
    index = ""
    if plot_datum[:index]
      index = " index #{plot_datum[:index]} "
    end
    if plot_datum[:title]
      title = "title '#{gnuplot_label_escape(plot_datum[:title])}'"
    else
      title = "notitle"
    end
    "'#{rel_path(gpfile.path, datafile.path)}' #{index} using #{plot_datum[:using]}"+
        " #{title} #{plot_datum[:with]} "
  end.join(", \\\n     ")

  xpos = -1
  xtics = config[:item_labels].map do |label|
    xpos += 1
    "\"#{label}\" #{xpos}"
  end.join(",")
  xtics = "(#{xtics})"
  xrange = "[-0.5:#{config[:item_labels].size - 0.5}]"

  script = <<EOS
set term postscript enhanced color
set output "#{rel_path(gpfile.path, config[:output])}"
set size #{config[:size]}
set title "#{gnuplot_label_escape(config[:title])}"
#{ylabel}
#{yrange}
set xrange #{xrange}
set xtic rotate by #{config[:item_label_angle]} scale 0
set xtics #{xtics}
#{config[:other_options]}
#{plot_stmt}
EOS
  gpfile.puts script
  gpfile.fsync
  Dir.chdir(File.dirname(gpfile.path)) do
    system_("gnuplot #{gpfile.path}")
  end
  gpfile.close
end

#plot_distribution(config) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jkr/plot.rb', line 15

def plot_distribution(config)
  dataset = config[:dataset]
  xrange_max = config[:xrange_max]
  xrange_min = config[:xrange_min] || 0
  title = config[:title]
  xlabel = config[:xlabel]
  eps_file = config[:output]
  datafile = if config[:datafile]
               File.open(config[:datafile], "w")
             else
               Tempfile.new("dist")
             end
  if config[:gpfile]
    gpfile = File.open(config[:gpfile], "w")
  else
    gpfile = Tempfile.new("plot_dist")
  end
  datafile_path = rel_path(gpfile.path, datafile)

  stepnum = 500

  plot_stmt = []
  data_idx = 0
  xrange_max_local = 0
  histset = []
  dataset.each do |tx_type,data|
    next if data.empty?
    hist = Array.new
    avg = data.inject(&:+) / data.size.to_f
    sterr = data.sterr
    stdev = data.stdev
    n90th_idx = (data.size * 0.9).to_i
    n90percent = data.sort[n90th_idx]
    xrange_max_local = [xrange_max_local, xrange_max || n90percent * 4].max
    step = xrange_max_local / stepnum
    maxidx = 0
    data.each_with_index do |num, data_idx|
      idx = (num / step).to_i
      maxidx = [idx, maxidx].max
      hist[idx] = (hist[idx] || 0) + 1
    end

    0.upto(maxidx) do |idx|
      unless hist[idx]
        hist[idx] = 0
      end
    end

    histset.push({:tx_type => tx_type, :step => step, :hist => hist, :max => hist.max})

    histmax = hist.max.to_f
    datafile.puts "##{tx_type}"
    0.upto(stepnum - 1) do |i|
      x = (i + 0.5) * step
      y = hist[i] || 0
      if config[:normalize]
        y = y / histmax
      end
      datafile.puts "#{x}\t#{y}"
    end
    datafile.puts
    datafile.puts

# index data_idx+1
    datafile.puts "#{avg}\t0"
    datafile.puts "#{avg}\t#{hist.max * 1.2}"
    datafile.puts
    datafile.puts

# index data_idx+2
    datafile.puts "#{n90percent}\t0"
    datafile.puts "#{n90percent}\t#{hist.max * 1.2}"
    datafile.puts
    datafile.puts

# index data_idx+3
    datafile.puts "#{avg-sterr * 3}\t0"
    datafile.puts "#{avg-sterr * 3}\t#{hist.max * 1.2}"
    datafile.puts
    datafile.puts
# index data_idx+4
    datafile.puts "#{avg+sterr * 3}\t0"
    datafile.puts "#{avg+sterr * 3}\t#{hist.max * 1.2}"
    datafile.puts

    plot_stmt.push(sprintf("'%s' index %d:%d using 1:2 with linespoints title '%s'",
                           datafile_path, data_idx, data_idx, tx_type))
    if config[:show_avgline]
      plot_stmt.push(sprintf("'%s' index %d:%d using 1:2 with lines lc 1 lt 2 lw 3 title '%s(avg)'",
                             datafile_path, data_idx+1, data_idx+1, tx_type))
    end
    if config[:show_90pline]
      plot_stmt.push(sprintf("'%s' index %d:%d using 1:2 with lines lc 2 lt 2 lw 3 title '%s(90%%-th)'",
                             datafile_path, data_idx+2, data_idx+2, tx_type))
    end
    if config[:show_confinterval]
      plot_stmt.push(sprintf("'%s' index %d:%d using 1:2 with lines lc 3 lt 3 lw 3 title '%s(std err)'",
                             datafile_path, data_idx+3, data_idx+3, tx_type))
      plot_stmt.push(sprintf("'%s' index %d:%d using 1:2 with lines lc 3 lt 3 lw 3 title '%s(std err)'",
                             datafile_path, data_idx+4, data_idx+4, tx_type))
    end
    data_idx += 5
  end

  datafile.fsync
  plot_stmt = plot_stmt.flatten.join(", \\\n     ")
  plot_stmt = "plot #{plot_stmt}"

  script = <<EOS
set term postscript enhanced color
set output "#{rel_path(gpfile.path, eps_file)}"
set size 0.9,0.6
set title "#{gnuplot_label_escape(title)}"
set ylabel "Frequency"
set xlabel "#{gnuplot_label_escape(xlabel)}"
set xrange [#{xrange_min}:#{xrange_max_local}]
#{config[:other_options]}
#{plot_stmt}
EOS
  gpfile.puts script
  gpfile.fsync
  Dir.chdir(File.dirname(gpfile.path)) do
    system_("gnuplot #{gpfile.path}")
  end
  gpfile.close
end

#plot_io_pgr_data(pgr_data, option) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/jkr/plot.rb', line 544

def plot_io_pgr_data(pgr_data, option)
  option[:dir] ||= "io_pgr"
  option[:start_time] ||= pgr_data.first["time"]
  option[:end_time] ||= pgr_data[-1]["time"]

  if ! Dir.exists?(option[:dir])
    FileUtils.mkdir_p(option[:dir])
  end

  start_time = option[:start_time]
  end_time = option[:end_time]

  datafile = File.open(File.join(option[:dir],
                                 "io_pgr.tsv"),
                       "w")
  devices = pgr_data.first["ioinfo"]["devices"]

  idx = 0
  devices.each do |device|
    do_read = false
    do_write = false
    unless pgr_data.all?{|rec| rec["ioinfo"][device]['r/s'] < 0.1}
      do_read = true
    end
    unless pgr_data.all?{|rec| rec["ioinfo"][device]['w/s'] < 0.1}
      do_read = true
    end

    if  do_read == false && do_write == false
      $stderr.puts("#{device} performs no I/O")
      next
    end

    datafile.puts("# #{device}")
    pgr_data.each do |rec|
      datafile.puts([rec["time"] - start_time,
                     rec["ioinfo"][device]['r/s'],
                     rec["ioinfo"][device]['w/s'],
                    ].map(&:to_s).join("\t"))
    end
    datafile.puts("\n\n")
    datafile.fsync

    plot_data = []
    if do_read
      plot_data.push({
                       :title => "read",
                       :datafile => datafile.path,
                       :using => "1:2",
                       :index => "#{idx}:#{idx}",
                       :with => "lines"
                     })
    end
    if do_write
      plot_data.push({
                       :title => "write",
                       :datafile => datafile.path,
                       :using => "1:3",
                       :index => "#{idx}:#{idx}",
                       :with => "lines"
                     })
    end

    idx += 1

    plot_scatter(:output => File.join(option[:dir],
                                      device + ".eps"),
                 :gpfile => File.join(option[:dir],
                                      "gp_" + device + ".gp"),
                 :xlabel => "elapsed time [sec]",
                 :ylabel => "IOPS",
                 :xrange => "[0:#{end_time - start_time}]",
                 :yrange => "[0:]",
                 :title => "IO performance",
                 :plot_data => plot_data,
                 :other_options => <<EOS
set rmargin 3
set lmargin 10
set key below
EOS
                 )
  end
end

#plot_mpstat_data(mpstat_data, option) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/jkr/plot.rb', line 425

def plot_mpstat_data(mpstat_data, option)
  option[:dir] ||= "mpstat"
  option[:start_time] ||= mpstat_data.first[:time]
  option[:end_time] ||= mpstat_data[-1][:time]

  if ! Dir.exists?(option[:dir])
    FileUtils.mkdir_p(option[:dir])
  end

  datafile = File.open(File.join(option[:dir],
                                 "mpstat.tsv"),
                       "w")

  start_time = option[:start_time]
  end_time = option[:end_time]

  data_idx = 0
  nr_cpu = mpstat_data.first[:data].size - 1
  (nr_cpu + 1).times do |cpu_idx|
    scale = 1.0
    if cpu_idx < nr_cpu
      label = cpu_idx.to_s
    else
      label = "ALL"
      scale = nr_cpu.to_f
    end

    datafile.puts("# cpu#{label}")
    mpstat_data.each do |rec|
      t = rec[:time] - start_time

      if cpu_idx < nr_cpu
        data = rec[:data].find{|x| x[:cpu] == cpu_idx}
      else
        data = rec[:data].find{|x| x[:cpu] == "all"}
      end
      datafile.puts([t,
                     data[:usr] * scale,
                     data[:nice] * scale,
                     data[:sys] * scale,
                     data[:iowait] * scale,
                     data[:irq] * scale,
                     data[:soft] * scale,
                     data[:steal] * scale,
                     data[:idle] * scale].map(&:to_s).join("\t"))
    end
    datafile.puts("\n\n")
    datafile.fsync

    plot_scatter(:output => File.join(option[:dir],
                                      "cpu#{label}.eps"),
                 :gpfile => File.join(option[:dir],
                                      "gp_cpu#{label}.gp"),
                 :title => nil,
                 :xlabel => "elapsed time [sec]",
                 :ylabel => "CPU usage [%]",
                 :xrange => "[0:#{end_time - start_time}]",
                 :yrange => "[0:#{105 * scale}]",
                 :plot_data => [{
                                  :title => "%usr",
                                  :using => "1:2",
                                  :index => "#{data_idx}:#{data_idx}",
                                  :with => "filledcurve x1",
                                  :datafile => datafile.path
                                },
                                {
                                  :title => "%nice",
                                  :using => "1:($2+$3)",
                                  :index => "#{data_idx}:#{data_idx}",
                                  :with => "filledcurve x1",
                                  :datafile => datafile.path
                                },
                                {
                                  :title => "%sys",
                                  :using => "1:($2+$3+$4)",
                                  :index => "#{data_idx}:#{data_idx}",
                                  :with => "filledcurve x1",
                                  :datafile => datafile.path
                                },
                                {
                                  :title => "%iowait",
                                  :using => "1:($2+$3+$4+$5)",
                                  :index => "#{data_idx}:#{data_idx}",
                                  :with => "filledcurve x1",
                                  :datafile => datafile.path
                                },
                                {
                                  :title => "%irq",
                                  :using => "1:($2+$3+$4+$5+$6)",
                                  :index => "#{data_idx}:#{data_idx}",
                                  :with => "filledcurve x1",
                                  :datafile => datafile.path
                                },
                                {
                                  :title => "%soft",
                                  :using => "1:($2+$3+$4+$5+$6+$7)",
                                  :index => "#{data_idx}:#{data_idx}",
                                  :with => "filledcurve x1",
                                  :datafile => datafile.path
                                },
                                {
                                  :title => "%steal",
                                  :using => "1:($2+$3+$4+$5+$6+$7+$8)",
                                  :index => "#{data_idx}:#{data_idx}",
                                  :with => "filledcurve x1",
                                  :datafile => datafile.path
                                }].reverse,
                 :size => "0.9,0.9",
                 :other_options => <<EOS
set rmargin 3
set lmargin 5
set key below
EOS
                 )

    data_idx += 1
  end
end

#plot_scatter(config) ⇒ Object



335
336
337
338
339
340
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/jkr/plot.rb', line 335

def plot_scatter(config)
  [:plot_data, :output, :xlabel, :ylabel].each do |key|
    unless config.keys.include?(key)
      raise ArgumentError.new("key '#{key.to_s}' is required for scatter graph")
    end
  end

  xrange = if config[:xrange]
             "set xrange #{config[:xrange]}"
           else
             ""
           end
  yrange = if config[:yrange]
             "set yrange #{config[:yrange]}"
           else
             ""
           end
  if config[:gpfile]
    gpfile = File.open(config[:gpfile], "w")
  else
    gpfile = Tempfile.new("gnuplot")
  end

  plot_stmt = "plot " + config[:plot_data].map {|plot_datum|
    unless plot_datum[:expression] ||
        [:datafile, :using].every?{|key| plot_datum[key]}
      raise ArgumentError.new("key ('datafile', 'using', 'with') or 'expression' is required for a plot_datum")
    end

    plot_target = nil
    if plot_datum[:expression]
      plot_target = plot_datum[:expression]
    else
      index = ""
      if plot_datum[:index]
        index = " index #{plot_datum[:index]} "
      end

      plot_target = "'#{rel_path(gpfile.path, plot_datum[:datafile])}' #{index} using #{plot_datum[:using]}"
    end

    unless plot_datum[:title]
      title = "notitle"
    else
      title = "title '#{gnuplot_label_escape(plot_datum[:title])}'"
    end

    if plot_datum[:with]
      with = "with #{plot_datum[:with]}"
    else
      with = ""
    end
    "#{plot_target} #{with} #{title} " + plot_datum[:other_options].to_s
  }.join(", \\\n     ")

  title_stmt = "unset title"
  if config[:title]
    title_stmt = "set title \"#{gnuplot_label_escape(config[:title])}\""
  end

  case config[:output]
  when /\.svg\Z/
    terminal = 'svg'
  else
    terminal = 'postscript enhanced color'
  end

  script = <<EOS
set term #{terminal}
set output "#{rel_path(gpfile.path, config[:output])}"
#{if config[:size]; then "set size " + config[:size]; else; ""; end}
#{title_stmt}
set ylabel "#{gnuplot_label_escape(config[:ylabel])}"
set xlabel "#{gnuplot_label_escape(config[:xlabel])}"
set rmargin 10
set lmargin 10
#{xrange}
#{yrange}
set grid
#{config[:other_options]}
#{plot_stmt}
EOS
  gpfile.puts script
  gpfile.fsync
  Dir.chdir(File.dirname(gpfile.path)) do
    system_("gnuplot #{gpfile.path}")
  end
  gpfile.close
end

#plot_timeseries(config) ⇒ Object



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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/jkr/plot.rb', line 142

def plot_timeseries(config)
  [:plot_data, :output, :title, :xlabel, :ylabel].each do |key|
    unless config[key]
      raise ArgumentError.new("key '#{key.to_s}' is required for time-series graph")
      return
    end
  end
  
  xrange = if config[:xrange]
             "set xrange #{config[:xrange]}"
           else
             ""
           end
  yrange = if config[:yrange]
             "set yrange #{config[:yrange]}"
           else
             ""
           end

  if config[:gpfile]
    gpfile = File.open(config[:gpfile], "w")
  else
    gpfile = Tempfile.new("plot_timeseries")
  end

  plot_stmt = "plot " + config[:plot_data].map {|plot_datum|
    [:datafile, :using, :with, :title].each do |key|
      unless plot_datum[key]
        raise ArgumentError.new("key '#{key.to_s}' is required for a plot_datum of time-series graph")
        return
      end
    end
    index = ""
    if plot_datum[:index]
      index = " index #{plot_datum[:index]} "
    end
    "'#{rel_path(gpfile.path, plot_datum[:datafile])}' #{index} using #{plot_datum[:using]} with #{plot_datum[:with]}"+
    " title '#{gnuplot_label_escape(plot_datum[:title])}' " + plot_datum[:other_options].to_s
  }.join(", \\\n     ")

  script = <<EOS
set term postscript enhanced color
set output "#{rel_path(gpfile.path, config[:output])}"
set size 0.9,0.6
set title "#{gnuplot_label_escape(config[:title])}"
set ylabel "#{config[:ylabel]}"
set xlabel "#{config[:xlabel]}"
set rmargin 3
set lmargin 10
#{xrange}
#{yrange}
set grid
#{config[:other_options]}
#{plot_stmt}
EOS
  gpfile.puts script
  gpfile.fsync
  Dir.chdir(File.dirname(gpfile.path)) do
    system_("gnuplot #{gpfile.path}")
  end
  gpfile.close
end

#rel_path(basefile, path) ⇒ Object



4
5
6
7
8
# File 'lib/jkr/plot.rb', line 4

def rel_path(basefile, path)
  base = Pathname.new(File.dirname(basefile))
  path = Pathname.new(path)
  path.relative_path_from(base).to_s
end