| 1 |
/* |
| 2 |
* stap_merge.c - systemtap merge program |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Copyright (C) Red Hat Inc, 2005-2007 |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <string.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
static void usage (char *prog) |
| 28 |
{ |
| 29 |
fprintf(stderr, "%s [-v] [-o output_filename] input_files ...\n", prog); |
| 30 |
exit(-1); |
| 31 |
} |
| 32 |
|
| 33 |
#define TIMESTAMP_SIZE (sizeof(int)) |
| 34 |
#define MAX_NR_CPUS 1024 |
| 35 |
|
| 36 |
int main (int argc, char *argv[]) |
| 37 |
{ |
| 38 |
char *buf, *outfile_name = NULL; |
| 39 |
int c, i, j, rc, dropped=0; |
| 40 |
long count=0, min, num[MAX_NR_CPUS] = { 0 }; |
| 41 |
FILE *ofp = NULL; |
| 42 |
FILE *fp[MAX_NR_CPUS] = { 0 }; |
| 43 |
int ncpus, len, verbose = 0; |
| 44 |
int bufsize = 65536; |
| 45 |
|
| 46 |
buf = malloc(bufsize); |
| 47 |
if (buf == NULL) { |
| 48 |
fprintf(stderr, "Memory allocation failed.\n"); |
| 49 |
exit(-2); |
| 50 |
} |
| 51 |
|
| 52 |
while ((c = getopt (argc, argv, "vo:")) != EOF) { |
| 53 |
switch (c) { |
| 54 |
case 'v': |
| 55 |
verbose = 1; |
| 56 |
break; |
| 57 |
case 'o': |
| 58 |
outfile_name = optarg; |
| 59 |
break; |
| 60 |
default: |
| 61 |
usage(argv[0]); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if (optind == argc) |
| 66 |
usage (argv[0]); |
| 67 |
|
| 68 |
i = 0; |
| 69 |
while (optind < argc) { |
| 70 |
if (i >= MAX_NR_CPUS) { |
| 71 |
fprintf(stderr, "too many files (MAX_NR_CPUS=%d)\n", MAX_NR_CPUS); |
| 72 |
return -1; |
| 73 |
} |
| 74 |
fp[i] = fopen(argv[optind++], "r"); |
| 75 |
if (!fp[i]) { |
| 76 |
fprintf(stderr, "error opening file %s.\n", argv[optind - 1]); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
if (fread(buf, 4, 1, fp[i]) != 1) // read magic word |
| 80 |
fprintf(stderr, "warning: erro reading magic word\n"); |
| 81 |
if (fread (buf, TIMESTAMP_SIZE, 1, fp[i])) |
| 82 |
num[i] = *((int *)buf); |
| 83 |
else |
| 84 |
num[i] = 0; |
| 85 |
i++; |
| 86 |
} |
| 87 |
ncpus = i; |
| 88 |
|
| 89 |
if (!outfile_name) |
| 90 |
ofp = stdout; |
| 91 |
else { |
| 92 |
ofp = fopen(outfile_name, "w"); |
| 93 |
if (!ofp) { |
| 94 |
fprintf(stderr, "ERROR: couldn't open output file %s: errcode = %s\n", |
| 95 |
outfile_name, strerror(errno)); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
do { |
| 101 |
min = num[0]; |
| 102 |
j = 0; |
| 103 |
for (i = 1; i < ncpus; i++) { |
| 104 |
if (min == 0 || (num[i] && num[i] < min)) { |
| 105 |
min = num[i]; |
| 106 |
j = i; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (fread(&len, sizeof(int), 1, fp[j])) { |
| 111 |
if (verbose) |
| 112 |
fprintf(stdout, "[CPU:%d, seq=%ld, length=%d]\n", j, min, len); |
| 113 |
if (len > bufsize) { |
| 114 |
bufsize = len; |
| 115 |
if (verbose) fprintf(stderr, "reallocating %d bytes\n", bufsize); |
| 116 |
buf = realloc(buf, bufsize); |
| 117 |
if (buf == NULL) { |
| 118 |
fprintf(stderr, "Memory allocation failed.\n"); |
| 119 |
exit(-2); |
| 120 |
} |
| 121 |
} |
| 122 |
if ((rc = fread(buf, len, 1, fp[j])) <= 0 ) { |
| 123 |
fprintf(stderr, "fread error: got %d\n", rc); |
| 124 |
exit(-3); |
| 125 |
} |
| 126 |
if ((rc = fwrite(buf, len, 1, ofp)) <= 0 ) { |
| 127 |
fprintf(stderr, "fread error: got %d\n", rc); |
| 128 |
exit(-3); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (min && ++count != min) { |
| 133 |
fprintf(stderr, "got %ld. expected %ld\n", min, count); |
| 134 |
dropped += min - count ; |
| 135 |
count = min; |
| 136 |
} |
| 137 |
|
| 138 |
if (fread(buf, 4, 1, fp[i]) != 1) // read magic word |
| 139 |
fprintf(stderr, "warning: erro reading magic word\n"); |
| 140 |
if (fread (buf, TIMESTAMP_SIZE, 1, fp[j])) |
| 141 |
num[j] = *((int *)buf); |
| 142 |
else |
| 143 |
num[j] = 0; |
| 144 |
} while (min); |
| 145 |
|
| 146 |
for (i = 0; i < ncpus; i++) |
| 147 |
fclose (fp[i]); |
| 148 |
fclose (ofp); |
| 149 |
printf ("sequence had %d drops\n", dropped); |
| 150 |
return 0; |
| 151 |
} |