gz-bench.c 4.6 KB
Newer Older
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <zlib.h>

#include "darshan-log-format.h"

#define BUFSIZE (10*1024*1024)

int main(int argc, char **argv)
{
    gzFile file;
    int ret;
    struct darshan_job job;
    struct darshan_file cp_file;
    char tmp_string[1024];
    int no_files_flag = 0;
    void* big_buffer;
    void* gz_buffer;
    int size;
    int num_records;
    int i;
    z_stream tmp_stream;
    double percentage;

    if(argc != 2)
    {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        return(-1);
    }

    file = gzopen(argv[1], "r");
    if(!file)
    {
        perror("gzopen");
        return(-1);
    }
   
    /* read job info */
    ret = gzread(file, &job, sizeof(job));
    if(ret < sizeof(job))
    {
        if(gzeof(file))
        {
            fprintf(stderr, "Error: invalid log file (too short).\n");
            gzclose(file);
            return(-1);
        }
        perror("gzread");
        gzclose(file);
        return(-1);
    }

    /* check version string */
    if(strcmp(job.version_string, CP_VERSION))
    {
        fprintf(stderr, "Error: incompatible darshan file.\n");
        fprintf(stderr, "Error: expected version %s, but got %s\n", CP_VERSION, job.version_string);
        gzclose(file);
        return(-1);
    }

    /* read trailing exe string */
    ret = gzread(file, tmp_string, (CP_EXE_LEN + 1));
    if(ret < (CP_EXE_LEN + 1))
    {
        if(gzeof(file))
        {
            no_files_flag = 1;
        }
        perror("gzread");
        gzclose(file);
        return(-1);
    }

    printf("# size of file statistics: %d bytes\n", sizeof(cp_file));
    printf("# exe: %s\n", tmp_string);
    printf("# uid: %d\n", job.uid);
    printf("# start_time: %ld\n", (long)job.start_time);
    printf("# end_time: %ld\n", (long)job.end_time);
    printf("# nprocs: %d\n", job.nprocs);
    printf("# run time: %ld\n", (long)(job.end_time - job.start_time + 1));
   
    if(no_files_flag)
    {
        /* it looks like the app didn't open any files */
        printf("# no files opened.\n");
        gzclose(file);
        return(0);
    }

    /* allocate buffers to experiment with compression */
    big_buffer = malloc(BUFSIZE);
    if(!big_buffer)
    {
        perror("malloc");
        gzclose(file);
        return(-1);
    }
    gz_buffer = malloc(BUFSIZE);
    if(!gz_buffer)
    {
        perror("malloc");
        gzclose(file);
        return(-1);
    }

    /* read in as many file records as we can */
    size = gzread(file, big_buffer, BUFSIZE);
    if(size < 1 || (size % sizeof(cp_file)))
    {
        fprintf(stderr, "Error: got weird size out of file: %d.\n", size);
        gzclose(file);
        return(-1);
    }

    num_records = size/sizeof(cp_file);
    printf("# number of records available: %d\n", num_records);
    printf("# using gzip default compression settings.\n");
    printf("<records>\t<native size>\t<compressed size>\t<percentage>\n");

    /* loop through and compress various numbers of records */
    /* TODO: we could loop more and time this as well to get timing info
     * while we are at it?
     */
    for(i=1; i<=num_records; i++)
    {
        memset(&tmp_stream, 0, sizeof(tmp_stream));
        
        /* set up state for compression */
        tmp_stream.zalloc = Z_NULL;
        tmp_stream.zfree = Z_NULL;
        tmp_stream.opaque = Z_NULL;

        /* TODO: what about trying different compression/memory settings? */
        ret = deflateInit2(&tmp_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 
            31, 8, Z_DEFAULT_STRATEGY);
        if(ret != Z_OK)
        {
            fprintf(stderr, "Error: deflateInit2() failure.\n");
            gzclose(file);
            return(-1);
        }

        tmp_stream.next_in = big_buffer;
        tmp_stream.avail_in = i*sizeof(cp_file);
        tmp_stream.next_out = gz_buffer;
        tmp_stream.avail_out = BUFSIZE;

        ret = deflate(&tmp_stream, Z_FINISH);
        if(ret != Z_STREAM_END)
        {
            fprintf(stderr, "Error: deflate() failure.\n");
            gzclose(file);
            return(-1);
        }

        /* NOTE: the results of deflate in this example can be appended to 
         * a .gz file and maintain gzip compatibility
         */

        percentage = ((double)(i*sizeof(cp_file)));
        percentage -= tmp_stream.total_out;
        percentage = percentage / (i*sizeof(cp_file));

        printf("%d\t%d\t%lu\t%f\n", i, i*sizeof(cp_file), tmp_stream.total_out,
            percentage);
        
        deflateEnd(&tmp_stream);
    }

    gzclose(file);
    return(0);
}