moby/pkg
Sebastiaan van Stijn 93853eca94
pkg/fileutils: GetTotalUsedFds(): slight optimization for macOS
This patch contains some optimizations I still had stashed when working
on eaa9494b71.

- Use the bytes package for handling the output of "lsof", instead of
  converting to a string.
- Count the number of newlines in the output, instead of splitting the
  output into a slice of strings. We're only interested in the number
  of lines in the output.
- Use lsof's -F option to only print the file-descriptor for each line,
  as we don't need other information.
- Use the -l, -n, and -P options to omit converting usernames, host names,
  and port numbers.

From the [LSOF(8)][1] man-page:

   -l    This option inhibits the conversion of user ID numbers to
         login names. It is also useful when login name lookup is
         working improperly or slowly.

   -n    This option inhibits the conversion of network numbers to host
         names for network files. Inhibiting conversion can make lsof run faster.
         It is also useful when host name lookup is not working properly.

   -P    This option inhibits the conversion of port numbers to port names for network files.
         Inhibiting the conversion can make lsof run a little faster.
         It is also useful when host name lookup is not working properly.

Output looks something like;

    lsof -lnP -Ff -p 39849
    p39849
    fcwd
    ftxt
    ftxt
    f0
    f1
    f2
    f3
    f4
    f5
    f6
    f7
    f8
    f9
    f10
    f11

Before/After:

    BenchmarkGetTotalUsedFds-10  122  9479384 ns/op   10816 B/op  63 allocs/op
    BenchmarkGetTotalUsedFds-10  154  7814697 ns/op    7257 B/op  60 allocs/op

[1]: https://opensource.apple.com/source/lsof/lsof-49/lsof/lsof.man.auto.html

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-11 10:50:31 +02:00
..
aaparser pkg/aaparser: deprecate GetVersion, as it's no longer used 2023-05-09 16:47:03 +02:00
archive pkg/archive: format code with gofumpt 2023-06-29 00:25:09 +02:00
authorization pkg/authorization: format code with gofumpt 2023-06-29 00:25:20 +02:00
broadcaster Various code-cleanup 2018-05-23 17:50:54 +02:00
capabilities pkg/capabilities: format code with gofumpt 2023-06-29 00:31:51 +02:00
chrootarchive pkg/chrootarchive: format code with gofumpt 2023-06-29 00:33:03 +02:00
containerfs remove pre-go1.17 build-tags 2023-05-19 20:38:51 +02:00
directory remove pre-go1.17 build-tags 2023-05-19 20:38:51 +02:00
dmesg Use Klogctl from x/sys/unix to read Linux kernel log 2019-08-22 08:25:13 +02:00
fileutils pkg/fileutils: GetTotalUsedFds(): slight optimization for macOS 2023-07-11 10:50:31 +02:00
homedir pkg/homedir: deprecate Key() utility 2023-06-26 14:46:03 +02:00
idtools run getent with a noop stdin 2023-06-12 09:35:40 +02:00
ioutils pkg/ioutils: format code with gofumpt 2023-06-29 00:31:51 +02:00
jsonmessage pkg/jsonmessage: format code with gofumpt 2023-06-29 00:31:51 +02:00
longpath pkg/ioutils: TempDir: move to pkg/longpath 2022-12-20 23:24:12 +01:00
loopback Switch all logging to use containerd log pkg 2023-06-24 00:23:44 +00:00
meminfo remove pre-go1.17 build-tags 2023-05-19 20:38:51 +02:00
namesgenerator Sort entries in pkg/namesgenerator 2022-11-01 23:13:34 +00:00
parsers pkg/parsers: format code with gofumpt 2023-06-29 00:26:03 +02:00
pidfile pkg/pidfile: implement Read() 2022-11-04 01:50:26 +01:00
platform Switch all logging to use containerd log pkg 2023-06-24 00:23:44 +00:00
platforms add support for image inspect with containerd-integration 2023-01-05 16:20:41 +01:00
plugingetter Move plugin client to separate interface 2018-05-30 15:22:10 -04:00
plugins pkg/plugins: format code with gofumpt 2023-06-29 00:31:52 +02:00
pools bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-11 00:06:42 +01:00
process remove pre-go1.17 build-tags 2023-05-19 20:38:51 +02:00
progress refactor: move from io/ioutil to io and os package 2021-08-27 14:56:57 +08:00
reexec remove pre-go1.17 build-tags 2023-05-19 20:38:51 +02:00
rootless Switch all logging to use containerd log pkg 2023-06-24 00:23:44 +00:00
stack all: replace strings.Replace with strings.ReplaceAll 2022-05-09 19:45:40 +08:00
stdcopy pkg/stdcopy: format code with gofumpt 2023-06-29 00:31:52 +02:00
streamformatter bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-11 00:06:42 +01:00
stringid pkg/stringid: optimize performance 2022-12-16 14:44:52 +01:00
sysinfo Switch all logging to use containerd log pkg 2023-06-24 00:23:44 +00:00
system pkg/system: format code with gofumpt 2023-06-29 00:26:02 +02:00
tailfile testing: move fuzzers over from OSS-Fuzz 2022-11-30 17:31:03 +01:00
tarsum pkg/tarsum: format code with gofumpt 2023-06-29 00:25:20 +02:00
useragent Add canonical import comment 2018-02-05 16:51:57 -05:00
README.md Rename a few docker to moby 2017-10-25 13:56:12 +02:00

pkg/ is a collection of utility packages used by the Moby project without being specific to its internals.

Utility packages are kept separate from the moby core codebase to keep it as small and concise as possible. If some utilities grow larger and their APIs stabilize, they may be moved to their own repository under the Moby organization, to facilitate re-use by other projects. However that is not the priority.

The directory pkg is named after the same directory in the camlistore project. Since Brad is a core Go maintainer, we thought it made sense to copy his methods for organizing Go code :) Thanks Brad!

Because utility packages are small and neatly separated from the rest of the codebase, they are a good place to start for aspiring maintainers and contributors. Get in touch if you want to help maintain them!