[go: up one dir, main page]

File: ziomon_dacc.c

package info (click to toggle)
s390-tools 2.15.1-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 8,216 kB
  • sloc: ansic: 130,144; sh: 9,397; cpp: 8,359; perl: 2,517; makefile: 1,960; asm: 1,016
file content (980 lines) | stat: -rw-r--r-- 23,324 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
/*
 * FCP adapter trace utility
 *
 * Data access library
 *
 * Copyright IBM Corp. 2008, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "ziomon_dacc.h"
#include "ziomon_msg_tools.h"
#include "ziomon_util.h"


#define ZIOMON_DACC_GARBAGE_MSG	-1U

extern const char *toolname;
extern int verbose;

/*
 * Structure of binary file:
 *                                                                          |
 * +-----+---+---+-----+---+---+---------+---+---+---------+-      -+-----+ |
 * | hdr | l | t | dat | l | t |   dat   | l | t |   dat   |        | dat | |
 * +-----+---+---+-----+---+---+---------+---+---+---------+- .... -+-----+ |
 *                                                                          |
 *                                                                        limit
 * NOTES:
 * The messages might differ in length, depending how many monitored devices
 * were available in each interval. When we *would* exceed the limit, we wrap
 * around, overwriting old messages. Hence it is utterly important that we keep
 * the lengths 'l' intact, since we will have random garbage after one of the
 * messages once we wrapped around for the first time.
 * 't' denotes the type of the message, 'dat' is the (variable length) data.
 * The length is specified as __u32 and specifies the length of the variable
 * part of the message entry, beginning after the message type.
 * 'length' will always specify the length of the variable part of the message
 *
 * Aggregated statistics are put into a separate file.
 * Messages inside the aggregated file are written in fixed order (so we do not
 * need struct file_header to figure out what is where).
 * Note that we write a single garbage message for any message that might have
 * not been used yet!
 */


/* indicates whether we already wrapped or not */
static int wrapped = -1;

#ifndef NDEBUG
static int open_count = 0;
#endif


/**
 * Calculate size of the message. In contrast to the message's
 * length attribute, this also includes the header */
static __u32 get_total_msg_size(struct message *msg) {
	return (msg->length + 8);
}


/**
 * Position at first _physical_ message, not necessarily the first
 * logical message */
static int position_at_first_msg(FILE * fp)
{
	return fseek(fp, sizeof(struct file_header) - sizeof(__u64), SEEK_SET);
}


static void swap_header(struct file_header *f_hdr)
{
	swap_32(f_hdr->magic);
	swap_32(f_hdr->version);
	swap_64(f_hdr->size_limit);
	swap_32(f_hdr->interval_length);
	swap_32(f_hdr->msgid_utilization);
	swap_32(f_hdr->msgid_ioerr);
	swap_32(f_hdr->msgid_blkiomon);
	swap_32(f_hdr->msgid_zfcpdd);
	swap_64(f_hdr->end_time);
	swap_64(f_hdr->first_msg_offset);
	swap_64(f_hdr->begin_time);
}


static void swap_msg_header(struct message *msg)
{
	swap_32(msg->length);
	swap_32(msg->type);
}


static int write_f_header(FILE *fp, struct file_header *f_hdr)
{
	int rc = 0;

	rewind(fp);
	swap_header(f_hdr);
	if (fwrite(f_hdr, sizeof(struct file_header)
		   - sizeof(__u64), 1, fp) != 1) {
		fprintf(stderr, "%s: Failed to write"
			" header\n", toolname);
		rc = -1;
	}
	swap_header(f_hdr);

	return rc;
}

/**
 * write garbage of specified length to file, where 'length' is the total
 * length of the garbage message.
 * Note: This is only done for consistency, we rewind to start of garbage
 * immediately so we may overwrite it next time again
 */
static int write_garbage_message(FILE *fp, int length)
{
	struct message msg;

	vverbose_msg("writing garbage at pos=%ld, total size=%d\n", ftell(fp), length);
	msg.type = ZIOMON_DACC_GARBAGE_MSG;
	msg.length = length - 8;

	swap_msg_header(&msg);
	if (!fwrite(&msg, 8, 1, fp)) {
		fprintf(stderr, "%s: Error writing"
			" garbage\n", toolname);
		return -1;
	}

	return 0;
}


