]> granicus.if.org Git - sysstat/commitdiff
sar: Take into account a change of CPU number in sar datafile (6)
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 14 Mar 2014 17:14:24 +0000 (18:14 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 14 Mar 2014 17:14:24 +0000 (18:14 +0100)
Update sadf so that it can read the new datafile format.

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

diff --git a/sa.h b/sa.h
index 789f1f7c3e1f3d17397f608d99e18aafb06ec291..ce20504468da6ffcfb9dab15e441b751b6311c0b 100644 (file)
--- a/sa.h
+++ b/sa.h
@@ -805,8 +805,6 @@ extern __read_funct_t
 /* Other functions */
 extern void
        allocate_bitmaps(struct activity * []);
-extern void
-       allocate_cpu_structures(struct activity * [], unsigned int);
 extern void
        allocate_structures(struct activity * []);
 extern int
@@ -863,8 +861,11 @@ extern void
        print_report_hdr(unsigned int, struct tm *, struct file_header *, int);
 extern void
        read_file_stat_bunch(struct activity * [], int, int, int, struct file_activity *);
-extern unsigned int
-       read_new_cpu_nr(int, struct activity * []);
+extern __nr_t
+       read_vol_act_structures(int, struct activity * [], char *, struct file_magic *,
+                               unsigned int);
+extern int
+       reallocate_vol_act_structures(struct activity * [], unsigned int, unsigned int);
 extern int
        sa_fread(int, void *, int, int);
 extern void
diff --git a/sadf.c b/sadf.c
index 94fe09743bb8085ac31e5d29ed3b6fc5bb5d4f27..819e9ef6a73d6a8b268743a53a2d3d254043a6a9 100644 (file)
--- a/sadf.c
+++ b/sadf.c
@@ -780,10 +780,14 @@ int write_textual_stats(int curr, int use_tm_start, int use_tm_end, int reset,
  *                     been used or not) can be saved for current record.
  * @loctime            Structure where timestamp (expressed in local time)
  *                     can be saved for current record.
+ * @file               Name of file being read.
+ * @file_magic         file_magic structure filled with file magic header 
+ *                     data.
  ***************************************************************************
  */
 void sadf_print_special(int curr, int use_tm_start, int use_tm_end, int rtype, int ifd,
-                       struct tm *rectime, struct tm *loctime)
+                       struct tm *rectime, struct tm *loctime, char *file,
+                       struct file_magic *file_magic)
 {
        char cur_date[32], cur_time[32];
        int dp = 1;
@@ -802,8 +806,9 @@ void sadf_print_special(int curr, int use_tm_start, int use_tm_end, int rtype, i
        }
 
        if (rtype == R_RESTART) {
-               /* Don't forget to read new number of CPU */
-               new_cpu_nr = read_new_cpu_nr(ifd, act);
+               /* Don't forget to read the volatile activities structures */
+               new_cpu_nr = read_vol_act_structures(ifd, act, file, file_magic,
+                                                    file_hdr.sa_vol_act_nr);
                
                if (!dp)
                        return;
@@ -849,6 +854,8 @@ void sadf_print_special(int curr, int use_tm_start, int use_tm_end, int rtype, i
  *             be saved for current record.
  * @loctime    Structure where timestamp (expressed in local time) can be
  *             saved for current record.
+ * @file       Name of file being read.
+ * @file_magic file_magic structure filled with file magic header data.
  *
  * OUT:
  * @curr       Index in array for next sample statistics.
@@ -860,7 +867,8 @@ void sadf_print_special(int curr, int use_tm_start, int use_tm_end, int rtype, i
  */
 void rw_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf,
                       unsigned int act_id, int *reset, struct file_activity *file_actlst,
-                       __nr_t cpu_nr, struct tm *rectime, struct tm *loctime)
+                       __nr_t cpu_nr, struct tm *rectime, struct tm *loctime,
+                       char *file, struct file_magic *file_magic)
 {
        unsigned char rtype;
        int next;
@@ -899,7 +907,8 @@ void rw_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf,
 
                        if (rtype == R_COMMENT) {
                                sadf_print_special(*curr, tm_start.use, tm_end.use,
-                                                  R_COMMENT, ifd, rectime, loctime);
+                                                  R_COMMENT, ifd, rectime, loctime,
+                                                  file, file_magic);
                                continue;
                        }
 
@@ -926,6 +935,43 @@ void rw_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf,
        *reset = TRUE;
 }
 
+/*
+ ***************************************************************************
+ * Save or restore number of items for all known activities.
+ * 
+ * IN:
+ * @save_act_nr        Array containing number of items to restore for each
+ *             activity.
+ * @action     DO_SAVE to save number of items, or DO_RESTORE to restore.
+ * 
+ * OUT:
+ * @save_act_nr        Array containing number of items saved for each activity.
+ ***************************************************************************
+ */
+void sr_act_nr(__nr_t save_act_nr[], int action)
+{
+       int i;
+       
+       if (action == DO_SAVE) {
+               /* Save number of items for all activities */
+               for (i = 0; i < NR_ACT; i++) {
+                       save_act_nr[i] = act[i]->nr;
+               }
+       }
+       else if (action == DO_RESTORE) {
+               /*
+                * Restore number of items for all activities
+                * and reallocate structures accordingly.
+                */
+               for (i = 0; i < NR_ACT; i++) {
+                       if (save_act_nr[i] > 0) {
+                               reallocate_vol_act_structures(act, save_act_nr[i],
+                                                             act[i]->id);
+                       }
+               }
+       }
+}
+
 /*
  ***************************************************************************
  * Display activities for textual (XML-like) formats.
@@ -941,15 +987,17 @@ void rw_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf,
  *             be saved for current record.
  * @loctime    Structure where timestamp (expressed in local time) can be
  *             saved for current record.
+ * @file       Name of file being read.
  ***************************************************************************
  */
 void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfile,
                          struct file_magic *file_magic, __nr_t cpu_nr,
-                         struct tm *rectime, struct tm *loctime)
+                         struct tm *rectime, struct tm *loctime, char *file)
 {
        int curr, tab = 0, rtype;
        int eosaf = TRUE, next, reset = FALSE;
-       unsigned int save_cpu_nr, new_cpu_nr;
+       __nr_t save_act_nr[NR_ACT];
+       unsigned int new_cpu_nr;
        long cnt = 1;
        off_t fpos;
 
@@ -958,8 +1006,8 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                perror("lseek");
                exit(2);
        }
