-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeSize.R
More file actions
executable file
·545 lines (430 loc) · 16.9 KB
/
codeSize.R
File metadata and controls
executable file
·545 lines (430 loc) · 16.9 KB
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
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
334
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
424
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
543
544
545
#!/usr/bin/Rscript
################################################################################
#
# Crunch GHS NM output to determine what code is taking up the most space
#
# Author: Chip Boling
# Date: Dec. 18, 2014
#
# When using GNM to create the input file, use the '-h -v -p -no_debug -S -a -X' options
# for best results
########################################
# Customized global
local
discardSections <- c("(UNDEF)", "syscall")
demangleCppNames <- function(inputData, verbose)
{
# Demangle C++ names as much as possible
# No parameters in function
inputData$symbol <- gsub("__Fv$", "()", inputData$symbol)
# C++ destructor and constructor(no parameters)
inputData$symbol <- sub("^__dt__([0-9]+)(.*)Fv$", "~\\2::\\2(void)", inputData$symbol)
inputData$symbol <- sub("^__ct__([0-9]+)(.*)Fv$", "~\\2::\\2(void)", inputData$symbol)
return(inputData)
}
cleanupSymbolNames <- function(inputData, verbose)
{
## Do some name mangling and path cleanup to make the symbol names more reasonable
if (verbose) { print("Cleaning up filenames")}
# Convert the .2F to a slash '/'. This covers most source files. Then delete any
# ..[num] at end of line.
inputData$symbol <- gsub(".2F", "/", inputData$symbol, fixed=TRUE)
inputData$symbol <- sub("..([0-9]+)$", "", inputData$symbol)
# Now look for ../ in the symbol name and use it to split into a symbol and file name.
# Not all symbols (library/stl/...) may have a file name. Best way is to replace the first
# '../' with a space and then use split. We know that a space is not already present as
# that is what we used to read the file in from disk.
if (verbose) { print("Creating separate symbol and filename columns")}
inputData$symbol <- sub(".../", " /", inputData$symbol, fixed=TRUE)
inputData$symbol <- sub("../", " /", inputData$symbol, fixed=TRUE)
inputData$symbol <- sub("./", " /", inputData$symbol, fixed=TRUE)
inputData$symbol <- sub("..", " /", inputData$symbol, fixed=TRUE)
getSymbol <- function(x) { str_split(x, " ", n=2)[[1]][1] }
getFilename <- function(x) { str_split(x, " ", n=2)[[1]][2] }
inputData[, file:=getFilename(symbol), by=symbol]
inputData[, symbol:=getSymbol(symbol), by=symbol]
demangleCppNames(inputData)
}
cleanupColumns <- function(inputData, verbose)
{
# Discard undesired Sections
if (verbose) { print("Discarding undesirable sections")}
inputData <- inputData[!inputData$section %in% discardSections, ]
# Perform major cleanup of the symbol names
cleanupSymbolNames(inputData, verbose)
}
readInputFile <- function(inputPath, verbose)
{
print(sprintf("Reading input file: '%s'", inputPath))
classes <- c(A="numeric", B="factor", C="character")
#inputData <- fread(inputPath, header=FALSE, colClasses=classes)
inputData <- as.data.table(read.table(inputPath, sep="", header=FALSE, colClasses=classes))
# Input is expected to have 3 columns
cNameOld <- c("V1", "V2", "V3")
cNameNew <- c("address", "section", "symbol")
if (ncol(inputData) != 3)
{
msg <- sprintf("Input file did not have 3 columns, found %d",ncol(inputData))
if (ncol(inputData) < 3)
{
stop(msg)
}
else
{
warning(msg)
}
for (idx in 4:ncol(inputData))
{
cNameOld <- append(cNameOld, sprintf("V%d", idx))
cNameNew <- append(cnameNew, sprintf("V%d", idx))
}
}
# Give columns some useful names
setnames(inputData, cNameOld, cNameNew)
# Scrub N/A records
numRowsBefore =nrow(inputData)
na.omit(inputData)
if (verbose & numRowsBefore != nrow(inputData))
{
print(sprintf("Input reduced from by %d rows (from %d) by omitting NA's",
numRowsBefore - nrow(inputData), numRowsBefore))
}
# Cleanup our columns
cleanupColumns(inputData, verbose)
}
createSizeColumn <- function(inputData, tableName, verbose)
{
# Create the 'size' column for each symbol
if (verbose) { print(sprintf(" Creating 'size' column for '%s' section table", tableName)) }
inputData$size <- as.numeric(NA)
if (nrow(inputData) > 1)
{
for (row in 2:nrow(inputData))
{
prevSize <- inputData$address[[row - 1]]
thisSize <- inputData$address[[row]]
inputData$size[row] <- thisSize - prevSize
}
}
setcolorder(inputData, c("address", "size", "symbol", "file"))
return(inputData)
}
discardSmallSymbols <- function(inputData, minSize, tableName, verbose)
{
# Discard symbols smaller than 'minSize'
if (verbose)
{
print(sprintf(" Dropping symbols smaller than %d octets for '%s' table",
minSize, tableName))
}
nbefore <- nrow(inputData)
inputData <- inputData[!is.na(inputData$size) & inputData$size >= minSize,]
if (verbose & nbefore > nrow(inputData))
{
print(sprintf(" Dropped %d small symbols from '%s' table",
nbefore - nrow(inputData), tableName))
}
return(inputData)
}
createSectionTables <- function(inputData, minSymSize, verbose)
{
# This function takes the input data and creates a unique data.table object for each
# section. It then returns a named list of data.table objects where the list element
# name is the section. ie) tableList[['.text']] will return the data.table related
# to the ".text" section
tableList <- list()
sections <- unique(inputData$section)
idx <- 1
for (sect in sections)
{
if (verbose) { print(sprintf("Creating section table for section '%s'", sect))}
# Extract separate table and drop the section column. Make the 'address' column
# the index
table <- inputData[inputData$section==sect,]
table <- table[,section:=NULL]
tableName <- sect
setkey(table, address)
table <- createSizeColumn(table, tableName, verbose)
if (minSymSize > 0)
{
table <- discardSmallSymbols(table, minSymSize, tableName, verbose)
}
tableList[[idx]] <- table
names(tableList)[idx] <- tableName
idx <- idx + 1
}
return(tableList)
}
discardSmallSections <- function(tableList, minSize, verbose)
{
# Discard empty sections and sections smaller than 'minSize'
if (verbose) { print(sprintf("Dropping section tables smaller than %d octets", minSize)) }
finalList <- list()
idx = 1
finalIdx = 1
for (table in tableList)
{
tableName <- names(tableList)[idx]
sectSize <- sum(table$size, na.rm=TRUE)
if (sectSize >= minSize)
{
finalList[[finalIdx]] <- table
names(finalList)[finalIdx] <- tableName
finalIdx = finalIdx + 1
if (verbose) { print(sprintf(" Section %s is %d octets", tableName, sectSize)) }
}
else if (verbose)
{
print(sprintf(" Dropped section %s. Only %d octets", tableName, sectSize))
}
idx <- idx + 1
}
return(finalList)
}
outputSectionReport <- function(tableList)
{
print("------------------------------------------------")
print("Symbol Table Results")
print("--------------------\n")
print(" Table : Size");
idx <- 1
for (table in tableList)
{
tableName <- names(tableList)[idx]
sum <- sum(table$size)
print(sprintf(" %14s : %9d", tableName, sum))
idx <- idx + 1
}
invisible()
}
# Output largest symbols (per table)
outputLargestSymbols <- function(tableList, cutoff, maxLines)
{
print("------------------------------------------------")
print(sprintf("Largest Symbol per section by descending size. Report minimum = %d octets", cutoff))
print("--------------------------------------------------------------------------------------")
print("")
idx <- 1
for (table in tableList)
{
na.omit(table, cols="size")
subTable <- table[table$size >= cutoff, ]
if (nrow(subTable) > 0)
{
# And sort it
subTable <- setorder(subTable, -size)
# Output Results
max <- nrow(subTable)
if (maxLines < max)
{
max <- maxLines
}
print(sprintf(" Section: %s. %d rows of %d", names(tableList)[idx], max,
nrow(table)))
print(" Size : Symbol : File");
for (row in 1:max)
{
print(sprintf(" %9d : %40s : %s", as.integer(subTable$size[row]),
subTable$symbol[row], subTable$file[row]))
}
print("")
print(" -------------------------------------------")
print("")
}
idx <- idx + 1
}
invisible()
}
# Output symbols sizes by directory
outputDirectorySizes <- function(tableList, cutoff, maxLines)
{
print ("------------------------------------------------")
print (sprintf("Largest directories Report minimum = %d octets", cutoff))
print ("")
print (" TODO: Not yet implemented")
print ("")
invisible()
}
# Output symbols sizes by directory/file
outputFileSizes <- function(tableList, cutoff, maxLines)
{
print ("------------------------------------------------")
print (sprintf("Largest files. Report minimum = %d octets", cutoff))
print ("")
print (" TODO: Not yet implemented")
print ("")
invisible()
}
outputCsvFile <- function(tableList, csvOutput)
{
# Recreate the one big list. But first add back in section name
for (idx in 1:length(tableList))
{
tableList[[idx]]$section <- as.factor(names(tableList)[idx])
}
allSections <- rbindlist(tableList)
# Now write it. We append since the first line already exists and has our command line
# inserted there
write.table(allSections, file=csvOutput, sep = ",", row.names=FALSE, append=TRUE)
invisible()
}
checkPkgs <- function(pkgs, repo)
{
pkg.inst <- installed.packages()
have.pkg <- pkgs %in% rownames(pkg.inst)
if (any(!have.pkg))
{
message("\nSome packages need to be installed.\n")
#r <- readline("Install necessary packages [y/n]? ")
cat(" Install necessary packages [y/n]? ")
answer <- readLines(con="stdin", 1)
cat(answer, "\n")
if(tolower(answer) == "y")
{
need <- pkgs[!have.pkg]
message("\nInstalling packages ", paste(need, collapse = ", "))
# Use 0-clouds RStudion since it provides redirection to other servers worldwide
install.packages(need, repos=repo)
}
}
}
code_size <- function(inputFile="./nm.txt",
minSymSize=1,
minSectSize=256,
maxSymbolCutoff=4096,
maxDirCutoff=128 * 1024,
maxFileCutoff=64 * 1024,
maxLines=100,
csvOutput="",
verbose=FALSE)
{
## Compute the size requirements from each file
##
## inputFile - Input NM file path created with GHS 'gnm' and the '-h -v -p -S -a -X'
## options. Default is './nm.txt'
##
## minSymSize - Minimum symbol size (in octets). All symbols smaller than this will be
## discarded. Default is 1 octet (zero length symbols discarded)
##
## minSectSize - Minimum section size (in octets). All sections smaller than this will be
## discarded. Default is 256 octets
##
## maxSymbolCutoff - Size a symbol must be to make it into the maximum symbol report.
## Default is 4096
##
## maxDirCutoff - Size that the sum of all symbols in a directory (and
## subdirectories) must be to make it into the directory report.
## Default is 128K.
##
## maxFileCutoff - Size that the sum of all symbols in a file must be to make it into the
## file report. Default is 64K.
##
## csvOutput - Output filename for CSV output for entire data. By default no CSV
## output is generated.
# Read our input data
inputData <- readInputFile(inputFile, verbose)
# Create separate tables based on section
tableList <- createSectionTables(inputData, minSymSize, verbose)
# Toss out small tables
if (minSectSize > 0)
{
nbefore <- length(tableList)
tableList <- discardSmallSections(tableList, minSectSize, verbose)
if (verbose & nbefore > length(tableList))
{
print(sprintf("Dropped %d small sections tables", nbefore - length(tableList)))
}
}
# Output the section report
outputSectionReport(tableList)
# Output largest symbols
outputLargestSymbols(tableList, maxSymbolCutoff, maxLines)
# Output largest directories
outputDirectorySizes(tableList, maxDirCutoff, maxLines)
# Output largest directories
outputFileSizes(tableList, maxFileCutoff, maxLines)
# CSV output if requested
if (length(csvOutput) > 0)
{
# Write the 'command line' used to create the CSV
cmdLine <- sprintf("# codeSize --file=\"%s\" --symSize=%d --secSize=%d --output=\"%s\"",
inputFile, minSymSize, minSectSize, csvOutput)
fileConn<-file(csvOutput)
writeLines(cmdLine, fileConn)
close(fileConn)
outputCsvFile(tableList, csvOutput)
}
print ("------------------------------------------------")
print ("Done...")
}
####################################################################################
#
# Make sure we have all required packages
#
checkPkgs(c("Rcpp", "plyr", "stringr", "data.table", "getopt"),
repo="http://cran.stat.ucla.edu/")
suppressPackageStartupMessages(library(data.table))
library(data.table)
library(stringr)
library(plyr)
library(getopt)
# Get command args
cmdArgs <- commandArgs(TRUE)
if (length(cmdArgs) > 0)
{
# Parse input with getopt() library
spec = matrix(c(
'file', 'f', '2', 'character', 'The input file name, default is nm.txt',
'dir', 'd', '2', 'character', 'The input base directory, default is current working directory',
'symSize', 's', '2', 'integer', 'Discard symbols smaller than this. Default is 1 octet',
'secSize', 'S', '2', 'integer', 'Discard sections smaller than this. Default is 256 octets',
'maxSymb', 'm', '2', 'integer', 'Only report symbols this size and larger in max symbol report. Default is 4096 octets',
'dirSize', 'D', '2', 'integer', 'Size that the sum of all symbols in a directory (and subdirectories) must be to make it into the directory report. Default is 128K.',
'fileSize', 'F', '2', 'integer', 'Size that the sum of all symbols in a file must be to make it into the file report. Default is 64K.',
'maxLines', 'M', '2', 'integer', 'Maximum number of output lines per report/sub-report for sections that may have many. Default is 100.',
'output', 'o', '2', 'character', 'Output CSV filename for cleaned map. Default is "" (no CSV output)',
'verbose', 'v', '0', 'logical', 'Enable verbose output',
'help', '?', '0', 'logical', 'Print out help text'
), byrow=TRUE, ncol=5)
opt = getopt(spec);
# Asked for help?
if (!is.null(opt$help))
{
cat(getopt(spec, usage=TRUE))
q(status=1)
}
# Update defaults here and also in the 'code_size' function if you want consistent behaviour
# from within RStudio (debugging) and from the command line
if (is.null(opt$file)) { opt$file = "./nm.txt" }
if (is.null(opt$symSize)) { opt$symSize = 1 }
if (is.null(opt$secSize)) { opt$secSize = 256 }
if (is.null(opt$maxSymb)) { opt$maxSymb = 4096 }
if (is.null(opt$dirSize)) { opt$dirSize = 128 * 1024 }
if (is.null(opt$fileSize)) { opt$fileSize = 64 * 1024 }
if (is.null(opt$maxLines)) { opt$maxLines = 100 }
if (is.null(opt$verbose)) { opt$verbose = FALSE }
if (is.null(opt$output)) { opt$output = "" }
code_size(inputFile=opt$file,
minSymSize=opt$symSize,
minSectSize=opt$secSize,
maxSymbolCutoff=opt$maxSymb,
maxDirCutoff=opt$dirSize,
maxFileCutoff=opt$fileSize,
maxLines=opt$maxLines,
csvOutput=opt$output,
verbose=opt$verbose)
}
if (length(cmdArgs) == 0)
{
# No arguments on the command line. May be running it from shell command line or sourcing
# the file within RStudio for debugging purposes
cmdArgs <- commandArgs()
if ("--interactive" %in% cmdArgs)
{
# print("Just sourcing inside RStudio")
}
else
{
code_size()
}
}