static int read_message_header(FILE *fp, __u32 *length, __u32 *type)
{
#ifndef NDEBUG
	long pos = ftell(fp);
#endif

	if (fread(length, 4, 1, fp) != 1) {
		if (feof(fp))
			return 1;	/* end of file reached */
		else {
			fprintf(stderr, "%s: Error reading"
				" message length\n", toolname);
			return -1;
		}
	}
	if (fread(type, 4, 1, fp) != 1) {
		fprintf(stderr, "%s: Error reading message"
			" type\n", toolname);
		return -1;
	}
	swap_32(*type);
	swap_32(*length);

	vverbose_msg("read %smsg at pos=%ld, data size=%d\n",
		     (*type == ZIOMON_DACC_GARBAGE_MSG ? "garbage " : ""), pos, *length);

	return 0;
}

/**
 * Read the next message.
 * @param msgid_blkiomon: ID of blkiomon messages or IS_NO_BLKIOMON_MSG if we
 *                        already know that it is none, or IS_BLKIOMON_MSG
 *                        if we know it is.
 */
#define IS_NO_BLKIOMON_MSG	0xfffffffe
#define IS_BLKIOMON_MSG		0xffffffff

static int read_message(FILE *fp, struct message *msg, __u32 ver,
			__u32 msgid_blkiomon)
{
	int rc;

	if ( (rc = read_message_header(fp, &msg->length, &msg->type)) )
		return rc;

	if (msg->type == ZIOMON_DACC_GARBAGE_MSG) {
		fseek(fp, msg->length, SEEK_CUR);
		msg->data = NULL;
	}
	else {
		msg->data = malloc(msg->length);
		if (fread(msg->data, msg->length, 1, fp) != 1) {
			fprintf(stderr, "%s: Error reading %u Bytes message"
				" content\n", toolname, msg->length);
			return -1;
		}
		if (ver == DATA_MGR_V2 && msgid_blkiomon != IS_NO_BLKIOMON_MSG
		    && (msg->type == IS_BLKIOMON_MSG || msg->type == msgid_blkiomon))
			conv_blkiomon_v2_to_v3(msg);
	}

	return 0;
}


static int read_message_preview(FILE *fp, struct message_preview *msg,
				struct file_header *f_hdr)
{
	int rc;

	msg->pos = ftell(fp);
	if ( (rc = read_message_header(fp, &msg->length, &msg->type)) )
		return rc;

	/* Eventually read timestamp and fforward to next msg */
	if (msg->type != ZIOMON_DACC_GARBAGE_MSG) {
		/* per convention, the first 8 bytes of the actual message
		 * is the timestamp. */
		assert(msg->length >= 8);
		if (fread(&msg->timestamp, 8, 1, fp) != 1) {
			fprintf(stderr, "%s: Error reading"
				" message timestamp\n", toolname);
			return -1;
		}
		swap_64(msg->timestamp);
		fseek(fp, msg->length - 8, SEEK_CUR);
		msg->is_blkiomon_v2 = (f_hdr->version == DATA_MGR_V2
				       && msg->type == f_hdr->msgid_blkiomon);
	}
	else
		fseek(fp, msg->length, SEEK_CUR);

	return 0;
}


/**
 * Add msg to the bunch of aggregated messages.
 */
static int aggregate_message(FILE *fp, struct message ***del_msg,
			     int *num_del_msg, struct file_header *f_hdr)
{
	struct message tmp_msg;
	long cur_pos = ftell(fp);
	struct message **tmp = *del_msg;
	int i;

	/* read message and rewind */
	if (read_message(fp, &tmp_msg, f_hdr->version, f_hdr->msgid_blkiomon))
		return -1;
	fseek(fp, cur_pos, SEEK_SET);

	/* append the message that we read to the array */
	if (tmp_msg.type != ZIOMON_DACC_GARBAGE_MSG) {
		*del_msg = malloc((*num_del_msg + 1)*sizeof(struct message*));
		for (i = 0; i < *num_del_msg; ++i)
			(*del_msg)[i] = tmp[i];
		if (*num_del_msg > 0)
			free(tmp);
		// add new msg at end
		(*del_msg)[*num_del_msg] = malloc(sizeof(struct message));
		*(*del_msg)[*num_del_msg] = tmp_msg;
		(*num_del_msg)++;
	}

	return 0;
}


