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

libpmi: cleanup old code and optimize client reads #5423

Merged
merged 8 commits into from Sep 5, 2023

Conversation

garlick
Copy link
Member

@garlick garlick commented Sep 4, 2023

This is some gratuitous cleanup of old code in libpmi. The main change is replacing the internal dgetline() function with buffered stdio in the PMI client, which substantially reduces the number of read(2) calls. E.g. just a little strace snippet from the tail end of a simple PMI conversation, before and after:

master

write(18, "cmd=finalize\n", 13)         = 13
read(18, "c", 1)                        = 1
read(18, "m", 1)                        = 1
read(18, "d", 1)                        = 1
read(18, "=", 1)                        = 1
read(18, "f", 1)                        = 1
read(18, "i", 1)                        = 1
read(18, "n", 1)                        = 1
read(18, "a", 1)                        = 1
read(18, "l", 1)                        = 1
read(18, "i", 1)                        = 1
read(18, "z", 1)                        = 1
read(18, "e", 1)                        = 1
read(18, "_", 1)                        = 1
read(18, "a", 1)                        = 1
read(18, "c", 1)                        = 1
read(18, "k", 1)                        = 1
read(18, " ", 1)                        = 1
read(18, "r", 1)                        = 1
read(18, "c", 1)                        = 1
read(18, "=", 1)                        = 1
read(18, "0", 1)                        = 1
read(18, "\n", 1)                       = 1

libpmi_stdio

write(18, "cmd=finalize\n", 13)         = 13
read(18, "cmd=finalize_ack rc=0\n", 4096) = 22

I don't think that's actually going to affect performance much since the collective exchange operation dominates in a typical PMI session, but...blech! At least it reduces the gag reflex.

Problem: flux-start includes dgetline.h but doesn't use it

Drop include directive.
Problem: the libpmi "simple client" reads single characters
from the server socket in order to build a line without the
need to buffer characters.

Although this approach is functional, it is a best practice to
minimize the number of read(2) calls using buffering when possible.

Use libc buffered I/O instead.
Problem: dgetline.c is only used by libpmi tests now.

Wean tests off of dgetline.c and drop it:
- use a flux buffer watcher in the test server
- use stdio buffered I/O in the test clients
Problem: some older code in libpmi is formatted in a way
that clashes with the modern Flux code base.

Adjustments:
- make long parameter lists one per line
- make long conditional lists one condition per line (1 indent)
- declare pointers with no space between '*' and name
- use '*' to declare simple pointer parameters not the equivalent but
  less clear '[]' notation inherited from the canonical Argonne headers
Problem: PMI2_ERR_OTHER is the only PMI2 error not decoded by
pmi_strerror().

The PMI-1 and PMI-2 error codes are all the same except PMI-2 adds
PMI2_ERR_OTHER.  Since Flux can bootstrap from libpmi2, these errors
can be seen by Flux in real life.

Add PMI2_ERR_OTHER to the error string table.
@garlick garlick changed the title libpmi: cleanup old code libpmi: cleanup old code and optmize client reads Sep 4, 2023
@garlick garlick force-pushed the libpmi_stdio branch 3 times, most recently from 872a88e to 88421c1 Compare September 4, 2023 23:06
Problem: strlcpy() is a handy function that is not widely available
on linux.

Add openbsd strlcpy() to the internal libutil convenience library.
Problem: github CodeQL calls out an unbounded write in
libpmi/pmi2.c.  The bounds are actually checked but this
would be clearer and possibly easier for static analysis to
understand with strlcpy(3).

Use strlcpy(3).
Copy link
Member

@chu11 chu11 left a comment

Choose a reason for hiding this comment

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

nothing caught my eye

Problem: github CodeQL calls out a missed overflow check in
users of keyval_parse_uint().

keyval_parse_uint() and keyval_parse_int() assign the result
of strtoul() and strtol() respectively without checking for under/
overflow.

Add checks.
Update unit tests.
@garlick
Copy link
Member Author

garlick commented Sep 5, 2023

Thanks! Just repushed with one of the commit messages slightly improved. I'll set MWP.

@garlick garlick changed the title libpmi: cleanup old code and optmize client reads libpmi: cleanup old code and optimize client reads Sep 5, 2023
@codecov
Copy link

codecov bot commented Sep 5, 2023

Codecov Report

Merging #5423 (66f9d71) into master (772e1ab) will decrease coverage by 0.01%.
The diff coverage is 90.72%.

@@            Coverage Diff             @@
##           master    #5423      +/-   ##
==========================================
- Coverage   83.61%   83.61%   -0.01%     
==========================================
  Files         480      480              
  Lines       80560    80565       +5     
==========================================
+ Hits        67359    67362       +3     
- Misses      13201    13203       +2     
Files Changed Coverage Δ
src/cmd/flux-start.c 83.76% <ø> (ø)
src/common/libpmi/pmi_strerror.c 75.00% <ø> (ø)
src/common/libutil/strlcpy.c 71.42% <71.42%> (ø)
src/common/libpmi/simple_client.c 72.42% <90.32%> (-0.61%) ⬇️
src/common/libpmi/pmi2.c 87.73% <94.11%> (-0.69%) ⬇️
src/common/libpmi/pmi.c 93.57% <94.73%> (+1.83%) ⬆️
src/common/libpmi/keyval.c 100.00% <100.00%> (ø)
src/common/libpmi/simple_server.c 84.18% <100.00%> (ø)

... and 9 files with indirect coverage changes

@mergify mergify bot merged commit 8a34714 into flux-framework:master Sep 5, 2023
31 checks passed
@garlick garlick deleted the libpmi_stdio branch September 5, 2023 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants