]> granicus.if.org Git - sysstat/blob - sa_conv.c
sadf: Properly convert structures with compatible changes
[sysstat] / sa_conv.c
1 /*
2  * sa_conv.c: Convert an old format sa file to the up-to-date format.
3  * (C) 1999-2017 by Sebastien GODARD (sysstat <at> orange.fr)
4  *
5  ***************************************************************************
6  * This program is free software; you can redistribute it and/or modify it *
7  * under the terms of the GNU General Public License as published  by  the *
8  * Free Software Foundation; either version 2 of the License, or (at  your *
9  * option) any later version.                                              *
10  *                                                                         *
11  * This program is distributed in the hope that it  will  be  useful,  but *
12  * WITHOUT ANY WARRANTY; without the implied warranty  of  MERCHANTABILITY *
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
14  * for more details.                                                       *
15  *                                                                         *
16  * You should have received a copy of the GNU General Public License along *
17  * with this program; if not, write to the Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA              *
19  ***************************************************************************
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <errno.h>
27
28 #include "version.h"
29 #include "sadf.h"
30 #include "sa_conv.h"
31
32 #ifdef USE_NLS
33 # include <locale.h>
34 # include <libintl.h>
35 # define _(string) gettext(string)
36 #else
37 # define _(string) (string)
38 #endif
39
40 extern int endian_mismatch;
41 extern unsigned int act_types_nr[];
42 extern unsigned int rec_types_nr[];
43 extern unsigned int hdr_types_nr[];
44
45 unsigned int oact_types_nr[] = {OLD_FILE_ACTIVITY_ULL_NR, OLD_FILE_ACTIVITY_UL_NR, OLD_FILE_ACTIVITY_U_NR};
46
47 /*
48  ***************************************************************************
49  * Read and upgrade file's magic data section.
50  *
51  * IN:
52  * @dfile       System activity data file name.
53  * @stdfd       File descriptor for STDOUT.
54  *
55  * OUT:
56  * @fd          File descriptor for sa datafile to convert.
57  * @file_magic  File's magic structure.
58  * @hdr_size    Size of header structure read from file.
59  * @previous_format
60  *              Format magic number of file to convert, converted to
61  *              current endianness.
62  * @endian_mismatch
63  *              TRUE if data read from file don't match current machine's
64  *              endianness.
65  *
66  * RETURNS:
67  * -1 on error, 0 otherwise.
68  ***************************************************************************
69  */
70 int upgrade_magic_section(char dfile[], int *fd, int stdfd, struct file_magic *file_magic,
71                           unsigned int *hdr_size, int *previous_format, int *endian_mismatch)
72 {
73         struct file_magic fm;
74         unsigned int fm_types_nr[] = {FILE_MAGIC_ULL_NR, FILE_MAGIC_UL_NR, FILE_MAGIC_U_NR};
75
76         /* Open and read sa magic structure */
77         sa_open_read_magic(fd, dfile, file_magic, TRUE, endian_mismatch, FALSE);
78
79         switch (file_magic->format_magic) {
80
81                 case FORMAT_MAGIC:
82                 case FORMAT_MAGIC_SWAPPED:
83                         *previous_format = FORMAT_MAGIC;
84                         return 0;
85                         break;
86
87                 case FORMAT_MAGIC_2171:
88                 case FORMAT_MAGIC_2171_SWAPPED:
89                         *previous_format = FORMAT_MAGIC_2171;
90                         break;
91
92                 case FORMAT_MAGIC_2173:
93                 case FORMAT_MAGIC_2173_SWAPPED:
94                         *previous_format = FORMAT_MAGIC_2173;
95                         break;
96
97                 default:
98                         fprintf(stderr, _("Cannot convert the format of this file\n"));
99                         return -1;
100                         break;
101         }
102
103         fprintf(stderr, "file_magic: ");
104         if (*previous_format == FORMAT_MAGIC_2171) {
105                 /*
106                  * We have read too many bytes: file_magic structure
107                  * was smaller with previous sysstat versions.
108                  * Go back 4 (unsigned int header_size) + 64 (char pad[64]) bytes.
109                  */
110                 if (lseek(*fd, -68, SEEK_CUR) < 0) {
111                         fprintf(stderr, "\nlseek: %s\n", strerror(errno));
112                         return -1;
113                 }
114         }
115
116         /* Set format magic number to that of current version */
117         file_magic->format_magic = (*endian_mismatch ? FORMAT_MAGIC_SWAPPED
118                                                      : FORMAT_MAGIC);
119
120         /* Save original header structure's size */
121         *hdr_size = file_magic->header_size;
122
123         /* Fill new structure members */
124         file_magic->header_size = FILE_HEADER_SIZE;
125         file_magic->hdr_types_nr[0] = FILE_HEADER_ULL_NR;
126         file_magic->hdr_types_nr[1] = FILE_HEADER_UL_NR;
127         file_magic->hdr_types_nr[2] = FILE_HEADER_U_NR;
128         memset(file_magic->pad, 0, sizeof(unsigned char) * FILE_MAGIC_PADDING);
129
130         /* Indicate that file has been upgraded */
131         enum_version_nr(&fm);
132         file_magic->upgraded = (fm.sysstat_patchlevel << 8) +
133                                fm.sysstat_sublevel + 1;
134
135         memcpy(&fm, file_magic, FILE_MAGIC_SIZE);
136         /* Restore endianness before writing */
137         if (*endian_mismatch) {
138                 /* Start swapping at field "header_size" position */
139                 swap_struct(fm_types_nr, &(fm.header_size), 0);
140         }
141
142         /* Write file's magic structure */
143         if (write(stdfd, &fm, FILE_MAGIC_SIZE) != FILE_MAGIC_SIZE) {
144                 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
145                 return -1;
146         }
147         fprintf(stderr, "OK\n");
148
149         return 0;
150 }
151
152 /*
153  ***************************************************************************
154  * Upgrade file_header structure (from 0x2171 format to current format).
155  *
156  * IN:
157  * @buffer      File's header structure (as read from file).
158  * @previous_format
159  *              Format magic number of file to convert.
160  * @endian_mismatch
161  *              TRUE if data read from file don't match current
162  *              machine's endianness.
163  *
164  * OUT:
165  * @file_hdr    File's header structure (up-to-date format).
166  * @arch_64     TRUE if file's data come from a 64-bit machine.
167  * @vol_act_nr  Number of volatile activity structures following a restart
168  *              record for 0x2173 file format.
169  ***************************************************************************
170  */
171 void upgrade_file_header(void *buffer, struct file_header *file_hdr, int previous_format,
172                          int endian_mismatch, int *arch_64, unsigned int *vol_act_nr)
173 {
174         struct file_header_2171 *f_hdr_2171 = (struct file_header_2171 *) buffer;
175         struct file_header_2173 *f_hdr_2173 = (struct file_header_2173 *) buffer;
176         unsigned int hdr_2171_types_nr[] = {FILE_HEADER_2171_ULL_NR, FILE_HEADER_2171_UL_NR, FILE_HEADER_2171_U_NR};
177         unsigned int hdr_2173_types_nr[] = {FILE_HEADER_2173_ULL_NR, FILE_HEADER_2173_UL_NR, FILE_HEADER_2173_U_NR};
178         int i;
179
180         memset(file_hdr, 0, FILE_HEADER_SIZE);
181         file_hdr->sa_hz = HZ;
182
183         if (previous_format == FORMAT_MAGIC_2171) {
184                 *arch_64 = (f_hdr_2171->sa_sizeof_long == SIZEOF_LONG_64BIT);
185
186                 /* Normalize endianness for f_hdr_2171 structure */
187                 if (endian_mismatch) {
188                         swap_struct(hdr_2171_types_nr, f_hdr_2171, *arch_64);
189                 }
190
191                 file_hdr->sa_ust_time = (unsigned long long) f_hdr_2171->sa_ust_time;
192                 /* sa_cpu_nr field will be updated later */
193                 file_hdr->sa_act_nr = f_hdr_2171->sa_act_nr;
194                 file_hdr->sa_year = (int) f_hdr_2171->sa_year;
195                 file_hdr->sa_day = f_hdr_2171->sa_day;
196                 file_hdr->sa_month = f_hdr_2171->sa_month;
197                 file_hdr->sa_sizeof_long = f_hdr_2171->sa_sizeof_long;
198                 strncpy(file_hdr->sa_sysname, f_hdr_2171->sa_sysname, UTSNAME_LEN);
199                 file_hdr->sa_sysname[UTSNAME_LEN - 1] = '\0';
200                 strncpy(file_hdr->sa_nodename, f_hdr_2171->sa_nodename, UTSNAME_LEN);
201                 file_hdr->sa_nodename[UTSNAME_LEN - 1] = '\0';
202                 strncpy(file_hdr->sa_release, f_hdr_2171->sa_release, UTSNAME_LEN);
203                 file_hdr->sa_release[UTSNAME_LEN - 1] = '\0';
204                 strncpy(file_hdr->sa_machine, f_hdr_2171->sa_machine, UTSNAME_LEN);
205                 file_hdr->sa_machine[UTSNAME_LEN - 1] = '\0';
206         }
207
208         else if (previous_format == FORMAT_MAGIC_2173) {
209                 *arch_64 = (f_hdr_2173->sa_sizeof_long == SIZEOF_LONG_64BIT);
210
211                 /* Normalize endianness for f_hdr_2171 structure */
212                 if (endian_mismatch) {
213                         swap_struct(hdr_2173_types_nr, f_hdr_2173, *arch_64);
214                 }
215
216                 file_hdr->sa_ust_time = (unsigned long long) f_hdr_2173->sa_ust_time;
217                 /* sa_cpu_nr field will be updated later */
218                 file_hdr->sa_act_nr = f_hdr_2173->sa_act_nr;
219                 file_hdr->sa_year = (int) f_hdr_2173->sa_year;
220                 file_hdr->sa_day = f_hdr_2173->sa_day;
221                 file_hdr->sa_month = f_hdr_2173->sa_month;
222                 file_hdr->sa_sizeof_long = f_hdr_2173->sa_sizeof_long;
223                 strncpy(file_hdr->sa_sysname, f_hdr_2173->sa_sysname, UTSNAME_LEN);
224                 file_hdr->sa_sysname[UTSNAME_LEN - 1] = '\0';
225                 strncpy(file_hdr->sa_nodename, f_hdr_2173->sa_nodename, UTSNAME_LEN);
226                 file_hdr->sa_nodename[UTSNAME_LEN - 1] = '\0';
227                 strncpy(file_hdr->sa_release, f_hdr_2173->sa_release, UTSNAME_LEN);
228                 file_hdr->sa_release[UTSNAME_LEN - 1] = '\0';
229                 strncpy(file_hdr->sa_machine, f_hdr_2173->sa_machine, UTSNAME_LEN);
230                 file_hdr->sa_machine[UTSNAME_LEN - 1] = '\0';
231
232                 *vol_act_nr = f_hdr_2173->sa_vol_act_nr;
233         }
234
235         for (i = 0; i < 3; i++) {
236                 file_hdr->act_types_nr[i] = act_types_nr[i];
237                 file_hdr->rec_types_nr[i] = rec_types_nr[i];
238         }
239         file_hdr->act_size = FILE_ACTIVITY_SIZE;
240         file_hdr->rec_size = RECORD_HEADER_SIZE;
241 }
242
243 /*
244  ***************************************************************************
245  * Read and upgrade file's header section.
246  *
247  * IN:
248  * @dfile       System activity data file name.
249  * @fd          File descriptor for sa datafile to convert.
250  * @stdfd       File descriptor for STDOUT.
251  * @act         Array of activities.
252  * @file_magic  File's magic structure.
253  * @hdr_size    Size of header structure read from file.
254  * @previous_format
255  *              Format magic number of file to convert.
256  * @endian_mismatch
257  *              TRUE if data read from file don't match current machine's
258  *              endianness.
259  *
260  * OUT:
261  * @file_hdr    File's header structure (up-to-date format).
262  * @arch_64     TRUE if file's data come from a 64-bit machine.
263  * @vol_act_nr  Number of volatile activity structures following a restart
264  *              record for 0x2173 file format.
265  * @ofile_actlst
266  *              Activity list in file.
267  *
268  * RETURNS:
269  * -1 on error, 0 otherwise.
270 ***************************************************************************
271  */
272 int upgrade_header_section(char dfile[], int fd, int stdfd, struct activity *act[],
273                            struct file_magic *file_magic, struct file_header *file_hdr,
274                            unsigned int hdr_size, int previous_format, int *arch_64,
275                            int endian_mismatch, unsigned int *vol_act_nr,
276                            struct old_file_activity **ofile_actlst)
277 {
278         struct old_file_activity *ofal;
279         struct file_header fh;
280         int i, n, p;
281         unsigned int a_cpu = FALSE;
282         void *buffer = NULL;
283
284         fprintf(stderr, "file_header: ");
285
286         /* Read file header structure */
287         n = (previous_format == FORMAT_MAGIC_2171 ? FILE_HEADER_SIZE_2171
288                                                   : hdr_size);
289         SREALLOC(buffer, char, n);
290         sa_fread(fd, buffer, n, HARD_SIZE);
291
292         /* Upgrade file_header structure */
293         upgrade_file_header(buffer, file_hdr, previous_format,
294                             endian_mismatch, arch_64, vol_act_nr);
295
296         free(buffer);
297
298         /* Sanity check */
299         if (file_hdr->sa_act_nr > MAX_NR_ACT)
300                 goto invalid_header;
301
302         /* Read file activity list */
303         SREALLOC(*ofile_actlst, struct old_file_activity, OLD_FILE_ACTIVITY_SIZE * file_hdr->sa_act_nr);
304         ofal = *ofile_actlst;
305
306         for (i = 0; i < file_hdr->sa_act_nr; i++, ofal++) {
307
308                 sa_fread(fd, ofal, OLD_FILE_ACTIVITY_SIZE, HARD_SIZE);
309
310                 /* Normalize endianness for file_activity structures */
311                 if (endian_mismatch) {
312                         swap_struct(oact_types_nr, ofal, *arch_64);
313                 }
314
315                 if ((ofal->nr < 1) || (ofal->nr2 < 1))
316                         /*
317                          * Every activity, known or unknown,
318                          * should have at least one item and sub-item.
319                          */
320                         goto invalid_header;
321
322                 if ((p = get_activity_position(act, ofal->id, RESUME_IF_NOT_FOUND)) >= 0) {
323                         /* This is a known activity, maybe with an unknown format */
324
325                         if ((ofal->id == A_CPU) && !a_cpu) {
326                                 /*
327                                  * We have found the CPU activity: Set sa_cpu_nr field
328                                  * in file_header structure that should contains the
329                                  * number of CPU when the file was created.
330                                  */
331                                 file_hdr->sa_cpu_nr = ofal->nr;
332                                 a_cpu = TRUE;
333                         }
334
335                         /* Size of an activity cannot be zero */
336                         if (!ofal->size)
337                                 goto invalid_header;
338
339                         /* Size of activity in file is larger than up-to-date activity size */
340                         if (ofal->size > act[p]->msize) {
341                                 act[p]->msize = ofal->size;
342                         }
343
344                         /*
345                          * When upgrading a file:
346                          * ofal->size   : Size of an item for current activity, as
347                          *                read from the file.
348                          * act[p]->msize: Size of the buffer in memory where an item
349                          *                for current activity, as read from the file,
350                          *                will be saved. We have:
351                          *                act[p]->msize >= {ofal->size ; act[p]->fsize}
352                          * act[p]->fsize: Size of an item for current activity with
353                          *                up-to-date format.
354                          */
355                         act[p]->nr_ini = ofal->nr;
356                         act[p]->nr2    = ofal->nr2;
357                         /*
358                          * Don't set act[p]->fsize! Should retain the size of an item
359                          * for up-to-date format!
360                          */
361                 }
362         }
363
364         if (!a_cpu) {
365                 /*
366                  * CPU activity should always be in file for versions older than 11.7.1
367                  * and have a known format (expected magical number).
368                  */
369                 fprintf(stderr, _("\nCPU activity not found in file. Aborting...\n"));
370                 return -1;
371         }
372
373         memcpy(&fh, file_hdr, FILE_HEADER_SIZE);
374         /* Restore endianness before writing */
375         if (endian_mismatch) {
376                 /* Start swapping at field "header_size" position */
377                 swap_struct(hdr_types_nr, &fh, *arch_64);
378         }
379
380         /* Write file_header structure */
381         if ((n = write(stdfd, &fh, FILE_HEADER_SIZE)) != FILE_HEADER_SIZE) {
382                 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
383                 return -1;
384         }
385
386         fprintf(stderr, "OK\n");
387
388         return 0;
389
390 invalid_header:
391
392         fprintf(stderr, _("\nInvalid data found. Aborting...\n"));
393
394         return -1;
395
396 }
397
398 /*
399  ***************************************************************************
400  * Convert a long integer value to a long long integer value.
401  * The long integer value may be 32 or 64-bit wide and come from a 32 or
402  * 64-bit architecture.
403  *
404  * Note: Consider the value 0x01020304 read on a 32-bit machine.
405  * Big-endian, saved as:   01 02 03 04
406  * Lille-endian, saved as: 04 03 02 01
407  * The value should be saved as an 64-bit value and endianness should be
408  * preserved:
409  * Big-endian:    00 00 00 00 01 02 03 04
410  * Little-endian: 04 03 02 01 00 00 00 00
411  *
412  * IN:
413  * @buffer      Address of value to convert.
414  * @endian_mismatch
415  *              TRUE if data read from file don't match current machine's
416  *              endianness.
417  * @arch_64     TRUE if file's data come from a 64-bit machine.
418  ***************************************************************************
419  */
420 unsigned long long moveto_long_long(void *buffer, int endian_mismatch, int arch_64)
421 {
422         unsigned int *u_int;
423         unsigned long long *ull_int, ull_i;
424
425         if (arch_64) {
426                 ull_int = (unsigned long long *) buffer;
427                 return *ull_int;
428         }
429
430         u_int = (unsigned int *) buffer;
431         if (endian_mismatch) {
432                 ull_i = (unsigned long long) *u_int;
433                 return (ull_i >> 32) | (ull_i << 32);
434         }
435         else {
436                 return (unsigned long long) *u_int;
437         }
438 }
439
440 /*
441  ***************************************************************************
442  * Upgrade stats_cpu structure (from ACTIVITY_MAGIC_BASE format to
443  * ACTIVITY_MAGIC_BASE + 1 format).
444  *
445  * IN:
446  * @act         Array of activities.
447  * @p           Position of activity in array.
448  * @st_size     Size of the structure read from file.
449  ***************************************************************************
450  */
451 void upgrade_stats_cpu(struct activity *act[], int p, int st_size)
452 {
453         int i;
454         struct stats_cpu *scc;
455         struct stats_cpu_8a *scp;
456
457         for (i = 0; i < act[p]->nr_ini; i++) {
458                 /*
459                  * For previous structure's format: Use msize (which has possibly been set to
460                  * the size of the structure read from disk if it's greater than that of
461                  * current format: See upgrade_header_section()).
462                  * For current structure format: Use fsize. This is the normal size of
463                  * structure's up-to-date format that will be written to file.
464                  */
465                 scp = (struct stats_cpu_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
466                 scc = (struct stats_cpu *)    ((char *) act[p]->buf[1] + i * act[p]->fsize);
467
468                 scc->cpu_user = scp->cpu_user;
469                 scc->cpu_nice = scp->cpu_nice;
470                 scc->cpu_sys = scp->cpu_sys;
471                 scc->cpu_idle = scp->cpu_idle;
472                 scc->cpu_iowait = scp->cpu_iowait;
473                 scc->cpu_steal = scp->cpu_steal;
474                 scc->cpu_hardirq = scp->cpu_hardirq;
475                 scc->cpu_softirq = scp->cpu_softirq;
476                 scc->cpu_guest = scp->cpu_guest;
477
478                 if (st_size >= STATS_CPU_8A_SIZE) {
479                         /* guest_nice field has been added without a structure format change */
480                         scc->cpu_guest_nice = scp->cpu_guest_nice;
481                 }
482         }
483 }
484
485 /*
486  ***************************************************************************
487  * Upgrade stats_pcsw structure (from ACTIVITY_MAGIC_BASE format to
488  * ACTIVITY_MAGIC_BASE + 1 format).
489  *
490  * IN:
491  * @act         Array of activities.
492  * @p           Position of activity in array.
493  ***************************************************************************
494  */
495 void upgrade_stats_pcsw(struct activity *act[], int p)
496 {
497         struct stats_pcsw *spc = (struct stats_pcsw *) act[p]->buf[1];
498         struct stats_pcsw_8a *spp = (struct stats_pcsw_8a *) act[p]->buf[0];
499
500         spc->context_switch = spp->context_switch;
501         /*
502          * Copy a long into a long. Take into account that file may have been
503          * created on a 64-bit machine and we may be converting on a 32-bit machine.
504          */
505         memcpy(&spc->processes, &spp->processes, 8);
506 }
507
508 /*
509  ***************************************************************************
510  * Upgrade stats_irq structure (from ACTIVITY_MAGIC_BASE format to
511  * ACTIVITY_MAGIC_BASE + 1 format).
512  *
513  * IN:
514  * @act         Array of activities.
515  * @p           Position of activity in array.
516  ***************************************************************************
517  */
518 void upgrade_stats_irq(struct activity *act[], int p)
519 {
520         int i;
521         struct stats_irq *sic;
522         struct stats_irq_8a *sip;
523
524         for (i = 0; i < act[p]->nr_ini; i++) {
525                 sip = (struct stats_irq_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
526                 sic = (struct stats_irq *)    ((char *) act[p]->buf[1] + i * act[p]->fsize);
527
528                 sic->irq_nr = sip->irq_nr;
529         }
530 }
531
532 /*
533  ***************************************************************************
534  * Upgrade stats_io structure (from ACTIVITY_MAGIC_BASE format to
535  * ACTIVITY_MAGIC_BASE + 1 format).
536  *
537  * IN:
538  * @act         Array of activities.
539  * @p           Position of activity in array.
540  * @endian_mismatch
541  *              TRUE if data read from file don't match current machine's
542  *              endianness.
543  ***************************************************************************
544  */
545 void upgrade_stats_io(struct activity *act[], int p, int endian_mismatch)
546 {
547         struct stats_io *sic = (struct stats_io *) act[p]->buf[1];
548         struct stats_io_8a *sip = (struct stats_io_8a *) act[p]->buf[0];
549
550         sic->dk_drive = moveto_long_long(&sip->dk_drive, endian_mismatch, FALSE);
551         sic->dk_drive_rio = moveto_long_long(&sip->dk_drive_rio, endian_mismatch, FALSE);
552         sic->dk_drive_wio = moveto_long_long(&sip->dk_drive_wio, endian_mismatch, FALSE);
553         sic->dk_drive_rblk = moveto_long_long(&sip->dk_drive_rblk, endian_mismatch, FALSE);
554         sic->dk_drive_wblk = moveto_long_long(&sip->dk_drive_wblk, endian_mismatch, FALSE);
555 }
556
557 /*
558  ***************************************************************************
559  * Upgrade stats_memory structure (from ACTIVITY_MAGIC_BASE format to
560  * ACTIVITY_MAGIC_BASE + 1 format).
561  *
562  * IN:
563  * @act         Array of activities.
564  * @p           Position of activity in array.
565  * @st_size     Size of the structure read from file.
566  * @endian_mismatch
567  *              TRUE if data read from file don't match current machine's
568  *              endianness.
569  * @arch_64     TRUE if file's data come from a 64-bit machine.
570  ***************************************************************************
571  */
572 void upgrade_stats_memory(struct activity *act[], int p, int st_size,
573                           int endian_mismatch, int arch_64)
574 {
575         struct stats_memory *smc = (struct stats_memory *) act[p]->buf[1];
576         struct stats_memory_8a *smp = (struct stats_memory_8a *) act[p]->buf[0];
577
578         smc->frmkb = moveto_long_long(&smp->frmkb, endian_mismatch, arch_64);
579         smc->bufkb = moveto_long_long(&smp->bufkb, endian_mismatch, arch_64);
580         smc->camkb = moveto_long_long(&smp->camkb, endian_mismatch, arch_64);
581         smc->tlmkb = moveto_long_long(&smp->tlmkb, endian_mismatch, arch_64);
582         smc->frskb = moveto_long_long(&smp->frskb, endian_mismatch, arch_64);
583         smc->tlskb = moveto_long_long(&smp->tlskb, endian_mismatch, arch_64);
584         smc->caskb = moveto_long_long(&smp->caskb, endian_mismatch, arch_64);
585         smc->comkb = moveto_long_long(&smp->comkb, endian_mismatch, arch_64);
586         smc->activekb = moveto_long_long(&smp->activekb, endian_mismatch, arch_64);
587         smc->inactkb = moveto_long_long(&smp->inactkb, endian_mismatch, arch_64);
588
589         /* Some fields have been added without a structure format change */
590         if (st_size >= STATS_MEMORY_8A_1_SIZE) {
591                                 smc->dirtykb = moveto_long_long(&smp->dirtykb, endian_mismatch, arch_64);
592         }
593         if (st_size >= STATS_MEMORY_8A_2_SIZE) {
594                 smc->anonpgkb = moveto_long_long(&smp->anonpgkb, endian_mismatch, arch_64);
595                 smc->slabkb = moveto_long_long(&smp->slabkb, endian_mismatch, arch_64);
596                 smc->kstackkb = moveto_long_long(&smp->kstackkb, endian_mismatch, arch_64);
597                 smc->pgtblkb = moveto_long_long(&smp->pgtblkb, endian_mismatch, arch_64);
598                 smc->vmusedkb = moveto_long_long(&smp->vmusedkb, endian_mismatch, arch_64);
599         }
600         if (st_size >= STATS_MEMORY_8A_SIZE) {
601                 smc->availablekb = moveto_long_long(&(smp->availablekb), endian_mismatch, arch_64);;
602         }
603 }
604
605 /*
606  ***************************************************************************
607  * Upgrade stats_ktables structure (from ACTIVITY_MAGIC_BASE format to
608  * ACTIVITY_MAGIC_BASE + 1 format).
609  *
610  * IN:
611  * @act         Array of activities.
612  * @p           Position of activity in array.
613  * @endian_mismatch
614  *              TRUE if data read from file don't match current machine's
615  *              endianness.
616  ***************************************************************************
617  */
618 void upgrade_stats_ktables(struct activity *act[], int p, int endian_mismatch)
619 {
620         struct stats_ktables *skc = (struct stats_ktables *) act[p]->buf[1];
621         struct stats_ktables_8a *skp = (struct stats_ktables_8a *) act[p]->buf[0];
622
623         skc->file_used = moveto_long_long(&skp->file_used, endian_mismatch, FALSE);
624         skc->inode_used = moveto_long_long(&skp->inode_used, endian_mismatch, FALSE);
625         skc->dentry_stat = moveto_long_long(&skp->dentry_stat, endian_mismatch, FALSE);
626         skc->pty_nr = moveto_long_long(&skp->pty_nr, endian_mismatch, FALSE);
627 }
628
629 /*
630  ***************************************************************************
631  * Upgrade stats_queue structure (from ACTIVITY_MAGIC_BASE or
632  * ACTIVITY_MAGIC_BASE + 1 format to ACTIVITY_MAGIC_BASE + 2 format).
633  *
634  * IN:
635  * @act         Array of activities.
636  * @p           Position of activity in array.
637  * @magic       Structure format magic value.
638  * @endian_mismatch
639  *              TRUE if data read from file don't match current machine's
640  *              endianness.
641  * @arch_64     TRUE if file's data come from a 64-bit machine.
642  ***************************************************************************
643  */
644 void upgrade_stats_queue(struct activity *act[], int p, unsigned int magic,
645                          int endian_mismatch, int arch_64)
646 {
647         struct stats_queue *sqc = (struct stats_queue *) act[p]->buf[1];
648
649         if (magic == ACTIVITY_MAGIC_BASE) {
650                 struct stats_queue_8a *sqp = (struct stats_queue_8a *) act[p]->buf[0];
651
652                 sqc->nr_running = moveto_long_long(&sqp->nr_running, endian_mismatch, arch_64);
653                 sqc->procs_blocked = 0ULL;      /* New field */
654                 sqc->nr_threads = moveto_long_long(&sqp->nr_threads, endian_mismatch, FALSE);
655                 sqc->load_avg_1 = sqp->load_avg_1;
656                 sqc->load_avg_5 = sqp->load_avg_5;
657                 sqc->load_avg_15 = sqp->load_avg_15;
658         }
659         else {
660                 struct stats_queue_8b *sqp = (struct stats_queue_8b *) act[p]->buf[0];
661
662                 sqc->nr_running = moveto_long_long(&sqp->nr_running, endian_mismatch, arch_64);
663                 sqc->procs_blocked = moveto_long_long(&sqp->procs_blocked, endian_mismatch, arch_64);
664                 sqc->nr_threads = moveto_long_long(&sqp->nr_threads, endian_mismatch, FALSE);
665                 sqc->load_avg_1 = sqp->load_avg_1;
666                 sqc->load_avg_5 = sqp->load_avg_5;
667                 sqc->load_avg_15 = sqp->load_avg_15;
668         }
669 }
670
671 /*
672  ***************************************************************************
673  * Upgrade stats_serial structure (from ACTIVITY_MAGIC_BASE format to
674  * ACTIVITY_MAGIC_BASE + 1 format).
675  *
676  * IN:
677  * @act         Array of activities.
678  * @p           Position of activity in array.
679  ***************************************************************************
680  */
681 void upgrade_stats_serial(struct activity *act[], int p, int endian_mismatch)
682 {
683         int i;
684         unsigned int line;
685         struct stats_serial *ssc;
686
687         for (i = 0; i < act[p]->nr_ini; i++) {
688                 ssc = (struct stats_serial *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
689
690                 /* Line number now starts at 0 instead of 1 */
691                 line = (endian_mismatch ? __builtin_bswap32(ssc->line) : ssc->line);
692                 if (!line)
693                         break;
694                 line--;
695                 ssc->line = (endian_mismatch ? __builtin_bswap32(line) : line);
696         }
697
698         /*
699          * Updating @nr_ini is part of the upgrade process so that
700          * only the required number of structures gets written to disk.
701          * Don't do it later as a line number of 0 no longer indicates the
702          * end of the list.
703          */
704         act[p]->nr_ini = i;
705 }
706
707 /*
708  ***************************************************************************
709  * Upgrade stats_disk structure (from ACTIVITY_MAGIC_BASE format to
710  * ACTIVITY_MAGIC_BASE + 1 format).
711  *
712  * IN:
713  * @act         Array of activities.
714  * @p           Position of activity in array.
715  * @magic       Structure format magic value.
716  * @endian_mismatch
717  *              TRUE if data read from file don't match current machine's
718  *              endianness.
719  * @arch_64     TRUE if file's data come from a 64-bit machine.
720  ***************************************************************************
721  */
722 void upgrade_stats_disk(struct activity *act[], int p, unsigned int magic,
723                         int endian_mismatch, int arch_64)
724 {
725         int i;
726         struct stats_disk *sdc;
727
728         if (magic == ACTIVITY_MAGIC_BASE) {
729                 struct stats_disk_8a *sdp;
730
731                 for (i = 0; i < act[p]->nr_ini; i++) {
732                         sdp = (struct stats_disk_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
733                         sdc = (struct stats_disk *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
734
735                         sdc->nr_ios = moveto_long_long(&sdp->nr_ios, endian_mismatch, arch_64);
736                         sdc->rd_sect = (unsigned long) sdp->rd_sect;
737                         sdc->wr_sect = (unsigned long) sdp->wr_sect;
738                         sdc->rd_ticks = (unsigned int) sdp->rd_ticks;
739                         sdc->wr_ticks = (unsigned int) sdp->wr_ticks;
740                         sdc->tot_ticks = (unsigned int) sdp->tot_ticks;
741                         sdc->rq_ticks = (unsigned int) sdp->rq_ticks;
742                         sdc->major = sdp->major;
743                         sdc->minor = sdp->minor;
744                 }
745         }
746         else {
747                 struct stats_disk_8b *sdp;
748
749                 for (i = 0; i < act[p]->nr_ini; i++) {
750                         sdp = (struct stats_disk_8b *) ((char *) act[p]->buf[0] + i * act[p]->msize);
751                         sdc = (struct stats_disk *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
752
753                         sdc->nr_ios = sdp->nr_ios;
754                         memcpy(&sdc->rd_sect, &sdp->rd_sect, 8);
755                         memcpy(&sdc->wr_sect, &sdp->wr_sect, 8);
756                         sdc->rd_ticks = sdp->rd_ticks;
757                         sdc->wr_ticks = sdp->wr_ticks;
758                         sdc->tot_ticks = sdp->tot_ticks;
759                         sdc->rq_ticks = sdp->rq_ticks;
760                         sdc->major = sdp->major;
761                         sdc->minor = sdp->minor;
762                 }
763         }
764 }
765
766 /*
767  ***************************************************************************
768  * Upgrade stats_net_dev structure (from ACTIVITY_MAGIC_BASE or
769  * ACTIVITY_MAGIC_BASE + 1 format to ACTIVITY_MAGIC_BASE + 3 format).
770  *
771  * IN:
772  * @act         Array of activities.
773  * @p           Position of activity in array.
774  * @magic       Structure format magic value.
775  * @endian_mismatch
776  *              TRUE if data read from file don't match current machine's
777  *              endianness.
778  * @arch_64     TRUE if file's data come from a 64-bit machine.
779  ***************************************************************************
780  */
781 void upgrade_stats_net_dev(struct activity *act[], int p, unsigned int magic,
782                            int endian_mismatch, int arch_64)
783 {
784         int i;
785         struct stats_net_dev *sndc;
786
787         if (magic == ACTIVITY_MAGIC_BASE) {
788                 struct stats_net_dev_8a *sndp;
789
790                 for (i = 0; i < act[p]->nr_ini; i++) {
791                         sndp = (struct stats_net_dev_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
792                         sndc = (struct stats_net_dev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
793
794                         sndc->rx_packets = moveto_long_long(&sndp->rx_packets, endian_mismatch, arch_64);
795                         sndc->tx_packets = moveto_long_long(&sndp->tx_packets, endian_mismatch, arch_64);
796                         sndc->rx_bytes = moveto_long_long(&sndp->rx_bytes, endian_mismatch, arch_64);
797                         sndc->tx_bytes = moveto_long_long(&sndp->tx_bytes, endian_mismatch, arch_64);
798                         sndc->rx_compressed = moveto_long_long(&sndp->rx_compressed, endian_mismatch, arch_64);
799                         sndc->tx_compressed = moveto_long_long(&sndp->tx_compressed, endian_mismatch, arch_64);
800                         sndc->multicast = moveto_long_long(&sndp->multicast, endian_mismatch, arch_64);
801                         sndc->speed = 0; /* New field */
802                         strncpy(sndc->interface, sndp->interface, MAX_IFACE_LEN);
803                         sndc->interface[MAX_IFACE_LEN - 1] = '\0';
804                         sndc->duplex = '\0'; /* New field */
805                 }
806         }
807         else if (magic == ACTIVITY_MAGIC_BASE) {
808                 struct stats_net_dev_8b *sndp;
809
810                 for (i = 0; i < act[p]->nr_ini; i++) {
811                         sndp = (struct stats_net_dev_8b *) ((char *) act[p]->buf[0] + i * act[p]->msize);
812                         sndc = (struct stats_net_dev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
813
814                         sndc->rx_packets = sndp->rx_packets;
815                         sndc->tx_packets = sndp->tx_packets;
816                         sndc->rx_bytes = sndp->rx_bytes;
817                         sndc->tx_bytes = sndp->tx_bytes;
818                         sndc->rx_compressed = sndp->rx_compressed;
819                         sndc->tx_compressed = sndp->tx_compressed;
820                         sndc->multicast = sndp->multicast;
821                         sndc->speed = 0; /* New field */
822                         strncpy(sndc->interface, sndp->interface, MAX_IFACE_LEN);
823                         sndc->interface[MAX_IFACE_LEN - 1] = '\0';
824                         sndc->duplex = '\0'; /* New field */
825                 }
826         }
827         else {
828                 struct stats_net_dev_8c *sndp;
829
830                 for (i = 0; i < act[p]->nr_ini; i++) {
831                         sndp = (struct stats_net_dev_8c *) ((char *) act[p]->buf[0] + i * act[p]->msize);
832                         sndc = (struct stats_net_dev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
833
834                         sndc->rx_packets = sndp->rx_packets;
835                         sndc->tx_packets = sndp->tx_packets;
836                         sndc->rx_bytes = sndp->rx_bytes;
837                         sndc->tx_bytes = sndp->tx_bytes;
838                         sndc->rx_compressed = sndp->rx_compressed;
839                         sndc->tx_compressed = sndp->tx_compressed;
840                         sndc->multicast = sndp->multicast;
841                         sndc->speed = sndp->speed;
842                         strncpy(sndc->interface, sndp->interface, MAX_IFACE_LEN);
843                         sndc->interface[MAX_IFACE_LEN - 1] = '\0';
844                         sndc->duplex = sndp->duplex;
845                 }
846         }
847 }
848
849 /*
850  ***************************************************************************
851  * Upgrade stats_net_edev structure (from ACTIVITY_MAGIC_BASE format to
852  * ACTIVITY_MAGIC_BASE + 1 format).
853  *
854  * IN:
855  * @act         Array of activities.
856  * @p           Position of activity in array.
857  * @magic       Structure format magic value.
858  * @endian_mismatch
859  *              TRUE if data read from file don't match current machine's
860  *              endianness.
861  * @arch_64     TRUE if file's data come from a 64-bit machine.
862  ***************************************************************************
863  */
864 void upgrade_stats_net_edev(struct activity *act[], int p, unsigned int magic,
865                             int endian_mismatch, int arch_64)
866 {
867         int i;
868         struct stats_net_edev *snedc;
869
870         if (magic == ACTIVITY_MAGIC_BASE) {
871                 struct stats_net_edev_8a *snedp;
872
873                 for (i = 0; i < act[p]->nr_ini; i++) {
874                         snedp = (struct stats_net_edev_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
875                         snedc = (struct stats_net_edev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
876
877                         snedc->collisions = moveto_long_long(&snedp->collisions, endian_mismatch, arch_64);
878                         snedc->rx_errors = moveto_long_long(&snedp->rx_errors, endian_mismatch, arch_64);
879                         snedc->tx_errors = moveto_long_long(&snedp->tx_errors, endian_mismatch, arch_64);
880                         snedc->rx_dropped = moveto_long_long(&snedp->rx_dropped, endian_mismatch, arch_64);
881                         snedc->tx_dropped = moveto_long_long(&snedp->tx_dropped, endian_mismatch, arch_64);
882                         snedc->rx_fifo_errors = moveto_long_long(&snedp->rx_fifo_errors, endian_mismatch, arch_64);
883                         snedc->tx_fifo_errors = moveto_long_long(&snedp->tx_fifo_errors, endian_mismatch, arch_64);
884                         snedc->rx_frame_errors = moveto_long_long(&snedp->rx_frame_errors, endian_mismatch, arch_64);
885                         snedc->tx_carrier_errors = moveto_long_long(&snedp->tx_carrier_errors, endian_mismatch, arch_64);
886                         strncpy(snedc->interface, snedp->interface, MAX_IFACE_LEN);
887                         snedc->interface[MAX_IFACE_LEN - 1] = '\0';
888                 }
889         }
890         else {
891                 struct stats_net_edev_8b *snedp;
892
893                 for (i = 0; i < act[p]->nr_ini; i++) {
894                         snedp = (struct stats_net_edev_8b *) ((char *) act[p]->buf[0] + i * act[p]->msize);
895                         snedc = (struct stats_net_edev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
896
897                         snedc->collisions = snedp->collisions;
898                         snedc->rx_errors = snedp->rx_errors;
899                         snedc->tx_errors = snedp->tx_errors;
900                         snedc->rx_dropped = snedp->rx_dropped;
901                         snedc->tx_dropped = snedp->tx_dropped;
902                         snedc->rx_fifo_errors = snedp->rx_fifo_errors;
903                         snedc->tx_fifo_errors = snedp->tx_fifo_errors;
904                         snedc->rx_frame_errors = snedp->rx_frame_errors;
905                         snedc->tx_carrier_errors = snedp->tx_carrier_errors;
906                         strncpy(snedc->interface, snedp->interface, MAX_IFACE_LEN);
907                         snedc->interface[MAX_IFACE_LEN - 1] = '\0';
908                 }
909         }
910 }
911
912 /*
913  ***************************************************************************
914  * Upgrade stats_net_ip structure (from ACTIVITY_MAGIC_BASE format to
915  * ACTIVITY_MAGIC_BASE + 1 format).
916  *
917  * IN:
918  * @act         Array of activities.
919  * @p           Position of activity in array.
920  * @magic       Structure format magic value.
921  * @endian_mismatch
922  *              TRUE if data read from file don't match current machine's
923  *              endianness.
924  * @arch_64     TRUE if file's data come from a 64-bit machine.
925  ***************************************************************************
926  */
927 void upgrade_stats_net_ip(struct activity *act[], int p, unsigned int magic,
928                           int endian_mismatch, int arch_64)
929 {
930         struct stats_net_ip *snic = (struct stats_net_ip *) act[p]->buf[1];
931
932         if (magic == ACTIVITY_MAGIC_BASE) {
933                 struct stats_net_ip_8a *snip = (struct stats_net_ip_8a *) act[p]->buf[0];
934
935                 snic->InReceives = moveto_long_long(&snip->InReceives, endian_mismatch, arch_64);
936                 snic->ForwDatagrams = moveto_long_long(&snip->ForwDatagrams, endian_mismatch, arch_64);
937                 snic->InDelivers = moveto_long_long(&snip->InDelivers, endian_mismatch, arch_64);
938                 snic->OutRequests = moveto_long_long(&snip->OutRequests, endian_mismatch, arch_64);
939                 snic->ReasmReqds = moveto_long_long(&snip->ReasmReqds, endian_mismatch, arch_64);
940                 snic->ReasmOKs = moveto_long_long(&snip->ReasmOKs, endian_mismatch, arch_64);
941                 snic->FragOKs = moveto_long_long(&snip->FragOKs, endian_mismatch, arch_64);
942                 snic->FragCreates = moveto_long_long(&snip->FragCreates, endian_mismatch, arch_64);
943         }
944         else {
945                 struct stats_net_ip_8b *snip = (struct stats_net_ip_8b *) act[p]->buf[0];
946
947                 snic->InReceives = snip->InReceives;
948                 snic->ForwDatagrams = snip->ForwDatagrams;
949                 snic->InDelivers = snip->InDelivers;
950                 snic->OutRequests = snip->OutRequests;
951                 snic->ReasmReqds = snip->ReasmReqds;
952                 snic->ReasmOKs = snip->ReasmOKs;
953                 snic->FragOKs = snip->FragOKs;
954                 snic->FragCreates = snip->FragCreates;
955         }
956 }
957
958 /*
959  ***************************************************************************
960  * Upgrade stats_net_eip structure (from ACTIVITY_MAGIC_BASE format to
961  * ACTIVITY_MAGIC_BASE + 1 format).
962  *
963  * IN:
964  * @act         Array of activities.
965  * @p           Position of activity in array.
966  * @magic       Structure format magic value.
967  * @endian_mismatch
968  *              TRUE if data read from file don't match current machine's
969  *              endianness.
970  * @arch_64     TRUE if file's data come from a 64-bit machine.
971  ***************************************************************************
972  */
973 void upgrade_stats_net_eip(struct activity *act[], int p, unsigned int magic,
974                            int endian_mismatch, int arch_64)
975 {
976         struct stats_net_eip *sneic = (struct stats_net_eip *) act[p]->buf[1];
977
978         if (magic == ACTIVITY_MAGIC_BASE) {
979                 struct stats_net_eip_8a *sneip = (struct stats_net_eip_8a *) act[p]->buf[0];
980
981                 sneic->InHdrErrors = moveto_long_long(&sneip->InHdrErrors, endian_mismatch, arch_64);
982                 sneic->InAddrErrors = moveto_long_long(&sneip->InAddrErrors, endian_mismatch, arch_64);
983                 sneic->InUnknownProtos = moveto_long_long(&sneip->InUnknownProtos, endian_mismatch, arch_64);
984                 sneic->InDiscards = moveto_long_long(&sneip->InDiscards, endian_mismatch, arch_64);
985                 sneic->OutDiscards = moveto_long_long(&sneip->OutDiscards, endian_mismatch, arch_64);
986                 sneic->OutNoRoutes = moveto_long_long(&sneip->OutNoRoutes, endian_mismatch, arch_64);
987                 sneic->ReasmFails = moveto_long_long(&sneip->ReasmFails, endian_mismatch, arch_64);
988                 sneic->FragFails = moveto_long_long(&sneip->FragFails, endian_mismatch, arch_64);
989         }
990         else {
991                 struct stats_net_eip_8b *sneip = (struct stats_net_eip_8b *) act[p]->buf[0];
992
993                 sneic->InHdrErrors = sneip->InHdrErrors;
994                 sneic->InAddrErrors = sneip->InAddrErrors;
995                 sneic->InUnknownProtos = sneip->InUnknownProtos;
996                 sneic->InDiscards = sneip->InDiscards;
997                 sneic->OutDiscards = sneip->OutDiscards;
998                 sneic->OutNoRoutes = sneip->OutNoRoutes;
999                 sneic->ReasmFails = sneip->ReasmFails;
1000                 sneic->FragFails = sneip->FragFails;
1001         }
1002 }
1003
1004 /*
1005  ***************************************************************************
1006  * Upgrade stats_net_ip6 structure (from ACTIVITY_MAGIC_BASE format to
1007  * ACTIVITY_MAGIC_BASE + 1 format).
1008  *
1009  * IN:
1010  * @act         Array of activities.
1011  * @p           Position of activity in array.
1012  * @magic       Structure format magic value.
1013  * @endian_mismatch
1014  *              TRUE if data read from file don't match current machine's
1015  *              endianness.
1016  * @arch_64     TRUE if file's data come from a 64-bit machine.
1017  ***************************************************************************
1018  */
1019 void upgrade_stats_net_ip6(struct activity *act[], int p, unsigned int magic,
1020                            int endian_mismatch, int arch_64)
1021 {
1022         struct stats_net_ip6 *snic6 = (struct stats_net_ip6 *) act[p]->buf[1];
1023
1024         if (magic == ACTIVITY_MAGIC_BASE) {
1025                 struct stats_net_ip6_8a *snip6 = (struct stats_net_ip6_8a *) act[p]->buf[0];
1026
1027                 snic6->InReceives6 = moveto_long_long(&snip6->InReceives6, endian_mismatch, arch_64);
1028                 snic6->OutForwDatagrams6 = moveto_long_long(&snip6->OutForwDatagrams6, endian_mismatch, arch_64);
1029                 snic6->InDelivers6 = moveto_long_long(&snip6->InDelivers6, endian_mismatch, arch_64);
1030                 snic6->OutRequests6 = moveto_long_long(&snip6->OutRequests6, endian_mismatch, arch_64);
1031                 snic6->ReasmReqds6 = moveto_long_long(&snip6->ReasmReqds6, endian_mismatch, arch_64);
1032                 snic6->ReasmOKs6 = moveto_long_long(&snip6->ReasmOKs6, endian_mismatch, arch_64);
1033                 snic6->InMcastPkts6 = moveto_long_long(&snip6->InMcastPkts6, endian_mismatch, arch_64);
1034                 snic6->OutMcastPkts6 = moveto_long_long(&snip6->OutMcastPkts6, endian_mismatch, arch_64);
1035                 snic6->FragOKs6 = moveto_long_long(&snip6->FragOKs6, endian_mismatch, arch_64);
1036                 snic6->FragCreates6 = moveto_long_long(&snip6->FragCreates6, endian_mismatch, arch_64);
1037         }
1038         else {
1039                 struct stats_net_ip6_8b *snip6 = (struct stats_net_ip6_8b *) act[p]->buf[0];
1040
1041                 snic6->InReceives6 = snip6->InReceives6;
1042                 snic6->OutForwDatagrams6 = snip6->OutForwDatagrams6;
1043                 snic6->InDelivers6 = snip6->InDelivers6;
1044                 snic6->OutRequests6 = snip6->OutRequests6;
1045                 snic6->ReasmReqds6 = snip6->ReasmReqds6;
1046                 snic6->ReasmOKs6 = snip6->ReasmOKs6;
1047                 snic6->InMcastPkts6 = snip6->InMcastPkts6;
1048                 snic6->OutMcastPkts6 = snip6->OutMcastPkts6;
1049                 snic6->FragOKs6 = snip6->FragOKs6;
1050                 snic6->FragCreates6 = snip6->FragCreates6;
1051         }
1052 }
1053
1054 /*
1055  ***************************************************************************
1056  * Upgrade stats_net_eip6 structure (from ACTIVITY_MAGIC_BASE format to
1057  * ACTIVITY_MAGIC_BASE + 1 format).
1058  *
1059  * IN:
1060  * @act         Array of activities.
1061  * @p           Position of activity in array.
1062  * @magic       Structure format magic value.
1063  * @endian_mismatch
1064  *              TRUE if data read from file don't match current machine's
1065  *              endianness.
1066  * @arch_64     TRUE if file's data come from a 64-bit machine.
1067  ***************************************************************************
1068  */
1069 void upgrade_stats_net_eip6(struct activity *act[], int p, unsigned int magic,
1070                             int endian_mismatch, int arch_64)
1071 {
1072         struct stats_net_eip6 *sneic6 = (struct stats_net_eip6 *) act[p]->buf[1];
1073
1074         if (magic == ACTIVITY_MAGIC_BASE) {
1075                 struct stats_net_eip6_8a *sneip6 = (struct stats_net_eip6_8a *) act[p]->buf[0];
1076
1077                 sneic6->InHdrErrors6 = moveto_long_long(&sneip6->InHdrErrors6, endian_mismatch, arch_64);
1078                 sneic6->InAddrErrors6 = moveto_long_long(&sneip6->InAddrErrors6, endian_mismatch, arch_64);
1079                 sneic6->InUnknownProtos6 = moveto_long_long(&sneip6->InUnknownProtos6, endian_mismatch, arch_64);
1080                 sneic6->InTooBigErrors6 = moveto_long_long(&sneip6->InTooBigErrors6, endian_mismatch, arch_64);
1081                 sneic6->InDiscards6 = moveto_long_long(&sneip6->InDiscards6, endian_mismatch, arch_64);
1082                 sneic6->OutDiscards6 = moveto_long_long(&sneip6->OutDiscards6, endian_mismatch, arch_64);
1083                 sneic6->InNoRoutes6 = moveto_long_long(&sneip6->InNoRoutes6, endian_mismatch, arch_64);
1084                 sneic6->OutNoRoutes6 = moveto_long_long(&sneip6->OutNoRoutes6, endian_mismatch, arch_64);
1085                 sneic6->ReasmFails6 = moveto_long_long(&sneip6->ReasmFails6, endian_mismatch, arch_64);
1086                 sneic6->FragFails6 = moveto_long_long(&sneip6->FragFails6, endian_mismatch, arch_64);
1087                 sneic6->InTruncatedPkts6 = moveto_long_long(&sneip6->InTruncatedPkts6, endian_mismatch, arch_64);
1088         }
1089         else {
1090                 struct stats_net_eip6_8b *sneip6 = (struct stats_net_eip6_8b *) act[p]->buf[0];
1091
1092                 sneic6->InHdrErrors6 = sneip6->InHdrErrors6;
1093                 sneic6->InAddrErrors6 = sneip6->InAddrErrors6;
1094                 sneic6->InUnknownProtos6 = sneip6->InUnknownProtos6;
1095                 sneic6->InTooBigErrors6 = sneip6->InTooBigErrors6;
1096                 sneic6->InDiscards6 = sneip6->InDiscards6;
1097                 sneic6->OutDiscards6 = sneip6->OutDiscards6;
1098                 sneic6->InNoRoutes6 = sneip6->InNoRoutes6;
1099                 sneic6->OutNoRoutes6 = sneip6->OutNoRoutes6;
1100                 sneic6->ReasmFails6 = sneip6->ReasmFails6;
1101                 sneic6->FragFails6 = sneip6->FragFails6;
1102                 sneic6->InTruncatedPkts6 = sneip6->InTruncatedPkts6;
1103         }
1104 }
1105
1106 /*
1107  ***************************************************************************
1108  * Upgrade stats_huge structure (from ACTIVITY_MAGIC_BASE format to
1109  * ACTIVITY_MAGIC_BASE + 1 format).
1110  *
1111  * IN:
1112  * @act         Array of activities.
1113  * @p           Position of activity in array.
1114  * @endian_mismatch
1115  *              TRUE if data read from file don't match current machine's
1116  *              endianness.
1117  * @arch_64     TRUE if file's data come from a 64-bit machine.
1118  ***************************************************************************
1119  */
1120 void upgrade_stats_huge(struct activity *act[], int p,
1121                         int endian_mismatch, int arch_64)
1122 {
1123         struct stats_huge *shc = (struct stats_huge *) act[p]->buf[1];
1124         struct stats_huge_8a *shp = (struct stats_huge_8a *) act[p]->buf[0];
1125
1126         shc->frhkb = moveto_long_long(&shp->frhkb, endian_mismatch, arch_64);
1127         shc->tlhkb = moveto_long_long(&shp->tlhkb, endian_mismatch, arch_64);
1128 }
1129
1130 /*
1131  ***************************************************************************
1132  * Upgrade stats_pwr_wghfreq structure (from ACTIVITY_MAGIC_BASE format to
1133  * ACTIVITY_MAGIC_BASE + 1 format).
1134  *
1135  * IN:
1136  * @act         Array of activities.
1137  * @p           Position of activity in array.
1138  ***************************************************************************
1139  */
1140 void upgrade_stats_pwr_wghfreq(struct activity *act[], int p)
1141 {
1142         int i, k;
1143
1144         struct stats_pwr_wghfreq *spc, *spc_k;
1145         struct stats_pwr_wghfreq_8a *spp, *spp_k;
1146
1147         for (i = 0; i < act[p]->nr_ini; i++) {
1148                 spp = (struct stats_pwr_wghfreq_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize * act[p]->nr2);
1149                 spc = (struct stats_pwr_wghfreq *) ((char *) act[p]->buf[1] + i * act[p]->fsize * act[p]->nr2);
1150
1151                 for (k = 0; k < act[p]->nr2; k++) {
1152                         spc_k = (struct stats_pwr_wghfreq *) ((char *) spc + k * act[p]->fsize);
1153                         if (!spc_k->freq)
1154                                 break;
1155                         spp_k = (struct stats_pwr_wghfreq_8a *) ((char *) spp + k * act[p]->msize);
1156
1157                         spc_k->time_in_state = spp_k->time_in_state;
1158                         memcpy(&spc_k->freq, &spp_k->freq, 8);
1159                 }
1160         }
1161 }
1162
1163 /*
1164  ***************************************************************************
1165  * Upgrade stats_filesystem structure (from ACTIVITY_MAGIC_BASE format to
1166  * ACTIVITY_MAGIC_BASE + 1 format).
1167  *
1168  * IN:
1169  * @act         Array of activities.
1170  * @p           Position of activity in array.
1171  * @st_size     Size of the structure read from file.
1172  ***************************************************************************
1173  */
1174 void upgrade_stats_filesystem(struct activity *act[], int p, int st_size)
1175 {
1176         int i;
1177         struct stats_filesystem *sfc;
1178         struct stats_filesystem_8a *sfp;
1179
1180         for (i = 0; i < act[p]->nr_ini; i++) {
1181                 sfp = (struct stats_filesystem_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
1182                 sfc = (struct stats_filesystem *)    ((char *) act[p]->buf[1] + i * act[p]->fsize);
1183
1184                 sfc->f_blocks = sfp->f_blocks;
1185                 sfc->f_bfree = sfp->f_bfree;
1186                 sfc->f_bavail = sfp->f_bavail;
1187                 sfc->f_files = sfp->f_files;
1188                 sfc->f_ffree = sfp->f_ffree;
1189                 strncpy(sfc->fs_name, sfp->fs_name, MAX_FS_LEN);
1190                 sfc->fs_name[MAX_FS_LEN - 1] = '\0';
1191
1192                 if (st_size <= STATS_FILESYSTEM_8A_1_SIZE) {
1193                         /* mountp didn't exist with older versions */
1194                         sfc->mountp[0] = '\0';
1195                 }
1196                 else {
1197                         strncpy(sfc->mountp, sfp->mountp, MAX_FS_LEN);
1198                         sfc->mountp[MAX_FS_LEN - 1] = '\0';
1199                 }
1200         }
1201 }
1202
1203 /*
1204  ***************************************************************************
1205  * Count number of stats_disk structures that need to be written.
1206  *
1207  * IN:
1208  * @act         Array of activities.
1209  * @p           Position of activity in array.
1210  ***************************************************************************
1211  */
1212 __nr_t count_stats_disk(struct activity *act[], int p)
1213 {
1214         int i;
1215         __nr_t nr_dsk = 0;
1216         struct stats_disk *sdc;
1217
1218         for (i = 0; i < act[p]->nr_ini; i++) {
1219                 sdc = (struct stats_disk *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
1220                 if (!(sdc->major + sdc->minor))
1221                         break;
1222
1223                 nr_dsk++;
1224         }
1225
1226         return nr_dsk;
1227 }
1228
1229 /*
1230  ***************************************************************************
1231  * Count number of stats_net_dev structures that need to be written.
1232  *
1233  * IN:
1234  * @act         Array of activities.
1235  * @p           Position of activity in array.
1236  ***************************************************************************
1237  */
1238 __nr_t count_stats_net_dev(struct activity *act[], int p)
1239 {
1240         int i;
1241         __nr_t nr_dev = 0;
1242         struct stats_net_dev *sndc;
1243
1244         for (i = 0; i < act[p]->nr_ini; i++) {
1245                 sndc = (struct stats_net_dev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
1246                 if (!strcmp(sndc->interface, ""))
1247                         break;
1248
1249                 nr_dev++;
1250         }
1251
1252         return nr_dev;
1253 }
1254
1255 /*
1256  ***************************************************************************
1257  * Count number of stats_net_edev structures that need to be written.
1258  *
1259  * IN:
1260  * @act         Array of activities.
1261  * @p           Position of activity in array.
1262  ***************************************************************************
1263  */
1264 __nr_t count_stats_net_edev(struct activity *act[], int p)
1265 {
1266         int i;
1267         __nr_t nr_edev = 0;
1268         struct stats_net_edev *snedc;
1269
1270         for (i = 0; i < act[p]->nr_ini; i++) {
1271                 snedc = (struct stats_net_edev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
1272                 if (!strcmp(snedc->interface, ""))
1273                         break;
1274
1275                 nr_edev++;
1276         }
1277
1278         return nr_edev;
1279 }
1280
1281 /*
1282  ***************************************************************************
1283  * Count number of stats_pwr_usb structures that need to be written.
1284  *
1285  * IN:
1286  * @act         Array of activities.
1287  * @p           Position of activity in array.
1288  ***************************************************************************
1289  */
1290 __nr_t count_stats_pwr_usb(struct activity *act[], int p)
1291 {
1292         int i;
1293         __nr_t nr_usb = 0;
1294         struct stats_pwr_usb *suc;
1295
1296         for (i = 0; i < act[p]->nr_ini; i++) {
1297                 suc = (struct stats_pwr_usb *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
1298                 if (!suc->bus_nr)
1299                         break;
1300
1301                 nr_usb++;
1302         }
1303
1304         return nr_usb;
1305 }
1306
1307 /*
1308  ***************************************************************************
1309  * Count number of stats_filesystem structures that need to be written.
1310  *
1311  * IN:
1312  * @act         Array of activities.
1313  * @p           Position of activity in array.
1314  ***************************************************************************
1315  */
1316 __nr_t count_stats_filesystem(struct activity *act[], int p)
1317 {
1318         int i;
1319         __nr_t nr_fs = 0;
1320         struct stats_filesystem *sfc;
1321
1322         for (i = 0; i < act[p]->nr_ini; i++) {
1323                 sfc = (struct stats_filesystem *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
1324                 if (!sfc->f_blocks)
1325                         break;
1326
1327                 nr_fs++;
1328         }
1329
1330         return nr_fs;
1331 }
1332
1333 /*
1334  ***************************************************************************
1335  * Count number of stats_fchost structures that need to be written.
1336  *
1337  * IN:
1338  * @act         Array of activities.
1339  * @p           Position of activity in array.
1340  ***************************************************************************
1341  */
1342 __nr_t count_stats_fchost(struct activity *act[], int p)
1343 {
1344         int i;
1345         __nr_t nr_fc = 0;
1346         struct stats_fchost *sfcc;
1347
1348         for (i = 0; i < act[p]->nr_ini; i++) {
1349                 sfcc = (struct stats_fchost *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
1350                 if (!sfcc->fchost_name[0])
1351                         break;
1352
1353                 nr_fc++;
1354         }
1355
1356         return nr_fc;
1357 }
1358
1359 /*
1360  ***************************************************************************
1361  * Upgrade file's activity list section.
1362  *
1363  * IN:
1364  * @stdfd               File descriptor for STDOUT.
1365  * @act                 Array of activities.
1366  * @file_hdr            Pointer on file_header structure.
1367  * @ofile_actlst        Activity list in file.
1368  * @endian_mismatch
1369  *              TRUE if data read from file don't match current machine's
1370  *              endianness.
1371  * @arch_64     TRUE if file's data come from a 64-bit machine.
1372  *
1373  * OUT:
1374  * @file_actlst         Activity list with up-to-date format.
1375  *
1376  * RETURNS:
1377  * -1 on error, 0 otherwise.
1378 ***************************************************************************
1379  */
1380 int upgrade_activity_section(int stdfd, struct activity *act[],
1381                              struct file_header *file_hdr,
1382                              struct old_file_activity *ofile_actlst,
1383                              struct file_activity **file_actlst,
1384                              int endian_mismatch, int arch_64)
1385 {
1386         int i, j, p;
1387         struct old_file_activity *ofal;
1388         struct file_activity *fal, fa;
1389
1390         fprintf(stderr, "file_activity: ");
1391
1392         SREALLOC(*file_actlst, struct file_activity, FILE_ACTIVITY_SIZE * file_hdr->sa_act_nr);
1393         fal = *file_actlst;
1394         ofal = ofile_actlst;
1395
1396         for (i = 0; i < file_hdr->sa_act_nr; i++, ofal++, fal++) {
1397
1398                 /* Every activity should be known at the moment (may change in the future) */
1399                 p = get_activity_position(act, ofal->id, EXIT_IF_NOT_FOUND);
1400                 fal->id = ofal->id;
1401                 fal->nr = ofal->nr;
1402                 fal->nr2 = ofal->nr2;
1403                 fal->magic = act[p]->magic;     /* Update activity magic number */
1404                 fal->has_nr = HAS_COUNT_FUNCTION(act[p]->options);
1405                 /* Also update its size, which may have changed with recent versions */
1406                 fal->size = act[p]->fsize;
1407                 for (j = 0; j < 3; j++) {
1408                         fal->types_nr[j] = act[p]->gtypes_nr[j];
1409                 }
1410
1411                 memcpy(&fa, fal, FILE_ACTIVITY_SIZE);
1412                 /* Restore endianness before writing */
1413                 if (endian_mismatch) {
1414                         /* Start swapping at field "header_size" position */
1415                         swap_struct(act_types_nr, &fa, arch_64);
1416                 }
1417
1418                 /*
1419                  * Even unknown activities must be written
1420                  * (they are counted in sa_act_nr).
1421                  */
1422                 if (write(stdfd, &fa, FILE_ACTIVITY_SIZE) != FILE_ACTIVITY_SIZE) {
1423                         fprintf(stderr, "\nwrite: %s\n", strerror(errno));
1424                         return -1;
1425                 }
1426                 fprintf(stderr, "%s ", act[p]->name);
1427         }
1428
1429         fprintf(stderr, "OK\n");
1430
1431         return 0;
1432 }
1433
1434 /*
1435  ***************************************************************************
1436  * Upgrade a record header.
1437  *
1438  * IN:
1439  * @fd          File descriptor for sa datafile to convert.
1440  * @stdfd       File descriptor for STDOUT.
1441  * @orec_hdr    Record's header structure to convert.
1442  * @endian_mismatch
1443  *              TRUE if data read from file don't match current machine's
1444  *              endianness.
1445  * @arch_64     TRUE if file's data come from a 64-bit machine.
1446  *
1447  * RETURNS:
1448  * -1 on error, 0 otherwise.
1449 ***************************************************************************
1450  */
1451 int upgrade_record_header(int fd, int stdfd, struct old_record_header *orec_hdr,
1452                           int endian_mismatch, int arch_64)
1453 {
1454         struct record_header rec_hdr;
1455
1456         /* Convert current record header */
1457         rec_hdr.uptime_cs = orec_hdr->uptime0 * 100 / HZ;       /* Uptime in cs, not jiffies */
1458         rec_hdr.ust_time = (unsigned long long) orec_hdr->ust_time;
1459         rec_hdr.record_type = orec_hdr->record_type;
1460         rec_hdr.hour = orec_hdr->hour;
1461         rec_hdr.minute = orec_hdr->minute;
1462         rec_hdr.second = orec_hdr->second;
1463
1464         /* Restore endianness before writing */
1465         if (endian_mismatch) {
1466                 swap_struct(rec_types_nr, &rec_hdr, arch_64);
1467         }
1468
1469         /* Write record header */
1470         if (write(stdfd, &rec_hdr, RECORD_HEADER_SIZE) != RECORD_HEADER_SIZE) {
1471                 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
1472                 return -1;
1473         }
1474
1475         fprintf(stderr, "H");
1476
1477         return 0;
1478 }
1479
1480 /*
1481  ***************************************************************************
1482  * Upgrade a COMMENT record.
1483  *
1484  * IN:
1485  * @fd          File descriptor for sa datafile to convert.
1486  * @stdfd       File descriptor for STDOUT.
1487  *
1488  * RETURNS:
1489  * -1 on error, 0 otherwise.
1490 ***************************************************************************
1491  */
1492 int upgrade_comment_record(int fd, int stdfd)
1493 {
1494         char file_comment[MAX_COMMENT_LEN];
1495
1496         /* Read the COMMENT record */
1497         sa_fread(fd, file_comment, MAX_COMMENT_LEN, HARD_SIZE);
1498         file_comment[MAX_COMMENT_LEN - 1] = '\0';
1499
1500         /* Then write it. No changes at this time */
1501         if (write(stdfd, file_comment, MAX_COMMENT_LEN) != MAX_COMMENT_LEN) {
1502                 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
1503                 return -1;
1504         }
1505
1506         fprintf(stderr, "C");
1507
1508         return 0;
1509 }
1510
1511 /*
1512  ***************************************************************************
1513  * Upgrade a RESTART record.
1514  *
1515  * IN:
1516  * @fd          File descriptor for sa datafile to convert.
1517  * @stdfd       File descriptor for STDOUT.
1518  * @act         Array of activities.
1519  * @file_hdr    Pointer on file_header structure.
1520  * @previous_format
1521  *              TRUE is sa datafile has an old format which is no longer
1522  *              compatible with current one.
1523  * @endian_mismatch
1524  *              TRUE if data read from file don't match current machine's
1525  *              endianness.
1526  * @arch_64     TRUE if file's data come from a 64-bit machine.
1527  * @vol_act_nr  Number of volatile activity structures.
1528  *
1529  * RETURNS:
1530  * -1 on error, 0 otherwise.
1531 ***************************************************************************
1532  */
1533 int upgrade_restart_record(int fd, int stdfd, struct activity *act[],
1534                            struct file_header *file_hdr, int previous_format,
1535                            int endian_mismatch, int arch_64, unsigned int vol_act_nr)
1536 {
1537
1538         int i, p;
1539         struct old_file_activity ofile_act;
1540         /* Number of cpu read in the activity list. See upgrade_header_section() */
1541         __nr_t cpu_nr = file_hdr->sa_cpu_nr;
1542
1543         if (previous_format == FORMAT_MAGIC_2173) {
1544                 /*
1545                  * For versions from 10.3.1 to 11.6.x,
1546                  * the restart record is followed by a list
1547                  * of volatile activity structures. Among them is A_CPU activity.
1548                  */
1549                 for (i = 0; i < vol_act_nr; i++) {
1550                         sa_fread(fd, &ofile_act, OLD_FILE_ACTIVITY_SIZE, HARD_SIZE);
1551
1552                         /* Normalize endianness for file_activity structures */
1553                         if (endian_mismatch) {
1554                                 swap_struct(oact_types_nr, &ofile_act, arch_64);
1555                         }
1556
1557                         if (ofile_act.id && (ofile_act.nr > 0)) {
1558                                 p = get_activity_position(act, ofile_act.id, EXIT_IF_NOT_FOUND);
1559                                 act[p]->nr_ini = ofile_act.nr;
1560
1561                                 if (ofile_act.id == A_CPU) {
1562                                         cpu_nr = ofile_act.nr;
1563                                 }
1564                         }
1565                 }
1566                 /* Reallocate structures */
1567                 allocate_structures(act);
1568         }
1569
1570         /* Restore endianness before writing */
1571         if (endian_mismatch) {
1572                 cpu_nr = __builtin_bswap32(cpu_nr);
1573         }
1574
1575         /* Write new number of CPU following the restart record */
1576         if (write(stdfd, &cpu_nr, sizeof(__nr_t)) != sizeof(__nr_t)) {
1577                 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
1578                 return -1;
1579         }
1580
1581         fprintf(stderr, "R");
1582
1583         return 0;
1584 }
1585
1586 /*
1587  ***************************************************************************
1588  * Upgrade a record which is not a COMMENT or a RESTART one.
1589  *
1590  * IN:
1591  * @fd          File descriptor for sa datafile to convert.
1592  * @stdfd       File descriptor for STDOUT.
1593  * @act         Array of activities.
1594  * @file_hdr    Pointer on file_header structure (up-to-date format).
1595  * @ofile_actlst
1596  *              Activity list in file.
1597  * @file_actlst Activity list in file (up-to-date format).
1598  * @endian_mismatch
1599  *              TRUE if data read from file don't match current machine's
1600  *              endianness.
1601  * @arch_64     TRUE if file's data come from a 64-bit machine.
1602  *
1603  * RETURNS:
1604  * -1 on error, 0 otherwise.
1605 ***************************************************************************
1606  */
1607 int upgrade_common_record(int fd, int stdfd, struct activity *act[], struct file_header *file_hdr,
1608                           struct old_file_activity *ofile_actlst, struct file_activity *file_actlst,
1609                           int endian_mismatch, int arch_64)
1610 {
1611         int i, j, k, p;
1612         __nr_t nr_struct, nr;
1613         struct old_file_activity *ofal = ofile_actlst;
1614         struct file_activity *fal = file_actlst;
1615         char cc;
1616
1617         /*
1618          * This is not a special record, so read the extra fields,
1619          * even if the format of the activity is unknown.
1620          */
1621         for (i = 0; i < file_hdr->sa_act_nr; i++, ofal++, fal++) {
1622
1623                 /* Every activity should be known at the moment (may change in the future) */
1624                 p = get_activity_position(act, fal->id, EXIT_IF_NOT_FOUND);
1625
1626                 /* Warning: Stats structures keep their original endianness here */
1627                 if ((act[p]->nr_ini > 0) &&
1628                     ((act[p]->nr_ini > 1) || (act[p]->nr2 > 1)) &&
1629                     (act[p]->msize > ofal->size)) {
1630                         for (j = 0; j < act[p]->nr_ini; j++) {
1631                                 for (k = 0; k < act[p]->nr2; k++) {
1632                                         sa_fread(fd,
1633                                                  (char *) act[p]->buf[0] + (j * act[p]->nr2 + k) * act[p]->msize,
1634                                                  ofal->size, HARD_SIZE);
1635                                 }
1636                         }
1637                 }
1638                 else if (act[p]->nr_ini > 0) {
1639                         sa_fread(fd, act[p]->buf[0], ofal->size * act[p]->nr_ini * act[p]->nr2, HARD_SIZE);
1640                 }
1641
1642                 /*
1643                  * NB: Cannot upgrade a stats structure with
1644                  * a magic number higher than currently known.
1645                  */
1646                 if (ofal->magic < act[p]->magic) {
1647                         cc = 'u';
1648
1649                         /* Known activity but old format */
1650                         switch (fal->id) {
1651
1652                                 case A_CPU:
1653                                         upgrade_stats_cpu(act, p, ofal->size);
1654                                         break;
1655
1656                                 case A_PCSW:
1657                                         upgrade_stats_pcsw(act, p);
1658                                         break;
1659
1660                                 case A_IRQ:
1661                                         upgrade_stats_irq(act, p);
1662                                         break;
1663
1664                                 case A_IO:
1665                                         upgrade_stats_io(act, p, endian_mismatch);
1666                                         break;
1667
1668                                 case A_QUEUE:
1669                                         upgrade_stats_queue(act, p, ofal->magic,
1670                                                             endian_mismatch, arch_64);
1671                                         break;
1672
1673                                 case A_MEMORY:
1674                                         upgrade_stats_memory(act, p, ofal->size,
1675                                                              endian_mismatch, arch_64);
1676                                         break;
1677
1678                                 case A_KTABLES:
1679                                         upgrade_stats_ktables(act, p, endian_mismatch);
1680                                         break;
1681
1682                                 case A_SERIAL:
1683                                         upgrade_stats_serial(act, p, endian_mismatch);
1684                                         break;
1685
1686                                 case A_DISK:
1687                                         upgrade_stats_disk(act, p, ofal->magic,
1688                                                            endian_mismatch, arch_64);
1689                                         break;
1690
1691                                 case A_NET_DEV:
1692                                         upgrade_stats_net_dev(act, p, ofal->magic,
1693                                                               endian_mismatch, arch_64);
1694                                         break;
1695
1696                                 case A_NET_EDEV:
1697                                         upgrade_stats_net_edev(act, p, ofal->magic,
1698                                                                endian_mismatch, arch_64);
1699                                         break;
1700
1701                                 case A_NET_IP:
1702                                         upgrade_stats_net_ip(act, p, ofal->magic,
1703                                                              endian_mismatch, arch_64);
1704                                         break;
1705
1706                                 case A_NET_EIP:
1707                                         upgrade_stats_net_eip(act, p, ofal->magic,
1708                                                               endian_mismatch, arch_64);
1709                                         break;
1710
1711                                 case A_NET_IP6:
1712                                         upgrade_stats_net_ip6(act, p, ofal->magic,
1713                                                               endian_mismatch, arch_64);
1714                                         break;
1715
1716                                 case A_NET_EIP6:
1717                                         upgrade_stats_net_eip6(act, p, ofal->magic,
1718                                                                endian_mismatch, arch_64);
1719                                         break;
1720
1721                                 case A_HUGE:
1722                                         upgrade_stats_huge(act, p, endian_mismatch, arch_64);
1723                                         break;
1724
1725                                 case A_PWR_FREQ:
1726                                         upgrade_stats_pwr_wghfreq(act, p);
1727                                         break;
1728
1729                                 case A_FS:
1730                                         upgrade_stats_filesystem(act, p, ofal->size);
1731                                         break;
1732                                 }
1733                 }
1734                 else {
1735                         cc = '.';
1736                         /* Known activity with current up-to-date format */
1737                         for (j = 0; j < act[p]->nr_ini; j++) {
1738                                 for (k = 0; k < act[p]->nr2; k++) {
1739                                         memcpy((char *) act[p]->buf[1] + (j * act[p]->nr2 + k) * act[p]->msize,
1740                                                (char *) act[p]->buf[0] + (j * act[p]->nr2 + k) * act[p]->msize,
1741                                                fal->size);
1742                                 }
1743                         }
1744                 }
1745
1746                 nr_struct = act[p]->nr_ini;
1747                 if (fal->has_nr) {
1748
1749                         switch (fal->id) {
1750
1751                                 case A_SERIAL:
1752                                         /* Nothing to do: Already done in upgrade_stats_serial() */
1753                                         break;
1754
1755                                 case A_DISK:
1756                                         nr_struct = count_stats_disk(act, p);
1757                                         break;
1758
1759                                 case A_NET_DEV:
1760                                         nr_struct = count_stats_net_dev(act, p);
1761                                         break;
1762
1763                                 case A_NET_EDEV:
1764                                         nr_struct = count_stats_net_edev(act, p);
1765                                         break;
1766
1767                                 case A_PWR_USB:
1768                                         nr_struct = count_stats_pwr_usb(act, p);
1769                                         break;
1770
1771                                 case A_FS:
1772                                         nr_struct = count_stats_filesystem(act, p);
1773                                         break;
1774
1775                                 case A_NET_FC:
1776                                         nr_struct = count_stats_fchost(act, p);
1777                                         break;
1778                         }
1779
1780                         /* Restore endianness before writing */
1781                         if (endian_mismatch) {
1782                                 nr = __builtin_bswap32(nr_struct);
1783                         }
1784                         else {
1785                                 nr = nr_struct;
1786                         }
1787
1788                         /* Write number of structures for current activity */
1789                         if (write(stdfd, &nr, sizeof(__nr_t)) != sizeof(__nr_t))
1790                                 goto write_error;
1791
1792                         fprintf(stderr, "n");
1793                 }
1794
1795                 for (j = 0; j < nr_struct; j++) {
1796                         for (k = 0; k < act[p]->nr2; k++) {
1797                                 if (write(stdfd,
1798                                           (char *) act[p]->buf[1] + (j * act[p]->nr2 + k) * act[p]->fsize,
1799                                           act[p]->fsize) != act[p]->fsize)
1800                                         goto write_error;
1801                         }
1802                 }
1803                 fprintf(stderr, "%c", cc);
1804         }
1805
1806         return 0;
1807
1808 write_error:
1809
1810         fprintf(stderr, "\nwrite: %s\n", strerror(errno));
1811
1812         return -1;
1813
1814 }
1815
1816 /*
1817  ***************************************************************************
1818  * Upgrade statistics records.
1819  *
1820  * IN:
1821  * @fd          File descriptor for sa datafile to convert.
1822  * @stdfd       File descriptor for STDOUT.
1823  * @act         Array of activities.
1824  * @file_hdr    Pointer on file_header structure.
1825  * @ofile_actlst
1826  *              Activity list in file.
1827  * @file_actlst Activity list with up-to-date format.
1828  * @previous_format
1829  *              TRUE is sa datafile has an old format which is no longer * @endian_mismatch
1830  *              TRUE if data read from file don't match current machine's
1831  *              endianness.
1832  * @arch_64     TRUE if file's data come from a 64-bit machine.
1833
1834  *              compatible with current one.
1835  * @endian_mismatch
1836  *              TRUE if data read from file don't match current machine's
1837  *              endianness.
1838  * @arch_64     TRUE if file's data come from a 64-bit machine.
1839  * @vol_act_nr  Number of volatile activity structures.
1840  *
1841  * RETURNS:
1842  * -1 on error, 0 otherwise.
1843 ***************************************************************************
1844  */
1845 int upgrade_stat_records(int fd, int stdfd, struct activity *act[], struct file_header *file_hdr,
1846                          struct old_file_activity *ofile_actlst, struct file_activity *file_actlst,
1847                          int previous_format, int endian_mismatch, int arch_64,
1848                          unsigned int vol_act_nr)
1849 {
1850         int rtype;
1851         int eosaf;
1852         struct old_record_header orec_hdr;
1853         unsigned int orec_types_nr[] = {OLD_RECORD_HEADER_ULL_NR, OLD_RECORD_HEADER_UL_NR, OLD_RECORD_HEADER_U_NR};
1854
1855         fprintf(stderr, _("Statistics:\n"));
1856
1857         do {
1858                 eosaf = sa_fread(fd, &orec_hdr, OLD_RECORD_HEADER_SIZE, SOFT_SIZE);
1859
1860                 /* Normalize endianness */
1861                 if (endian_mismatch) {
1862                         swap_struct(orec_types_nr, &orec_hdr, arch_64);
1863                 }
1864                 rtype = orec_hdr.record_type;
1865
1866                 if (!eosaf) {
1867                         /* Upgrade current record header */
1868                         if (upgrade_record_header(fd, stdfd, &orec_hdr,
1869                                                   endian_mismatch, arch_64) < 0)
1870                                 return -1;
1871
1872                         if (rtype == R_COMMENT) {
1873                                 /* Upgrade the COMMENT record */
1874                                 if (upgrade_comment_record(fd, stdfd) < 0)
1875                                         return -1;
1876                         }
1877                         else if (rtype == R_RESTART) {
1878                                 /* Upgrade the RESTART record */
1879                                 if (upgrade_restart_record(fd, stdfd, act, file_hdr,
1880                                                            previous_format, endian_mismatch,
1881                                                            arch_64, vol_act_nr) < 0)
1882                                         return -1;
1883                         }
1884                         else {
1885                                 /* Upgrade current statistics record */
1886                                 if (upgrade_common_record(fd, stdfd, act, file_hdr, ofile_actlst,
1887                                                           file_actlst, endian_mismatch, arch_64) < 0)
1888                                         return -1;
1889                         }
1890                 }
1891         }
1892         while (!eosaf);
1893
1894         fprintf(stderr, "\n");
1895
1896         return 0;
1897 }
1898
1899 /*
1900  ***************************************************************************
1901  * Close file descriptors and exit.
1902  *
1903  * IN:
1904  * @fd          File descriptor for sa datafile to convert.
1905  * @stdfd       File descriptor for STDOUT.
1906  * @exit_code   Exit code.
1907  ***************************************************************************
1908  */
1909 void upgrade_exit(int fd, int stdfd, int exit_code)
1910 {
1911         if (fd) {
1912                 close(fd);
1913         }
1914         if (stdfd) {
1915                 close(stdfd);
1916         }
1917         if (exit_code) {
1918                 exit(exit_code);
1919         }
1920 }
1921
1922 /*
1923  ***************************************************************************
1924  * Convert a sysstat activity data file from a previous version to the
1925  * up-to-date format. Presently data files from sysstat version 9.1.6 and
1926  * later are converted to current sysstat version format.
1927  *
1928  * IN:
1929  * @dfile       System activity data file name.
1930  * @act         Array of activities.
1931  ***************************************************************************
1932  */
1933 void convert_file(char dfile[], struct activity *act[])
1934 {
1935         int fd = 0, stdfd = 0, previous_format = 0;
1936         int arch_64 = TRUE;
1937         unsigned int vol_act_nr = 0, hdr_size;
1938         struct file_magic file_magic;
1939         struct file_header file_hdr;
1940         struct file_activity *file_actlst = NULL;
1941         struct old_file_activity *ofile_actlst = NULL;
1942
1943         /* Open stdout */
1944         if ((stdfd = dup(STDOUT_FILENO)) < 0) {
1945                 perror("dup");
1946                 upgrade_exit(0, 0, 2);
1947         }
1948
1949         /* Upgrade file's magic section */
1950         if (upgrade_magic_section(dfile, &fd, stdfd, &file_magic, &hdr_size,
1951                                   &previous_format, &endian_mismatch) < 0) {
1952                 upgrade_exit(fd, stdfd, 2);
1953         }
1954         if (previous_format == FORMAT_MAGIC) {
1955                 /* Nothing to do at the present time */
1956                 fprintf(stderr, _("\nFile format already up-to-date\n"));
1957                 goto success;
1958         }
1959
1960         /* Get HZ */
1961         get_HZ();
1962         fprintf(stderr, _("HZ: Using current value: %lu\n"), HZ);
1963
1964         /* Upgrade file's header section */
1965         if (upgrade_header_section(dfile, fd, stdfd, act, &file_magic,
1966                                    &file_hdr, hdr_size, previous_format, &arch_64,
1967                                    endian_mismatch, &vol_act_nr, &ofile_actlst) < 0) {
1968                 upgrade_exit(fd, stdfd, 2);
1969         }
1970
1971         /* Upgrade file's activity list section */
1972         if (upgrade_activity_section(stdfd, act, &file_hdr,
1973                                      ofile_actlst, &file_actlst,
1974                                      endian_mismatch, arch_64) < 0) {
1975                 upgrade_exit(fd, stdfd, 2);
1976         }
1977
1978         /* Perform required allocations */
1979         allocate_structures(act);
1980
1981         /* Upgrade statistics records */
1982         if (upgrade_stat_records(fd, stdfd, act, &file_hdr, ofile_actlst, file_actlst,
1983                                  previous_format, endian_mismatch, arch_64,
1984                                  vol_act_nr) < 0) {
1985                 upgrade_exit(fd, stdfd, 2);
1986         }
1987
1988         free(file_actlst);
1989         free(ofile_actlst);
1990         free_structures(act);
1991
1992         fprintf(stderr,
1993                 _("File successfully converted to sysstat format version %s\n"),
1994                 VERSION);
1995
1996 success:
1997         upgrade_exit(fd, stdfd, 0);
1998 }