Do not create state->enabled if DLOGUTIL_MODE_NONPRINTING 12/260712/2
authorMateusz Majewski <m.majewski2@samsung.com>
Thu, 1 Jul 2021 11:13:21 +0000 (13:13 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Fri, 2 Jul 2021 12:14:31 +0000 (14:14 +0200)
This would be just an array of all 1s, a waste of memory and a worthless
syscall.

Change-Id: I9ef737bcf417547573d0ec264c1607318381b9c4

src/libdlogutil/lib.c
src/libdlogutil/logretrieve.c

index e0ad739..56c39fb 100644 (file)
@@ -241,11 +241,13 @@ EXPORT_API int dlogutil_buffer_clear(dlogutil_state_s *state, log_id_t buffer)
        CHECK_PARAM(buffer < LOG_ID_MAX);
        CHECK_PARAM(state->mode == DLOGUTIL_MODE_NONPRINTING);
 
+       assert(!state->enabled);
+
        if (state->aliased[buffer] != LOG_ID_INVALID)
                buffer = state->aliased[buffer];
 
        for (int i = 0; i < state->fd_count; ++i)
-               if (state->enabled[i] && state->data_fds[i]->id == buffer)
+               if (state->data_fds[i]->id == buffer)
                        return (state->data_fds[i]->ops->clear(state->data_fds[i])) ? TIZEN_ERROR_IO_ERROR : TIZEN_ERROR_NONE;
 
        return TIZEN_ERROR_INVALID_PARAMETER;
@@ -269,11 +271,13 @@ EXPORT_API int dlogutil_buffer_get_capacity(dlogutil_state_s *state, log_id_t bu
        CHECK_PARAM(buffer < LOG_ID_MAX);
        CHECK_PARAM(state->mode == DLOGUTIL_MODE_NONPRINTING);
 
+       assert(!state->enabled);
+
        if (state->aliased[buffer] != LOG_ID_INVALID)
                buffer = state->aliased[buffer];
 
        for (int i = 0; i < state->fd_count; ++i)
-               if (state->enabled[i] && state->data_fds[i]->id == buffer)
+               if (state->data_fds[i]->id == buffer)
                        return (state->data_fds[i]->ops->get_capacity(state->data_fds[i], capacity)) ? TIZEN_ERROR_IO_ERROR : TIZEN_ERROR_NONE;
 
        return TIZEN_ERROR_INVALID_PARAMETER;
@@ -287,11 +291,13 @@ EXPORT_API int dlogutil_buffer_get_usage(dlogutil_state_s *state, log_id_t buffe
        CHECK_PARAM(buffer < LOG_ID_MAX);
        CHECK_PARAM(state->mode == DLOGUTIL_MODE_NONPRINTING);
 
+       assert(!state->enabled);
+
        if (state->aliased[buffer] != LOG_ID_INVALID)
                buffer = state->aliased[buffer];
 
        for (int i = 0; i < state->fd_count; ++i)
-               if (state->enabled[i] && state->data_fds[i]->id == buffer)
+               if (state->data_fds[i]->id == buffer)
                        return (state->data_fds[i]->ops->get_usage(state->data_fds[i], usage)) ? TIZEN_ERROR_IO_ERROR : TIZEN_ERROR_NONE;
 
        return TIZEN_ERROR_INVALID_PARAMETER;
index 602ce8c..bced5fd 100644 (file)
@@ -159,6 +159,54 @@ int put_logs_into_vector(struct fd_info **data_fds, int fd_count, struct sort_ve
        return TIZEN_ERROR_NONE;
 }
 
+static int state_prepare_single_print(dlogutil_state_s *state, int nfds)
+{
+       assert(state);
+       assert(state->mode != DLOGUTIL_MODE_NONPRINTING);
+
+       int r = state->data_fds[nfds]->ops->prepare_print(state->data_fds[nfds], state->dump_size, state->mode == DLOGUTIL_MODE_MONITOR, state->filter_object);
+       if (r < 0)
+               return TIZEN_ERROR_IO_ERROR;
+       if (r > 0) {
+               // everything went fine, but we're not printing this one
+               fdi_free(state->data_fds[nfds]);
+               state->data_fds[nfds] = NULL;
+               return TIZEN_ERROR_NONE;
+       }
+
+       int fd = state->data_fds[nfds]->fd;
+       r = fd_set_flags(fd, O_NONBLOCK);
+       if (r < 0)
+               return TIZEN_ERROR_IO_ERROR;
+       struct epoll_event ev = {
+               .data.ptr = state->data_fds[nfds],
+               .events = EPOLLIN,
+       };
+       state->enabled[nfds] = true;
+       epoll_ctl(state->epollfd, EPOLL_CTL_ADD, fd, &ev);
+       ++state->epoll_cnt;
+
+       return TIZEN_ERROR_NONE;
+}
+
+static int state_prepare_prints(dlogutil_state_s *state)
+{
+       assert(state);
+       assert(state->mode != DLOGUTIL_MODE_NONPRINTING);
+
+       state->enabled = calloc(state->fd_count, sizeof *state->enabled);
+       if (!state->enabled)
+               return TIZEN_ERROR_OUT_OF_MEMORY;
+
+       for (int nfds = 0; nfds < state->fd_count; ++nfds) {
+               int r = state_prepare_single_print(state, nfds);
+               if (r != TIZEN_ERROR_NONE)
+                       return r;
+       }
+
+       return TIZEN_ERROR_NONE;
+}
+
 int dlogutil_state_init(dlogutil_state_s *state, struct fd_info ***data_fds_ptr, int fd_count, dlogutil_config_s *config, bool sorting_needed, dlogutil_sorting_order_e real_sort_by, const struct log_config *conf, log_id_t aliased[LOG_ID_MAX])
 {
        assert(data_fds_ptr);
@@ -205,36 +253,10 @@ int dlogutil_state_init(dlogutil_state_s *state, struct fd_info ***data_fds_ptr,
        if (!sort_vector_finalize(&state->logs))
                return TIZEN_ERROR_OUT_OF_MEMORY;
 
-       state->enabled = calloc(fd_count, sizeof *state->enabled);
-       if (!state->enabled)
-               return TIZEN_ERROR_OUT_OF_MEMORY;
-
-       for (int nfds = 0; nfds < fd_count; ++nfds) {
-               if (state->mode == DLOGUTIL_MODE_NONPRINTING) {
-                       state->enabled[nfds] = true;
-                       continue;
-               }
-
-               int r = data_fds[nfds]->ops->prepare_print(data_fds[nfds], state->dump_size, state->mode == DLOGUTIL_MODE_MONITOR, state->filter_object);
-               if (r < 0)
-                       return TIZEN_ERROR_IO_ERROR;
-               if (r > 0) {
-                       // everything went fine, but we're not printing this one
-                       fdi_free(data_fds[nfds]);
-                       data_fds[nfds] = NULL;
-                       continue;
-               }
-               int fd = data_fds[nfds]->fd;
-               r = fd_set_flags(fd, O_NONBLOCK);
-               if (r < 0)
-                       return TIZEN_ERROR_IO_ERROR;
-               struct epoll_event ev = {
-                       .data.ptr = data_fds[nfds],
-                       .events = EPOLLIN,
-               };
-               state->enabled[nfds] = true;
-               epoll_ctl(state->epollfd, EPOLL_CTL_ADD, fd, &ev);
-               ++state->epoll_cnt;
+       if (state->mode != DLOGUTIL_MODE_NONPRINTING) {
+               int r = state_prepare_prints(state);
+               if (r != TIZEN_ERROR_NONE)
+                       return r;
        }
 
        state->evs = calloc(state->epoll_cnt, sizeof *state->evs);
@@ -274,6 +296,8 @@ static bool handle_flush(dlogutil_state_s *state, dlogutil_entry_s **out)
 
 static void remove_drained_buffers(dlogutil_state_s *state)
 {
+       assert(state->enabled);
+
        /* Instead of removing buffers when .read returns 0, we do it
         * in a separate loop, using their .eof "method". This is because
         * removing buffers that way would fail if EOF would be triggered