Skip to content

Commit 7a5b476

Browse files
committed
Faster manager_process_barrier_fd and drop message if BARRIER=1 found
- This function is called for each message, and almost all of them will not contain BARRIER=1. So 5 comparaisons are realized in previous code. Now only one strstr is used. - If BARRIER=1 is found, drop the message in all case. So always return true when BARRIER=1 is found at the beginning of a new line Follow up of systemd#15547
1 parent 6eb35fd commit 7a5b476

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/core/manager.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,16 +2285,35 @@ static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, ui
22852285
}
22862286

22872287
static bool manager_process_barrier_fd(const char *buf, FDSet *fds) {
2288+
const char *p;
2289+
22882290
assert(buf);
22892291

22902292
/* nothing else must be sent when using BARRIER=1 */
2291-
if (STR_IN_SET(buf, "BARRIER=1", "BARRIER=1\n")) {
2292-
if (fdset_size(fds) != 1)
2293-
log_warning("Got incorrect number of fds with BARRIER=1, closing them.");
2294-
return true;
2295-
} else if (startswith(buf, "BARRIER=1\n") || strstr(buf, "\nBARRIER=1\n") || endswith(buf, "\nBARRIER=1"))
2296-
log_warning("Extra notification messages sent with BARRIER=1, ignoring everything.");
2293+
p = strstr(buf, "BARRIER=1");
2294+
if (p != NULL) {
2295+
int barrier_found = -1;
2296+
2297+
if (p == buf) {
2298+
/* Check there is nothing after BARRIER=1 */
2299+
p += STRLEN("BARRIER=1");
2300+
if ((p[0] == '\0') || (p[0] == '\n' && p[1] == '\0'))
2301+
barrier_found = 1;
2302+
} else {
2303+
/* Check BARRIER=1 is not at the beginning of a new line */
2304+
if (p[-1] != '\n')
2305+
barrier_found = 0;
2306+
}
2307+
2308+
if (barrier_found > 0) {
2309+
if (fdset_size(fds) != 1)
2310+
log_warning("Got incorrect number of fds with BARRIER=1, closing them.");
2311+
} else if (barrier_found < 0)
2312+
log_warning("Extra notification messages sent with BARRIER=1, ignoring everything.");
22972313

2314+
/* Drop the message if BARRIER=1 was found */
2315+
return (barrier_found != 0);
2316+
}
22982317
return false;
22992318
}
23002319

0 commit comments

Comments
 (0)