30
30
framework.
31
31
"""
32
32
33
- import logging
34
33
import pstats
35
34
import sys
36
35
import threading
63
62
target = '%s.DEFAULT_PHASE_TIMEOUT_S' % __name__ ,
64
63
help = 'Test phase timeout in seconds' )
65
64
66
- # TODO(arsharma): Use the test state logger.
67
- _LOG = logging .getLogger (__name__ )
68
-
69
65
70
66
@attr .s (slots = True , frozen = True )
71
67
class ExceptionInfo (object ):
@@ -170,7 +166,8 @@ def __init__(self, phase_desc: phase_descriptor.PhaseDescriptor,
170
166
subtest_rec : Optional [test_record .SubtestRecord ]):
171
167
super (PhaseExecutorThread , self ).__init__ (
172
168
name = '<PhaseExecutorThread: (phase_desc.name)>' ,
173
- run_with_profiling = run_with_profiling )
169
+ run_with_profiling = run_with_profiling ,
170
+ logger = test_state .state_logger .getChild ('phase_executor_thread' ))
174
171
self ._phase_desc = phase_desc
175
172
self ._test_state = test_state
176
173
self ._subtest_rec = subtest_rec
@@ -238,6 +235,7 @@ class PhaseExecutor(object):
238
235
239
236
def __init__ (self , test_state : 'htf_test_state.TestState' ):
240
237
self .test_state = test_state
238
+ self .logger = test_state .state_logger .getChild ('phase_executor' )
241
239
# This lock exists to prevent stop() calls from being ignored if called when
242
240
# _execute_phase_once is setting up the next phase thread.
243
241
self ._current_phase_thread_lock = threading .Lock ()
@@ -267,8 +265,7 @@ def execute_phase(
267
265
requested and successfully ran for this phase execution.
268
266
"""
269
267
repeat_count = 1
270
- repeat_limit = (phase .options .repeat_limit or
271
- DEFAULT_RETRIES )
268
+ repeat_limit = (phase .options .repeat_limit or DEFAULT_RETRIES )
272
269
while not self ._stopping .is_set ():
273
270
is_last_repeat = repeat_count >= repeat_limit
274
271
phase_execution_outcome , profile_stats = self ._execute_phase_once (
@@ -277,9 +274,8 @@ def execute_phase(
277
274
# Give 3 default retries for timeout phase.
278
275
# Force repeat up to the repeat limit if force_repeat is set.
279
276
if ((phase_execution_outcome .is_timeout and
280
- phase .options .repeat_on_timeout ) or
281
- phase_execution_outcome .is_repeat or
282
- phase .options .force_repeat ) and not is_last_repeat :
277
+ phase .options .repeat_on_timeout ) or phase_execution_outcome .is_repeat
278
+ or phase .options .force_repeat ) and not is_last_repeat :
283
279
repeat_count += 1
284
280
continue
285
281
@@ -297,18 +293,18 @@ def _execute_phase_once(
297
293
"""Executes the given phase, returning a PhaseExecutionOutcome."""
298
294
# Check this before we create a PhaseState and PhaseRecord.
299
295
if phase_desc .options .run_if and not phase_desc .options .run_if ():
300
- _LOG .debug ('Phase %s skipped due to run_if returning falsey.' ,
301
- phase_desc .name )
296
+ self . logger .debug ('Phase %s skipped due to run_if returning falsey.' ,
297
+ phase_desc .name )
302
298
return PhaseExecutionOutcome (phase_descriptor .PhaseResult .SKIP ), None
303
299
304
300
override_result = None
305
301
with self .test_state .running_phase_context (phase_desc ) as phase_state :
306
302
if subtest_rec :
307
- _LOG . debug ('Executing phase %s under subtest %s' , phase_desc . name ,
308
- subtest_rec .name )
303
+ self . logger . debug ('Executing phase %s under subtest %s' ,
304
+ phase_desc . name , subtest_rec .name )
309
305
phase_state .set_subtest_name (subtest_rec .name )
310
306
else :
311
- _LOG .debug ('Executing phase %s' , phase_desc .name )
307
+ self . logger .debug ('Executing phase %s' , phase_desc .name )
312
308
with self ._current_phase_thread_lock :
313
309
# Checking _stopping must be in the lock context, otherwise there is a
314
310
# race condition: this thread checks _stopping and then switches to
@@ -328,7 +324,7 @@ def _execute_phase_once(
328
324
329
325
phase_state .result = phase_thread .join_or_die ()
330
326
if phase_state .result .is_repeat and is_last_repeat :
331
- _LOG .error ('Phase returned REPEAT, exceeding repeat_limit.' )
327
+ self . logger .error ('Phase returned REPEAT, exceeding repeat_limit.' )
332
328
phase_state .hit_repeat_limit = True
333
329
override_result = PhaseExecutionOutcome (
334
330
phase_descriptor .PhaseResult .STOP )
@@ -337,15 +333,15 @@ def _execute_phase_once(
337
333
# Refresh the result in case a validation for a partially set measurement
338
334
# or phase diagnoser raised an exception.
339
335
result = override_result or phase_state .result
340
- _LOG .debug ('Phase %s finished with result %s' , phase_desc .name ,
341
- result .phase_result )
336
+ self . logger .debug ('Phase %s finished with result %s' , phase_desc .name ,
337
+ result .phase_result )
342
338
return (result ,
343
339
phase_thread .get_profile_stats () if run_with_profiling else None )
344
340
345
341
def skip_phase (self , phase_desc : phase_descriptor .PhaseDescriptor ,
346
342
subtest_rec : Optional [test_record .SubtestRecord ]) -> None :
347
343
"""Skip a phase, but log a record of it."""
348
- _LOG .debug ('Automatically skipping phase %s' , phase_desc .name )
344
+ self . logger .debug ('Automatically skipping phase %s' , phase_desc .name )
349
345
with self .test_state .running_phase_context (phase_desc ) as phase_state :
350
346
if subtest_rec :
351
347
phase_state .set_subtest_name (subtest_rec .name )
@@ -359,16 +355,16 @@ def evaluate_checkpoint(
359
355
"""Evaluate a checkpoint, returning a PhaseExecutionOutcome."""
360
356
if subtest_rec :
361
357
subtest_name = subtest_rec .name
362
- _LOG . debug ('Evaluating checkpoint %s under subtest %s' , checkpoint . name ,
363
- subtest_name )
358
+ self . logger . debug ('Evaluating checkpoint %s under subtest %s' ,
359
+ checkpoint . name , subtest_name )
364
360
else :
365
- _LOG .debug ('Evaluating checkpoint %s' , checkpoint .name )
361
+ self . logger .debug ('Evaluating checkpoint %s' , checkpoint .name )
366
362
subtest_name = None
367
363
evaluated_millis = util .time_millis ()
368
364
try :
369
365
outcome = PhaseExecutionOutcome (checkpoint .get_result (self .test_state ))
370
- _LOG .debug ('Checkpoint %s result: %s' , checkpoint .name ,
371
- outcome .phase_result )
366
+ self . logger .debug ('Checkpoint %s result: %s' , checkpoint .name ,
367
+ outcome .phase_result )
372
368
if outcome .is_fail_subtest and not subtest_rec :
373
369
raise InvalidPhaseResultError (
374
370
'Checkpoint returned FAIL_SUBTEST, but subtest not running.' )
@@ -385,7 +381,7 @@ def evaluate_checkpoint(
385
381
def skip_checkpoint (self , checkpoint : phase_branches .Checkpoint ,
386
382
subtest_rec : Optional [test_record .SubtestRecord ]) -> None :
387
383
"""Skip a checkpoint, but log a record of it."""
388
- _LOG .debug ('Automatically skipping checkpoint %s' , checkpoint .name )
384
+ self . logger .debug ('Automatically skipping checkpoint %s' , checkpoint .name )
389
385
subtest_name = subtest_rec .name if subtest_rec else None
390
386
checkpoint_rec = test_record .CheckpointRecord .from_checkpoint (
391
387
checkpoint , subtest_name ,
@@ -417,11 +413,11 @@ def stop(
417
413
if phase_thread .is_alive ():
418
414
phase_thread .kill ()
419
415
420
- _LOG .debug ('Waiting for cancelled phase to exit: %s' , phase_thread )
416
+ self . logger .debug ('Waiting for cancelled phase to exit: %s' , phase_thread )
421
417
timeout = timeouts .PolledTimeout .from_seconds (timeout_s )
422
418
while phase_thread .is_alive () and not timeout .has_expired ():
423
419
time .sleep (0.1 )
424
- _LOG .debug ('Cancelled phase %s exit' ,
425
- "didn't" if phase_thread .is_alive () else 'did' )
420
+ self . logger .debug ('Cancelled phase %s exit' ,
421
+ "didn't" if phase_thread .is_alive () else 'did' )
426
422
# Clear the currently running phase, whether it finished or timed out.
427
423
self .test_state .stop_running_phase ()
0 commit comments