@@ -43,6 +43,9 @@ extern crate env_logger;
43
43
#[ macro_use]
44
44
extern crate log;
45
45
46
+ #[ cfg( feature = "flame-it" ) ]
47
+ use vm:: Settings ;
48
+
46
49
mod interpreter;
47
50
mod settings;
48
51
mod shell;
@@ -80,70 +83,13 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
80
83
config = config. init_stdlib ( ) ;
81
84
}
82
85
config = config. init_hook ( Box :: new ( init) ) ;
83
- let interp = config. interpreter ( ) ;
84
-
85
- #[ cfg( feature = "flame-it" ) ]
86
- let main_guard = flame:: start_guard ( "RustPython main" ) ;
87
86
87
+ let interp = config. interpreter ( ) ;
88
88
let exitcode = interp. run ( move |vm| run_rustpython ( vm, run_mode) ) ;
89
89
90
- #[ cfg( feature = "flame-it" ) ]
91
- {
92
- main_guard. end ( ) ;
93
- if let Err ( e) = write_profile ( & matches) {
94
- error ! ( "Error writing profile information: {}" , e) ;
95
- }
96
- }
97
90
ExitCode :: from ( exitcode)
98
91
}
99
92
100
- #[ cfg( feature = "flame-it" ) ]
101
- fn write_profile ( matches : & ArgMatches ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
102
- use std:: { fs, io} ;
103
-
104
- enum ProfileFormat {
105
- Html ,
106
- Text ,
107
- Speedscope ,
108
- }
109
-
110
- let profile_output = matches. value_of_os ( "profile_output" ) ;
111
-
112
- let profile_format = match matches. value_of ( "profile_format" ) {
113
- Some ( "html" ) => ProfileFormat :: Html ,
114
- Some ( "text" ) => ProfileFormat :: Text ,
115
- None if profile_output == Some ( "-" . as_ref ( ) ) => ProfileFormat :: Text ,
116
- Some ( "speedscope" ) | None => ProfileFormat :: Speedscope ,
117
- Some ( other) => {
118
- error ! ( "Unknown profile format {}" , other) ;
119
- // TODO: Need to change to ExitCode or Termination
120
- std:: process:: exit ( 1 ) ;
121
- }
122
- } ;
123
-
124
- let profile_output = profile_output. unwrap_or_else ( || match profile_format {
125
- ProfileFormat :: Html => "flame-graph.html" . as_ref ( ) ,
126
- ProfileFormat :: Text => "flame.txt" . as_ref ( ) ,
127
- ProfileFormat :: Speedscope => "flamescope.json" . as_ref ( ) ,
128
- } ) ;
129
-
130
- let profile_output: Box < dyn io:: Write > = if profile_output == "-" {
131
- Box :: new ( io:: stdout ( ) )
132
- } else {
133
- Box :: new ( fs:: File :: create ( profile_output) ?)
134
- } ;
135
-
136
- let profile_output = io:: BufWriter :: new ( profile_output) ;
137
-
138
- match profile_format {
139
- ProfileFormat :: Html => flame:: dump_html ( profile_output) ?,
140
- ProfileFormat :: Text => flame:: dump_text_to_writer ( profile_output) ?,
141
- ProfileFormat :: Speedscope => flamescope:: dump ( profile_output) ?,
142
- }
143
-
144
- Ok ( ( ) )
145
- }
146
-
147
93
fn setup_main_module ( vm : & VirtualMachine ) -> PyResult < Scope > {
148
94
let scope = vm. new_scope_with_builtins ( ) ;
149
95
let main_module = vm. new_module ( "__main__" , scope. globals . clone ( ) , None ) ;
@@ -206,6 +152,9 @@ fn install_pip(_installer: &str, _scope: Scope, vm: &VirtualMachine) -> PyResult
206
152
}
207
153
208
154
fn run_rustpython ( vm : & VirtualMachine , run_mode : RunMode ) -> PyResult < ( ) > {
155
+ #[ cfg( feature = "flame-it" ) ]
156
+ let main_guard = flame:: start_guard ( "RustPython main" ) ;
157
+
209
158
let scope = setup_main_module ( vm) ?;
210
159
211
160
let site_result = vm. import ( "site" , None , 0 ) ;
@@ -244,6 +193,57 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
244
193
}
245
194
}
246
195
}
196
+ #[ cfg( feature = "flame-it" ) ]
197
+ {
198
+ main_guard. end ( ) ;
199
+ if let Err ( e) = write_profile ( & vm. state . as_ref ( ) . settings ) {
200
+ error ! ( "Error writing profile information: {}" , e) ;
201
+ }
202
+ }
203
+ Ok ( ( ) )
204
+ }
205
+
206
+ #[ cfg( feature = "flame-it" ) ]
207
+ fn write_profile ( settings : & Settings ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
208
+ use std:: { fs, io} ;
209
+
210
+ enum ProfileFormat {
211
+ Html ,
212
+ Text ,
213
+ Speedscope ,
214
+ }
215
+ let profile_output = settings. profile_output . as_deref ( ) ;
216
+ let profile_format = match settings. profile_format . as_deref ( ) {
217
+ Some ( "html" ) => ProfileFormat :: Html ,
218
+ Some ( "text" ) => ProfileFormat :: Text ,
219
+ None if profile_output == Some ( "-" . as_ref ( ) ) => ProfileFormat :: Text ,
220
+ Some ( "speedscope" ) | None => ProfileFormat :: Speedscope ,
221
+ Some ( other) => {
222
+ error ! ( "Unknown profile format {}" , other) ;
223
+ // TODO: Need to change to ExitCode or Termination
224
+ std:: process:: exit ( 1 ) ;
225
+ }
226
+ } ;
227
+
228
+ let profile_output = profile_output. unwrap_or_else ( || match profile_format {
229
+ ProfileFormat :: Html => "flame-graph.html" . as_ref ( ) ,
230
+ ProfileFormat :: Text => "flame.txt" . as_ref ( ) ,
231
+ ProfileFormat :: Speedscope => "flamescope.json" . as_ref ( ) ,
232
+ } ) ;
233
+
234
+ let profile_output: Box < dyn io:: Write > = if profile_output == "-" {
235
+ Box :: new ( io:: stdout ( ) )
236
+ } else {
237
+ Box :: new ( fs:: File :: create ( profile_output) ?)
238
+ } ;
239
+
240
+ let profile_output = io:: BufWriter :: new ( profile_output) ;
241
+
242
+ match profile_format {
243
+ ProfileFormat :: Html => flame:: dump_html ( profile_output) ?,
244
+ ProfileFormat :: Text => flame:: dump_text_to_writer ( profile_output) ?,
245
+ ProfileFormat :: Speedscope => flamescope:: dump ( profile_output) ?,
246
+ }
247
247
248
248
Ok ( ( ) )
249
249
}
0 commit comments