Skip to content

Performance - #3691

Closed
krader1961 wants to merge 6 commits into
fish-shell:masterfrom
krader1961:performance
Closed

krader1961 wants to merge 6 commits into
fish-shell:masterfrom
krader1961:performance

Conversation

@krader1961

Copy link
Copy Markdown
Contributor

This mostly fixes issue #2007. I've been running this for a few hours on macOS and Ubuntu with no obvious problems. I'll also install it on Cygwin. The chunked read portion of the change should be safe and have no unintended side-effects. The changes to how signals are managed, the first commit in this PR, is a very different matter with significant risk that it could introduce hard to test or reproduce race conditions.

Kurtis Rader added 3 commits January 1, 2017 13:53
The shell was doing a log of signal blocking/unblocking that hurts
performance and can be avoided. This reduced the elapsed time for a
simple benchmark by 25%.

Partial fix for #2007
Refactor `builtin_read()` to split the code that does the actual reading
into separate functions. This introduces the `read_in_chunks()` function
but in this change it is just a clone of `read_one_char_at_a_time()`. It
will be modified to actually read in chunks in the next change.

Partial fix for #2007
Provide a more efficient method for reading lines (or null terminated
sequences) when the input is seekable.

Another partial fix for #2007
@zanchey

zanchey commented Jan 2, 2017

Copy link
Copy Markdown
Member

As discussed on Gitter, I think holding this until after 2.5.0 is wise.

@zanchey zanchey added this to the fish-future milestone Jan 2, 2017
@ridiculousfish
ridiculousfish self-requested a review January 3, 2017 07:10
@krader1961

Copy link
Copy Markdown
Contributor Author

I'm hoping the lack of feedback means people have applied this change and did not see any problems. If you haven't done so please take a few minutes to test it; even for just a quick test that it doesn't break anything you do regularly with fish.

@zanchey

zanchey commented Jan 7, 2017

Copy link
Copy Markdown
Member

I've been testing it, and it seems fine.

@floam

floam commented Jan 7, 2017

Copy link
Copy Markdown
Member

I'm digging it.

@ridiculousfish

Copy link
Copy Markdown
Member

I still plan to review!

@ridiculousfish

ridiculousfish commented Jan 10, 2017 •

Copy link
Copy Markdown
Member

A failing test case I found:

> function read_2_lines
	read line1
	read line2
	echo $line1 then $line2
end
> read_2_lines < /dev/stdin

Now type two lines, followed by control-D. Without the PR, it prints the two lines. With the PR, it doesn't terminate on the first ctrl-D, and seems to drop the second line. (Tested on 10.12.2)

@krader1961

Copy link
Copy Markdown
Contributor Author

@ridiculousfish: That's because the use of a function means the read commands are not reading from fd 0 even though it's logically stdin. And for some strange reason /dev/stdin is considered seekable even though it's just a symlink to /dev/fd/0 which is the tty. This is causing fish to use the chunked read which relies on the ability to seek backwards even though that won't work since it's a tty. The problem is this test in builtin_read():