/**
 * Calculate next message's size, including header, then rewind to current position
 */
static int get_next_msg_size(FILE *fp, int *length) {
	if (fread(length, 4, 1, fp) != 1)
		return -1;
	fseek(fp, -4, SEEK_CUR);
	swap_32(*length);
	(*length) += 8;

	return 0;
}


/**
 * Position at the right place in the file.
 * Returns 0 on success, <0 on error and >0 if an additional gargabe message
 * has to be written after this message. If so, the value returned
 * is length of the garbage message.
 */
static __s32 position_in_file(FILE *fp, struct message *msg,
			      struct file_header *f_hdr,
			      struct message ***del_msg, int *num_del_msg)
{
	__s32 next_msg_length;
	long start_pos;
	__s32 rc = 0;
	long cum_size = 0;

	/*
	 * Are we at the end of the file?
	 */
	if (get_next_msg_size(fp, &next_msg_length)) {
		/* end of file reached - size limit also reached? */
		if ((__u64)ftell(fp) + get_total_msg_size(msg) > f_hdr->size_limit) {
			if (position_at_first_msg(fp) < 0)
				return -1;
			vverbose_msg("end reached - WRAP\n");
			/* we have rewound, but need to aggregate first few messages now,
			   so we better go on... */
		}
		else
			return 0; /* still enough space left - finished */
	}

	/*
	 * Is there still enough room for our message?
	 */
	if ((__u64)ftell(fp) + get_total_msg_size(msg) > f_hdr->size_limit) {
		/* doesn't fit in anymore - wrap around */
		vverbose_msg("msg doesn't fit anymore - WRAP\n");
		/* aggregate final message so all msgs are still in sequence */
		if (get_next_msg_size(fp, &next_msg_length))
			return -1;
		if (aggregate_message(fp, del_msg, num_del_msg, f_hdr))
			return -1;
		if (write_garbage_message(fp, next_msg_length) < 0
		    || position_at_first_msg(fp) < 0)
			return -1;
	}

	/*
	 * Aggregate messages that will be overwritten
	 */
	vverbose_msg("make room for new message\n");
	start_pos = ftell(fp);
	while (cum_size < (long)get_total_msg_size(msg)) {
		if (get_next_msg_size(fp, &next_msg_length)) {
			/*  end of file reached, but we checked before
			    that it will fit in */
			cum_size = get_total_msg_size(msg);; /* prevents garbage msg */
			break;
		}
		if (aggregate_message(fp, del_msg, num_del_msg, f_hdr))
			return -1;
		cum_size += next_msg_length;
		fseek(fp, next_msg_length, SEEK_CUR);
		vverbose_msg("    read msg, cum_size=%ld, needed=%d\n", cum_size, get_total_msg_size(msg));
	}
	if (cum_size - get_total_msg_size(msg) > 0
	    && cum_size - get_total_msg_size(msg) < 8) {
		/* garbage message required, but even minimum size
		   would overwrite start of next msg - aggregate one more! */
		if (get_next_msg_size(fp, &next_msg_length)) {
			/* corner case: we are at EOF, but our message
			   doesn't delete everything from the previous one.
			   So we write the minimum garbage possible*/
			vverbose_msg("garbage needed, but end of file reached\n");
			cum_size += 8;
		}
		else {
			vverbose_msg("garbage needed, but doesn't fit - aggregate one more\n");
			if (aggregate_message(fp, del_msg, num_del_msg, f_hdr))
				return -1;
			cum_size += next_msg_length;
		}
	}

	rc = cum_size - get_total_msg_size(msg);
	fseek(fp, start_pos, SEEK_SET);

	return rc;
}


static int write_message(FILE *fp, struct message *msg)
{
	vverbose_msg("write msg at pos=%ld, total size=%d\n", ftell(fp),
		     get_total_msg_size(msg));
	swap_msg_header(msg);
	if (!fwrite(&msg->length, 4, 1, fp)) {
		fprintf(stderr, "%s: Writing of length"
			" failed\n", toolname);
		swap_msg_header(msg);
		return -1;
	}
	if (!fwrite(&msg->type, 4, 1, fp)) {
		fprintf(stderr, "%s: Writing of type failed\n", toolname);
		swap_msg_header(msg);
		return -2;
	}
	swap_msg_header(msg);

	if (!fwrite(msg->data, msg->length, 1, fp)) {
		fprintf(stderr, "%s: Writing of message data"
			" failed\n", toolname);
		return -3;
	}

	return 0;
}


