-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuUsage.R
84 lines (66 loc) · 2.2 KB
/
cpuUsage.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
args <- commandArgs(TRUE)
if (length(args) != 1) {
print("input: Rscript --vanilla cpuUsage.R <filename>")
quit()
}
# read data from file
mydata <- read.csv(args)
names(mydata) <- c("timestamp", "process", "percentage")
# change file structure for computation
mydata$percentage <- sub("%", "", mydata$percentage)
mydata$timestamp <- as.POSIXlt(strptime(mydata$timestamp, format = "%Y-%m-%d %H:%M:%S"))
mydata$percentage <- as.numeric(mydata$percentage)
# record unique process names
procs <- unique(mydata$process)
nproc <- length(procs)
# record capture time
times <- unique(mydata$timestamp)
ntime <- length(times)
# sort process by cpu usage
ratios <- NULL
for (i in 1:nproc) {
ratios[i] <- sum(mydata[mydata$process==procs[i],]$percentage)
}
procdata <- data.frame( process=procs, ratio=ratios)
procdata <- procdata[order(procdata$ratio, decreasing=TRUE),]
procs <- procdata$process
printline <- 20
# denote x,y range in graph
xrange <- range(mydata$timestamp)
yrange <- range(mydata$percentage)
# allocate color linetype and plotchar
colors <- rev(rainbow(printline))
linetype <- c(1:printline)
plotchar <- c(1:printline)
# open jpg file to record the plot
jpgname <- sub("csv", "jpg", args)
jpeg(filename = jpgname, width=1280, height=800)
# change times structure to POSIXct for axis
times <- as.POSIXct(times)
# describe layout
layout(matrix(c(1,2), nrow=2), heights=c(3,1))
# plot the scale of the plot
plot(xrange, yrange, type="n", main="CPU Usage/Timestamp", xlab="timestamp", ylab="percentage", xaxt="n")
axis(side=1, at=ts(times), labels=format(times, format="%H:%M:%S"))
# print each line
s <- NULL
for (i in 1:printline) {
s[1:ntime] <- 0
j <- 1
for (k in 1:ntime) {
comtime = mydata[mydata$process==procs[i], 1][j]
if (!is.na(comtime) && times[k]==comtime) {
s[k] <- mydata[mydata$process==procs[i], 3][j]
j=j+1
}
}
lines(times, s, type="b", lty=linetype[i], col=colors[i], pch=plotchar[i])
}
# margin setup
par(mar=c(5,4,0,2)+0.1)
# plot the legend
plot(xrange, yrange, type="n", axes=FALSE, ann=FALSE)
legend("center", legend=procs[1:printline], col=colors, pch=plotchar, lty=linetype, ncol=3)
# save the jpg file
dev.off()
quit()