2 * sa_conv.c: Convert an old format sa file to the up-to-date format.
3 * (C) 1999-2016 by Sebastien GODARD (sysstat <at> orange.fr)
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. *
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 *
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 ***************************************************************************
36 # define _(string) gettext(string)
38 # define _(string) (string)
42 ***************************************************************************
43 * Read and upgrade file's magic data section.
46 * @dfile System activity data file name.
47 * @stdfd File descriptor for STDOUT.
50 * @fd File descriptor for sa datafile to convert.
51 * @file_magic Pointer on file_magic structure.
52 * @previous_format TRUE is sa datafile has an old format which is no
53 * longer compatible with current one.
56 * -1 on error, 0 otherwise.
57 ***************************************************************************
59 int upgrade_magic_section(char dfile[], int *fd, int stdfd,
60 struct file_magic *file_magic, int *previous_format)
64 /* Open and read sa magic structure */
65 sa_open_read_magic(fd, dfile, file_magic, TRUE);
67 if ((file_magic->format_magic != FORMAT_MAGIC) &&
68 (file_magic->format_magic != PREVIOUS_FORMAT_MAGIC)) {
69 fprintf(stderr, _("Cannot convert the format of this file\n"));
73 fprintf(stderr, "file_magic: ");
74 if (file_magic->format_magic == PREVIOUS_FORMAT_MAGIC) {
76 * We have read too many bytes: file_magic structure
77 * was smaller with previous sysstat versions.
78 * Go back 4 (unsigned int header_size) + 64 (char pad[64]) bytes.
80 if (lseek(*fd, -68, SEEK_CUR) < 0) {
81 fprintf(stderr, "\nlseek: %s\n", strerror(errno));
84 /* Set format magic number to that of current version */
85 *previous_format = TRUE;
86 file_magic->format_magic = FORMAT_MAGIC;
88 /* Fill new structure members */
89 file_magic->header_size = FILE_HEADER_SIZE;
90 memset(file_magic->pad, 0, sizeof(unsigned char) * FILE_MAGIC_PADDING);
93 /* Indicate that file has been upgraded */
95 file_magic->upgraded = (fm.sysstat_patchlevel << 4) +
96 fm.sysstat_sublevel + 1;
98 /* Write file_magic structure */
99 if (write(stdfd, file_magic, FILE_MAGIC_SIZE) != FILE_MAGIC_SIZE) {
100 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
103 fprintf(stderr, "OK\n");
109 ***************************************************************************
110 * Upgrade file_header structure (from 0x2171 format to current format).
113 * @buffer Pointer on file's header structure (as read from file).
116 * @file_hdr Pointer on file_header structure (up-to-date format).
117 ***************************************************************************
119 void upgrade_file_header(void *buffer, struct file_header *file_hdr)
121 struct file_header_2171 *f_hdr = (struct file_header_2171 *) buffer;
123 file_hdr->sa_ust_time = f_hdr->sa_ust_time;
124 file_hdr->sa_act_nr = f_hdr->sa_act_nr;
125 file_hdr->sa_day = f_hdr->sa_day;
126 file_hdr->sa_month = f_hdr->sa_month;
127 file_hdr->sa_year = f_hdr->sa_year;
128 file_hdr->sa_sizeof_long = f_hdr->sa_sizeof_long;
129 strncpy(file_hdr->sa_sysname, f_hdr->sa_sysname, UTSNAME_LEN);
130 file_hdr->sa_sysname[UTSNAME_LEN - 1] = '\0';
131 strncpy(file_hdr->sa_nodename, f_hdr->sa_nodename, UTSNAME_LEN);
132 file_hdr->sa_nodename[UTSNAME_LEN - 1] = '\0';
133 strncpy(file_hdr->sa_release, f_hdr->sa_release, UTSNAME_LEN);
134 file_hdr->sa_release[UTSNAME_LEN - 1] = '\0';
135 strncpy(file_hdr->sa_machine, f_hdr->sa_machine, UTSNAME_LEN);
136 file_hdr->sa_machine[UTSNAME_LEN - 1] = '\0';
137 /* The last two values below will be updated later */
138 file_hdr->sa_vol_act_nr = 0;
139 file_hdr->sa_last_cpu_nr = 0;
143 ***************************************************************************
144 * Upgrade stats_io structure (from ACTIVITY_MAGIC_BASE format to
145 * ACTIVITY_MAGIC_BASE + 1 format).
148 * @act Array of activities.
149 * @p Position of activity in array.
150 ***************************************************************************
152 void upgrade_stats_io(struct activity *act[], int p)
154 struct stats_io *sic = (struct stats_io *) act[p]->buf[1];
155 struct stats_io_8a *sip = (struct stats_io_8a *) act[p]->buf[0];
157 sic->dk_drive = (unsigned long long) sip->dk_drive;
158 sic->dk_drive_rio = (unsigned long long) sip->dk_drive_rio;
159 sic->dk_drive_wio = (unsigned long long) sip->dk_drive_wio;
160 sic->dk_drive_rblk = (unsigned long long) sip->dk_drive_rblk;
161 sic->dk_drive_wblk = (unsigned long long) sip->dk_drive_wblk;
165 ***************************************************************************
166 * Upgrade stats_queue structure (from ACTIVITY_MAGIC_BASE format to
167 * ACTIVITY_MAGIC_BASE + 1 format).
170 * @act Array of activities.
171 * @p Position of activity in array.
172 ***************************************************************************
174 void upgrade_stats_queue(struct activity *act[], int p)
176 struct stats_queue *sqc = (struct stats_queue *) act[p]->buf[1];
177 struct stats_queue_8a *sqp = (struct stats_queue_8a *) act[p]->buf[0];
179 sqc->nr_running = sqp->nr_running;
180 sqc->procs_blocked = 0; /* New field */
181 sqc->load_avg_1 = sqp->load_avg_1;
182 sqc->load_avg_5 = sqp->load_avg_5;
183 sqc->load_avg_15 = sqp->load_avg_15;
184 sqc->nr_threads = sqp->nr_threads;
188 ***************************************************************************
189 * Upgrade stats_disk structure (from ACTIVITY_MAGIC_BASE format to
190 * ACTIVITY_MAGIC_BASE + 1 format).
193 * @act Array of activities.
194 * @p Position of activity in array.
195 ***************************************************************************
197 void upgrade_stats_disk(struct activity *act[], int p)
200 struct stats_disk *sdc;
201 struct stats_disk_8a *sdp;
203 for (i = 0; i < act[p]->nr; i++) {
204 sdp = (struct stats_disk_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
205 sdc = (struct stats_disk *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
207 sdc->nr_ios = (unsigned long long) sdp->nr_ios;
208 sdc->rd_sect = (unsigned long) sdp->rd_sect;
209 sdc->wr_sect = (unsigned long) sdp->wr_sect;
210 sdc->rd_ticks = (unsigned int) sdp->rd_ticks;
211 sdc->wr_ticks = (unsigned int) sdp->wr_ticks;
212 sdc->tot_ticks = (unsigned int) sdp->tot_ticks;
213 sdc->rq_ticks = (unsigned int) sdp->rq_ticks;
214 sdc->major = sdp->major;
215 sdc->minor = sdp->minor;
220 ***************************************************************************
221 * Upgrade stats_net_dev structure (from ACTIVITY_MAGIC_BASE or
222 * ACTIVITY_MAGIC_BASE + 1 format to ACTIVITY_MAGIC_BASE + 2 format).
225 * @act Array of activities.
226 * @p Position of activity in array.
227 * @m_format Structure format magic value.
228 ***************************************************************************
230 void upgrade_stats_net_dev(struct activity *act[], int p, unsigned int m_format)
233 struct stats_net_dev *sndc;
235 if (m_format == ACTIVITY_MAGIC_BASE) {
236 struct stats_net_dev_8a *sndp_a;
238 for (i = 0; i < act[p]->nr; i++) {
239 sndp_a = (struct stats_net_dev_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
240 sndc = (struct stats_net_dev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
242 sndc->rx_packets = (unsigned long long) sndp_a->rx_packets;
243 sndc->tx_packets = (unsigned long long) sndp_a->tx_packets;
244 sndc->rx_bytes = (unsigned long long) sndp_a->rx_bytes;
245 sndc->tx_bytes = (unsigned long long) sndp_a->tx_bytes;
246 sndc->rx_compressed = (unsigned long long) sndp_a->rx_compressed;
247 sndc->tx_compressed = (unsigned long long) sndp_a->tx_compressed;
248 sndc->multicast = (unsigned long long) sndp_a->multicast;
249 sndc->speed = 0; /* New field */
250 strncpy(sndc->interface, sndp_a->interface, MAX_IFACE_LEN);
251 sndc->interface[MAX_IFACE_LEN - 1] = '\0';
252 sndc->duplex = '\0'; /* New field */
256 struct stats_net_dev_8b *sndp_b;
258 for (i = 0; i < act[p]->nr; i++) {
259 sndp_b = (struct stats_net_dev_8b *) ((char *) act[p]->buf[0] + i * act[p]->msize);
260 sndc = (struct stats_net_dev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
262 sndc->rx_packets = sndp_b->rx_packets;
263 sndc->tx_packets = sndp_b->tx_packets;
264 sndc->rx_bytes = sndp_b->rx_bytes;
265 sndc->tx_bytes = sndp_b->tx_bytes;
266 sndc->rx_compressed = sndp_b->rx_compressed;
267 sndc->tx_compressed = sndp_b->tx_compressed;
268 sndc->multicast = sndp_b->multicast;
269 sndc->speed = 0; /* New field */
270 strncpy(sndc->interface, sndp_b->interface, MAX_IFACE_LEN);
271 sndc->interface[MAX_IFACE_LEN - 1] = '\0';
272 sndc->duplex = '\0'; /* New field */
278 ***************************************************************************
279 * Upgrade stats_net_edev structure (from ACTIVITY_MAGIC_BASE format to
280 * ACTIVITY_MAGIC_BASE + 1 format).
283 * @act Array of activities.
284 * @p Position of activity in array.
285 ***************************************************************************
287 void upgrade_stats_net_edev(struct activity *act[], int p)
290 struct stats_net_edev *snedc;
291 struct stats_net_edev_8a *snedp;
293 for (i = 0; i < act[p]->nr; i++) {
294 snedp = (struct stats_net_edev_8a *) ((char *) act[p]->buf[0] + i * act[p]->msize);
295 snedc = (struct stats_net_edev *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
297 snedc->collisions = (unsigned long long) snedp->collisions;
298 snedc->rx_errors = (unsigned long long) snedp->rx_errors;
299 snedc->tx_errors = (unsigned long long) snedp->tx_errors;
300 snedc->rx_dropped = (unsigned long long) snedp->rx_dropped;
301 snedc->tx_dropped = (unsigned long long) snedp->tx_dropped;
302 snedc->rx_fifo_errors = (unsigned long long) snedp->rx_fifo_errors;
303 snedc->tx_fifo_errors = (unsigned long long) snedp->tx_fifo_errors;
304 snedc->rx_frame_errors = (unsigned long long) snedp->rx_frame_errors;
305 snedc->tx_carrier_errors = (unsigned long long) snedp->tx_carrier_errors;
306 strncpy(snedc->interface, snedp->interface, MAX_IFACE_LEN);
307 snedc->interface[MAX_IFACE_LEN - 1] = '\0';
312 ***************************************************************************
313 * Upgrade stats_net_ip structure (from ACTIVITY_MAGIC_BASE format to
314 * ACTIVITY_MAGIC_BASE + 1 format).
317 * @act Array of activities.
318 * @p Position of activity in array.
319 ***************************************************************************
321 void upgrade_stats_net_ip(struct activity *act[], int p)
323 struct stats_net_ip *snic = (struct stats_net_ip *) act[p]->buf[1];
324 struct stats_net_ip_8a *snip = (struct stats_net_ip_8a *) act[p]->buf[0];
326 snic->InReceives = (unsigned long long) snip->InReceives;
327 snic->ForwDatagrams = (unsigned long long) snip->ForwDatagrams;
328 snic->InDelivers = (unsigned long long) snip->InDelivers;
329 snic->OutRequests = (unsigned long long) snip->OutRequests;
330 snic->ReasmReqds = (unsigned long long) snip->ReasmReqds;
331 snic->ReasmOKs = (unsigned long long) snip->ReasmOKs;
332 snic->FragOKs = (unsigned long long) snip->FragOKs;
333 snic->FragCreates = (unsigned long long) snip->FragCreates;
337 ***************************************************************************
338 * Upgrade stats_net_eip structure (from ACTIVITY_MAGIC_BASE format to
339 * ACTIVITY_MAGIC_BASE + 1 format).
342 * @act Array of activities.
343 * @p Position of activity in array.
344 ***************************************************************************
346 void upgrade_stats_net_eip(struct activity *act[], int p)
348 struct stats_net_eip *sneic = (struct stats_net_eip *) act[p]->buf[1];
349 struct stats_net_eip_8a *sneip = (struct stats_net_eip_8a *) act[p]->buf[0];
351 sneic->InHdrErrors = (unsigned long long) sneip->InHdrErrors;
352 sneic->InAddrErrors = (unsigned long long) sneip->InAddrErrors;
353 sneic->InUnknownProtos = (unsigned long long) sneip->InUnknownProtos;
354 sneic->InDiscards = (unsigned long long) sneip->InDiscards;
355 sneic->OutDiscards = (unsigned long long) sneip->OutDiscards;
356 sneic->OutNoRoutes = (unsigned long long) sneip->OutNoRoutes;
357 sneic->ReasmFails = (unsigned long long) sneip->ReasmFails;
358 sneic->FragFails = (unsigned long long) sneip->FragFails;
362 ***************************************************************************
363 * Upgrade stats_net_ip6 structure (from ACTIVITY_MAGIC_BASE format to
364 * ACTIVITY_MAGIC_BASE + 1 format).
367 * @act Array of activities.
368 * @p Position of activity in array.
369 ***************************************************************************
371 void upgrade_stats_net_ip6(struct activity *act[], int p)
373 struct stats_net_ip6 *snic6 = (struct stats_net_ip6 *) act[p]->buf[1];
374 struct stats_net_ip6_8a *snip6 = (struct stats_net_ip6_8a *) act[p]->buf[0];
376 snic6->InReceives6 = (unsigned long long) snip6->InReceives6;
377 snic6->OutForwDatagrams6 = (unsigned long long) snip6->OutForwDatagrams6;
378 snic6->InDelivers6 = (unsigned long long) snip6->InDelivers6;
379 snic6->OutRequests6 = (unsigned long long) snip6->OutRequests6;
380 snic6->ReasmReqds6 = (unsigned long long) snip6->ReasmReqds6;
381 snic6->ReasmOKs6 = (unsigned long long) snip6->ReasmOKs6;
382 snic6->InMcastPkts6 = (unsigned long long) snip6->InMcastPkts6;
383 snic6->OutMcastPkts6 = (unsigned long long) snip6->OutMcastPkts6;
384 snic6->FragOKs6 = (unsigned long long) snip6->FragOKs6;
385 snic6->FragCreates6 = (unsigned long long) snip6->FragCreates6;
389 ***************************************************************************
390 * Upgrade stats_net_eip6 structure (from ACTIVITY_MAGIC_BASE format to
391 * ACTIVITY_MAGIC_BASE + 1 format).
394 * @act Array of activities.
395 * @p Position of activity in array.
396 ***************************************************************************
398 void upgrade_stats_net_eip6(struct activity *act[], int p)
400 struct stats_net_eip6 *sneic6 = (struct stats_net_eip6 *) act[p]->buf[1];
401 struct stats_net_eip6_8a *sneip6 = (struct stats_net_eip6_8a *) act[p]->buf[0];
403 sneic6->InHdrErrors6 = (unsigned long long) sneip6->InHdrErrors6;
404 sneic6->InAddrErrors6 = (unsigned long long) sneip6->InAddrErrors6;
405 sneic6->InUnknownProtos6 = (unsigned long long) sneip6->InUnknownProtos6;
406 sneic6->InTooBigErrors6 = (unsigned long long) sneip6->InTooBigErrors6;
407 sneic6->InDiscards6 = (unsigned long long) sneip6->InDiscards6;
408 sneic6->OutDiscards6 = (unsigned long long) sneip6->OutDiscards6;
409 sneic6->InNoRoutes6 = (unsigned long long) sneip6->InNoRoutes6;
410 sneic6->OutNoRoutes6 = (unsigned long long) sneip6->OutNoRoutes6;
411 sneic6->ReasmFails6 = (unsigned long long) sneip6->ReasmFails6;
412 sneic6->FragFails6 = (unsigned long long) sneip6->FragFails6;
413 sneic6->InTruncatedPkts6 = (unsigned long long) sneip6->InTruncatedPkts6;
417 ***************************************************************************
418 * Read and upgrade file's header section.
421 * @dfile System activity data file name.
422 * @fd File descriptor for sa datafile to convert.
423 * @stdfd File descriptor for STDOUT.
424 * @act Array of activities.
425 * @file_magic Pointer on file_magic structure.
426 * @previous_format TRUE is sa datafile has an old format which is no
427 * longer compatible with current one.
430 * @file_hdr Pointer on file_header structure.
431 * @file_actlst Activity list in file.
432 * @vol_id_seq Sequence of volatile activities.
435 * -1 on error, 0 otherwise.
436 ***************************************************************************
438 int upgrade_header_section(char dfile[], int fd, int stdfd,
439 struct activity *act[], struct file_magic *file_magic,
440 struct file_header *file_hdr, int previous_format,
441 struct file_activity **file_actlst, unsigned int vol_id_seq[])
444 unsigned int a_cpu = FALSE;
446 struct file_activity *fal;
448 /* Read file header structure */
449 fprintf(stderr, "file_header: ");
451 if (previous_format) {
452 /* Previous format had 2 unsigned int less */
453 n = FILE_HEADER_SIZE - 8;
456 n = file_magic->header_size;
459 SREALLOC(buffer, char, n);
461 sa_fread(fd, buffer, n, HARD_SIZE);
463 if (previous_format) {
464 /* Upgrade file_header structure */
465 upgrade_file_header(buffer, file_hdr);
468 memcpy(file_hdr, buffer, MINIMUM(n, FILE_HEADER_SIZE));
474 if (file_hdr->sa_act_nr > MAX_NR_ACT)
477 /* Read file activity list */
478 SREALLOC(*file_actlst, struct file_activity, FILE_ACTIVITY_SIZE * file_hdr->sa_act_nr);
482 for (i = 0; i < file_hdr->sa_act_nr; i++, fal++) {
484 sa_fread(fd, fal, FILE_ACTIVITY_SIZE, HARD_SIZE);
486 if ((fal->nr < 1) || (fal->nr2 < 1))
488 * Every activity, known or unknown,
489 * should have at least one item and sub-item.
493 if ((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) >= 0) {
494 /* This is a known activity, maybe with an unknown format */
496 if (IS_VOLATILE(act[p]->options) && previous_format) {
498 * Current activity is known by current version
499 * as a volatile one: So increment the number of
500 * volatile activities in file's header (but only
501 * for old format data files, since up-to-date
502 * format data files already have the right value here).
504 file_hdr->sa_vol_act_nr += 1;
507 * Create the sequence of volatile activities.
508 * Used only for old format datafiles.
509 * For new format datafiles, this is not necessary
510 * since this sequence already exists following
511 * the RESTART record.
513 vol_id_seq[j++] = act[p]->id;
516 if (fal->id == A_CPU) {
518 * Old format data files don't know volatile
519 * activities. The number of CPU is a constant
520 * all along the file.
522 if (previous_format) {
523 file_hdr->sa_last_cpu_nr = fal->nr;
528 /* Size of an activity cannot be zero */
532 /* Size of activity in file is larger than up-to-date activity size */
533 if (fal->size > act[p]->msize) {
534 act[p]->msize = fal->size;
538 * When upgrading a file:
539 * fal->size : Size of an item for current activity, as
540 * read from the file.
541 * act[p]->msize: Size of the buffer in memory where an item
542 * for current activity, as read from the file,
543 * will be saved. We have:
544 * act[p]->msize >= {fal->size ; act[p]->fsize}
545 * act[p]->fsize: Size of an item for current activity with
548 act[p]->nr = fal->nr;
549 act[p]->nr2 = fal->nr2;
551 * Don't set act[p]->fsize! Should retain the size of an item
552 * for up-to-date format!
559 * CPU activity should always be in file
560 * and have a known format (expected magical number).
562 fprintf(stderr, _("\nCPU activity not found in file. Aborting...\n"));
566 /* Write file_header structure */
567 if ((n = write(stdfd, file_hdr, FILE_HEADER_SIZE)) != FILE_HEADER_SIZE) {
568 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
572 fprintf(stderr, "OK\n");
578 fprintf(stderr, _("\nInvalid data found. Aborting...\n"));
585 ***************************************************************************
586 * Upgrade file's activity list section.
589 * @stdfd File descriptor for STDOUT.
590 * @act Array of activities.
591 * @file_hdr Pointer on file_header structure.
592 * @file_actlst Activity list in file.
595 * -1 on error, 0 otherwise.
596 ***************************************************************************
598 int upgrade_activity_section(int stdfd, struct activity *act[],
599 struct file_header *file_hdr,
600 struct file_activity *file_actlst)
603 struct file_activity file_act;
604 struct file_activity *fal;
606 fprintf(stderr, "file_activity: ");
610 for (i = 0; i < file_hdr->sa_act_nr; i++, fal++) {
613 if ((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) >= 0) {
614 /* Update activity magic number */
615 file_act.magic = act[p]->magic;
616 /* Also update its size, which may have changed with recent versions */
617 file_act.size = act[p]->fsize;
621 * Even unknown activities must be written
622 * (they are counted in sa_act_nr).
624 if (write(stdfd, &file_act, FILE_ACTIVITY_SIZE) != FILE_ACTIVITY_SIZE) {
625 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
629 fprintf(stderr, "%s ", act[p]->name);
632 fprintf(stderr, "OK\n");
638 ***************************************************************************
639 * Upgrade a COMMENT record.
642 * @fd File descriptor for sa datafile to convert.
643 * @stdfd File descriptor for STDOUT.
646 * -1 on error, 0 otherwise.
647 ***************************************************************************
649 int upgrade_comment_record(int fd, int stdfd)
651 char file_comment[MAX_COMMENT_LEN];
653 /* Read the COMMENT record */
654 sa_fread(fd, file_comment, MAX_COMMENT_LEN, HARD_SIZE);
655 file_comment[MAX_COMMENT_LEN - 1] = '\0';
657 /* Then write it. No changes at this time */
658 if (write(stdfd, file_comment, MAX_COMMENT_LEN) != MAX_COMMENT_LEN) {
659 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
663 fprintf(stderr, "C");
669 ***************************************************************************
670 * Upgrade a RESTART record.
673 * @fd File descriptor for sa datafile to convert.
674 * @stdfd File descriptor for STDOUT.
675 * @act Array of activities.
676 * @file_hdr Pointer on file_header structure.
677 * @previous_format TRUE is sa datafile has an old format which is no
678 * longer compatible with current one.
679 * @vol_id_seq Sequence of volatile activities.
682 * -1 on error, 0 otherwise.
683 ***************************************************************************
685 int upgrade_restart_record(int fd, int stdfd, struct activity *act[],
686 struct file_header *file_hdr, int previous_format,
687 unsigned int vol_id_seq[])
690 struct file_activity file_act;
693 * This is a RESTART record.
694 * Only new format data file have additional structures
695 * to read here for volatile activities.
697 for (i = 0; i < file_hdr->sa_vol_act_nr; i++) {
699 if (!previous_format) {
700 sa_fread(fd, &file_act, FILE_ACTIVITY_SIZE, HARD_SIZE);
702 reallocate_vol_act_structures(act, file_act.nr, file_act.id);
706 memset(&file_act, 0, FILE_ACTIVITY_SIZE);
708 /* Old format: Sequence of volatile activities is in vol_id_seq */
709 p = get_activity_position(act, vol_id_seq[i], EXIT_IF_NOT_FOUND);
711 /* Set only the necessary fields */
712 file_act.id = act[p]->id;
713 file_act.nr = act[p]->nr;
716 if (write(stdfd, &file_act, FILE_ACTIVITY_SIZE) != FILE_ACTIVITY_SIZE) {
717 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
722 fprintf(stderr, "R");
728 ***************************************************************************
729 * Upgrade a record which is not a COMMENT or a RESTART one.
732 * @fd File descriptor for sa datafile to convert.
733 * @stdfd File descriptor for STDOUT.
734 * @act Array of activities.
735 * @file_hdr Pointer on file_header structure.
736 * @file_actlst Activity list in file.
739 * -1 on error, 0 otherwise.
740 ***************************************************************************
742 int upgrade_common_record(int fd, int stdfd, struct activity *act[],
743 struct file_header *file_hdr,
744 struct file_activity *file_actlst)
747 struct file_activity *fal;
752 * This is not a special record, so read the extra fields,
753 * even if the format of the activity is unknown.
756 for (i = 0; i < file_hdr->sa_act_nr; i++, fal++) {
758 if ((p = get_activity_position(act, fal->id, RESUME_IF_NOT_FOUND)) < 0) {
759 /* An unknown activity should still be read and written */
760 size = (size_t) fal->size * (size_t) fal->nr * (size_t) fal->nr2;
762 /* Buffer may have been allocated from a previous iteration in the loop */
768 SREALLOC(buffer, void, size);
769 sa_fread(fd, buffer, fal->size * fal->nr * fal->nr2, HARD_SIZE);
770 if (write(stdfd, (char *) buffer, size) != size) {
771 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
778 if ((act[p]->nr > 0) &&
779 ((act[p]->nr > 1) || (act[p]->nr2 > 1)) &&
780 (act[p]->msize > fal->size)) {
782 for (j = 0; j < act[p]->nr; j++) {
783 for (k = 0; k < act[p]->nr2; k++) {
785 (char *) act[p]->buf[0] + (j * act[p]->nr2 + k) * act[p]->msize,
786 fal->size, HARD_SIZE);
791 else if (act[p]->nr > 0) {
792 sa_fread(fd, act[p]->buf[0], fal->size * act[p]->nr * act[p]->nr2, HARD_SIZE);
795 if (act[p]->magic != fal->magic) {
797 /* Known activity but old format */
801 upgrade_stats_io(act, p);
805 upgrade_stats_queue(act, p);
809 upgrade_stats_disk(act, p);
813 upgrade_stats_net_dev(act, p, fal->magic);
817 upgrade_stats_net_edev(act, p);
821 upgrade_stats_net_ip(act, p);
825 upgrade_stats_net_eip(act, p);
829 upgrade_stats_net_ip6(act, p);
833 upgrade_stats_net_eip6(act, p);
838 /* Known activity with current up-to-date format */
839 for (j = 0; j < act[p]->nr; j++) {
840 for (k = 0; k < act[p]->nr2; k++) {
841 memcpy((char *) act[p]->buf[1] + (j * act[p]->nr2 + k) * act[p]->msize,
842 (char *) act[p]->buf[0] + (j * act[p]->nr2 + k) * act[p]->msize,
848 for (j = 0; j < act[p]->nr; j++) {
849 for (k = 0; k < act[p]->nr2; k++) {
851 (char *) act[p]->buf[1] + (j * act[p]->nr2 + k) * act[p]->msize,
854 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
862 fprintf(stderr, ".");
872 ***************************************************************************
873 * Upgrade statistics records.
876 * @fd File descriptor for sa datafile to convert.
877 * @stdfd File descriptor for STDOUT.
878 * @act Array of activities.
879 * @file_hdr Pointer on file_header structure.
880 * @previous_format TRUE is sa datafile has an old format which is no
881 * longer compatible with current one.
882 * @file_actlst Activity list in file.
883 * @vol_id_seq Sequence of volatile activities.
886 * -1 on error, 0 otherwise.
887 ***************************************************************************
889 int upgrade_stat_records(int fd, int stdfd, struct activity *act[],
890 struct file_header *file_hdr, int previous_format,
891 struct file_activity *file_actlst, unsigned int vol_id_seq[])
895 struct record_header record_hdr;
897 fprintf(stderr, _("Statistics: "));
900 eosaf = sa_fread(fd, &record_hdr, RECORD_HEADER_SIZE, SOFT_SIZE);
901 rtype = record_hdr.record_type;
905 if (write(stdfd, &record_hdr, RECORD_HEADER_SIZE)
906 != RECORD_HEADER_SIZE) {
907 fprintf(stderr, "\nwrite: %s\n", strerror(errno));
911 if (rtype == R_COMMENT) {
912 /* Upgrade the COMMENT record */
913 if (upgrade_comment_record(fd, stdfd) < 0)
917 else if (rtype == R_RESTART) {
918 /* Upgrade the RESTART record */
919 if (upgrade_restart_record(fd, stdfd, act, file_hdr,
920 previous_format, vol_id_seq) < 0)
925 /* Upgrade current statistics record */
926 if (upgrade_common_record(fd, stdfd, act, file_hdr,
938 ***************************************************************************
939 * Close file descriptors and exit.
942 * @fd File descriptor for sa datafile to convert.
943 * @stdfd File descriptor for STDOUT.
944 * @exit_code Exit code.
945 ***************************************************************************
947 void upgrade_exit(int fd, int stdfd, int exit_code)
960 ***************************************************************************
961 * Convert a sysstat activity data file from a previous version to the
962 * up-to-date format. Presently data files from sysstat version 9.1.6 and
963 * later are converted to current sysstat version format.
966 * @dfile System activity data file name.
967 * @act Array of activities.
968 ***************************************************************************
970 void convert_file(char dfile[], struct activity *act[])
972 int fd = 0, stdfd = 0;
973 int previous_format = FALSE;
974 struct file_magic file_magic;
975 struct file_header file_hdr;
976 struct file_activity *file_actlst = NULL;
977 unsigned int vol_id_seq[NR_ACT];
980 if ((stdfd = dup(STDOUT_FILENO)) < 0) {
982 upgrade_exit(0, 0, 2);
985 /* Upgrade file's magic section */
986 if (upgrade_magic_section(dfile, &fd, stdfd, &file_magic,
987 &previous_format) < 0) {
988 upgrade_exit(fd, stdfd, 2);
991 /* Upgrade file's header section */
992 if (upgrade_header_section(dfile, fd, stdfd, act,
993 &file_magic, &file_hdr,
994 previous_format, &file_actlst,
996 upgrade_exit(fd, stdfd, 2);
999 /* Upgrade file's activity list section */
1000 if (upgrade_activity_section(stdfd, act, &file_hdr,
1002 upgrade_exit(fd, stdfd, 2);
1005 /* Perform required allocations */
1006 allocate_structures(act);
1008 /* Upgrade statistics records */
1009 if (upgrade_stat_records(fd, stdfd, act, &file_hdr,
1010 previous_format, file_actlst,
1012 upgrade_exit(fd, stdfd, 2);
1016 _("\nFile successfully converted to sysstat format version %s\n"),
1020 free_structures(act);