int add_msg(FILE *fp, struct message *msg, struct file_header *f_hdr,
	    struct message ***del_msg, int *num_del_msg)
{
	__s32 add_garbage;
	long cur_pos, old_pos = ftell(fp);

	*num_del_msg = 0;
	add_garbage = position_in_file(fp, msg, f_hdr, del_msg, num_del_msg);
	if (add_garbage < 0)
		return -1;

	if (write_message(fp, msg) < 0)
		return -2;

	if (add_garbage > 0) {
		cur_pos = ftell(fp);
		if (write_garbage_message(fp, add_garbage) < 0)
			return -3;
		fseek(fp, cur_pos, SEEK_SET);
	}

	f_hdr->end_time = *(__u64*)(msg->data);
	swap_64(f_hdr->end_time);	/* msg content is BE by convention */
	cur_pos = ftell(fp);
	if (f_hdr->first_msg_offset != 0 || cur_pos < old_pos)
		f_hdr->first_msg_offset = ftell(fp);
	if (write_f_header(fp, f_hdr))
		return -4;
	fseek(fp, cur_pos, SEEK_SET);

	return 0;
}


int init_file(FILE *fp, struct file_header *f_hdr, long version)
{
	f_hdr->magic = DATA_MGR_MAGIC;
	if (version == 2)
		f_hdr->version = DATA_MGR_V2;
	else if (version == 3)
		f_hdr->version = DATA_MGR_V3;
	else {
		fprintf(stderr, "%s: Unsupported version: %ld\n",
	                        toolname, version);
		return -2;
	}
	f_hdr->first_msg_offset = 0;
	f_hdr->end_time = 0;
	f_hdr->begin_time = 0;

	if (write_f_header(fp, f_hdr))
		return -1;

	return 0;
}


static int check_version(__u32 ver) {
	if (ver != DATA_MGR_V2 && ver != DATA_MGR_V3) {
		fprintf(stderr, "%s: Wrong version: .log data is in version %u"
			" format, while this tool only supports version %u"
			" and %u.\n"
			" Get the matching tool version and try again.\n",
			toolname, ver, DATA_MGR_V2, DATA_MGR_V3);
		return -2;
	}
	
	return 0;
}


static int get_header(FILE *fp, struct file_header *hdr)
{
	rewind(fp);
	if (fread(hdr, sizeof(struct file_header)
		  - sizeof(__u64), 1, fp) != 1) {
		fprintf(stderr, "%s: Could not read header\n", toolname);
		return -1;
	}
	swap_header(hdr);
	if (hdr->magic != DATA_MGR_MAGIC) {
		fprintf(stderr, "%s: Unregocgnized data in .log file.\n",
			toolname);
		return -2;
	}
	if (check_version(hdr->version))
		return -2;
	hdr->begin_time = 0;

	return 0;
}


int open_log_file(FILE **fp, const char *filename, struct file_header *fhdr)
{
	int rc = 0;
	char *fname = NULL;
	struct message_preview msg_prev;

	fname = (char*)malloc(strlen(filename) + strlen(DACC_FILE_EXT_LOG) + 1);
	sprintf(fname, "%s%s", filename, DACC_FILE_EXT_LOG);
	*fp = fopen(fname, "r");
	if (!*fp) {
		fprintf(stderr, "%s: Could not open %s"
			" - file not accessible?", toolname, fname);
		rc = 1;
		goto out;
	}
	if (get_header(*fp, fhdr)) {
		rc = -2;
		goto out;
	}
	if (get_next_msg_preview(*fp, &msg_prev, fhdr)) {
		rc = -3;
		goto out;
	}
	rewind_to(*fp, &msg_prev);
	fhdr->begin_time = msg_prev.timestamp;

out:
	free(fname);
	if (rc < 0)
		fclose(*fp);

	return rc;
}


void close_log_file(FILE *fp)
{
	wrapped = -1;
	if (fp)
		fclose(fp);
}


