Skip to content
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

detached sessions counter in _lp_jobcount_color is extremely slow #552

Closed
dogeared opened this issue Jul 6, 2018 · 4 comments
Closed

detached sessions counter in _lp_jobcount_color is extremely slow #552

dogeared opened this issue Jul 6, 2018 · 4 comments
Labels
slow An issue related to slow prompt generation tmux Related to tmux specific implemetation
Milestone

Comments

@dogeared
Copy link

dogeared commented Jul 6, 2018

Shell: bash
Operating system: Max OS X High Sierra
Liquid Prompt version (tag, commit): eda83ef

With LP_DEBUG_TIME=1 and LP_ENABLE_JOBS=1, I am seeing these results:

real	0m3.764s
user	0m0.287s
sys	0m1.016s

With LP_ENABLE_JOBS=0, I am seeing these results:

real	0m0.050s
user	0m0.017s
sys	0m0.028s

Looking at the _lp_jobcount_color function, the culprit seems to be the first section:

# Count detached sessions
if (( _LP_ENABLE_DETACHED_SESSIONS )); then
        local -i detached=0
        (( _LP_ENABLE_SCREEN )) && detached=$(screen -ls 2> /dev/null | \grep -c '[Dd]etach[^)]*)$')
        (( _LP_ENABLE_TMUX )) && detached+=$(tmux list-sessions 2> /dev/null | \grep -cv 'attached')
        (( detached > 0 )) && ret+="${LP_COLOR_JOB_D}${detached}d${NO_COL}"
fi

If I comment out the detached sessions section and set LP_ENABLE_JOBS=1, then the response is as performant as before:

real	0m0.049s
user	0m0.017s
sys	0m0.032s

I've made sure that screen and tmux are at the latest version with brew.

Digging in a little further, it is the call to screen that's slowing everything down:

time screen -ls
No Sockets found in /var/folders/g2/dgh3sg3x3dz55h74npd10_cm0000gn/T/.screen.

real	0m3.538s
user	0m0.242s
sys	0m0.894s

Any ideas why this may be happening?

@Rycieos
Copy link
Collaborator

Rycieos commented Jul 6, 2018

I'm assuming you use screen, otherwise you could uninstall or disable it.

This isn't exactly a liquidprompt issue, since it's an issue with screen.

My guess is that the filesystem in that directory is slow, and screen is suffering as a result. You can try to debug with strace. Here is my strace output for comparison (note I'm on Linux, not MacOS):

$ time screen -ls
There is a screen on:
        25073.pts-0.jaguar      (Detached)
1 Socket in /var/run/screen/S-mark.

real    0m0.008s
user    0m0.002s
sys     0m0.006s
$ strace -frT screen -ls
...
     0.000065 stat("/var/run/screen", {st_mode=S_IFDIR|0777, st_size=60, ...}) = 0 <0.000033>
     0.000085 access("/var/run/screen/S-mark", F_OK) = 0 <0.000035>
     0.000075 stat("/var/run/screen/S-mark", {st_mode=S_IFDIR|0700, st_size=60, ...}) = 0 <0.000033>
     0.000078 umask(02)                 = 0 <0.000021>
     0.000063 uname({sysname="Linux", nodename="jaguar", ...}) = 0 <0.000020>
     0.000062 setgid(1000)              = 0 <0.000025>
     0.000060 setuid(1000)              = 0 <0.000026>
     0.000073 geteuid()                 = 1000 <0.000020>
     0.000055 getegid()                 = 1000 <0.000021>
     0.000062 openat(AT_FDCWD, "/var/run/screen/S-mark", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3 <0.000036>
     0.000087 getdents(3, /* 3 entries */, 32768) = 88 <0.000026>
     0.000075 stat("/var/run/screen/S-mark/25073.pts-0.jaguar", {st_mode=S_IFSOCK|0600, st_size=0, ...}) = 0 <0.000033>
     0.000096 socket(AF_LOCAL, SOCK_STREAM, 0) = 4 <0.000047>
     0.000096 geteuid()                 = 1000 <0.000022>
     0.000058 getegid()                 = 1000 <0.000021>
     0.000057 connect(4, {sa_family=AF_LOCAL, sun_path="/var/run/screen/S-mark/25073.pts-0.jaguar"}, 43) = 0 <0.000114>
     0.000186 geteuid()                 = 1000 <0.000029>
     0.000071 getegid()                 = 1000 <0.000026>
     0.000065 geteuid()                 = 1000 <0.000024>
     0.000062 getegid()                 = 1000 <0.000024>
     0.000062 close(4)                  = 0 <0.000034>
     0.000073 getdents(3, /* 0 entries */, 32768) = 0 <0.000031>
     0.000073 close(3)                  = 0 <0.000027>
     0.000082 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0 <0.000026>
     0.000079 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4d10bef000 <0.000045>
     0.000112 write(1, "There is a screen on:\r\n", 23) = 23 <0.000062>
     0.000114 write(1, "\t25073.pts-0.jaguar\t(Detached)\n", 31) = 31 <0.000061>

The stat(), access(), getdents(), socket(), and connect() calls are the ones we care about, since they read the socket files in the target directory. In my case, that all takes about .0001 seconds.

@dolmen dolmen added slow An issue related to slow prompt generation tmux Related to tmux specific implemetation labels Jul 25, 2018
@dolmen
Copy link
Collaborator

dolmen commented Jul 25, 2018

As the issue is with screen I suggest to report the issue to that project (but link the report here to help us track it).

@dolmen dolmen closed this as completed Jul 25, 2018
@sjaks
Copy link

sjaks commented Nov 4, 2020

Does @dogeared have any updates on this? Is there an issue in screen upstream? We are still facing this issue.

LP_ENABLE_JOBS=0:
real	0m0.007s
user	0m0.006s
sys	0m0.000s
LP_ENABLE_JOBS=1:
real	0m0.451s
user	0m0.309s
sys	0m0.145s

I am using version 1.11-3ubuntu1 on Ubuntu 20.04 server accessed via SSH. Similarly to @dogeared, commenting out the following also helps:
https://github.com/nojhan/liquidprompt/blob/ec891eaada5cd427292055603c7dbe977d478e67/liquidprompt#L790-L795

@Rycieos
Copy link
Collaborator

Rycieos commented Dec 9, 2020

I still think that there is nothing Liquidprompt can do to fix this call being slow.

However, I added a new config option in v2.0.0-beta.1: $LP_ENABLED_DETACHED_SESSIONS. See the link for documentation.

You can set LP_ENABLED_DETACHED_SESSIONS=0 to disable this check without also disabling jobs.

@Rycieos Rycieos added this to the v2.0 milestone Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
slow An issue related to slow prompt generation tmux Related to tmux specific implemetation
Projects
None yet
Development

No branches or pull requests

4 participants