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