if (isatty(0) && streams.stdin_fd == STDIN_FILENO && !split_null) {

I have no idea why it's not doing

if (isatty(streams.stdin_fd) && !split_null) {

Do we really want to bypass the interactive read behavior just because we're executing the read inside a function which, at the moment, will always get a non-zero fd for streams.stdin_fd? Making that change fixes the problem and make test still passes. It also means that I now see the read> prompt as I would expect.

The code still needs to have a isatty() test before entering the chunked read implementation. That will also fix the problem you found but I still think the aforementioned change should also be made. Pushing a new commit in a few minutes.

@krader1961

Copy link
Copy Markdown
Contributor Author

I'll also add your example to the unit tests to help prevent regressions.

Comment thread src/postfork.cpp
errno = EINTR;
while (result == -1 && errno == EINTR) {
result = tcsetpgrp(STDIN_FILENO, j->pgid);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a crack at reducing the EINTR-logic duplication later. For now this is fine.

Comment thread src/signal.cpp
sigaction(SIGTSTP, &act, 0);
sigaction(SIGTTIN, &act, 0);
sigaction(SIGTTOU, &act, 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no signal guru - does this do the right thing with the call to killpg(shell_pgid, SIGTTIN); in reader_interactive_init()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code in fish seems to mimic bash's initialize_job_control() which also does a kill (0, SIGTTIN) if the tcgetpgrp() fails. The only reason I'm ignoring SIGTTIN is because it it related to SIGTTOU which needs to be ignored to avoid a pathological failure case when fish writes to a broken tty. However, reading from that broken tty should probably not be protected in the same manner. I'll revert that portion of the change.

Comment thread src/signal.h
// TODO: Determine if the following three functions will need to be reinstated. For the time being
// they are #define stubs since they appear to be unnecessary and noticably slow the shell down.
// Be sure to reinstate the CHECK_BLOCK macro if these functions are reinstated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me pretty nervous, since now signals may be delivered in code that doesn't expect it. I suspect there are a lot of places where we don't handle EINTR, e.g. calls to tcsetattr. Maybe we can develop a test that delivers a signals with some frequency, to give us some confidence that these aren't necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me nervous as well. However, the existing implementation to block/unblock every signal is clearly akin to firing a shotgun to avoid being bitten by a mosquito. This is why I implemented this by implementing no-op stubs rather than removing the calls to these functions. We can reinstate the original version quickly without having to worry about resolving change conflicts.

@ridiculousfish

Copy link
Copy Markdown
Member

My big concern here is removing the signal_block() and signal_unblock() calls. I don't understand why those calls are there in the first place, and I can't really predict what will happen if we remove them.

I would guess/hope that the chunked reads will be a big boost and turn the signal munging into background noise. Do you think it's important for performance to remove the signal_block and signal_unblock code? How can we do it safely?

@krader1961

Copy link
Copy Markdown
Contributor Author

The elimination of the signal blocking/unblocking is important. It doesn't have as big an impact as implementing chunked reads but eliminating that signal manipulation is still a significant source of improvement. There is no way to do it safely since, as you note, no one understands why the existing code is blocking/unblocking every signal. That behavior is obviously not needed. What is unclear is whether it is necessary to selectively block/unblock a few signals like SIGCHLD.

I have no objection to deferring merging the signal management aspect of this PR and proceeding with merging the chunked read aspect. The only reason for eliminating the seemingly unnecessary signal manipulation was the initial, and correct, hypothesis that it measurably affects the performance of fish.

@krader1961

Copy link
Copy Markdown
Contributor Author

FWIW, I would not be surprised if changing how fish blocks/unblocks signals introduces a regression. However I am inclined to accept that risk in order to learn more about why the original author of this code decided this strategy was needed. At least between minor releases.

@ridiculousfish

Copy link
Copy Markdown
Member

I like the idea of getting a handle on our signal situation. An easy thing we could try is a mode (similar to chaos_mode in history.cpp) that spawns a background process or thread, that signals fish with some high frequency. (In fact that sounds super fun to write)

Assuming that idea is at all viable, my preference is to merge the chunked reads now, and plan to merge the signal_block removals once we have some automated tests in place. I'd also be fine with merging the signal_block removals, but gate it by an environment variable (or whatever) so that we can get some more dogfooding.

@krader1961

krader1961 commented Jan 14, 2017 •

Copy link
Copy Markdown
Contributor Author

Okay, I'm going to merge this gated on an env var. That will allow the core devs and others to enable it all the time so we can gain confidence it doesn't break anything or learn why the signal blocking was needed. Unless we do that it becomes difficult to know if this change is being used since people are otherwise installing fish based on other commits which won't have the signal management change.

Also, the SIGCHLD handler should be installed with the SA_RESTART flag since there is absolutely no reason for it to interrupt a syscall. I strongly suspect, given where signals are being blocked, the lack of that flag on that signal is the primary reason for the crazy blocking/unblocking. There may be other signals, such as SIGWINCH and SIGTERM, which also should have that flag as they should not need to cause an otherwise blocking syscall to return early.

@krader1961

Copy link
Copy Markdown
Contributor Author

Note that bash uses SA_RESTART for SIGCHLD and SIGTERM with these comments:

  /* We don't want a child death to interrupt interruptible system calls, even
     if we take the time to reap children */

  /* If we're installing a SIGTERM handler for interactive shells, we want
     it to be as close to SIG_IGN as possible. */

We definitely want to do the same for SIGCHLD. TBD is whether that's appropriate for SIGTERM given that fish's architecture is quite different.

@krader1961

Copy link
Copy Markdown
Contributor Author

I've merged this series of changes along with the revisions discussed in previous comments.

Please add set FISH_NO_SIGNAL_BLOCK 1 to your ~/.config/fish/config.fish script or otherwise ensure it is in your environment when starting a fish shell. This should make fish approximately 25% faster if it was built after commit fd6d814. It is only read once when the shell is starting in order to minimize overhead so you can't change it on the fly . We need as many people as possible to test the behavior enabled by that variable before the 2.6.0 release.

@zanchey

zanchey commented May 13, 2017

Copy link
Copy Markdown
Member

An easy thing we could try is a mode (similar to chaos_mode in history.cpp) that spawns a background process or thread, that signals fish with some high frequency. (In fact that sounds super fun to write)

@ridiculousfish, what would that look like? Would while true; kill -SIGUSR1 FISH_PROCESS; end do the job?

@ridiculousfish

Copy link
Copy Markdown
Member

Yep. I think we would have to ensure that SIGUSR1 is not ignored.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants