darshan-load-mysql.c 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ftw.h>
#include <libgen.h>
#include <mysql.h>
#include <regex.h>
#include <sys/types.h>

#include "darshan-logutils.h"

#define MAXSQL (1024*1024)
Kevin Harms's avatar
Kevin Harms committed
13 14
#define STOPWALK (1)
#define CONTWALK (0)
15 16 17

const char *insert_job_fmt  = "insert into %s values('%d','%s','%s','%s',\
'%d','%d','%d','%d')";
18
const char *insert_mnt_fmt  = "insert into %s values('%d','%d','%lld','%s','%s')";
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
const char *insert_file_fmt = "insert into %s values('%d','%d','%lld','%d',\
'%s',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld','%lld',\
'%.16lf','%.16lf','%.16lf','%.16lf','%.16lf',\
'%.16lf','%.16lf','%.16lf','%.16lf','%.16lf',\
'%.16lf','%.16lf','%.16lf','%.16lf')";

MYSQL *mysql = NULL;
int debug = 0;

int tree_walk (const char *fpath, const struct stat *sb, int typeflag)
{
    struct darshan_file file;
    struct darshan_job  job;
    darshan_fd          dfile;
    int                 ret;
Kevin Harms's avatar
Kevin Harms committed
48
    int                 rc;
49 50 51 52 53 54 55 56 57
    int                 nofiles;
    char                exe[1024];
    char               *base;
    char               *dash;
    char               *username;
    char               *jobid;
    char               *sqlstmt;
    int                 count;
    int                 i;
58
    int64_t            *devs;
59 60 61 62 63
    char              **mnts;
    char              **fstypes;
    regex_t             regex;
    regmatch_t          match[1];

Kevin Harms's avatar
Kevin Harms committed
64 65 66
    rc      = CONTWALK;
    count   = 0;

67
    /* Only Process Files */
Kevin Harms's avatar
Kevin Harms committed
68
    if (typeflag != FTW_F) return CONTWALK;
69 70 71 72

    sqlstmt = malloc(MAXSQL);
    if (!sqlstmt)
    {
Kevin Harms's avatar
Kevin Harms committed
73
        return STOPWALK;
74 75 76 77 78 79 80
    }

    /* Process Log Files */
    dfile = darshan_log_open(fpath);
    if (dfile == NULL)
    {
        perror("darshan_log_open");
Kevin Harms's avatar
Kevin Harms committed
81 82
        rc = CONTWALK;
        goto exit;
83 84 85 86 87 88 89
    }

    ret = darshan_log_getjob(dfile, &job);
    if (ret < 0)
    {
        perror("darshan_log_getjob");
        fprintf(stderr, "%s\n", fpath);
Kevin Harms's avatar
Kevin Harms committed
90 91
        rc = CONTWALK;
        goto exit;
92 93 94 95 96 97 98 99 100
    }

    memset(exe, 0, sizeof(exe));

    ret = darshan_log_getexe(dfile, exe, &nofiles);
    if (ret < 0)
    {
        perror("darshan_log_getexe");
        fprintf(stderr, "%s\n", fpath);
Kevin Harms's avatar
Kevin Harms committed
101 102
        rc = CONTWALK;
        goto exit;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    }

    base     = basename(fpath);
    username = base;
    dash     = index(base, '_');
    *dash    = '\0';
    jobid    = dash+1;

    /*
     * Find jobid for log file name
     */
    ret = regcomp(&regex, "_id[[:digit:]]+_", REG_EXTENDED);
    if (ret != 0)
    {
        char buf[256];
        regerror(ret, &regex, buf, sizeof(buf));
        fprintf(stderr, "regcomp: %s\n", buf);
Kevin Harms's avatar
Kevin Harms committed
120 121
        rc = STOPWALK;
        goto exit;
122 123 124 125 126 127 128 129
    }

    ret = regexec(&regex, jobid, 1, match, 0);
    if (ret != 0)
    {
        char buf[256];
        regerror(ret, &regex, buf, sizeof(buf));
        fprintf(stderr, "regexec: %s\n", buf);
Kevin Harms's avatar
Kevin Harms committed
130 131
        rc = STOPWALK;
        goto exit;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    }

    regfree(&regex);

    dash   = jobid;
    jobid += (match[0].rm_so + 3);
    dash  += (match[0].rm_eo - 1);
    *dash  = 0;

    /*
     * Insert Job Record
     */
    snprintf(sqlstmt, MAXSQL, insert_job_fmt, "darshan_job_surveyor",
        atoi(jobid), username, job.version_string, exe, job.uid,
        job.start_time, job.end_time, job.nprocs);

    if (debug) printf("sql: %s\n", sqlstmt);
    ret = mysql_query(mysql, sqlstmt);
    if (ret)
    {
        fprintf(stderr, "log not processed: %s [mysql: %d (%s)\n",
            fpath, mysql_errno(mysql), mysql_error(mysql));
Kevin Harms's avatar
Kevin Harms committed
154
        rc = CONTWALK;
155 156 157 158 159 160 161 162 163 164 165
        goto exit;
    }

    /*
     * Insert MountPoint Record (if present)
     */
    ret = darshan_log_getmounts(dfile,&devs,&mnts,&fstypes,&count,&nofiles);
    if (ret < 0)
    {
        perror("darshan_log_getmounts");
        fprintf(stderr, "%s\n", fpath);
Kevin Harms's avatar
Kevin Harms committed
166 167
        rc = STOPWALK;
        goto exit;
168 169 170 171 172
    }

    for (i=0; (i<count); i++)
    {
        snprintf(sqlstmt,MAXSQL,insert_mnt_fmt, "darshan_mountpoints_surveyor",
173
            atoi(jobid), job.start_time, lld(devs[i]), mnts[i], fstypes[i]);
174 175 176 177 178 179 180

        if (debug) printf("sql: %s\n", sqlstmt);
        ret = mysql_query(mysql, sqlstmt);
        if (ret)
        {
            fprintf(stderr, "mysql: %d (%s)\n", mysql_errno(mysql),
                mysql_error(mysql));
Kevin Harms's avatar
Kevin Harms committed
181 182
            rc = STOPWALK;
            goto exit;
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
        }
    }

    /*
     * Insert File Records (if present)
     */
    if (!nofiles)
    {
        while ((ret = darshan_log_getfile(dfile, &job, &file)) == 1)
        {
            snprintf(sqlstmt, MAXSQL, insert_file_fmt, "darshan_file_surveyor",
                atoi(jobid), job.start_time, file.hash, file.rank, file.name_suffix,
                file.counters[CP_INDEP_OPENS],
                file.counters[CP_COLL_OPENS],
                file.counters[CP_INDEP_READS],
                file.counters[CP_INDEP_WRITES],
                file.counters[CP_COLL_READS],
                file.counters[CP_COLL_WRITES],
                file.counters[CP_SPLIT_READS],
                file.counters[CP_SPLIT_WRITES],
                file.counters[CP_NB_READS],
                file.counters[CP_NB_WRITES],
                file.counters[CP_SYNCS],
                file.counters[CP_POSIX_READS],
                file.counters[CP_POSIX_WRITES],
                file.counters[CP_POSIX_OPENS],
                file.counters[CP_POSIX_SEEKS],
                file.counters[CP_POSIX_STATS],
                file.counters[CP_POSIX_MMAPS],
                file.counters[CP_POSIX_FREADS],
                file.counters[CP_POSIX_FWRITES],
                file.counters[CP_POSIX_FOPENS],
                file.counters[CP_POSIX_FSEEKS],
                file.counters[CP_POSIX_FSYNCS],
                file.counters[CP_POSIX_FDSYNCS],
                file.counters[CP_INDEP_NC_OPENS],
                file.counters[CP_COLL_NC_OPENS],
                file.counters[CP_HDF5_OPENS],
                file.counters[CP_COMBINER_NAMED],
                file.counters[CP_COMBINER_DUP],
                file.counters[CP_COMBINER_CONTIGUOUS],
                file.counters[CP_COMBINER_VECTOR],
                file.counters[CP_COMBINER_HVECTOR_INTEGER],
                file.counters[CP_COMBINER_HVECTOR],
                file.counters[CP_COMBINER_INDEXED],
                file.counters[CP_COMBINER_HINDEXED_INTEGER],
                file.counters[CP_COMBINER_HINDEXED],
                file.counters[CP_COMBINER_INDEXED_BLOCK],
                file.counters[CP_COMBINER_STRUCT_INTEGER],
                file.counters[CP_COMBINER_STRUCT],
                file.counters[CP_COMBINER_SUBARRAY],
                file.counters[CP_COMBINER_DARRAY],
                file.counters[CP_COMBINER_F90_REAL],
                file.counters[CP_COMBINER_F90_COMPLEX],
                file.counters[CP_COMBINER_F90_INTEGER],
                file.counters[CP_COMBINER_RESIZED],
                file.counters[CP_HINTS],
                file.counters[CP_VIEWS],
                file.counters[CP_MODE],
                file.counters[CP_BYTES_READ],
                file.counters[CP_BYTES_WRITTEN],
                file.counters[CP_MAX_BYTE_READ],
                file.counters[CP_MAX_BYTE_WRITTEN],
                file.counters[CP_CONSEC_READS],
                file.counters[CP_CONSEC_WRITES],
                file.counters[CP_SEQ_READS],
                file.counters[CP_SEQ_WRITES],
                file.counters[CP_RW_SWITCHES],
                file.counters[CP_MEM_NOT_ALIGNED],
                file.counters[CP_MEM_ALIGNMENT],
                file.counters[CP_FILE_NOT_ALIGNED],
                file.counters[CP_FILE_ALIGNMENT],
                file.counters[CP_MAX_READ_TIME_SIZE],
                file.counters[CP_MAX_WRITE_TIME_SIZE],
                file.counters[CP_SIZE_READ_0_100],
                file.counters[CP_SIZE_READ_100_1K],
                file.counters[CP_SIZE_READ_1K_10K],
                file.counters[CP_SIZE_READ_10K_100K],
                file.counters[CP_SIZE_READ_100K_1M],
                file.counters[CP_SIZE_READ_1M_4M],
                file.counters[CP_SIZE_READ_4M_10M],
                file.counters[CP_SIZE_READ_10M_100M],
                file.counters[CP_SIZE_READ_100M_1G],
                file.counters[CP_SIZE_READ_1G_PLUS],
                file.counters[CP_SIZE_WRITE_0_100],
                file.counters[CP_SIZE_WRITE_100_1K],
                file.counters[CP_SIZE_WRITE_1K_10K],
                file.counters[CP_SIZE_WRITE_10K_100K],
                file.counters[CP_SIZE_WRITE_100K_1M],
                file.counters[CP_SIZE_WRITE_1M_4M],
                file.counters[CP_SIZE_WRITE_4M_10M],
                file.counters[CP_SIZE_WRITE_10M_100M],
                file.counters[CP_SIZE_WRITE_100M_1G],
                file.counters[CP_SIZE_WRITE_1G_PLUS],
                file.counters[CP_SIZE_READ_AGG_0_100],
                file.counters[CP_SIZE_READ_AGG_100_1K],
                file.counters[CP_SIZE_READ_AGG_1K_10K],
                file.counters[CP_SIZE_READ_AGG_10K_100K],
                file.counters[CP_SIZE_READ_AGG_100K_1M],
                file.counters[CP_SIZE_READ_AGG_1M_4M],
                file.counters[CP_SIZE_READ_AGG_4M_10M],
                file.counters[CP_SIZE_READ_AGG_10M_100M],
                file.counters[CP_SIZE_READ_AGG_100M_1G],
                file.counters[CP_SIZE_READ_AGG_1G_PLUS],
                file.counters[CP_SIZE_WRITE_AGG_0_100],
                file.counters[CP_SIZE_WRITE_AGG_100_1K],
                file.counters[CP_SIZE_WRITE_AGG_1K_10K],
                file.counters[CP_SIZE_WRITE_AGG_10K_100K],
                file.counters[CP_SIZE_WRITE_AGG_100K_1M],
                file.counters[CP_SIZE_WRITE_AGG_1M_4M],
                file.counters[CP_SIZE_WRITE_AGG_4M_10M],
                file.counters[CP_SIZE_WRITE_AGG_10M_100M],
                file.counters[CP_SIZE_WRITE_AGG_100M_1G],
                file.counters[CP_SIZE_WRITE_AGG_1G_PLUS],
                file.counters[CP_EXTENT_READ_0_100],
                file.counters[CP_EXTENT_READ_100_1K],
                file.counters[CP_EXTENT_READ_1K_10K], 
                file.counters[CP_EXTENT_READ_10K_100K],
                file.counters[CP_EXTENT_READ_100K_1M],
                file.counters[CP_EXTENT_READ_1M_4M],
                file.counters[CP_EXTENT_READ_4M_10M],
                file.counters[CP_EXTENT_READ_10M_100M],
                file.counters[CP_EXTENT_READ_100M_1G],
                file.counters[CP_EXTENT_READ_1G_PLUS],
                file.counters[CP_EXTENT_WRITE_0_100],
                file.counters[CP_EXTENT_WRITE_100_1K],
                file.counters[CP_EXTENT_WRITE_1K_10K],
                file.counters[CP_EXTENT_WRITE_10K_100K],
                file.counters[CP_EXTENT_WRITE_100K_1M],
                file.counters[CP_EXTENT_WRITE_1M_4M],
                file.counters[CP_EXTENT_WRITE_4M_10M],
                file.counters[CP_EXTENT_WRITE_10M_100M],
                file.counters[CP_EXTENT_WRITE_100M_1G],
                file.counters[CP_EXTENT_WRITE_1G_PLUS],
                file.counters[CP_STRIDE1_STRIDE],
                file.counters[CP_STRIDE2_STRIDE],
                file.counters[CP_STRIDE3_STRIDE],
                file.counters[CP_STRIDE4_STRIDE],
                file.counters[CP_STRIDE1_COUNT],
                file.counters[CP_STRIDE2_COUNT],
                file.counters[CP_STRIDE3_COUNT],
                file.counters[CP_STRIDE4_COUNT],
                file.counters[CP_ACCESS1_ACCESS],
                file.counters[CP_ACCESS2_ACCESS],
                file.counters[CP_ACCESS3_ACCESS],
                file.counters[CP_ACCESS4_ACCESS],
                file.counters[CP_ACCESS1_COUNT],
                file.counters[CP_ACCESS2_COUNT],
                file.counters[CP_ACCESS3_COUNT],
                file.counters[CP_ACCESS4_COUNT],
                file.counters[CP_DEVICE],
                file.counters[CP_SIZE_AT_OPEN],
                file.fcounters[CP_F_OPEN_TIMESTAMP],
                file.fcounters[CP_F_READ_START_TIMESTAMP],
                file.fcounters[CP_F_WRITE_START_TIMESTAMP],
                file.fcounters[CP_F_CLOSE_TIMESTAMP],
                file.fcounters[CP_F_READ_END_TIMESTAMP],
                file.fcounters[CP_F_WRITE_END_TIMESTAMP],
                file.fcounters[CP_F_POSIX_READ_TIME],
                file.fcounters[CP_F_POSIX_WRITE_TIME],
                file.fcounters[CP_F_POSIX_META_TIME],
                file.fcounters[CP_F_MPI_META_TIME],
                file.fcounters[CP_F_MPI_READ_TIME],
                file.fcounters[CP_F_MPI_WRITE_TIME],
                file.fcounters[CP_F_MAX_READ_TIME],
                file.fcounters[CP_F_MAX_WRITE_TIME]);

            if (debug) printf("sql: %s\n", sqlstmt);
            ret = mysql_query(mysql, sqlstmt);
            if (ret)
            {
                fprintf(stderr, "mysql: %d (%s)\n", mysql_errno(mysql),
                    mysql_error(mysql));
Kevin Harms's avatar
Kevin Harms committed
356 357
                rc = STOPWALK;
                goto exit;
358 359 360 361 362 363 364 365 366 367 368
            }
        }
    }

exit:
    if (dfile) darshan_log_close(dfile);

    if (count > 0)
    {
        for(i=0; i<count; i++)
        {
Kevin Harms's avatar
Kevin Harms committed
369 370
            if (mnts[i]) free(mnts[i]);
            if (fstypes[i]) free(fstypes[i]);
371
        }
Kevin Harms's avatar
Kevin Harms committed
372 373 374
        if (devs) free(devs);
        if (mnts) free(mnts);
        if (fstypes) free(fstypes);
375 376 377 378 379 380 381 382 383 384 385 386 387 388
    }

    ret = mysql_commit(mysql);
    if (ret)
    {
        fprintf(stderr, "mysql: %d (%s)\n", mysql_errno(mysql),
            mysql_error(mysql));
    }

    if (sqlstmt)
    {
        free(sqlstmt);
    }
    
Kevin Harms's avatar
Kevin Harms committed
389
    return rc;
390 391 392 393 394
}

int main (int argc, char **argv)
{
    const char *base;
395 396 397 398
    const char *host   = "db-host";
    const char *user   = "username";
    const char *passwd = "password";
    const char *db     = "darshan";
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
    int         ret = 0;

    if(argc != 2)
    {
        fprintf(stderr, "Error: bad arguments.\n");
        return(-1);
    }

    base = argv[1];

    mysql = mysql_init(NULL);
    if (mysql == NULL)
    {
        fprintf(stderr, "mysql_init failed");
        exit(-1);
    }

    mysql = mysql_real_connect(mysql, host, user, passwd, db, 0, NULL, 0);
    if (mysql == NULL)
    {
        fprintf(stderr, "mysql_real_connect");
        exit(-1);
    }

    ret = ftw(base, tree_walk, 512);
    if(ret != 0)
    {
        fprintf(stderr, "Error: failed to walk path: %s\n", base);
        return(-1);
    }

    mysql_close(mysql);

    return 0;
}