Skip to content

Instantly share code, notes, and snippets.

@sgibb
Created January 22, 2017 16:55
Show Gist options
  • Save sgibb/5bb4625364f076cc5ca0c4bbb57d630c to your computer and use it in GitHub Desktop.
Save sgibb/5bb4625364f076cc5ca0c4bbb57d630c to your computer and use it in GitHub Desktop.
monitor synapter memory usage
#!/bin/sh
PROCESSPID=${1}
pidstat -urIh -p ${PROCESSPID} 1 1 | grep -v "^Linux\|^ *$" > memstat.txt && pidstat -urIh -p ${PROCESSPID} 60 | grep -v "^#\|^ *$\|^Linux" | tee -a memstat.txt
## overwrite message to get timestamps for normal messages
msghandler <- function(x) {
cat(format(Sys.time(), "%s"), "|", x$message,
sep="", append=TRUE, file="timestamps.txt")
}
timestampFile <- "timestamps.txt"
memstatFile <- "memstat.txt"
lines <- readLines(timestampFile)
lines <- lines[grep("^[0-9]+", lines)]
lines <- gsub(".---", "", lines)
tm <- read.table(text=lines, col.names=c("timestamp", "text"), sep="|", quote="", stringsAsFactors=FALSE)
tm <- tm[as.logical(nzchar(tm$text)),]
lines <- readLines(memstatFile, warn=FALSE)
cn <- as.character(read.table(text=lines[1L], nrows=1, comment="", stringsAsFactors=FALSE)[1, -1])
mm <- read.table(text=lines[c(-1L, -length(lines))], header=FALSE, col.names=cn, stringsAsFactors=FALSE)
## remove first occurence of duplicated timestamps
#tm <- tm[!rev(duplicated(rev(tm$timestamp))),]
## remove everything that is before running synapter
minTime <- min(tm$timestamp)
maxTime <- max(tm$timestamp)
mm <- mm[minTime <= mm$Time & mm$Time <= maxTime,]
mm$Time <- mm$Time - minTime
tm$timestamp <- tm$timestamp - minTime
mm$Time <- mm$Time / 60
tm$timestamp <- tm$timestamp / 60
mm$VSZ <- mm$VSZ/1024
mm$RSS <- mm$RSS/1024
png("usage.png", width=1e4, height=1e3)
plot(mm$Time, mm$VSZ, type="b", col="#377eb8", pch=15, ylim=c(0, max(mm$VSZ)),
xaxt="n",
main="Memory/CPU Usage synergise2",
xlab="Time [min]", ylab="Memory Usage [MB]")
axis(1, at=seq(0, max(tm$timestamp), by=5))
lines(mm$Time, mm$RSS, type="b", col="#4daf4a", pch=16)
grid()
abline(v=tm$timestamp, col="#e41a1c", lty=3)
text(x=tm$timestamp, y=0, labels=tm$text, srt=90, col="#e41a1c", pos=4)
abline(h=2e4, col="#ff7f00", lty=4)
text(x=par("usr")[2], y=2e4, labels="20 GB", col="#ff7f00", pos=2)
par(new=TRUE)
plot(mm$Time, mm$X.usr, type="b", pch=17, col="#984ea3",
xaxt="n", yaxt="n", xlab="", ylab="", ylim=c(0, max(mm$X.usr)))
axis(4)
mtext("CPU usage [%]", side=4, line=3)
legend("bottomright", legend=c("Virtual Size", "Resident Set Size", "CPU Usage", "Events"),
col=c("#377eb8", "#4daf4a", "#984ea3", "#e41a1c"),
pch=c(15:17, NA), lwd=c(0, 0, 0, 1), bg="#ffffff")
dev.off()
source("msghandler.R")
withCallingHandlers({ source("01_makeMaster.R"); source("02_synergise.R"); source("03_create_msnsets.R") }, message=msghandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment