Re: Allowing printf("%m") only where it actually works
Tom Lane <tgl@sss.pgh.pa.us>
Attachments
- timeprintf.c (text/x-c)
I wrote: > So this is all pretty much of a mess. If we annotate the elog functions > differently from printf's annotation then we risk getting these complaints > in elog.c, but if we don't do that then we can't really describe their > semantics correctly. We could possibly mark the replacement snprintf > functions with gnu_printf, but that's a lie with respect to the very > point at issue about %m. Unless we were to teach snprintf.c about %m > ... which probably wouldn't be hard, but I'm not sure I want to go there. Actually ... the more I think about this, the less insane that idea seems. Consider the following approach: 1. Teach src/port/snprintf.c about %m. While I've not written a patch for this, it looks pretty trivial. 2. Teach configure to test for %m and if it's not there, use the replacement snprintf. (Note: we're already forcing snprintf replacement in cross-compiles, so the added run-time test isn't losing anything.) 3. Get rid of elog.c's hand-made substitution of %m strings, and instead just let it pass the correct errno value down. (We'd likely need to do some fooling in appendStringInfoVA and related functions to preserve call-time errno, but that's not complicated, nor expensive.) 4. (Optional) Get rid of strerror(errno) calls in favor of %m, even in frontend code. Once we've done this, we have uniform printf semantics across all platforms, which is kind of nice from a testing standpoint, as well as being less of a cognitive load for developers. And we can stick with the existing approach of using the gnu_printf archetype across the board; that's no longer a lie for the snprintf.c code. One objection to this is the possible performance advantage of the native printf functions over snprintf.c. I did a bit of investigation of that using the attached testbed, and found that the quality of implementation of the native functions seems pretty variable: RHEL6's glibc on x86_64 (this is just a comparison point, since we'd not be replacing glibc's printf anyway): snprintf time = 756.795 ms total, 0.000756795 ms per iteration pg_snprintf time = 824.643 ms total, 0.000824643 ms per iteration macOS High Sierra on x86_64: snprintf time = 264.071 ms total, 0.000264071 ms per iteration pg_snprintf time = 348.41 ms total, 0.00034841 ms per iteration FreeBSD 11.0 on x86_64: snprintf time = 628.873 ms total, 0.000628873 ms per iteration pg_snprintf time = 606.684 ms total, 0.000606684 ms per iteration OpenBSD 6.0 on x86_64 (same hardware as FreeBSD test): snprintf time = 331.245 ms total, 0.000331245 ms per iteration pg_snprintf time = 539.849 ms total, 0.000539849 ms per iteration NetBSD 8.99 on armv6: snprintf time = 2423.39 ms total, 0.00242339 ms per iteration pg_snprintf time = 3769.16 ms total, 0.00376916 ms per iteration So we would be taking a hit on most platforms, but I've not really seen sprintf as a major component of very many profiles. Moreover, at least for the elog/ereport use-case, we'd be buying back some nontrivial part of that hit by getting rid of expand_fmt_string(). Also worth noting is that we've never made any effort at all to micro-optimize snprintf.c, so maybe there's some gold to be mined there to reduce the penalty. A different objection, possibly more serious than the performance one, is that if we get in the habit of using %m in frontend code then at some point we'd inevitably back-patch such a usage. (Worse, it'd pass testing on glibc platforms, only to fail elsewhere.) I don't see a bulletproof answer to that except to back-patch this set of changes, which might be a bridge too far. Aside from the back-patching angle, though, this seems pretty promising. Thoughts? regards, tom lane PS: here's the testbed I used for the above numbers. Feel free to try other platforms or other test-case formats. Compile this with something like gcc -Wall -O2 -I pgsql/src/include -I pgsql/src/port timeprintf.c (point the -I switches into a configured PG source tree); you might need to add "-lrt" or some such to get clock_gettime(). Then run with "./a.out 1000000" or so.
Commits
-
In pg_log_generic(), be more paranoid about preserving errno.
- cf665ad4c89e 12.0 landed
- fb30c9c1c5c3 13.0 landed
-
Make src/common/exec.c's error logging less ugly.
- b6b297d20df9 12.0 landed
-
Select appropriate PG_PRINTF_ATTRIBUTE for recent NetBSD.
- aed9fa0bd897 12.0 landed
-
Fix detection of the result type of strerror_r().
- e5baf8c27e6c 9.4.20 landed
- 8b36dc588d10 9.5.15 landed
- 7871a36255e2 11.0 landed
- 2855421ec728 9.6.11 landed
- 0aa1e0ef167d 10.6 landed
- 08aad3c81eff 9.3.25 landed
-
Try another way to detect the result type of strerror_r().
- 751f532b9766 12.0 landed
-
Clean up *printf macros to avoid conflict with format archetypes.
- 8b91d258844a 12.0 landed
-
Fix link failures due to snprintf/strerror changes.
- a6b88d682cbe 12.0 landed
-
Implement %m in src/port/snprintf.c, and teach elog.c to rely on that.
- d6c55de1f99a 12.0 landed
-
Always use our own versions of *printf().
- 96bf88d52711 12.0 landed
-
Incorporate strerror_r() into src/port/snprintf.c, too.
- 758ce9b77948 12.0 landed
-
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
- 26e9d4d4ef16 12.0 landed
-
Revert "Distinguish printf-like functions that support %m from those that don't."
- 46b5e7c4b5be 12.0 landed
-
Produce compiler errors if errno is referenced inside elog/ereport calls.
- a2a8acd15217 12.0 landed
-
Distinguish printf-like functions that support %m from those that don't.
- 3a60c8ff892a 12.0 landed
-
Fix unportable usage of printf("%m").
- a13b47a59ffc 11.0 cited
-
Be more robust when strerror() doesn't give a useful result.
- 8e68816cc256 9.4.0 cited