]> granicus.if.org Git - sysstat/commitdiff
sar/sadf: Merge functions set_record_timestamp_string()
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 22 Jan 2016 08:11:17 +0000 (09:11 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 22 Jan 2016 08:11:17 +0000 (09:11 +0100)
sar and sadf had the same functions with almost the same features.
Merge them.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
sa.h
sa_common.c
sadf.c
sar.c

diff --git a/sa.h b/sa.h
index aa83b04aa1fa6648697a2ee0a0a39d2d159c3736..ca639e5770abfe3852a75db90e27bf5a1dabce40 100644 (file)
--- a/sa.h
+++ b/sa.h
@@ -96,6 +96,7 @@
 #define S_F_COMMENT            0x00001000
 #define S_F_PERSIST_NAME       0x00002000
 #define S_F_LOCAL_TIME         0x00004000
+#define S_F_PREFD_TIME_OUTPUT  0x00008000
 
 #define WANT_SINCE_BOOT(m)             (((m) & S_F_SINCE_BOOT)   == S_F_SINCE_BOOT)
 #define WANT_SA_ROTAT(m)               (((m) & S_F_SA_ROTAT)     == S_F_SA_ROTAT)
 #define DISPLAY_COMMENT(m)             (((m) & S_F_COMMENT)      == S_F_COMMENT)
 #define DISPLAY_PERSIST_NAME_S(m)      (((m) & S_F_PERSIST_NAME) == S_F_PERSIST_NAME)
 #define PRINT_LOCAL_TIME(m)            (((m) & S_F_LOCAL_TIME)   == S_F_LOCAL_TIME)
+#define USE_PREFD_TIME_OUTPUT(m)       (((m) & S_F_PREFD_TIME_OUTPUT)   == S_F_PREFD_TIME_OUTPUT)
 
 #define AO_F_NULL              0x00000000
 
@@ -964,5 +966,8 @@ extern void
        set_default_file(char *, int, int);
 extern void
        set_hdr_rectime(unsigned int, struct tm *, struct file_header *);
+extern void
+       set_record_timestamp_string(unsigned int, struct record_header *,
+                                   char *, char *, int, struct tm *);
 
 #endif  /* _SA_H */
index cb3afeacfa6cd36f0d5987a2bf1f363abcb0c1df..9401653baa367632e2d9627401994e85f708891c 100644 (file)
@@ -2191,3 +2191,54 @@ int sa_get_record_timestamp_struct(unsigned int l_flags, struct record_header *r
 
        return rc;
 }
+
+/*
+ ***************************************************************************
+ * Set current record's timestamp strings (date and time) using the time
+ * data saved in @rectime structure. The string may be the number of seconds
+ * since the epoch if flag S_F_SEC_EPOCH has been set.
+ *
+ * IN:
+ * @l_flags    Flags indicating the type of time expected by the user.
+ *             S_F_SEC_EPOCH means the time should be expressed in seconds
+ *             since the epoch (01/01/1970).
+ * @record_hdr Record header containing the number of seconds since the
+ *             epoch.
+ * @cur_date   String where timestamp's date will be saved. May be NULL.
+ * @cur_time   String where timestamp's time will be saved.
+ * @len                Maximum length of timestamp strings.
+ * @rectime    Structure with current timestamp (expressed in local time or
+ *             in UTC depending on whether options -T or -t have been used
+ *             or not) that should be broken down in date and time strings.
+ *
+ * OUT:
+ * @cur_date   Timestamp's date string (if expected).
+ * @cur_time   Timestamp's time string. May contain the number of seconds
+ *             since the epoch (01-01-1970) if corresponding option has
+ *             been used.
+ ***************************************************************************
+*/
+void set_record_timestamp_string(unsigned int l_flags, struct record_header *record_hdr,
+                                char *cur_date, char *cur_time, int len, struct tm *rectime)
+{
+       /* Set cur_time date value */
+       if (PRINT_SEC_EPOCH(l_flags) && cur_date) {
+               sprintf(cur_time, "%ld", record_hdr->ust_time);
+               strcpy(cur_date, "");
+       }
+       else {
+               /*
+                * If options -T or -t have been used then cur_time is
+                * expressed in local time. Else it is expressed in UTC.
+                */
+               if (cur_date) {
+                       strftime(cur_date, len, "%Y-%m-%d", rectime);
+               }
+               if (USE_PREFD_TIME_OUTPUT(l_flags)) {
+                       strftime(cur_time, len, "%X", rectime);
+               }
+               else {
+                       strftime(cur_time, len, "%H:%M:%S", rectime);
+               }
+       }
+}
diff --git a/sadf.c b/sadf.c
index e2ac62ec89bee18e87bb782c4479def953a0a3d9..1b1591f8a3e7d168ea44952ba1919dd46db573bb 100644 (file)
--- a/sadf.c
+++ b/sadf.c
@@ -176,45 +176,6 @@ void check_format_options(void)
        }
 }
 
