darshan-stdio-logutils.c 7.84 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
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
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#define _GNU_SOURCE
#include "darshan-util-config.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include "darshan-logutils.h"

/* integer counter name strings for the STDIO module */
#define X(a) #a,
char *stdio_counter_names[] = {
    STDIO_COUNTERS
};

/* floating point counter name strings for the STDIO module */
char *stdio_f_counter_names[] = {
    STDIO_F_COUNTERS
};
#undef X

/* prototypes for each of the STDIO module's logutil functions */
35
static int darshan_log_get_stdio_record(darshan_fd fd, void* stdio_buf);
Philip Carns's avatar
Philip Carns committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
static int darshan_log_put_stdio_record(darshan_fd fd, void* stdio_buf, int ver);
static void darshan_log_print_stdio_record(void *file_rec,
    char *file_name, char *mnt_pt, char *fs_type, int ver);
static void darshan_log_print_stdio_description(void);
static void darshan_log_print_stdio_record_diff(void *file_rec1, char *file_name1,
    void *file_rec2, char *file_name2);

/* structure storing each function needed for implementing the darshan
 * logutil interface. these functions are used for reading, writing, and
 * printing module data in a consistent manner.
 */
struct darshan_mod_logutil_funcs stdio_logutils =
{
    .log_get_record = &darshan_log_get_stdio_record,
    .log_put_record = &darshan_log_put_stdio_record,
    .log_print_record = &darshan_log_print_stdio_record,
    .log_print_description = &darshan_log_print_stdio_description,
    .log_print_diff = &darshan_log_print_stdio_record_diff
};

/* retrieve a STDIO record from log file descriptor 'fd', storing the
57
58
 * buffer in 'stdio_buf'. Return 1 on successful record read, 0 on no 
 * more data, and -1 on error.
Philip Carns's avatar
Philip Carns committed
59
 */
60
static int darshan_log_get_stdio_record(darshan_fd fd, void* stdio_buf)
Philip Carns's avatar
Philip Carns committed
61
{
62
    struct darshan_stdio_file *file;
Philip Carns's avatar
Philip Carns committed
63
64
65
66
    int i;
    int ret;

    /* read a STDIO module record from the darshan log file */
67
68
    ret = darshan_log_get_mod(fd, DARSHAN_STDIO_MOD, stdio_buf,
        sizeof(struct darshan_stdio_file));
Philip Carns's avatar
Philip Carns committed
69
70
    if(ret < 0)
        return(-1);
71
    else if(ret < sizeof(struct darshan_stdio_file))
Philip Carns's avatar
Philip Carns committed
72
73
74
75
        return(0);
    else
    {
        /* if the read was successful, do any necessary byte-swapping */
76
        file = (struct darshan_stdio_file *)stdio_buf;
Philip Carns's avatar
Philip Carns committed
77
78
79
        if(fd->swap_flag)
        {
            /* swap bytes if necessary */
80
81
            DARSHAN_BSWAP64(&file->base_rec.id);
            DARSHAN_BSWAP64(&file->base_rec.rank);
Philip Carns's avatar
Philip Carns committed
82
            for(i=0; i<STDIO_NUM_INDICES; i++)
83
                DARSHAN_BSWAP64(&file->counters[i]);
Philip Carns's avatar
Philip Carns committed
84
            for(i=0; i<STDIO_F_NUM_INDICES; i++)
85
                DARSHAN_BSWAP64(&file->fcounters[i]);
Philip Carns's avatar
Philip Carns committed
86
87
88
89
90
91
92
93
94
95
96
        }

        return(1);
    }
}

/* write the STDIO record stored in 'stdio_buf' to log file descriptor 'fd'.
 * Return 0 on success, -1 on failure
 */
static int darshan_log_put_stdio_record(darshan_fd fd, void* stdio_buf, int ver)
{
97
    struct darshan_stdio_file *rec = (struct darshan_stdio_file *)stdio_buf;
Philip Carns's avatar
Philip Carns committed
98
99
100
    int ret;

    /* append STDIO record to darshan log file */
101
102
    ret = darshan_log_put_mod(fd, DARSHAN_STDIO_MOD, rec,
        sizeof(struct darshan_stdio_file), ver);
Philip Carns's avatar
Philip Carns committed
103
104
105
106
107
108
109
110
111
112
113
    if(ret < 0)
        return(-1);

    return(0);
}

/* print all I/O data record statistics for the given STDIO record */
static void darshan_log_print_stdio_record(void *file_rec, char *file_name,
    char *mnt_pt, char *fs_type, int ver)
{
    int i;
114
115
    struct darshan_stdio_file *stdio_rec =
        (struct darshan_stdio_file *)file_rec;
Philip Carns's avatar
Philip Carns committed
116
117
118
119
120
121

    /* print each of the integer and floating point counters for the STDIO module */
    for(i=0; i<STDIO_NUM_INDICES; i++)
    {
        /* macro defined in darshan-logutils.h */
        DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
122
            stdio_rec->base_rec.rank, stdio_rec->base_rec.id, stdio_counter_names[i],
Philip Carns's avatar
Philip Carns committed
123
124
125
126
127
128
129
            stdio_rec->counters[i], file_name, mnt_pt, fs_type);
    }

    for(i=0; i<STDIO_F_NUM_INDICES; i++)
    {
        /* macro defined in darshan-logutils.h */
        DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
130
            stdio_rec->base_rec.rank, stdio_rec->base_rec.id, stdio_f_counter_names[i],
Philip Carns's avatar
Philip Carns committed
131
132
133
134
135
136
137
138
139
140
            stdio_rec->fcounters[i], file_name, mnt_pt, fs_type);
    }

    return;
}

/* print out a description of the STDIO module record fields */
static void darshan_log_print_stdio_description()
{
    printf("\n# description of STDIO counters:\n");
Philip Carns's avatar
Philip Carns committed
141
    printf("#   STDIO_{OPENS|WRITES|READS|SEEKS|FLUSHES} are types of operations.\n");
142
143
144
145
146
    printf("#   STDIO_BYTES_*: total bytes read and written.\n");
    printf("#   STDIO_MAX_BYTE_*: highest offset byte read and written.\n");
    printf("#   STDIO_F_*_START_TIMESTAMP: timestamp of the first call to that type of function.\n");
    printf("#   STDIO_F_*_END_TIMESTAMP: timestamp of the completion of the last call to that type of function.\n");
    printf("#   STDIO_F_*_TIME: cumulative time spent in different types of functions.\n");
Philip Carns's avatar
Philip Carns committed
147
148
149
150
151
152
153

    return;
}

static void darshan_log_print_stdio_record_diff(void *file_rec1, char *file_name1,
    void *file_rec2, char *file_name2)
{
154
155
    struct darshan_stdio_file *file1 = (struct darshan_stdio_file *)file_rec1;
    struct darshan_stdio_file *file2 = (struct darshan_stdio_file *)file_rec2;
Philip Carns's avatar
Philip Carns committed
156
157
158
159
160
161
162
163
164
165
    int i;

    /* NOTE: we assume that both input records are the same module format version */

    for(i=0; i<STDIO_NUM_INDICES; i++)
    {
        if(!file2)
        {
            printf("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
166
                file1->base_rec.rank, file1->base_rec.id, stdio_counter_names[i],
Philip Carns's avatar
Philip Carns committed
167
168
169
170
171
172
173
                file1->counters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
174
                file2->base_rec.rank, file2->base_rec.id, stdio_counter_names[i],
Philip Carns's avatar
Philip Carns committed
175
176
177
178
179
180
                file2->counters[i], file_name2, "", "");
        }
        else if(file1->counters[i] != file2->counters[i])
        {
            printf("- ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
181
                file1->base_rec.rank, file1->base_rec.id, stdio_counter_names[i],
Philip Carns's avatar
Philip Carns committed
182
183
184
                file1->counters[i], file_name1, "", "");
            printf("+ ");
            DARSHAN_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
185
                file2->base_rec.rank, file2->base_rec.id, stdio_counter_names[i],
Philip Carns's avatar
Philip Carns committed
186
187
188
189
190
191
192
193
194
195
                file2->counters[i], file_name2, "", "");
        }
    }

    for(i=0; i<STDIO_F_NUM_INDICES; i++)
    {
        if(!file2)
        {
            printf("- ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
196
                file1->base_rec.rank, file1->base_rec.id, stdio_f_counter_names[i],
Philip Carns's avatar
Philip Carns committed
197
198
199
200
201
202
203
                file1->fcounters[i], file_name1, "", "");

        }
        else if(!file1)
        {
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
204
                file2->base_rec.rank, file2->base_rec.id, stdio_f_counter_names[i],
Philip Carns's avatar
Philip Carns committed
205
206
207
208
209
210
                file2->fcounters[i], file_name2, "", "");
        }
        else if(file1->fcounters[i] != file2->fcounters[i])
        {
            printf("- ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
211
                file1->base_rec.rank, file1->base_rec.id, stdio_f_counter_names[i],
Philip Carns's avatar
Philip Carns committed
212
213
214
                file1->fcounters[i], file_name1, "", "");
            printf("+ ");
            DARSHAN_F_COUNTER_PRINT(darshan_module_names[DARSHAN_STDIO_MOD],
215
                file2->base_rec.rank, file2->base_rec.id, stdio_f_counter_names[i],
Philip Carns's avatar
Philip Carns committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
                file2->fcounters[i], file_name2, "", "");
        }
    }

    return;
}


/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */