Sample Header Ad - 728x90

Why is pgrep/pkill so much slower than ps?

2 votes
0 answers
450 views
I would have thought that pgrep would be much faster than ps -ef yet the contrary is true - pgrep takes about 4x longer. But only on my laptop e.g.:
$ time { ps -ef |grep foobar; }
real	0m0.027s
user	0m0.009s
sys	0m0.023s
Compare with:
$ time pgrep foobar
real	0m0.215s
user	0m0.189s
sys	0m0.021s
... the times above are representative of a typical run. Context: This is on an Intel i7 laptop running linux kernel 5.6.19-200.fc31.x86_64 I have another machine (a server) running 5.7.9-100.fc31.x86_64 and it does the pgrep in about 10-12ms with roughly the same number of processes (~260). Just weird. I tried kernel 5.7.11 on this laptop and it made no difference - pgrep was still about 4 times slower than ps. Same result after I rebooted to 5.6.19. Same result from the console. On my raspberry pi-3b+ (4 cores) with raspbian, pgrep is about twice the speed of ps!!! My laptop has an Intel i7, 8-Gb, 4-cores with hyperthreading and about 270 processes running The server is i5, 8-Gb, 4-core and about 265 processes running The pi is 1-Gb, 4-core and about 133 processes running **EDIT:** I upgraded to kernel-5.9.16-100.fc32 and now pgrep is only about 3x slower than ps!! **EDIT2:** a bit more info. strace on pgrep gives this:
0.000089 openat(AT_FDCWD, "/proc/29/cmdline", O_RDONLY) = 4
     0.000071 read(4, "", 2047)         = 0
     0.000052 close(4)                  = 0
     0.503431 stat("/proc/30", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
     0.012402 openat(AT_FDCWD, "/proc/30/status", O_RDONLY) = 4
     0.002080 read(4, "Name:\tmigration/4\nUmask:\t0000\nSt"..., 2048) = 967
     0.000078 close(4)                  = 0
... so there's quite a delay in getting info on that process. There's no detail under that process so I don't know what it's about
Asked by wef (492 rep)
Jan 6, 2021, 07:47 AM
Last activity: Jan 6, 2021, 11:44 PM