-/*
- ***************************************************************************
- * Set current record's timestamp strings (date and time). This timestamp is
- * expressed in UTC or in local time, depending on whether options -T or -t
- * have been used or not.
- *
- * IN:
- * @curr       Index in array for current sample statistics.
- * @cur_date   String where timestamp's date will be saved.
- * @cur_time   String where timestamp's time will be saved.
- * @len                Maximum length of timestamp strings.
- * @rectime    Structure with current timestamp (expressed in local time or
- *             in UTC depending on whether options -T or -t have been used
- *             or not) that should be broken down in date and time strings.
- *
- * OUT:
- * @cur_date   Timestamp's date string.
- * @cur_time   Timestamp's time string. May contain the number of seconds
- *             since the epoch (01-01-1970) if option -U has been used.
- ***************************************************************************
-*/
-void set_record_timestamp_string(int curr, char *cur_date, char *cur_time, int len,
-                                struct tm *rectime)
-{
-       /* Set cur_time date value */
-       if (PRINT_SEC_EPOCH(flags)) {
-               sprintf(cur_time, "%ld", record_hdr[curr].ust_time);
-               strcpy(cur_date, "");
-       }
-       else {
-               /*
-                * If options -T or -t have been used then cur_time is
-                * expressed in local time. Else it is expressed in UTC.
-                */
-               strftime(cur_date, len, "%Y-%m-%d", rectime);
-               strftime(cur_time, len, "%H:%M:%S", rectime);
-       }
-}
-
 /*
  ***************************************************************************
  * Print tabulations
@@ -318,7 +279,8 @@ void print_special_record(int curr, int use_tm_start, int use_tm_end, int rtype,
        }
        else {
                /* Set date and time strings to be displayed for current record */
-               set_record_timestamp_string(curr, cur_date, cur_time, 32, rectime);
+               set_record_timestamp_string(flags, &record_hdr[curr],
+                                           cur_date, cur_time, 32, rectime);
        }
 
        if (rtype == R_RESTART) {
@@ -660,7 +622,8 @@ int logic2_write_stats(int curr, int reset, long *cnt, int use_tm_start,
        }
 
        /* Set current timestamp string */
-       set_record_timestamp_string(curr, cur_date, cur_time, 32, rectime);
+       set_record_timestamp_string(flags, &record_hdr[curr],
+                                   cur_date, cur_time, 32, rectime);
 
        write_mech_stats(curr, dt, itv, g_itv, cur_date, cur_time, act_id);
 
@@ -753,7 +716,8 @@ int logic1_write_stats(int curr, int use_tm_start, int use_tm_end, int reset,
        }
 
        /* Set date and time strings for current record */
-       set_record_timestamp_string(curr, cur_date, cur_time, 32, rectime);
+       set_record_timestamp_string(flags, &record_hdr[curr],
+                                   cur_date, cur_time, 32, rectime);
 
        if (*fmt[f_position]->f_timestamp) {
                (*fmt[f_position]->f_timestamp)(&tab, F_BEGIN, cur_date, cur_time,
diff --git a/sar.c b/sar.c
index 2e8c6c91aaa94af88fbc3fa68e3dcfd55ab664f8..573b78f788813b286c57348b606ab887ae7a48f3 100644 (file)
--- a/sar.c
+++ b/sar.c
@@ -324,35 +324,6 @@ int check_line_hdr(void)
        return rc;
 }
 
-/*
- ***************************************************************************
- * Set current record's timestamp string.
- *
- * IN:
- * @curr       Index in array for current sample statistics.
- * @len                Maximum length of timestamp string.
- *
- * OUT:
- * @cur_time   Timestamp string.
- *
- * RETURNS:
- * 1 if an error was detected, or 0 otherwise.
- ***************************************************************************
-*/
-int set_record_timestamp_string(int curr, char *cur_time, int len)
-{
-       /* Fill timestamp structure */
-       if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[curr],
-                                          &rectime, NULL))
-               /* Error detected */
-               return 1;
-
-       /* Set cur_time date value */
-       strftime(cur_time, len, "%X", &rectime);
-
-       return 0;
-}
-
 /*
  ***************************************************************************
  * Print statistics average.
@@ -469,12 +440,19 @@ int write_stats(int curr, int read_from_file, long *cnt, int use_tm_start,
                        return 0;
        }
 
-       /* Set previous timestamp */
-       if (set_record_timestamp_string(!curr, timestamp[!curr], 16))
+       /* Get then set previous timestamp */
+       if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[!curr],
+                                          &rectime, NULL))
                return 0;
-       /* Set current timestamp */
-       if (set_record_timestamp_string(curr,  timestamp[curr],  16))
+       set_record_timestamp_string(S_F_PREFD_TIME_OUTPUT, &record_hdr[!curr],
+                                   NULL, timestamp[!curr], 16, &rectime);
+
+       /* Get then set current timestamp */
+       if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[curr],
+                                          &rectime, NULL))
                return 0;
+       set_record_timestamp_string(S_F_PREFD_TIME_OUTPUT, &record_hdr[curr],
+                                   NULL, timestamp[curr], 16, &rectime);
 
        /* Check if we are beginning a new day */
        if (use_tm_start && record_hdr[!curr].ust_time &&
@@ -625,8 +603,11 @@ int sar_print_special(int curr, int use_tm_start, int use_tm_end, int rtype,
        int dp = 1;
        unsigned int new_cpu_nr;
 
-       if (set_record_timestamp_string(curr, cur_time, 26))
+       if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[curr],
+                                          &rectime, NULL))
                return 0;
+       set_record_timestamp_string(S_F_PREFD_TIME_OUTPUT, &record_hdr[curr],
+                                   NULL, cur_time, 26, &rectime);
 
        /* The record must be in the interval specified by -s/-e options */
        if ((use_tm_start && (datecmp(&rectime, &tm_start) < 0)) ||