void close_data_files(FILE *fp)
{
#ifndef NDEBUG
	open_count--;
	assert(open_count == 0);
#endif
	close_log_file(fp);
}


static void swap_agg_header(struct aggr_data *hdr)
{
	swap_32(hdr->magic);
	swap_32(hdr->version);
	swap_64(hdr->begin_time);
	swap_64(hdr->num_zfcpdd);
	swap_64(hdr->num_blkiomon);
	swap_64(hdr->end_time);
}


static void conv_agg_header_from_BE(struct aggr_data *hdr) {
	swap_agg_header(hdr);
}


static void conv_agg_header_to_BE(struct aggr_data *hdr) {
	swap_agg_header(hdr);
}


static int read_aggr_file(FILE *fp, struct aggr_data *data)
{
	__u64 i;
	int rc;
	struct message msg;

	rewind(fp);
	if (fread(data, DACC_AGGR_FILE_HDR_LEN, 1, fp) != 1) {
		fprintf(stderr, "%s: Error reading aggregation"
			" content\n", toolname);
		return -1;
	}
	conv_agg_header_from_BE(data);
	if (data->magic != DATA_MGR_MAGIC_AGGR) {
		fprintf(stderr, "%s: Unregocgnized data in .agg file.\n",
			toolname);
		return -1;
	}
	if (check_version(data->version))
		return -1;

	data->util_aggr = NULL;
	if ( (rc = read_message(fp, &msg, data->version, IS_NO_BLKIOMON_MSG)) < 0)
		return -2;

	if (msg.type != ZIOMON_DACC_GARBAGE_MSG) {
		data->util_aggr = malloc(sizeof(struct message));
		*(data->util_aggr) = msg;
	}

	data->ioerr_aggr = NULL;
	if ( (rc = read_message(fp, &msg, data->version, IS_NO_BLKIOMON_MSG)) < 0)
		return -3;
	if (msg.type != ZIOMON_DACC_GARBAGE_MSG) {
		data->ioerr_aggr = malloc(sizeof(struct message));
		*(data->ioerr_aggr) = msg;
	}

	if (data->num_blkiomon > 0) {
		data->blkio_aggr = calloc(data->num_blkiomon, sizeof(struct message*));
		for (i=0; i<data->num_blkiomon; ++i) {
			if ( (rc = read_message(fp, &msg, data->version, IS_BLKIOMON_MSG)) < 0)
				return -4;
			data->blkio_aggr[i] = malloc(sizeof(struct message));
			*(data->blkio_aggr[i]) = msg;
		}
	}
	else {
		/* this _must_ be a garbage message */
		data->blkio_aggr = NULL;
		if ( (rc = read_message(fp, &msg, data->version, IS_NO_BLKIOMON_MSG)) < 0)
			return -1;
	}

	if (data->num_zfcpdd > 0) {
		data->zfcpdd_aggr = calloc(data->num_zfcpdd, sizeof(struct message*));
		for (i=0; i<data->num_zfcpdd; ++i) {
			if ( (rc = read_message(fp, &msg, data->version, IS_BLKIOMON_MSG)) < 0)
				return -4;
			data->zfcpdd_aggr[i] = malloc(sizeof(struct message));
			*(data->zfcpdd_aggr[i]) = msg;
		}
	}
	else {
		/* this _must_ be a garbage message */
		data->zfcpdd_aggr = NULL;
		if ( (rc = read_message(fp, &msg, data->version, IS_NO_BLKIOMON_MSG)) < 0)
			return -1;
	}

	return 0;
}


int open_agg_file(FILE **fp, const char *filename, struct aggr_data *agg)
{
	int rc = 0;
	char *fname = NULL;

	fname = (char*)malloc(strlen(filename) + strlen(DACC_FILE_EXT_AGG) + 1);
	sprintf(fname, "%s%s", filename, DACC_FILE_EXT_AGG);

	/* test the water */
	if (access(fname, F_OK) != 0) {
		rc = 1;
		goto out;
	}
	*fp = fopen(fname, "r");
	if (!*fp) {
		fprintf(stderr, "%s: Could not open %s"
			" - file not accessible?", toolname, fname);
		rc = 1;
		goto out;
	}
	if (read_aggr_file(*fp, agg)) {
		fclose(*fp);
		rc = -1;
		goto out;
	}

out:
	free(fname);

	return rc;
}


