GitLab maintenance scheduled for Today, 2020-08-11, from 17:00 to 18:00 CT - Services will be unavailable during this time.

darshan-load-mysql.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8
#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>
9 10
#include <getopt.h>
#include <readline/readline.h>
11 12 13 14

#include "darshan-logutils.h"

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

18 19 20 21 22 23
#define OPT_HOST (1)
#define OPT_USER (2)
#define OPT_PASS (3)
#define OPT_DB   (4)
#define OPT_PATH (5)

24
const char *insert_job_fmt  = "insert into %s values('%d','%s','%s','%s',\
25
'%d','%ld','%ld','%d')";
26
const char *insert_mnt_fmt  = "insert into %s values('%d','%d','%lld','%s','%s')";
27
const char *insert_file_fmt = "insert into %s values('%d','%ld','%lld','%d',\
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
'%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;
54
    darshan_fd          dfile = NULL;
55
    int                 ret;
Kevin Harms's avatar
Kevin Harms committed
56
    int                 rc;
57 58 59 60 61 62
    int                 nofiles;
    char                exe[1024];
    char               *base;
    char               *dash;
    char               *username;
    char               *jobid;
63
    char               *sqlstmt = NULL;
64 65
    int                 count;
    int                 i;
66
    int64_t            *devs;
67 68 69 70
    char              **mnts;
    char              **fstypes;
    regex_t             regex;
    regmatch_t          match[1];
71 72
    char               *filepath = NULL;
    int                 prev_rank;
73

Kevin Harms's avatar
Kevin Harms committed
74 75 76
    rc      = CONTWALK;
    count   = 0;

77
    /* Only Process Files */
Kevin Harms's avatar
Kevin Harms committed
78
    if (typeflag != FTW_F) return CONTWALK;
79 80 81 82

    sqlstmt = malloc(MAXSQL);
    if (!sqlstmt)
    {
83 84 85 86 87 88 89 90 91 92 93
        perror("malloc");
        rc = STOPWALK;
        goto exit;
    }

    filepath = strdup(fpath);
    if (!filepath)
    {
        perror("strdup");
        rc = STOPWALK;
        goto exit;
94 95 96 97 98 99 100
    }

    /* Process Log Files */
    dfile = darshan_log_open(fpath);
    if (dfile == NULL)
    {
        perror("darshan_log_open");
Kevin Harms's avatar
Kevin Harms committed
101 102
        rc = CONTWALK;
        goto exit;
103 104 105 106 107 108 109
    }

    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
110 111
        rc = CONTWALK;
        goto exit;
112 113 114 115 116 117 118 119 120
    }

    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
121 122
        rc = CONTWALK;
        goto exit;
123 124
    }

125
    base     = basename(filepath);
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    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
140 141
        rc = STOPWALK;
        goto exit;
142 143 144 145 146 147 148 149
    }

    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
150 151
        rc = STOPWALK;
        goto exit;
152 153 154 155 156 157 158 159 160 161 162 163
    }

    regfree(&regex);

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

    /*
     * Insert Job Record
     */
164
    snprintf(sqlstmt, MAXSQL, insert_job_fmt, "darshan_job_intrepid",
165 166 167 168 169 170 171
        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)
    {
172 173 174 175
        fprintf(stderr, "log not processed: %s [mysql: %d (%s)] : \
jobid=%d start_time=%ld\n",
            fpath, mysql_errno(mysql), mysql_error(mysql),
            atoi(jobid), job.start_time);
Kevin Harms's avatar
Kevin Harms committed
176
        rc = CONTWALK;
177 178 179 180 181 182 183 184 185 186 187
        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
188 189
        rc = STOPWALK;
        goto exit;
190 191 192 193
    }

    for (i=0; (i<count); i++)
    {
194
        snprintf(sqlstmt,MAXSQL,insert_mnt_fmt, "darshan_mountpoints_intrepid",
195
            atoi(jobid), job.start_time, lld(devs[i]), mnts[i], fstypes[i]);
196 197 198 199 200 201 202

        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
203 204
            rc = STOPWALK;
            goto exit;
205 206 207 208 209 210 211 212 213 214
        }
    }

    /*
     * Insert File Records (if present)
     */
    if (!nofiles)
    {
        while ((ret = darshan_log_getfile(dfile, &job, &file)) == 1)
        {
215 216 217 218 219 220 221 222 223 224 225 226
            /*
             * Work around issue where bogus file data was in older logs.
             * Bogus data was files taht were 'stat'd but not opened.
             */
            if (file.rank != -1 && file.rank < prev_rank)
            {
                continue;
            }
            if (file.rank != -1)
                prev_rank = file.rank;

            snprintf(sqlstmt, MAXSQL, insert_file_fmt, "darshan_file_intrepid",
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
                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
389 390
                rc = STOPWALK;
                goto exit;
391 392 393 394 395 396 397 398 399 400 401
            }
        }
    }

exit:
    if (dfile) darshan_log_close(dfile);

    if (count > 0)
    {
        for(i=0; i<count; i++)
        {
Kevin Harms's avatar
Kevin Harms committed
402 403
            if (mnts[i]) free(mnts[i]);
            if (fstypes[i]) free(fstypes[i]);
404
        }
Kevin Harms's avatar
Kevin Harms committed
405 406 407
        if (devs) free(devs);
        if (mnts) free(mnts);
        if (fstypes) free(fstypes);
408 409
    }

410
    if (sqlstmt)
411
    {
412
        free(sqlstmt);
413 414
    }

415
    if (filepath)
416
    {
417
        free(filepath);
418 419
    }
    
Kevin Harms's avatar
Kevin Harms committed
420
    return rc;
421 422 423 424
}

int main (int argc, char **argv)
{
425 426 427 428 429 430 431 432
    char base[256] = "";
    char host[256] = "";
    char user[256] = "";
    char pass[256] = "";
    char db[256]   = "";
    int  ret = 0;

    while(1)
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
        static struct option options[] = {
            {"host", 1, NULL, OPT_HOST},
            {"user", 1, NULL, OPT_USER},
            {"pass", 1, NULL, OPT_PASS},
            {"db",   1, NULL, OPT_DB},
            {"path", 1, NULL, OPT_PATH},
            {NULL,   0, NULL, 0}
        };
        int o = getopt_long(argc, argv, "", options, NULL);

        if (o == -1) break;

        switch(o)
        {
        case OPT_HOST:
            strncpy(host, optarg, sizeof(host));
            break;
        case OPT_USER:
            strncpy(user, optarg, sizeof(user));
            break;
        case OPT_PASS:
            strncpy(pass, optarg, sizeof(pass));
            break;
        case OPT_DB:
            strncpy(db, optarg, sizeof(db));
            break;
        case OPT_PATH:
            strncpy(base, optarg, sizeof(base));
            break;
        }
464 465
    }

466 467 468 469 470 471 472 473
    if (strcmp(pass, "") == 0)
    {
        char *line = readline(NULL);
        if (line)
        {
            strncpy(pass, line, sizeof(pass));
        }
    }
474 475 476 477 478 479 480 481

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

482
    mysql = mysql_real_connect(mysql, host, user, pass, db, 0, NULL, 0);
483 484 485 486 487 488
    if (mysql == NULL)
    {
        fprintf(stderr, "mysql_real_connect");
        exit(-1);
    }

489 490 491
    /* Turn off auto commits, hopefuly for performance sake */
    (void) mysql_autocommit(mysql, 0);

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

499 500 501 502 503 504 505
    ret = mysql_commit(mysql);
    if (ret)
    {
        fprintf(stderr, "mysql: %d (%s)\n", mysql_errno(mysql),
            mysql_error(mysql));
    }

506 507 508 509
    mysql_close(mysql);

    return 0;
}