Simplify sort_vector_push 22/226422/4
authorMateusz Majewski <m.majewski2@samsung.com>
Mon, 2 Mar 2020 08:05:11 +0000 (09:05 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Wed, 11 Mar 2020 15:34:09 +0000 (16:34 +0100)
This is done by assuming that the vector is not full. It is better to
handle that case elsewhere.

Change-Id: Ie62d9c71628ec98edc8daaafb6b97cb14503cf12

Makefile.am
include/sort_vector.h
src/libdlogutil/fd_info.c
src/libdlogutil/sort_vector.c
src/tests/fd_info.c
src/tests/logutil.c
src/tests/sort_vector.c

index 6327645..f097951 100644 (file)
@@ -316,7 +316,13 @@ src_tests_sort_vector_SOURCES = src/tests/sort_vector.c src/libdlogutil/sort_vec
 src_tests_sort_vector_CFLAGS = $(check_CFLAGS)
 src_tests_sort_vector_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=log_config_get_int,--wrap=log_entry_is_earlier,--wrap=free
 
-src_tests_fd_info_SOURCES = src/tests/fd_info.c src/libdlogutil/fd_info.c
+src_tests_fd_info_SOURCES = src/tests/fd_info.c \
+       src/libdlogutil/fd_info.c \
+       src/libdlogutil/sort_vector.c \
+       src/shared/logcommon.c \
+       src/shared/logconfig.c \
+       src/shared/parsers.c \
+       src/shared/queued_entry_timestamp.c
 src_tests_fd_info_CFLAGS = $(check_CFLAGS)
 src_tests_fd_info_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=sort_vector_push,--wrap=malloc,--wrap=free,--wrap=close,--wrap=dlogutil_entry_get_timestamp,--wrap=log_should_print_line
 
index c0aaf35..79c9be7 100644 (file)
@@ -49,7 +49,8 @@ int sort_vector_empty(struct sort_vector *logs);
 int sort_vector_full(struct sort_vector *logs);
 int sort_vector_used_size(struct sort_vector *logs);
 void sort_vector_pop(struct sort_vector *logs);
+dlogutil_entry_s *sort_vector_pop_ret(struct sort_vector *logs);
 void sort_vector_apply_config(struct sort_vector *logs, const struct log_config *config);
-int sort_vector_push(struct sort_vector *logs, dlogutil_entry_s *p, dlogutil_entry_cb callback, void *userdata);
+void sort_vector_push(struct sort_vector *logs, dlogutil_entry_s *p);
 long sort_vector_time_span(struct sort_vector *logs);
 int sort_vector_flush(struct sort_vector *logs, dlogutil_entry_cb callback, void *userdata, bool force);
index 079a898..ec3b9e4 100644 (file)
@@ -92,6 +92,7 @@ int fdi_push_log(struct fd_info *fdi, struct sort_vector *logs, dlogutil_entry_c
        }
 
        int r;
+       dlogutil_entry_s *entry = NULL;
 
        if (IS_VECTOR_SIZE_SORTABLE(logs->size)) {
                struct timespec ts;
@@ -103,10 +104,20 @@ int fdi_push_log(struct fd_info *fdi, struct sort_vector *logs, dlogutil_entry_c
                if (ts.tv_sec > logs->start.tv_sec || (ts.tv_sec == logs->start.tv_sec && ts.tv_nsec > logs->start.tv_nsec))
                        logs->old_logs_dumped = true;
 
-               r = sort_vector_push(logs, temp, callback, userdata);
-       } else {
-               r = callback(temp, userdata);
-               free(temp);
+               if (sort_vector_full(logs)) {
+                       if (logs->dump == DLOGUTIL_MODE_CONTINUOUS || logs->dump == DLOGUTIL_MAX_DUMP_SIZE)
+                               entry = sort_vector_pop_ret(logs);
+                       else
+                               sort_vector_pop(logs);
+               }
+
+               sort_vector_push(logs, temp);
+       } else
+               entry = temp;
+
+       if (entry) {
+               r = callback(entry, userdata);
+               free(entry);
        }
 
        return r;
index f3756e1..62ca2a2 100644 (file)
@@ -100,6 +100,15 @@ void sort_vector_pop(struct sort_vector *logs)
        logs->begin = (logs->begin + 1) % logs->size;
 }
 
+dlogutil_entry_s *sort_vector_pop_ret(struct sort_vector *logs)
+{
+       assert(!sort_vector_empty(logs));
+       dlogutil_entry_s *const ret = logs->data[logs->begin];
+       logs->data[logs->begin] = NULL;
+       logs->begin = (logs->begin + 1) % logs->size;
+       return ret;
+}
+
 int sort_vector_used_size(struct sort_vector *logs)
 {
        if (logs->end >= logs->begin)
@@ -113,23 +122,15 @@ int sort_vector_used_size(struct sort_vector *logs)
  * @details Push a log to the sorting container
  * @param[in] p Pushee (taken ownership of)
  * @param[in] logs Log sorting vector
- * @return 0 on success, -errno on failure
+ * @pre Vector not full
  */
-int sort_vector_push(struct sort_vector *logs, dlogutil_entry_s *p, dlogutil_entry_cb callback, void *userdata)
+void sort_vector_push(struct sort_vector *logs, dlogutil_entry_s *p)
 {
        assert(logs);
        assert(logs->data);
        assert(IS_VECTOR_SIZE_SORTABLE(logs->size));
        assert(p);
-
-       if (sort_vector_full(logs)) {
-               if (logs->dump == DLOGUTIL_MODE_CONTINUOUS || logs->dump == DLOGUTIL_MAX_DUMP_SIZE) {
-                       const int r = callback(sort_vector_back(logs), userdata);
-                       if (r != 0)
-                               return r;
-               }
-               sort_vector_pop(logs);
-       }
+       assert(!sort_vector_full(logs));
 
        /* Insertion sort. Logs usually appear in order, so it makes a lot of sense to just iterate
         * linearly from the end to try and find the correct index. The algorithm starts working
@@ -190,7 +191,6 @@ int sort_vector_push(struct sort_vector *logs, dlogutil_entry_s *p, dlogutil_ent
 
        logs->data[i] = p;
        logs->end = (logs->end + 1) % logs->size;
-       return 0;
 }
 
 long sort_vector_time_span(struct sort_vector *logs)
index c6af04e..b12f70d 100644 (file)
@@ -45,15 +45,11 @@ int callback(const dlogutil_entry_s *entry, void *userdata)
 }
 
 static bool sv_pushed;
-static int sv_push_retval;
-int __wrap_sort_vector_push(struct sort_vector *logs, dlogutil_entry_s *p, dlogutil_entry_cb comp_callback, void *userdata)
+void __wrap_sort_vector_push(struct sort_vector *logs, dlogutil_entry_s *p)
 {
        assert(p == (dlogutil_entry_s *) 0xBADFEEL);
-       assert(comp_callback == callback);
-       assert(userdata == (struct log_file *) 0xDEFACED);
 
        sv_pushed = true;
-       return sv_push_retval;
 }
 
 struct fd_info G_fdi;
@@ -166,13 +162,6 @@ int main()
        assert(!sv_flushed);
        sv_pushed = false;
 
-       sv_push_retval = -83;
-       expect_free1 = &G_fdi;
-       assert(-83 == fdi_push_log(&G_fdi, &sv, callback, (void *) 0xDEFACED, (dlogutil_filter_options_s *) 404));
-       assert(sv_pushed);
-       assert(!sv_flushed);
-       sv_push_retval = 0;
-
        sv.old_logs_dumped = false;
        sv.start.tv_sec = 2;
        sv.start.tv_nsec = 0;
index 4baeefa..edd9c18 100644 (file)
@@ -66,7 +66,16 @@ int __wrap_fdi_push_log(struct fd_info *fdi, struct sort_vector *logs, dlogutil_
        memcpy(ent, fdi->ops->peek_entry(fdi), sizeof(dlogutil_entry_s));
        struct fdi_internal *ii = fdi->priv_data;
        ii->has_log = false;
-       return sort_vector_push(logs, ent, callback, userdata);
+
+       int ret = 0;
+       if (sort_vector_full(logs)) {
+               if (logs->dump == DLOGUTIL_MODE_CONTINUOUS || logs->dump == DLOGUTIL_MAX_DUMP_SIZE)
+                       ret = callback(sort_vector_pop_ret(logs), userdata);
+               else
+                       sort_vector_pop(logs);
+       }
+       sort_vector_push(logs, ent);
+       return ret;
 }
 
 int main()
@@ -133,7 +142,7 @@ int main()
                assert(ent2);
                ent2->sec_sent_mono = 1;
                ent2->nsec_sent_mono = (79 + i) * 10000000;
-               sort_vector_push(&sv, ent2, callback, (void *)0xDDD);
+               sort_vector_push(&sv, ent2);
        }
 
        assert(sort_vector_time_span(&sv) == 110);
@@ -145,7 +154,7 @@ int main()
        assert(ent3);
        ent3->sec_sent_mono = 0;
        ent3->nsec_sent_mono = 999999999;
-       sort_vector_push(&sv, ent3, callback, (void *)0xDDD);
+       sort_vector_push(&sv, ent3);
        assert(sort_vector_time_span(&sv) == 900);
 
        struct fd_ops test_ops = {
@@ -186,7 +195,7 @@ int main()
        assert(ent_future);
        ent_future->sec_sent_mono = 2;
        ent_future->nsec_sent_mono = 0;
-       sort_vector_push(&sv, ent_future, callback, (void *)0xDDD);
+       sort_vector_push(&sv, ent_future);
        written_logs = 0;
        sort_vector_flush(&sv, callback, (void *)0xDDD, false);
        assert(written_logs == 0);
index b3c174a..20d5593 100644 (file)
@@ -75,7 +75,10 @@ int main()
        struct log_file lf;
        memset(&lf, 0, sizeof(struct log_file));
 
-#define ADD(x) assert(sort_vector_push(&sv, (dlogutil_entry_s *) x, callback, (void *)0xDA7A) == callback_ret)
+#define ADD(x) \
+       if (sort_vector_full(&sv)) \
+               total_flushed += (size_t) sort_vector_pop_ret(&sv); \
+       sort_vector_push(&sv, (dlogutil_entry_s *) x)
 #define POP(x) \
        free_total = 0; \
        sort_vector_pop(&sv); \
@@ -137,19 +140,6 @@ int main()
        assert((dlogutil_entry_s *) 20 == sort_vector_back(&sv));
        assert(6 == sort_vector_used_size(&sv));
 
-       callback_ret = 13;
-       ADD(27);
-       assert(20 == total_flushed);
-       total_flushed = 0;
-       /* ┌──┬──┬──┬──┬──┬──┬──┐
-        * │33│40│41│  │20│24│25│
-        * └──┴──┴──┴──┴──┴──┴──┘ */
-       assert(!sort_vector_empty(&sv));
-       assert(sort_vector_full(&sv));
-       assert((dlogutil_entry_s *) 20 == sort_vector_back(&sv));
-       assert(6 == sort_vector_used_size(&sv));
-       callback_ret = 0;
-
 #undef ADD
 #undef POP