forked from bonzini/smalltalk
/
Profile.st
123 lines (99 loc) · 3.66 KB
/
Profile.st
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
"======================================================================
|
| GNU Smalltalk profiling tool
|
|
======================================================================"
"======================================================================
|
| Copyright 2009 Free Software Foundation, Inc.
| Written by Paolo Bonzini.
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
|
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
| details.
|
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING. If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
======================================================================"
PackageLoader fileInPackage: 'ProfileTools'.
DLD addLibrary: 'libc'.
SystemDictionary extend [
SmalltalkArgv := OrderedCollection new.
]
| helpString output profiler profilerClass last |
commands := OrderedCollection new.
output := nil.
profilerClass := CallGraphProfiler.
helpString :=
'Usage:
gst-profile [ flag ... ] [FILE ARGS]
Options:
-f --file=FILE file in FILE
-e --eval=CODE evaluate CODE
-o --output=FILE output file for callgrind_annotate
-h --help show this message
--no-separate-blocks do not track blocks separately
--version print version information and exit
FILE is always parsed, even if --file or --eval are used. It is also
always parsed last. Use /dev/null to pass arguments directly to --file
or --eval options.
'.
"Parse the command-line arguments."
[Smalltalk
arguments: '-f|--file: -e|--eval: -o|--output: -h|--help --version
--no-separate-blocks'
do: [ :opt :arg |
opt = 'help' ifTrue: [
helpString displayOn: stdout.
ObjectMemory quit: 0 ].
opt = 'no-separate-blocks' ifTrue: [
profilerClass := MethodCallGraphProfiler ].
opt = 'version' ifTrue: [
('gst-profile - %1' % {Smalltalk version}) displayNl.
ObjectMemory quit: 0 ].
opt = 'output' ifTrue: [
output isNil ifFalse: [ self error: 'multiple output files' ].
output := arg ].
opt = 'file' ifTrue: [
commands add: (File name: arg) ].
opt = 'eval' ifTrue: [
commands add: arg ].
opt isNil ifTrue: [
last isNil
ifTrue: [ last := arg ]
ifFalse: [ SystemDictionary.SmalltalkArgv addLast: arg ] ].
]
ifError: [
helpString displayOn: stderr.
ObjectMemory quit: 1 ].
last isNil ifFalse: [
commands add: (File name: last) ].
commands isEmpty ifTrue: [ self error: 'no commands given' ]
] on: Error do: [ :ex |
('gst-profile: ', ex messageText, '
') displayOn: stderr.
stderr flush.
helpString displayOn: stderr.
ObjectMemory quit: 1 ].
SystemDictionary compile:
'getpid [ <cCall: ''getpid'' returning: #int args: #()> ]'.
SystemDictionary compile:
'arguments [ ^SmalltalkArgv asArray ]'.
profiler := profilerClass new.
output isNil ifTrue: [
output := Directory working / ('gst-profile.%1' % { Smalltalk getpid }) ].
commands do: [ :each |
"Using #readStream makes it work both for Strings and Files.
TODO: use hooks instead, maybe directly in Profiler?."
profiler withProfilerDo: [ each readStream fileIn ] ].
profiler printCallGraphToFile: output.