util: read all ready log sources each iteration 78/158878/21
authorMichal Bloch <m.bloch@samsung.com>
Mon, 16 Apr 2018 13:02:50 +0000 (15:02 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Thu, 5 Jul 2018 21:53:54 +0000 (21:53 +0000)
This is necessary to be able to implement the "pick oldest" heuristic.

It should also result in a slight increase in sorting quality even now,
but this is mostly an afterthought.

Change-Id: Ic0e03bca96744c56cf19e6cc58954f83f6d3ac9c
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/logutil/logutil.c

index 7ea1863..ddf2c6d 100755 (executable)
@@ -102,59 +102,61 @@ static void do_print(struct fd_info **data_fds, int fd_count, struct sort_vector
        assert(logs);
        assert(l_file);
 
-       int nfds;
-       int r;
-       struct fd_info *fdi = NULL;
        int epoll_cnt = 0;
-
-       int epollfd;
-       struct epoll_event ev = { .events = EPOLLIN };
-
-       epollfd = epoll_create1(0);
+       int epollfd = epoll_create1(0);
+       struct epoll_event *evs = NULL;
        if (epollfd < 0) {
                DBG("epoll_create failed: %m");
                return;
        }
 
-       for (nfds = 0; nfds < fd_count; ++nfds) {
+       for (int nfds = 0; nfds < fd_count; ++nfds) {
                if (!data_fds[nfds])
                        continue;
                int fd = data_fds[nfds]->fd;
-               ev.data.ptr = data_fds[nfds];
                if (fd_set_flags(fd, O_NONBLOCK) < 0)
                        goto cleanup;
+               struct epoll_event ev = {
+                       .data.ptr = data_fds[nfds],
+                       .events = EPOLLIN,
+               };
                epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
                ++epoll_cnt;
        }
 
-       while (epoll_cnt > 0) {
-               if (!fdi && logs->dump == DUMP_CONTINUOUS)
-                       flush_logs(logs, l_file);
+       evs = calloc(epoll_cnt, sizeof *evs);
+       if (!evs) {
+               ERR("no memory for epoll array");
+               goto cleanup;
+       }
 
-               nfds = epoll_wait(epollfd, &ev, 1, 100);
-               if (nfds > 0)
-                       fdi = (struct fd_info*) ev.data.ptr;
-               else {
-                       fdi = NULL;
-                       continue;
-               }
+       while (epoll_cnt > 0) {
+               int nfds = epoll_wait(epollfd, evs, epoll_cnt, 100);
+               if (nfds < 0)
+                       goto cleanup;
 
-               r = fdi->ops->read(fdi);
-               if (r < 0) {
-                       if (r == -EAGAIN)
-                               continue;
-                       else
-                               break;
-               }
+               for (int i = 0; i < nfds; ++i) {
+                       struct fd_info *fdi = evs[i].data.ptr;
+                       int r = fdi->ops->read(fdi);
+                       if (r < 0) {
+                               if (r == -EAGAIN)
+                                       continue;
+                               else
+                                       break;
+                       }
 
-               while (fdi->ops->has_log(fdi))
-                       if (fdi_push_log(fdi, logs, l_file))
-                               break;
+                       while (fdi->ops->has_log(fdi))
+                               if (fdi_push_log(fdi, logs, l_file))
+                                       break;
 
-               if (r == 0) {
-                       epoll_ctl(epollfd, EPOLL_CTL_DEL, fdi->fd, NULL);
-                       --epoll_cnt;
+                       if (r == 0) {
+                               epoll_ctl(epollfd, EPOLL_CTL_DEL, fdi->fd, NULL);
+                               --epoll_cnt;
+                       }
                }
+
+               if (logs->dump == DUMP_CONTINUOUS && !nfds)
+                       flush_logs(logs, l_file);
        }
 
        if (logs->dump != DUMP_CONTINUOUS && logs->dump != DUMP_INFINITE)
@@ -165,6 +167,7 @@ static void do_print(struct fd_info **data_fds, int fd_count, struct sort_vector
        flush_logs(logs, l_file);
 
 cleanup:
+       free(evs);
        close(epollfd);
 }