void close_agg_file(FILE *fp)
{
	if (fp)
		fclose(fp);
}


static int seek_initial_file_pos(FILE *fp, struct file_header *f_hdr)
{
	int rc = 0;
	long pos;

	if (f_hdr->first_msg_offset)
		pos = f_hdr->first_msg_offset;
	else {
		pos = sizeof(struct file_header) - sizeof(__u64);
		rc = 1;	/* no need to wrap */
	}
	fseek(fp, pos, SEEK_SET);

	return rc;
}


int get_next_msg(FILE *fp, struct message *msg, struct file_header *f_hdr)
{
	int rc;

	if (wrapped < 0)
		wrapped = seek_initial_file_pos(fp, f_hdr);

	do {
		if (f_hdr->first_msg_offset != 0 && wrapped
		    && ftell(fp) >= (long long)f_hdr->first_msg_offset)
			return 1;	/* final msg read */

		rc = read_message(fp, msg, f_hdr->version, f_hdr->msgid_blkiomon);
		if (rc > 0 && !wrapped) {
			position_at_first_msg(fp);
			rc = read_message(fp, msg, f_hdr->version, f_hdr->msgid_blkiomon);
			wrapped++;
		}
	} while (!rc && msg->type == ZIOMON_DACC_GARBAGE_MSG);

	return rc;
}


int get_next_msg_preview(FILE *fp, struct message_preview *msg,
			 struct file_header *f_hdr)
{
	int rc;

	if (wrapped < 0)
		wrapped = seek_initial_file_pos(fp, f_hdr);

	do {
		if (f_hdr->first_msg_offset != 0 && wrapped
		    && ftell(fp) >= (long long)f_hdr->first_msg_offset)
			return 1;	/* final msg read */

		rc = read_message_preview(fp, msg, f_hdr);
		if (rc > 0 && !wrapped) {
			position_at_first_msg(fp);
			rc = read_message_preview(fp, msg, f_hdr);
			wrapped++;
		}
	} while (!rc && msg->type == ZIOMON_DACC_GARBAGE_MSG);

	return rc;
}


void rewind_to(FILE *fp, struct message_preview *msg)
{
	assert(msg->pos > 0);
	fseek(fp, msg->pos, SEEK_SET);
}


int get_complete_msg(FILE *fp, struct message_preview *msg_prev,
		     struct message *msg)
{
	long pos = ftell(fp);
	int rc;

	fseek(fp, msg_prev->pos, SEEK_SET);
	if (msg_prev->is_blkiomon_v2)
		// make sure message is converted
		rc = read_message(fp, msg, DATA_MGR_V2, msg_prev->type);
	else
		// use an arbitrary version != V2
		rc = read_message(fp, msg, DATA_MGR_V3, msg_prev->type);
	fseek(fp, pos, SEEK_SET);

	return rc;
}


void discard_msg(struct message *msg)
{
	if (msg) {
		free(msg->data);
		msg->data = NULL;
	}
}


int write_aggr_file(FILE *fp, struct aggr_data *data)
{
	__u64 i;

	conv_agg_header_to_BE(data);
	rewind(fp);
	i = fwrite(data, DACC_AGGR_FILE_HDR_LEN, 1, fp);
	conv_agg_header_from_BE(data);
	if (i != 1)
		return -1;

	if (data->util_aggr) {
		if (write_message(fp, data->util_aggr))
			return -2;
	}
	else if (write_garbage_message(fp, 8))
			return -3;

	if (data->ioerr_aggr) {
		if (write_message(fp, data->ioerr_aggr))
			return -4;
	}
	else if (write_garbage_message(fp, 8))
		return -5;

	if (data->num_blkiomon > 0) {
		for (i = 0; i<data->num_blkiomon; ++i) {
			if (write_message(fp, data->blkio_aggr[i]))
				return -6;
		}
	}
	else if (write_garbage_message(fp, 8))
		return -7;

	if (data->num_zfcpdd > 0) {
		for (i=0; i<data->num_zfcpdd; ++i) {
			if (write_message(fp, data->zfcpdd_aggr[i]))
				return -8;
		}
	}
	else if (write_garbage_message(fp, 8))
		return -9;

	return 0;
}