-       /* Save number of CPU items for current file position */
-       save_cpu_nr = act[get_activity_position(act, A_CPU)]->nr;
+       /* Save number of activities items for current file position */
+       sr_act_nr(save_act_nr, DO_SAVE);
 
        /* Print header (eg. XML file header) */
        if (*fmt[f_position]->f_header) {
@@ -991,10 +1039,11 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                                else if (rtype == R_RESTART) {
                                        /*
                                         * Ignore RESTART record (don't display it)
-                                        * but anyway we have to reallocate CPU structures
-                                        * according to new CPU count (value saved after RESTART record).
+                                        * but anyway we have to reallocate volatile
+                                        * activities structures.
                                         */
-                                       read_new_cpu_nr(ifd, act);
+                                       read_vol_act_structures(ifd, act, file, file_magic,
+                                                               file_hdr.sa_vol_act_nr);
                                }
                                else {
                                        /*
@@ -1034,10 +1083,11 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                                        else if (rtype == R_RESTART) {
                                                /*
                                                 * Ignore RESTART record (don't display it)
-                                                * but anyway we have to reallocate CPU structures
-                                                * according to new CPU count (value saved after RESTART record.
+                                                * but anyway we have to reallocate volatile
+                                                * activities structures.
                                                 */
-                                               read_new_cpu_nr(ifd, act);
+                                               read_vol_act_structures(ifd, act, file, file_magic,
+                                                                       file_hdr.sa_vol_act_nr);
                                        }
                                        else {
                                                /* This is not a special record, so read the extra fields */
@@ -1080,10 +1130,11 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                                                else if (rtype == R_RESTART) {
                                                        /*
                                                         * Ignore RESTART record (don't display it)
-                                                        * but anyway we have to reallocate CPU structures
-                                                        * according to new CPU count (value saved after RESTART record.
+                                                        * but anyway we have to reallocate volatile
+                                                        * activities structures.
                                                         */
-                                                       read_new_cpu_nr(ifd, act);
+                                                       read_vol_act_structures(ifd, act, file, file_magic,
+                                                                               file_hdr.sa_vol_act_nr);
                                                }
                                                else {
                                                        /* This is not a special record: Read the extra fields */
@@ -1108,8 +1159,12 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                perror("lseek");
                exit(2);
        }
-       /* ... and restore number of CPU items for this position in file */
-       allocate_cpu_structures(act, save_cpu_nr);
+       /*
+        * ... and restore number of items for volatile activities
+        * for this position in file.
+        */
+       sr_act_nr(save_act_nr, DO_RESTORE);
+       /* allocate_cpu_structures(act, save_cpu_nr);*/ /* FIXME */
 
        /* Process now RESTART entries to display restart messages */
        if (*fmt[f_position]->f_restart) {
@@ -1124,7 +1179,9 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                        rtype = record_hdr[0].record_type;
                        if (rtype == R_RESTART) {
                                /* Read new CPU count */
-                               new_cpu_nr = read_new_cpu_nr(ifd, act);
+                               new_cpu_nr = read_vol_act_structures(ifd, act, file, file_magic,
+                                                                    file_hdr.sa_vol_act_nr);
+                               /* new_cpu_nr = read_new_cpu_nr(ifd, act); */ /* FIXME */
                                
                                /* Display RESTART records */
                                write_textual_restarts(0, tm_start.use, tm_end.use, tab,
@@ -1154,8 +1211,11 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                perror("lseek");
                exit(2);
        }
-       /* ... and restore number of CPU items for this position in file */
-       allocate_cpu_structures(act, save_cpu_nr);
+       /*
+        * ... and restore number of items for volatile activities
+        * for this position in file.
+        */
+       sr_act_nr(save_act_nr, DO_RESTORE);
 
        /* Last, process COMMENT entries to display comments */
        if (DISPLAY_COMMENT(flags)) {
@@ -1176,10 +1236,11 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
                                else if (rtype == R_RESTART) {
                                        /*
                                         * Ignore RESTART record (don't display it)
-                                        * but anyway we have to reallocate CPU structures
-                                        * according to new CPU count (value saved after RESTART record.
+                                        * but anyway we have to reallocate volatile
+                                        * activities structures.
                                         */
-                                       read_new_cpu_nr(ifd, act);
+                                       read_vol_act_structures(ifd, act, file, file_magic,
+                                                               file_hdr.sa_vol_act_nr);
                                }
                                else {
                                        /* Not a special record: Read the extra fields */
@@ -1216,10 +1277,13 @@ void textual_display_loop(int ifd, struct file_activity *file_actlst, char *dfil
  *             be saved for current record.
  * @loctime    Structure where timestamp (expressed in local time) can be
  *             saved for current record.
+ * @file       Name of file being read.
+ * @file_magic file_magic structure filled with file magic header data.
  ***************************************************************************
  */
 void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr,
-                      struct tm *rectime, struct tm *loctime)
+                      struct tm *rectime, struct tm *loctime, char *file,
+                      struct file_magic *file_magic)
 {
        int i, p;
        int curr = 1, rtype;
@@ -1241,7 +1305,7 @@ void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr
                        rtype = record_hdr[0].record_type;
                        if ((rtype == R_RESTART) || (rtype == R_COMMENT)) {
                                sadf_print_special(0, tm_start.use, tm_end.use, rtype, ifd,
-                                                  rectime, loctime);
+                                                  rectime, loctime, file, file_magic);
                        }
                        else {
                                /*
@@ -1278,7 +1342,7 @@ void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr
                         */
                        rw_curr_act_stats(ifd, fpos, &curr, &cnt, &eosaf,
                                          ALL_ACTIVITIES, &reset, file_actlst,
-                                         cpu_nr, rectime, loctime);
+                                         cpu_nr, rectime, loctime, file, file_magic);
                }
                else {
                        /* For each requested activity... */
@@ -1297,7 +1361,8 @@ void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr
                                if (!HAS_MULTIPLE_OUTPUTS(act[p]->options)) {
                                        rw_curr_act_stats(ifd, fpos, &curr, &cnt, &eosaf,
                                                          act[p]->id, &reset, file_actlst,
-                                                         cpu_nr, rectime, loctime);
+                                                         cpu_nr, rectime, loctime, file,
+                                                         file_magic);
                                }
                                else {
                                        unsigned int optf, msk;
@@ -1310,7 +1375,8 @@ void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr
                                                        
                                                        rw_curr_act_stats(ifd, fpos, &curr, &cnt, &eosaf,
                                                                          act[p]->id, &reset, file_actlst,
-                                                                         cpu_nr, rectime, loctime);
+                                                                         cpu_nr, rectime, loctime, file,
+                                                                         file_magic);
                                                        act[p]->opt_flags = optf;
                                                }
                                        }
@@ -1328,7 +1394,8 @@ void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr
                                        if (rtype == R_COMMENT) {
                                                /* This was a COMMENT record: print it */
                                                sadf_print_special(curr, tm_start.use, tm_end.use,
-                                                                  R_COMMENT, ifd, rectime, loctime);
+                                                                  R_COMMENT, ifd, rectime, loctime,
+                                                                  file, file_magic);
                                        }
                                        else if (rtype != R_RESTART) {
                                                /* This is not a RESTART or a COMMENT record */
@@ -1343,7 +1410,8 @@ void main_display_loop(int ifd, struct file_activity *file_actlst, __nr_t cpu_nr
                /* The last record we read was a RESTART one: Print it */
                if (!eosaf && (record_hdr[curr].record_type == R_RESTART)) {
                        sadf_print_special(curr, tm_start.use, tm_end.use,
-                                          R_RESTART, ifd, rectime, loctime);
+                                          R_RESTART, ifd, rectime, loctime,
+                                          file, file_magic);
                }
        }
        while (!eosaf);
@@ -1387,11 +1455,11 @@ void read_stats_from_file(char dfile[])
 
        if (DISPLAY_GROUPED_STATS(fmt[f_position]->options)) {
                main_display_loop(ifd, file_actlst, cpu_nr,
-                                 &rectime, &loctime);
+                                 &rectime, &loctime, dfile, &file_magic);
        }
        else {
                textual_display_loop(ifd, file_actlst, dfile,
-                                    &file_magic, cpu_nr, &rectime, &loctime);
+                                    &file_magic, cpu_nr, &rectime, &loctime, dfile);
        }
 
        close(ifd);
diff --git a/sadf.h b/sadf.h
index 701ac787cee7d7d7b4fdc08c22cf47b7a4f23e41..df7537a111521de7dd4c513f7a2d83ded310d914 100644 (file)
--- a/sadf.h
+++ b/sadf.h
 #define F_MAIN 0x02
 #define F_END  0x04
 
+/* Various constants */
+#define DO_SAVE                0
+#define DO_RESTORE     1
+
 /*
  ***************************************************************************
  * Output format identification values.
index 5d16ce3ee9e30985823613a721c40b55481c438b..27f2f93ceea48f030ed989210f875860b380da21 100644 (file)
@@ -634,7 +634,7 @@ __printf_funct_t print_hdr_header(int *tab, int action, char *dfile,
                                 cpu_nr > 1 ? cpu_nr - 1 : 1);
                
                printf(_("Number of CPU for last samples in file: %u\n"),
-                      file_hdr->last_cpu_nr > 1 ? file_hdr->last_cpu_nr - 1 : 1);
+                      file_hdr->sa_last_cpu_nr > 1 ? file_hdr->sa_last_cpu_nr - 1 : 1);
                
                if ((loc_t = gmtime((const time_t *) &file_hdr->sa_ust_time)) != NULL) {
                        printf(_("File time: "));