Soften POSIX signal for termination of perf #384
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
A
SIGKILL
is, by definition, not allowed to be handled by a process. Therefore, a process receiving this signal isn't able to "clean up" before termination. This is particularly problematic in the case of programs that have "buffered" data (to limit filesystem I/O) and/or that are supposed to output their result when returning.Typically,
perf
is used with a target command (e.g.sleep
in theCOMMAND_TEMPLATES
). It collects samples (or does something else depending on the command and flags) until that command returns at which point, it writes back its result (perf.data
, output tostdout
, ...) before its own termination.However, if a
SIGKILL
(or other similar signal) is sent toperf
, this can't happen for the reason detailed above and its results are (at least partially) lost. Furthermore, in this case, the target command is still alive after this has happened as it is not a child of theperf
process.Finally, if a
SIGINT
(or other similar signal) is sent toperf
, it handles it by "properly" outputting its results (AFAIK) before returning; it ignores the target command, which stays alive.Therefore, we can trigger the "clean" reporting from
perf
and have a "clean exit" (no target command alive) by sendingSIGKILL
to the target command but nothing prevents us from sendingSIGINT
toperf
before that! By doing so, we guarantee "cleanperf
results" which adds robustness to the code.Issue
I am worried that the current implementation might send
SIGKILL
toperf
, depending on the output format ofps
(typically native to the device and varying between models):.killall()
(which sendsSIGKILL
, by default, to a list of processes) calls.get_pids_of()
which finds the list by runningps
and keeping the linesin
which the passed keyword (sleep
, here) can be found [*];ps
, itsCOMMAND
/CMD
column could output the arguments of the call (along with the command itself): in such a case, the previously described "grep
" would pick all lines containing'sleep'
which includes the ones such as'perf stat -a sleep 100'
;COMMAND
/CMD
,.killall()
ends up sendingSIGKILL
toperf
;This PR is motivated by comments on #382 and I believe that what I'm addressing here might be the cause. But, as this is an idea that came up from reading the code, I would really appreciate if it was tested on devices that currently report "corrupted" outputs from
perf
.ps
The lack of (single) standard behaviour for
ps
is pretty clear in its Ubuntuman
page:and formats exist with and without arguments:
I particularly like the following acknowledgement ("
most
"):So, depending on what the version of
ps
available on the device implements (and on which defaults it uses), its output is likely to vary between devices. For comparison:Comments
On top of this PR, could
.ps()
be re-implemented to useps
from the pushedbusybox
? This would allow a somewhat more standard behaviour or are there reasons why we don't want to do that? From a log, I believe this is not what is being done:[*] : Note that, in the case of
LinuxTarget
,.get_pids_of()
seems to be somewhat more robust as it uses-C
(which uses the command name) and will simply fail if that flag is not supported. Obviously, this is based on the hope that there are no implementations ofps
where-C
usesargs
...