void init_aggr_data_struct(struct aggr_data *data)
{
	data->magic = DATA_MGR_MAGIC_AGGR;
	data->version = DATA_MGR_V2;
	data->num_zfcpdd = 0;
	data->num_blkiomon = 0;
	data->end_time = 0;
	data->begin_time = 0;
	data->util_aggr = NULL;
	data->ioerr_aggr = NULL;
	data->blkio_aggr = NULL;
	data->zfcpdd_aggr = NULL;
}


void discard_aggr_data_struct(struct aggr_data *data)
{
	unsigned int i;

	if (data) {
		discard_msg(data->util_aggr);
		discard_msg(data->ioerr_aggr);
		for (i=0; i<data->num_blkiomon; ++i) {
			discard_msg(data->blkio_aggr[i]);
			free(data->blkio_aggr[i]);
		}
		for (i=0; i<data->num_zfcpdd; ++i) {
			discard_msg(data->zfcpdd_aggr[i]);
			free(data->zfcpdd_aggr[i]);
		}
		free(data->util_aggr);
		free(data->ioerr_aggr);
		free(data->blkio_aggr);
		free(data->zfcpdd_aggr);
	}
}


int open_data_files(FILE **fp, const char *filename, struct file_header *f_hdr,
	      struct aggr_data **agg)
{
	struct message_preview msg_prev;
	struct message msg;
	int rc = 0;
	int i;
	__u64 end_of_agg;
	time_t t;

#ifndef NDEBUG
	assert(open_count == 0);
	open_count++;
#endif

	verbose_msg("open data\n");

	/*
	 * Open .agg file if exists
	 */

	*agg = (struct aggr_data*)malloc(sizeof(struct aggr_data));
	if ( (rc = open_agg_file(fp, filename, *agg)) < 0 ) {
		free(*agg);
		return -1;
	}
	if (rc == 0) {
		verbose_msg("  found .agg file\n");
		close_agg_file(*fp);
	}
	else {
		verbose_msg("  no .agg file found\n");
		free(*agg);
		*agg = NULL;
	}

	/*
	 * Open .log file
	 */
	if ( (rc = open_log_file(fp, filename, f_hdr)) )
		return -1;

	/*
	 * Eventually add messages from final frame of .agg file and adjust
	 * respective boundaries
	 */

	if (*agg) {
		conv_aggr_data_msg_data_from_BE(*agg);

		/* We use the first message that we have as the basis to
		   calculate when the final timeframe of the .agg data
		   would have ended. Note that we always add all messages
		   that are interval/2 after that timestamp! */
		end_of_agg = ((*agg)->end_time - (*agg)->begin_time -
			f_hdr->interval_length / 2) % f_hdr->interval_length;
		if (end_of_agg)
			end_of_agg = (*agg)->end_time
				+ (f_hdr->interval_length - end_of_agg);

		i = 0;
		while ( (rc = get_next_msg_preview(*fp, &msg_prev, f_hdr)) == 0
			 && msg_prev.timestamp <= end_of_agg) {
			if (get_complete_msg(*fp, &msg_prev, &msg))
			    return -1;
			rc = add_to_agg(*agg, &msg, f_hdr);
			discard_msg(&msg);
			if (rc)
				return -1;
			++i;
		}
		if (rc < 0) {
			fprintf(stderr, "%s: Could not read"
				" any messages in %s%s\n", toolname, filename,
				DACC_FILE_EXT_LOG);
			return -1;
		}
		// condition of the check impossible to fail, but still...
		if (msg_prev.timestamp > end_of_agg)
			rewind_to(*fp, &msg_prev);

		// finally, adjust boundaries
		(*agg)->end_time = end_of_agg - f_hdr->interval_length / 2;
		f_hdr->begin_time = (*agg)->end_time + f_hdr->interval_length;
		if (verbose > 0) {
			t = (*agg)->end_time;
			verbose_msg("  adjust agg end time to  : %s",
				    ctime(&t));
			t = (*agg)->begin_time;
			verbose_msg("  adjust log begin time to: %s",
				    ctime(&t));
		}

		conv_aggr_data_msg_data_to_BE(*agg);
		verbose_msg("  added %d messages to aggregated structure\n",
			    i);
	}

	verbose_msg("open data finished\n");

	return 0;
}