From 81cbddc1be1c5d9a6e664d44253e11a4f5b55f51 Mon Sep 17 00:00:00 2001 From: Alwin Berger Date: Tue, 23 May 2023 12:06:07 +0200 Subject: [PATCH] paralellize plots --- fuzzers/FRET/benchmark/plot_multi.r | 127 +++++++++++++++++++--------- 1 file changed, 86 insertions(+), 41 deletions(-) diff --git a/fuzzers/FRET/benchmark/plot_multi.r b/fuzzers/FRET/benchmark/plot_multi.r index cdf227a4be..09c3b522dc 100644 --- a/fuzzers/FRET/benchmark/plot_multi.r +++ b/fuzzers/FRET/benchmark/plot_multi.r @@ -1,10 +1,18 @@ library("mosaic") library("dplyr") +library("foreach") +library("doParallel") + +#setup parallel backend to use many processors +cores=detectCores() +cl <- makeCluster(cores[1]-1) #not to overload your computer +registerDoParallel(cl) + args = commandArgs(trailingOnly=TRUE) if (length(args)==0) { - runtype="timedump_253048_1873f6/timedump" - target="waters" + runtype="timedump_253048_1873f6_full/timedump" + target="watersv2" outputpath="~/code/FRET/LibAFL/fuzzers/FRET/benchmark/" #MY_SELECTION <- c('state', 'afl', 'graph', 'random') SAVE_FILE=TRUE @@ -85,57 +93,94 @@ frame2maxlines <- function(tr) { return(tr) } -all_maxlines = c() -for (bn in BASENAMES) { +trace2maxpoints <- function(tr) { + minval = tr[1,1] + collect = tr[1,] + for (i in seq_len(dim(tr)[1])) { + if (minval < tr[i,1]) { + collect = rbind(collect,tr[i,]) + minval = tr[i,1] + } + } + return(collect) +} + +sample_maxpoints <- function(tr,po) { + index = 1 + collect=NULL + for (p in po) { + done = FALSE + while (!done) { + if (p<=tr[1,2] || (index < dim(tr)[1] && tr[index,2] <= p && p < tr[index+1,2])) { + tmp = tr[index,] + tmp[2] = p + collect = rbind(collect, tmp) + done = TRUE + } else { if ( p >= tr[dim(tr)[1],2] ) { + tmp = tr[dim(tr)[1],] + tmp[2] = p + collect = rbind(collect, tmp) + done = TRUE + } else { + index = index + 1 + } } + } + } + return(collect) +} + +#https://www.r-bloggers.com/2012/01/parallel-r-loops-for-windows-and-linux/ +all_runtypetables <- foreach (bn=BASENAMES) %do% { runtypefiles <- list.files(file.path(BENCHDIR,bn),pattern=sprintf(PATTERNS,target),full.names = TRUE) if (length(runtypefiles) > 0) { - runtypetables <- lapply(seq_len(length(runtypefiles)), - function(i)read.csv(runtypefiles[[i]], col.names=c(sprintf("%s%d",bn,i),"times"))) - runtypetables = trim_data(runtypetables) - runtypetables = lapply(runtypetables, function(i) i[1]) - list_of_maxlines = data2maxlines(runtypetables) - tmp_frame <- Reduce(bind_cols, list_of_maxlines) - statframe <- bind_cols(rowMeans(tmp_frame),apply(tmp_frame, 1, sd),apply(tmp_frame, 1, min),apply(tmp_frame, 1, max)) - names(statframe) <- c(bn, sprintf("%s_sd",bn), sprintf("%s_min",bn), sprintf("%s_max",bn)) - all_maxlines = c(all_maxlines, list(round(statframe))) - #all_maxlines = append(all_maxlines, list_of_maxlines) - #mean_maxline<-Reduce(function(a,b) a+b,list_of_maxlines,0)/length(runtypetables) - #all_maxlines=append(all_maxlines,mean_maxline) + runtypetables_reduced <- foreach(i=seq_len(length(runtypefiles))) %dopar% { + rtable = read.csv(runtypefiles[[i]], col.names=c(sprintf("%s%d",bn,i),sprintf("times%d",i))) + trace2maxpoints(rtable) + } + #runtypetables <- lapply(seq_len(length(runtypefiles)), + # function(i)read.csv(runtypefiles[[i]], col.names=c(sprintf("%s%d",bn,i),sprintf("times%d",i)))) + #runtypetables_reduced <- lapply(runtypetables, trace2maxpoints) + runtypetables_reduced + #all_runtypetables = c(all_runtypetables, list(runtypetables_reduced)) } } -min_length <- min(sapply(all_maxlines, function(x) dim(x)[1])) -all_maxlines=lapply(all_maxlines, function(v) v[1:min_length,]) +all_runtypetables = all_runtypetables[lapply(all_runtypetables, length) > 0] +all_points = sort(unique(Reduce(c, lapply(all_runtypetables, function(v) Reduce(c, lapply(v, function(w) w[[2]])))))) +all_maxlines <- foreach (rtt=all_runtypetables) %dopar% { + bn = substr(names(rtt[[1]])[1],1,nchar(names(rtt[[1]])[1])-1) + runtypetables_sampled = lapply(rtt, function(v) sample_maxpoints(v, all_points)[1]) + tmp_frame <- Reduce(cbind, runtypetables_sampled) + statframe <- data.frame(rowMeans(tmp_frame),apply(tmp_frame, 1, sd),apply(tmp_frame, 1, min),apply(tmp_frame, 1, max)) + names(statframe) <- c(bn, sprintf("%s_sd",bn), sprintf("%s_min",bn), sprintf("%s_max",bn)) + #statframe[sprintf("%s_times",bn)] = all_points + round(statframe) + #all_maxlines = c(all_maxlines, list(round(statframe))) +} one_frame<-data.frame(all_maxlines) -one_frame[length(one_frame)+1] <- seq_len(length(one_frame[[1]])) -names(one_frame)[length(one_frame)] <- 'iters' +one_frame[length(one_frame)+1] <- all_points/(3600 * 1000) +names(one_frame)[length(one_frame)] <- 'time' -typenames = names(one_frame)[which(names(one_frame) != 'iters')] +typenames = names(one_frame)[which(names(one_frame) != 'time')] +typenames = typenames[which(!endsWith(typenames, "_sd"))] ylow=min(one_frame[typenames]) yhigh=max(one_frame[typenames],worst_case) -typenames = typenames[which(!endsWith(typenames, "_sd"))] typenames = typenames[which(!endsWith(typenames, "_min"))] typenames = typenames[which(!endsWith(typenames, "_max"))] -#yhigh=3400000 -#yhigh=max(one_frame[typenames],405669) ml2lines <- function(ml) { lines = NULL - last = -1 - for (i in seq_len(length(ml))) { - if (ml[[i]] != last || (i >= length(ml))) { - if (i != 1) { - lines = rbind(lines, cbind(X=i, Y=last)) - } - lines = rbind(lines, cbind(X=i, Y=ml[[i]])) - last=ml[[i]] - } + last = 0 + for (i in seq_len(dim(ml)[1])) { + lines = rbind(lines, cbind(X=last, Y=ml[i,1])) + lines = rbind(lines, cbind(X=ml[i,2], Y=ml[i,1])) + last = ml[i,2] } return(lines) } plotting <- function(selection, filename, MY_COLORS_) { # filter out names of iters and sd cols -typenames = names(one_frame)[which(names(one_frame) != 'iters')] +typenames = names(one_frame)[which(names(one_frame) != 'times')] typenames = typenames[which(!endsWith(typenames, "_sd"))] typenames = typenames[which(!endsWith(typenames, "_min"))] typenames = typenames[which(!endsWith(typenames, "_max"))] @@ -149,16 +194,16 @@ if (SAVE_FILE) {png(file=sprintf("%s%s_%s.png",outputpath,target,filename), widt par(mar=c(4,4,1,1)) par(oma=c(0,0,0,0)) -plot(c(1,length(one_frame[[1]])),c(ylow,yhigh), col='white', xlab="Iters", ylab="WORT", pch='.') +plot(c(1,max(one_frame['time'])),c(ylow,yhigh), col='white', xlab="Time [h]", ylab="WORT [insn]", pch='.') for (t in seq_len(length(typenames))) { proj = one_frame[seq(1, dim(one_frame)[1], by=max(1, length(one_frame[[1]])/(10*w_))),] #points(proj[c('iters',typenames[t])], col=MY_COLORS_[t], pch='.') - avglines = ml2lines(one_frame[[typenames[t]]]) + avglines = ml2lines(one_frame[c(typenames[t],'time')]) lines(avglines, col=MY_COLORS_[t]) if (exists("RIBBON") && ( RIBBON=='both' || RIBBON=='span')) { - milines = ml2lines(one_frame[[sprintf("%s_min",typenames[t])]]) - malines = ml2lines(one_frame[[sprintf("%s_max",typenames[t])]]) + milines = ml2lines(one_frame[c(sprintf("%s_min",typenames[t]),'time')]) + malines = ml2lines(one_frame[c(sprintf("%s_max",typenames[t]),'time')]) lines(milines, col=MY_COLORS_[t], lty='dashed') lines(malines, col=MY_COLORS_[t], lty='dashed') #points(proj[c('iters',sprintf("%s_min",typenames[t]))], col=MY_COLORS_[t], pch='.') @@ -167,7 +212,7 @@ for (t in seq_len(length(typenames))) { if (exists("RIBBON") && RIBBON != '') { for (i in seq_len(dim(proj)[1])) { row = proj[i,] - x_ <- row['iters'][[1]] + x_ <- row['time'][[1]] y_ <- row[typenames[t]][[1]] sd_ <- row[sprintf("%s_sd",typenames[t])][[1]] min_ <- row[sprintf("%s_min",typenames[t])][[1]] @@ -203,9 +248,9 @@ if (exists("MY_SELECTION")) { #MY_SELECTION=c('state', 'afl', 'random', 'feedlongest', 'feedgeneration', 'feedgeneration10') #MY_SELECTION=c('state_int', 'afl_int', 'random_int', 'feedlongest_int', 'feedgeneration_int', 'feedgeneration10_int') #MY_SELECTION=c('state', 'frAFL', 'statenohash', 'feedgeneration10') - #MY_SELECTION=c('state_int', 'frAFL_int', 'statenohash_int', 'feedgeneration10_int') + MY_SELECTION=c('state_int', 'frAFL_int', 'statenohash_int', 'feedgeneration10_int') MY_SELECTION=typenames - RIBBON='both' + RIBBON='span' for (i in seq_len(length(MY_SELECTION))) { n <- MY_SELECTION[i] plotting(c(n), n, c(MY_COLORS[i]))