Skip to content

Commit

Permalink
Faster manager_process_barrier_fd and drop message if BARRIER=1 found
Browse files Browse the repository at this point in the history
 - 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
  • Loading branch information
benjarobin committed May 1, 2020
1 parent 6eb35fd commit 7a5b476
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/core/manager.c
Expand Up @@ -2285,16 +2285,35 @@ static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, ui
}

static bool manager_process_barrier_fd(const char *buf, FDSet *fds) {
const char *p;

assert(buf);

/* nothing else must be sent when using BARRIER=1 */
if (STR_IN_SET(buf, "BARRIER=1", "BARRIER=1\n")) {
if (fdset_size(fds) != 1)
log_warning("Got incorrect number of fds with BARRIER=1, closing them.");
return true;
} else if (startswith(buf, "BARRIER=1\n") || strstr(buf, "\nBARRIER=1\n") || endswith(buf, "\nBARRIER=1"))
log_warning("Extra notification messages sent with BARRIER=1, ignoring everything.");
p = strstr(buf, "BARRIER=1");
if (p != NULL) {
int barrier_found = -1;

if (p == buf) {
/* Check there is nothing after BARRIER=1 */
p += STRLEN("BARRIER=1");
if ((p[0] == '\0') || (p[0] == '\n' && p[1] == '\0'))
barrier_found = 1;
} else {
/* Check BARRIER=1 is not at the beginning of a new line */
if (p[-1] != '\n')
barrier_found = 0;
}

if (barrier_found > 0) {
if (fdset_size(fds) != 1)
log_warning("Got incorrect number of fds with BARRIER=1, closing them.");
} else if (barrier_found < 0)
log_warning("Extra notification messages sent with BARRIER=1, ignoring everything.");

/* Drop the message if BARRIER=1 was found */
return (barrier_found != 0);
}
return false;
}

Expand Down

0 comments on commit 7a5b476

Please sign in to comment.