-
-
Notifications
You must be signed in to change notification settings - Fork 482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sage-logger
: Replace use of /usr/bin/time
by bash keyword time
#37840
Conversation
Documentation preview for this PR (built with commit ffa4f46; changes) is ready! 🎉 |
Do we really need to have different functions for different systems? It seems this kind of case by case handling might lead to issues later. May I suggest something like this, which depends only on
Usage:
Variation:
|
What "issues" could that possibly be? |
I don't know, someone wants to change something about this code, and has to be aware of the 3-4 different versions of the code. Just like #37391 was only tested on mac and seemed to work fine, having different implementations for different platforms is a risk. Since everything can e implemented with bash, we just defer to bash having the same behavior on the different platforms. |
Based on my experience with portability work, let me just say that I don't find this believable. |
Note that |
Bash is fine so long as the shebang doesn't say In case it helps, the comments to the effect that bash time can't redirect to a file aren't quite right. For example (in bash),
Problem is, this will redirect stderr for the inner command, too. I'm sure there's a way around it by juggling file descriptors. |
Here: #37840 (comment) |
Mere inches above where I was typing, the perfect hiding place. It's not any more readable but I would bet that the bash version is more portable. |
A compromise time_cmd() {
exec 3>&1 4>&2
local time=$({ time sh -c "$@" 1>&3 2>&4; } 2>& 1 )
echo "$time" > $timefile
}
report_time() {
local time=$(echo $(cat $timefile 2>/dev/null))
if [[ $time =~ real\ ([0-9]+)m([0-9]+)\.[0-9]+s ]]; then
local minutes="${BASH_REMATCH[1]}"
local seconds="${BASH_REMATCH[2]}"
total_seconds=$((minutes * 60 + seconds))
if (( total_seconds >= 10 )); then
echo "$time"
fi
fi
} Would this work? |
LGTM. Two comments:
This is more or less what I proposed here: if you don't like the extra parameter, you can hardcode "10" there. Something like: time_cmd() {
local start_seconds=$SECONDS
exec 3>&1 4>&2
local time=$({ time sh -c "$@" 1>&3 2>&4; } 2>& 1 )
local total_seconds=$(($SECONDS - $start_seconds))
if (( total_seconds >= 10 )); then
echo "$time"
fi
} |
My variant is a compromise, in the sense that the suggested commands can be replaced with Matthias' Anyway, this PR is Matthias's. He is free to adopt whatever we suggest or not. |
sage-logger
: Handle more flavors of time
, not just macOS/FreeBSDsage-logger
: Replace use of /usr/bin/time
by bash keyword time
Finally coming back to this PR. Thanks all for the suggestions. I'm preparing a version that adapts from the suggested solutions that use the bash keyword |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works well. LGTM. Thanks.
Thanks! |
…sh keyword `time` <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Follow-up after - sagemath#37391 - sagemath#37785 ... to make the display of package build times available on Linux too. Fixes sagemath#37785 (comment), closes sagemath#37832. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> - Depends on sagemath#37785 URL: sagemath#37840 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee
…sh keyword `time` <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Follow-up after - sagemath#37391 - sagemath#37785 ... to make the display of package build times available on Linux too. Fixes sagemath#37785 (comment), closes sagemath#37832. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> - Depends on sagemath#37785 URL: sagemath#37840 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee
local out | ||
TIME_OUTPUT=$((time 1>&3 2>&4 sh -c "$1") 2>&1) | ||
local retstat=$? | ||
[ "$SECONDS" -lt "$max" ] || echo "$TIME_OUTPUT" > "$timefile" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ "$SECONDS" -lt "$max" ] || echo "$TIME_OUTPUT" > "$timefile" | |
[ "$SECONDS" -lt "$max" ] || echo "$TIME_OUTPUT" >&2 |
you don't really need to go via "$timefile", do you?
It seems more natural if time_cmd
has similar semantics/usage as regular time
(except the time is only printed out if >= 10s).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I do. This thing runs in a subshell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is: print to stderr instead of saving to a file. What's wrong with that? Saves a point of failure (communication via filesystem).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my experience as the principal maintainer of the Sage distro build system, let me just say that this concern ("point of failure") is wildly beside the point.
@@ -94,8 +92,7 @@ if [ -n "$SAGE_SILENT_BUILD" -a ${use_prefix} = true ]; then | |||
# Silent build. | |||
# Similar to https://www.gnu.org/software/automake/manual/html_node/Automake-Silent-Rules.html#Automake-Silent-Rules | |||
echo "[$logname] installing. Log file: $logfile" | |||
( exec>> $logfile 2>&1 ; $TIME sh -c "$cmd"; status=$?; | |||
[ -r $timefile ] && cat $timefile; exit $status ) | |||
( exec>> $logfile 2>&1; time_cmd "$cmd"; status=$?; report_time; exit $status ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
( exec>> $logfile 2>&1; time_cmd "$cmd"; status=$?; report_time; exit $status ) | |
( exec>> $logfile 2>&1; time_cmd "$cmd" ) |
or even maybe
( exec>> $logfile 2>&1; time_cmd "$cmd"; status=$?; report_time; exit $status ) | |
time cmd "$cmd" >> $logfile 2>&$1 |
Also, in L104 below, the time is reported again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One goes to the logfile. One goes to the terminal.
( exec 2>&1; | ||
$TIME sh -c "$cmd"; status=$?; report_time; | ||
exit $status ) | \ | ||
( exec 2>&1; time_cmd "$cmd"; status=$?; report_time; exit $status ) | \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here...
time_cmd() { | ||
local max=$(($SECONDS+10)) | ||
exec 3>&1 4>&2 | ||
local out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, I'll clean this one up in a follow up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done in #38026.
TIME_OUTPUT=$((time 1>&3 2>&4 sh -c "$1") 2>&1) | ||
local retstat=$? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIME_OUTPUT=$((time 1>&3 2>&4 sh -c "$1") 2>&1) | |
local retstat=$? | |
local TIME_OUTPUT=$((time 1>&3 2>&4 sh -c "$1") 2>&1) retstat=$? |
if you want't TIME_OUTPUT
to be local, this is one way.
OTOH, if you keep TIME_OUTPUT
global you can use it instead of timefile
.
@tornaria Thanks for reviewing and for additional comments. But let me express my worries about certain attitude of reviewers that I am observing recently. First my apologies for making remarks toward fellow reviewers, which I usually don't. From our doc on "reviewing", we see
We develop sage "incrementally". A PR should be good enough to be accepted, but it need not be "perfect" from one reviewer's point of view. If the reviewer has an idea to improve the code further, then he can open up his own PR building on the present PR, implementing the idea. If I have mistaken your good intention, then my apologies again. |
I don't understand what you mean. I'll make sure in the future to keep my 2c to myself. |
<!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> - Clean up after sagemath#37391, which used `sage-logger` for prefixing output; generating the log/time files was a side effect, which has not proven to be useful - Fixes sagemath#37887 @jhpalmieri ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> - Depends on sagemath#37840 (merged here) URL: sagemath#38026 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee, Matthias Köppe
<!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> - Clean up after sagemath#37391, which used `sage-logger` for prefixing output; generating the log/time files was a side effect, which has not proven to be useful - Fixes sagemath#37887 @jhpalmieri ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> - Depends on sagemath#37840 (merged here) URL: sagemath#38026 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee, Matthias Köppe
Follow-up after
sage-logger
: Suppress "No such file or directory" messages #37785... to make the display of package build times available on Linux too.
Fixes #37785 (comment), closes #37832.
📝 Checklist
⌛ Dependencies
sage-logger
: Suppress "No such file or directory" messages #37785