From 9c46b437fcb13a1bfff0d992f106231b3ce59120 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 10 Jan 2020 18:23:21 +0900 Subject: [PATCH] analyze: optimize table creation by using table_add_many() --- src/analyze/analyze-security.c | 43 +++----- src/analyze/analyze.c | 179 ++++++++++++--------------------- 2 files changed, 82 insertions(+), 140 deletions(-) diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 20c1401390..7fa17c99f5 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -1484,7 +1484,6 @@ static int assess(const struct security_info *info, Table *overview_table, Analy if (details_table) { const char *checkmark, *description, *color = NULL; - TableCell *cell; if (badness == UINT64_MAX) { checkmark = " "; @@ -1507,13 +1506,12 @@ static int assess(const struct security_info *info, Table *overview_table, Analy if (d) description = d; - r = table_add_cell_full(details_table, &cell, TABLE_STRING, checkmark, 1, 1, 0, 0, 0); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - if (color) - (void) table_set_color(details_table, cell, color); - r = table_add_many(details_table, + TABLE_STRING, checkmark, + TABLE_SET_MINIMUM_WIDTH, 1, + TABLE_SET_MAXIMUM_WIDTH, 1, + TABLE_SET_ELLIPSIZE_PERCENT, 0, + TABLE_SET_COLOR, color, TABLE_STRING, a->id, TABLE_SET_URL, a->url, TABLE_STRING, description, TABLE_UINT64, a->weight, TABLE_SET_ALIGN_PERCENT, 100, @@ -1521,7 +1519,7 @@ static int assess(const struct security_info *info, Table *overview_table, Analy TABLE_UINT64, a->range, TABLE_SET_ALIGN_PERCENT, 100, TABLE_EMPTY, TABLE_SET_ALIGN_PERCENT, 100); if (r < 0) - return log_error_errno(r, "Failed to add cells to table: %m"); + return table_log_add_error(r); } } @@ -1597,35 +1595,26 @@ static int assess(const struct security_info *info, Table *overview_table, Analy if (overview_table) { char buf[DECIMAL_STR_MAX(uint64_t) + 1 + DECIMAL_STR_MAX(uint64_t) + 1]; - TableCell *cell; + _cleanup_free_ char *url = NULL; - r = table_add_cell(overview_table, &cell, TABLE_STRING, info->id); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); if (info->fragment_path) { - _cleanup_free_ char *url = NULL; - r = file_url_from_path(info->fragment_path, &url); if (r < 0) return log_error_errno(r, "Failed to generate URL from path: %m"); - - (void) table_set_url(overview_table, cell, url); } xsprintf(buf, "%" PRIu64 ".%" PRIu64, exposure / 10, exposure % 10); - r = table_add_cell(overview_table, &cell, TABLE_STRING, buf); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(overview_table, cell, 100); - r = table_add_cell(overview_table, &cell, TABLE_STRING, badness_table[i].name); + r = table_add_many(overview_table, + TABLE_STRING, info->id, + TABLE_SET_URL, url, + TABLE_STRING, buf, + TABLE_SET_ALIGN_PERCENT, 100, + TABLE_STRING, badness_table[i].name, + TABLE_SET_COLOR, strempty(badness_table[i].color), + TABLE_STRING, special_glyph(badness_table[i].smiley)); if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_color(overview_table, cell, strempty(badness_table[i].color)); - - r = table_add_cell(overview_table, NULL, TABLE_STRING, special_glyph(badness_table[i].smiley)); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); + return table_log_add_error(r); } return 0; diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index 991e61de7e..703a501ae6 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -1113,13 +1113,11 @@ static int analyze_blame(int argc, char *argv[], void *userdata) { if (u->time <= 0) continue; - r = table_add_cell(table, NULL, TABLE_TIMESPAN_MSEC, &u->time); + r = table_add_many(table, + TABLE_TIMESPAN_MSEC, &u->time, + TABLE_STRING, u->name); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_STRING, u->name); - if (r < 0) - return r; + return table_log_add_error(r); } (void) pager_open(arg_pager_flags); @@ -1630,7 +1628,7 @@ static int dump_exit_status(int argc, char *argv[], void *userdata) { TABLE_INT, (int) i, TABLE_STRING, exit_status_class(i)); if (r < 0) - return r; + return table_log_add_error(r); } else for (int i = 1; i < argc; i++) { @@ -1646,7 +1644,7 @@ static int dump_exit_status(int argc, char *argv[], void *userdata) { TABLE_INT, status, TABLE_STRING, exit_status_class(status) ?: "-"); if (r < 0) - return r; + return table_log_add_error(r); } (void) pager_open(arg_pager_flags); @@ -1853,33 +1851,23 @@ static int dump_timespan(int argc, char *argv[], void *userdata) { if (r < 0) return r; - r = table_add_cell(table, NULL, TABLE_STRING, "Original:"); + r = table_add_many(table, + TABLE_STRING, "Original:", + TABLE_STRING, *input_timespan); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_STRING, *input_timespan); - if (r < 0) - return r; + return table_log_add_error(r); r = table_add_cell_stringf(table, NULL, "%ss:", special_glyph(SPECIAL_GLYPH_MU)); if (r < 0) - return r; + return table_log_add_error(r); - r = table_add_cell(table, NULL, TABLE_UINT64, &output_usecs); + r = table_add_many(table, + TABLE_UINT64, &output_usecs, + TABLE_STRING, "Human:", + TABLE_TIMESPAN, &output_usecs, + TABLE_SET_COLOR, ansi_highlight()); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_STRING, "Human:"); - if (r < 0) - return r; - - r = table_add_cell(table, &cell, TABLE_TIMESPAN, &output_usecs); - if (r < 0) - return r; - - r = table_set_color(table, cell, ansi_highlight()); - if (r < 0) - return r; + return table_log_add_error(r); r = table_print(table, NULL); if (r < 0) @@ -1925,57 +1913,42 @@ static int test_timestamp_one(const char *p) { if (r < 0) return r; - r = table_add_cell(table, NULL, TABLE_STRING, "Original form:"); + r = table_add_many(table, + TABLE_STRING, "Original form:", + TABLE_STRING, p, + TABLE_STRING, "Normalized form:", + TABLE_TIMESTAMP, &usec, + TABLE_SET_COLOR, ansi_highlight_blue()); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_STRING, p); - if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_STRING, "Normalized form:"); - if (r < 0) - return r; - - r = table_add_cell(table, &cell, TABLE_TIMESTAMP, &usec); - if (r < 0) - return r; - - r = table_set_color(table, cell, ansi_highlight_blue()); - if (r < 0) - return r; + return table_log_add_error(r); if (!in_utc_timezone()) { - r = table_add_cell(table, NULL, TABLE_STRING, "(in UTC):"); + r = table_add_many(table, + TABLE_STRING, "(in UTC):", + TABLE_TIMESTAMP_UTC, &usec); if (r < 0) - return r; - - r = table_add_cell(table, &cell, TABLE_TIMESTAMP_UTC, &usec); - if (r < 0) - return r; + return table_log_add_error(r); } r = table_add_cell(table, NULL, TABLE_STRING, "UNIX seconds:"); if (r < 0) - return r; + return table_log_add_error(r); if (usec % USEC_PER_SEC == 0) - r = table_add_cell_stringf(table, &cell, "@%"PRI_USEC, + r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC, usec / USEC_PER_SEC); else - r = table_add_cell_stringf(table, &cell, "@%"PRI_USEC".%06"PRI_USEC"", + r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC".%06"PRI_USEC"", usec / USEC_PER_SEC, usec % USEC_PER_SEC); if (r < 0) return r; - r = table_add_cell(table, NULL, TABLE_STRING, "From now:"); + r = table_add_many(table, + TABLE_STRING, "From now:", + TABLE_TIMESTAMP_RELATIVE, &usec); if (r < 0) - return r; - - r = table_add_cell(table, &cell, TABLE_TIMESTAMP_RELATIVE, &usec); - if (r < 0) - return r; + return table_log_add_error(r); return table_print(table, NULL); } @@ -2035,22 +2008,18 @@ static int test_calendar_one(usec_t n, const char *p) { return r; if (!streq(t, p)) { - r = table_add_cell(table, NULL, TABLE_STRING, "Original form:"); + r = table_add_many(table, + TABLE_STRING, "Original form:", + TABLE_STRING, p); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_STRING, p); - if (r < 0) - return r; + return table_log_add_error(r); } - r = table_add_cell(table, NULL, TABLE_STRING, "Normalized form:"); + r = table_add_many(table, + TABLE_STRING, "Normalized form:", + TABLE_STRING, t); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_STRING, t); - if (r < 0) - return r; + return table_log_add_error(r); for (unsigned i = 0; i < arg_iterations; i++) { usec_t next; @@ -2058,17 +2027,12 @@ static int test_calendar_one(usec_t n, const char *p) { r = calendar_spec_next_usec(spec, n, &next); if (r == -ENOENT) { if (i == 0) { - r = table_add_cell(table, NULL, TABLE_STRING, "Next elapse:"); + r = table_add_many(table, + TABLE_STRING, "Next elapse:", + TABLE_STRING, "never", + TABLE_SET_COLOR, ansi_highlight_yellow()); if (r < 0) - return r; - - r = table_add_cell(table, &cell, TABLE_STRING, "never"); - if (r < 0) - return r; - - r = table_set_color(table, cell, ansi_highlight_yellow()); - if (r < 0) - return r; + return table_log_add_error(r); } break; } @@ -2076,17 +2040,12 @@ static int test_calendar_one(usec_t n, const char *p) { return log_error_errno(r, "Failed to determine next elapse for '%s': %m", p); if (i == 0) { - r = table_add_cell(table, NULL, TABLE_STRING, "Next elapse:"); + r = table_add_many(table, + TABLE_STRING, "Next elapse:", + TABLE_TIMESTAMP, &next, + TABLE_SET_COLOR, ansi_highlight_blue()); if (r < 0) - return r; - - r = table_add_cell(table, &cell, TABLE_TIMESTAMP, &next); - if (r < 0) - return r; - - r = table_set_color(table, cell, ansi_highlight_blue()); - if (r < 0) - return r; + return table_log_add_error(r); } else { int k = DECIMAL_STR_WIDTH(i + 1); @@ -2097,34 +2056,28 @@ static int test_calendar_one(usec_t n, const char *p) { r = table_add_cell_stringf(table, NULL, "Iter. #%u:", i+1); if (r < 0) - return r; + return table_log_add_error(r); - r = table_add_cell(table, &cell, TABLE_TIMESTAMP, &next); + r = table_add_many(table, + TABLE_TIMESTAMP, &next, + TABLE_SET_COLOR, ansi_highlight_blue()); if (r < 0) - return r; - - r = table_set_color(table, cell, ansi_highlight_blue()); - if (r < 0) - return r; + return table_log_add_error(r); } if (!in_utc_timezone()) { - r = table_add_cell(table, NULL, TABLE_STRING, "(in UTC):"); + r = table_add_many(table, + TABLE_STRING, "(in UTC):", + TABLE_TIMESTAMP_UTC, &next); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_TIMESTAMP_UTC, &next); - if (r < 0) - return r; + return table_log_add_error(r); } - r = table_add_cell(table, NULL, TABLE_STRING, "From now:"); + r = table_add_many(table, + TABLE_STRING, "From now:", + TABLE_TIMESTAMP_RELATIVE, &next); if (r < 0) - return r; - - r = table_add_cell(table, NULL, TABLE_TIMESTAMP_RELATIVE, &next); - if (r < 0) - return r; + return table_log_add_error(r); n = next; }