root/orte/mca/sstore/base/sstore_base_fns.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. orte_sstore_base_local_snapshot_info_construct
  2. orte_sstore_base_local_snapshot_info_destruct
  3. orte_sstore_base_global_snapshot_info_construct
  4. orte_sstore_base_global_snapshot_info_destruct
  5. orte_sstore_base_tool_request_restart_handle
  6. orte_sstore_base_tool_get_attr
  7. orte_sstore_base_get_global_snapshot_ref
  8. orte_sstore_base_convert_key_to_string
  9. orte_sstore_base_convert_string_to_key
  10. orte_sstore_base_get_all_snapshots
  11. orte_sstore_base_extract_global_metadata
  12. orte_sstore_base_find_largest_seq_num
  13. orte_sstore_base_find_all_seq_nums
  14. orte_sstore_base_metadata_read_next_seq_num
  15. orte_sstore_base_metadata_seek_to_seq_num
  16. orte_sstore_base_metadata_read_next_token

   1 /*
   2  * Copyright (c)      2010 The Trustees of Indiana University.
   3  *                         All rights reserved.
   4  * Copyright (c) 2004-2011 The University of Tennessee and The University
   5  *                         of Tennessee Research Foundation.  All rights
   6  *                         reserved.
   7  * Copyright (c) 2018      Intel, Inc.  All rights reserved.
   8  * $COPYRIGHT$
   9  *
  10  * Additional copyrights may follow
  11  *
  12  * $HEADER$
  13  */
  14 
  15 #include "orte_config.h"
  16 
  17 #include <string.h>
  18 #ifdef HAVE_SYS_TYPES_H
  19 #include <sys/types.h>
  20 #endif
  21 #ifdef HAVE_UNISTD_H
  22 #include <unistd.h>
  23 #endif
  24 #ifdef HAVE_SYS_STAT_H
  25 #include <sys/stat.h>
  26 #endif /* HAVE_SYS_STAT_H */
  27 #ifdef HAVE_DIRENT_H
  28 #include <dirent.h>
  29 #endif /* HAVE_DIRENT_H */
  30 #include <time.h>
  31 
  32 #include "orte/constants.h"
  33 
  34 #include "orte/mca/mca.h"
  35 #include "opal/mca/base/base.h"
  36 #include "opal/util/argv.h"
  37 #include "opal/mca/crs/base/base.h"
  38 
  39 #include "orte/mca/errmgr/errmgr.h"
  40 #include "orte/runtime/orte_globals.h"
  41 #include "orte/util/proc_info.h"
  42 
  43 #include "orte/mca/sstore/sstore.h"
  44 #include "orte/mca/sstore/base/base.h"
  45 
  46 /******************
  47  * Local Functions
  48  ******************/
  49 
  50 /******************
  51  * Object Stuff
  52  ******************/
  53 OBJ_CLASS_INSTANCE(orte_sstore_base_local_snapshot_info_t,
  54                    opal_list_item_t,
  55                    orte_sstore_base_local_snapshot_info_construct,
  56                    orte_sstore_base_local_snapshot_info_destruct);
  57 
  58 void orte_sstore_base_local_snapshot_info_construct(orte_sstore_base_local_snapshot_info_t *snapshot)
  59 {
  60     snapshot->process_name.jobid  = 0;
  61     snapshot->process_name.vpid   = 0;
  62 
  63     snapshot->crs_comp = NULL;
  64     snapshot->compress_comp    = NULL;
  65     snapshot->compress_postfix = NULL;
  66 
  67     snapshot->start_time = NULL;
  68     snapshot->end_time   = NULL;
  69 }
  70 
  71 void orte_sstore_base_local_snapshot_info_destruct( orte_sstore_base_local_snapshot_info_t *snapshot)
  72 {
  73     snapshot->process_name.jobid  = 0;
  74     snapshot->process_name.vpid   = 0;
  75 
  76     if( NULL != snapshot->crs_comp ) {
  77         free(snapshot->crs_comp);
  78         snapshot->crs_comp = NULL;
  79     }
  80 
  81     if( NULL != snapshot->compress_comp ) {
  82         free(snapshot->compress_comp);
  83         snapshot->compress_comp = NULL;
  84     }
  85 
  86     if( NULL != snapshot->compress_postfix ) {
  87         free(snapshot->compress_postfix);
  88         snapshot->compress_postfix = NULL;
  89     }
  90 
  91     if( NULL != snapshot->start_time ) {
  92         free(snapshot->start_time);
  93         snapshot->start_time = NULL;
  94     }
  95 
  96     if( NULL != snapshot->end_time ) {
  97         free(snapshot->end_time);
  98         snapshot->end_time = NULL;
  99     }
 100 }
 101 
 102 OBJ_CLASS_INSTANCE(orte_sstore_base_global_snapshot_info_t,
 103                    opal_list_item_t,
 104                    orte_sstore_base_global_snapshot_info_construct,
 105                    orte_sstore_base_global_snapshot_info_destruct);
 106 
 107 void orte_sstore_base_global_snapshot_info_construct(orte_sstore_base_global_snapshot_info_t *snapshot)
 108 {
 109     OBJ_CONSTRUCT(&(snapshot->local_snapshots), opal_list_t);
 110 
 111     snapshot->ss_handle  = ORTE_SSTORE_HANDLE_INVALID;
 112 
 113     snapshot->start_time = NULL;
 114     snapshot->end_time   = NULL;
 115 
 116     snapshot->seq_num = -1;
 117 
 118     snapshot->num_seqs = 0;
 119     snapshot->all_seqs = NULL;
 120     snapshot->basedir = NULL;
 121     snapshot->reference = NULL;
 122     snapshot->amca_param = NULL;
 123     snapshot->tune_param = NULL;
 124     snapshot->metadata_filename = NULL;
 125 }
 126 
 127 void orte_sstore_base_global_snapshot_info_destruct( orte_sstore_base_global_snapshot_info_t *snapshot)
 128 {
 129     opal_list_item_t* item = NULL;
 130 
 131     while (NULL != (item = opal_list_remove_first(&snapshot->local_snapshots))) {
 132         OBJ_RELEASE(item);
 133     }
 134     OBJ_DESTRUCT(&(snapshot->local_snapshots));
 135 
 136     snapshot->ss_handle  = ORTE_SSTORE_HANDLE_INVALID;
 137 
 138     if( NULL != snapshot->start_time ) {
 139         free(snapshot->start_time);
 140         snapshot->start_time = NULL;
 141     }
 142 
 143     if( NULL != snapshot->end_time ) {
 144         free(snapshot->end_time);
 145         snapshot->end_time = NULL;
 146     }
 147 
 148     snapshot->seq_num = -1;
 149 
 150     snapshot->num_seqs = 0;
 151 
 152     if( NULL != snapshot->all_seqs ) {
 153         opal_argv_free(snapshot->all_seqs);
 154         snapshot->all_seqs = NULL;
 155     }
 156 
 157     if( NULL != snapshot->basedir ) {
 158         free(snapshot->basedir);
 159         snapshot->basedir = NULL;
 160     }
 161 
 162     if( NULL != snapshot->reference ) {
 163         free(snapshot->reference);
 164         snapshot->reference = NULL;
 165     }
 166 
 167     if( NULL != snapshot->amca_param ) {
 168         free(snapshot->amca_param);
 169         snapshot->amca_param = NULL;
 170     }
 171 
 172     if( NULL != snapshot->tune_param ) {
 173         free(snapshot->tune_param);
 174         snapshot->tune_param = NULL;
 175     }
 176 
 177     if( NULL != snapshot->metadata_filename ) {
 178         free(snapshot->metadata_filename);
 179         snapshot->metadata_filename = NULL;
 180     }
 181 }
 182 
 183 /***************
 184  * Tool interface functionality
 185  ***************/
 186 static orte_sstore_base_global_snapshot_info_t *tool_global_snapshot = NULL;
 187 
 188 int orte_sstore_base_tool_request_restart_handle(orte_sstore_base_handle_t *handle,
 189                                                  char *basedir, char *ref, int seq,
 190                                                  orte_sstore_base_global_snapshot_info_t *snapshot)
 191 {
 192     int ret, exit_status = ORTE_SUCCESS;
 193     char * tmp_str = NULL;
 194 
 195     if( NULL != tool_global_snapshot ) {
 196         OBJ_RELEASE(tool_global_snapshot);
 197     }
 198     tool_global_snapshot = snapshot;
 199     OBJ_RETAIN(tool_global_snapshot);
 200 
 201     snapshot->reference = strdup(ref);
 202     if( NULL == basedir ) {
 203         snapshot->basedir = strdup(orte_sstore_base_global_snapshot_dir);
 204     } else {
 205         snapshot->basedir = strdup(basedir);
 206     }
 207     opal_asprintf(&(snapshot->metadata_filename),
 208              "%s/%s/%s",
 209              snapshot->basedir,
 210              snapshot->reference,
 211              orte_sstore_base_global_metadata_filename);
 212 
 213     /*
 214      * Check the checkpoint location
 215      */
 216     opal_asprintf(&tmp_str, "%s/%s",
 217              snapshot->basedir,
 218              snapshot->reference);
 219     if (0 >  (ret = access(tmp_str, F_OK)) ) {
 220         opal_output(0, ("Error: The snapshot requested does not exist!\n"
 221                         "Check the path (%s)!"),
 222                     tmp_str);
 223         exit_status = ORTE_ERROR;
 224         goto cleanup;
 225     }
 226     if(NULL != tmp_str ) {
 227         free(tmp_str);
 228         tmp_str = NULL;
 229     }
 230 
 231     /*
 232      * If we were asked to find the largest seq num
 233      */
 234     if( seq < 0 ) {
 235         if( ORTE_SUCCESS != (ret = orte_sstore_base_find_largest_seq_num(snapshot, &seq)) ) {
 236             opal_output(0, ("Error: Failed to find a valid sequence number in snapshot metadata!\n"
 237                             "Check the metadata file (%s)!"),
 238                         snapshot->metadata_filename);
 239             exit_status = ORTE_ERROR;
 240             goto cleanup;
 241         }
 242         snapshot->seq_num = seq;
 243     } else {
 244         snapshot->seq_num = seq;
 245     }
 246 
 247     /*
 248      * Check the checkpoint sequence location
 249      */
 250     opal_asprintf(&tmp_str, "%s/%s/%d",
 251              snapshot->basedir,
 252              snapshot->reference,
 253              snapshot->seq_num);
 254     if (0 >  (ret = access(tmp_str, F_OK)) ) {
 255         opal_output(0, ("Error: The snapshot sequence requested does not exist!\n"
 256                         "Check the path (%s)!"),
 257                     tmp_str);
 258         exit_status = ORTE_ERROR;
 259         goto cleanup;
 260     }
 261     if(NULL != tmp_str ) {
 262         free(tmp_str);
 263         tmp_str = NULL;
 264     }
 265 
 266     /*
 267      * Build the list of processes attached to the snapshot
 268      */
 269     if( ORTE_SUCCESS != (ret = orte_sstore_base_extract_global_metadata(snapshot)) ) {
 270         opal_output(0, "Error: Failed to extract process information! Check the metadata file in (%s)!",
 271                     tmp_str);
 272         exit_status = ORTE_ERROR;
 273         goto cleanup;
 274     }
 275 
 276     /*
 277      * Save some basic infomation
 278      */
 279     snapshot->ss_handle = 1;
 280     *handle = 1;
 281 
 282  cleanup:
 283     if( NULL != tmp_str ) {
 284         free(tmp_str);
 285         tmp_str = NULL;
 286     }
 287 
 288     return exit_status;
 289 }
 290 
 291 int orte_sstore_base_tool_get_attr(orte_sstore_base_handle_t handle, orte_sstore_base_key_t key, char **value)
 292 {
 293     int ret, exit_status = ORTE_SUCCESS;
 294 
 295     if( SSTORE_METADATA_GLOBAL_SNAP_LOC_ABS == key ) {
 296         opal_asprintf(value, "%s/%s",
 297                  tool_global_snapshot->basedir,
 298                  tool_global_snapshot->reference);
 299     }
 300     else if( SSTORE_METADATA_LOCAL_SNAP_REF_FMT == key ) {
 301         *value = strdup(orte_sstore_base_local_snapshot_fmt);
 302     }
 303     else if( SSTORE_METADATA_LOCAL_SNAP_LOC == key ) {
 304         opal_asprintf(value, "%s/%s/%d",
 305                  tool_global_snapshot->basedir,
 306                  tool_global_snapshot->reference,
 307                  tool_global_snapshot->seq_num);
 308     }
 309     else if( SSTORE_METADATA_LOCAL_SNAP_REF_LOC_FMT == key ) {
 310         opal_asprintf(value, "%s/%s/%d/%s",
 311                  tool_global_snapshot->basedir,
 312                  tool_global_snapshot->reference,
 313                  tool_global_snapshot->seq_num,
 314                  orte_sstore_base_local_snapshot_fmt);
 315     }
 316     else if( SSTORE_METADATA_GLOBAL_SNAP_NUM_SEQ == key ) {
 317         if( NULL == tool_global_snapshot->all_seqs ) {
 318             if( ORTE_SUCCESS != (ret = orte_sstore_base_find_all_seq_nums(tool_global_snapshot,
 319                                                                           &(tool_global_snapshot->num_seqs),
 320                                                                           &(tool_global_snapshot->all_seqs)))) {
 321                 ORTE_ERROR_LOG(ORTE_ERROR);
 322                 exit_status = ORTE_ERROR;
 323                 goto cleanup;
 324             }
 325         }
 326         opal_asprintf(value, "%d", tool_global_snapshot->num_seqs);
 327     }
 328     else if( SSTORE_METADATA_GLOBAL_SNAP_ALL_SEQ == key ) {
 329         if( NULL == tool_global_snapshot->all_seqs ) {
 330             if( ORTE_SUCCESS != (ret = orte_sstore_base_find_all_seq_nums(tool_global_snapshot,
 331                                                                           &(tool_global_snapshot->num_seqs),
 332                                                                           &(tool_global_snapshot->all_seqs)))) {
 333                 ORTE_ERROR_LOG(ORTE_ERROR);
 334                 exit_status = ORTE_ERROR;
 335                 goto cleanup;
 336             }
 337         }
 338         *value = opal_argv_join(tool_global_snapshot->all_seqs, ',');
 339     }
 340     else if( SSTORE_METADATA_GLOBAL_AMCA_PARAM == key ) {
 341         *value = strdup(tool_global_snapshot->amca_param);
 342     }
 343     else if( SSTORE_METADATA_GLOBAL_TUNE_PARAM == key ) {
 344         *value = strdup(tool_global_snapshot->tune_param);
 345     }
 346     else {
 347         return ORTE_ERR_NOT_SUPPORTED;
 348     }
 349 
 350  cleanup:
 351     return exit_status;
 352 }
 353 
 354 /********************
 355  * Utility functions
 356  ********************/
 357 int orte_sstore_base_get_global_snapshot_ref(char **name_str, pid_t pid)
 358 {
 359     if( NULL == orte_sstore_base_global_snapshot_ref ) {
 360         opal_asprintf(name_str, "ompi_global_snapshot_%d.ckpt", pid);
 361     }
 362     else {
 363         *name_str = strdup(orte_sstore_base_global_snapshot_ref);
 364     }
 365 
 366     return ORTE_SUCCESS;
 367 }
 368 
 369 int orte_sstore_base_convert_key_to_string(orte_sstore_base_key_t key, char **key_str)
 370 {
 371     switch(key) {
 372     case SSTORE_METADATA_LOCAL_CRS_COMP:
 373         *key_str = strdup(SSTORE_METADATA_LOCAL_CRS_COMP_STR);
 374         break;
 375     case SSTORE_METADATA_LOCAL_COMPRESS_COMP:
 376         *key_str = strdup(SSTORE_METADATA_LOCAL_COMPRESS_COMP_STR);
 377         break;
 378     case SSTORE_METADATA_LOCAL_COMPRESS_POSTFIX:
 379         *key_str = strdup(SSTORE_METADATA_LOCAL_COMPRESS_POSTFIX_STR);
 380         break;
 381     case SSTORE_METADATA_LOCAL_PID:
 382         *key_str = strdup(SSTORE_METADATA_LOCAL_PID_STR);
 383         break;
 384     case SSTORE_METADATA_LOCAL_CONTEXT:
 385         *key_str = strdup(SSTORE_METADATA_LOCAL_CONTEXT_STR);
 386         break;
 387     case SSTORE_METADATA_LOCAL_MKDIR:
 388         *key_str = strdup(SSTORE_METADATA_LOCAL_MKDIR_STR);
 389         break;
 390     case SSTORE_METADATA_LOCAL_TOUCH:
 391         *key_str = strdup(SSTORE_METADATA_LOCAL_TOUCH_STR);
 392         break;
 393     case SSTORE_METADATA_LOCAL_SNAP_REF:
 394         *key_str = NULL;
 395         break;
 396     case SSTORE_METADATA_LOCAL_SNAP_REF_FMT:
 397         *key_str = strdup(SSTORE_METADATA_LOCAL_SNAP_REF_FMT_STR);
 398         break;
 399     case SSTORE_METADATA_LOCAL_SNAP_LOC:
 400         *key_str = NULL;
 401         break;
 402     case SSTORE_METADATA_LOCAL_SNAP_META:
 403         *key_str = NULL;
 404         break;
 405     case SSTORE_METADATA_GLOBAL_SNAP_REF:
 406         *key_str = NULL;
 407         break;
 408     case SSTORE_METADATA_GLOBAL_SNAP_LOC:
 409         *key_str = NULL;
 410         break;
 411     case SSTORE_METADATA_GLOBAL_SNAP_LOC_ABS:
 412         *key_str = NULL;
 413         break;
 414     case SSTORE_METADATA_GLOBAL_SNAP_META:
 415         *key_str = NULL;
 416         break;
 417     case SSTORE_METADATA_GLOBAL_SNAP_SEQ:
 418         *key_str = strdup(SSTORE_METADATA_GLOBAL_SNAP_SEQ_STR);
 419         break;
 420     case SSTORE_METADATA_GLOBAL_AMCA_PARAM:
 421         *key_str = strdup(SSTORE_METADATA_GLOBAL_AMCA_PARAM_STR);
 422         break;
 423     case SSTORE_METADATA_GLOBAL_TUNE_PARAM:
 424         *key_str = strdup(SSTORE_METADATA_GLOBAL_TUNE_PARAM_STR);
 425         break;
 426     default:
 427         *key_str = NULL;
 428         break;
 429     }
 430 
 431     return ORTE_SUCCESS;
 432 }
 433 
 434 int orte_sstore_base_convert_string_to_key(char *key_str, orte_sstore_base_key_t *key)
 435 {
 436     if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_CRS_COMP_STR, strlen(SSTORE_METADATA_LOCAL_CRS_COMP_STR))) {
 437         *key = SSTORE_METADATA_LOCAL_CRS_COMP;
 438     }
 439     else if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_COMPRESS_COMP_STR, strlen(SSTORE_METADATA_LOCAL_COMPRESS_COMP_STR))) {
 440         *key = SSTORE_METADATA_LOCAL_COMPRESS_COMP;
 441     }
 442     else if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_COMPRESS_POSTFIX_STR, strlen(SSTORE_METADATA_LOCAL_COMPRESS_POSTFIX_STR))) {
 443         *key = SSTORE_METADATA_LOCAL_COMPRESS_POSTFIX;
 444     }
 445     else if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_PID_STR, strlen(SSTORE_METADATA_LOCAL_PID_STR))) {
 446         *key = SSTORE_METADATA_LOCAL_PID;
 447     }
 448     else if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_CONTEXT_STR, strlen(SSTORE_METADATA_LOCAL_CONTEXT_STR))) {
 449         *key = SSTORE_METADATA_LOCAL_CONTEXT;
 450     }
 451     else if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_MKDIR_STR, strlen(SSTORE_METADATA_LOCAL_MKDIR_STR))) {
 452         *key = SSTORE_METADATA_LOCAL_MKDIR;
 453     }
 454     else if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_TOUCH_STR, strlen(SSTORE_METADATA_LOCAL_TOUCH_STR))) {
 455         *key = SSTORE_METADATA_LOCAL_TOUCH;
 456     }
 457     else if( 0 == strncmp(key_str, SSTORE_METADATA_LOCAL_SNAP_REF_FMT_STR, strlen(SSTORE_METADATA_LOCAL_SNAP_REF_FMT_STR))) {
 458         *key = SSTORE_METADATA_LOCAL_SNAP_REF_FMT;
 459     }
 460     else if( 0 == strncmp(key_str, SSTORE_METADATA_GLOBAL_SNAP_SEQ_STR, strlen(SSTORE_METADATA_GLOBAL_SNAP_SEQ_STR))) {
 461         *key = SSTORE_METADATA_GLOBAL_SNAP_SEQ;
 462     }
 463     else if( 0 == strncmp(key_str, SSTORE_METADATA_GLOBAL_AMCA_PARAM_STR, strlen(SSTORE_METADATA_GLOBAL_AMCA_PARAM_STR))) {
 464         *key = SSTORE_METADATA_GLOBAL_AMCA_PARAM;
 465     }
 466     else if( 0 == strncmp(key_str, SSTORE_METADATA_GLOBAL_TUNE_PARAM_STR, strlen(SSTORE_METADATA_GLOBAL_TUNE_PARAM_STR))) {
 467         *key = SSTORE_METADATA_GLOBAL_TUNE_PARAM;
 468     }
 469     else {
 470         *key = SSTORE_METADATA_MAX;
 471     }
 472 
 473     return ORTE_SUCCESS;
 474 }
 475 
 476 int orte_sstore_base_get_all_snapshots(opal_list_t *all_snapshots, char *basedir)
 477 {
 478 #ifndef HAVE_DIRENT_H
 479     return ORTE_ERR_NOT_SUPPORTED;
 480 #else
 481     int ret, exit_status = ORTE_SUCCESS;
 482     char *loc_basedir = NULL;
 483     char * tmp_str = NULL, * metadata_file = NULL;
 484     DIR *dirp = NULL;
 485     struct dirent *dir_entp = NULL;
 486     struct stat file_status;
 487     orte_sstore_base_global_snapshot_info_t *global_snapshot = NULL;
 488 
 489     /* Sanity check */
 490     if( NULL == all_snapshots ||
 491         (NULL == orte_sstore_base_global_snapshot_dir && NULL == basedir)) {
 492         ORTE_ERROR_LOG(ORTE_ERROR);
 493         exit_status = ORTE_ERROR;
 494         goto cleanup;
 495     }
 496 
 497     if( NULL == basedir ) {
 498         loc_basedir = strdup(orte_sstore_base_global_snapshot_dir);
 499     } else {
 500         loc_basedir = strdup(basedir);
 501     }
 502 
 503     /*
 504      * Get all subdirectories under the base directory
 505      */
 506     dirp = opendir(loc_basedir);
 507     while( NULL != (dir_entp = readdir(dirp))) {
 508         /* Skip "." and ".." if they are in the list */
 509         if( 0 == strncmp("..", dir_entp->d_name, strlen("..") ) ||
 510             0 == strncmp(".",  dir_entp->d_name, strlen(".")  ) ) {
 511             continue;
 512         }
 513 
 514         /* Add the full path */
 515         opal_asprintf(&tmp_str, "%s/%s", loc_basedir, dir_entp->d_name);
 516         if(0 != (ret = stat(tmp_str, &file_status) ) ){
 517             free( tmp_str);
 518             tmp_str = NULL;
 519             continue;
 520         } else {
 521             /* Is it a directory? */
 522             if(S_ISDIR(file_status.st_mode) ) {
 523                 opal_asprintf(&metadata_file, "%s/%s",
 524                          tmp_str,
 525                          orte_sstore_base_global_metadata_filename);
 526                 if(0 != (ret = stat(metadata_file, &file_status) ) ){
 527                     free( tmp_str);
 528                     tmp_str = NULL;
 529                     free( metadata_file);
 530                     metadata_file = NULL;
 531                     continue;
 532                 } else {
 533                     if(S_ISREG(file_status.st_mode) ) {
 534                         global_snapshot = OBJ_NEW(orte_sstore_base_global_snapshot_info_t);
 535 
 536                         global_snapshot->ss_handle = 1;
 537                         global_snapshot->basedir = strdup(loc_basedir);
 538                         opal_asprintf(&(global_snapshot->reference),
 539                                  "%s",
 540                                  dir_entp->d_name);
 541                         opal_asprintf(&(global_snapshot->metadata_filename),
 542                                  "%s/%s/%s",
 543                                  global_snapshot->basedir,
 544                                  global_snapshot->reference,
 545                                  orte_sstore_base_global_metadata_filename);
 546 
 547                         opal_list_append(all_snapshots, &(global_snapshot->super));
 548                     }
 549                 }
 550                 free( metadata_file);
 551                 metadata_file = NULL;
 552             }
 553         }
 554 
 555         free( tmp_str);
 556         tmp_str = NULL;
 557     }
 558 
 559     closedir(dirp);
 560 
 561  cleanup:
 562     if( NULL != loc_basedir ) {
 563         free(loc_basedir);
 564         loc_basedir = NULL;
 565     }
 566 
 567     if( NULL != tmp_str) {
 568         free( tmp_str);
 569         tmp_str = NULL;
 570     }
 571 
 572     return exit_status;
 573 #endif /* HAVE_DIRENT_H */
 574 }
 575 
 576 int orte_sstore_base_extract_global_metadata(orte_sstore_base_global_snapshot_info_t *global_snapshot)
 577 {
 578     int ret, exit_status = ORTE_SUCCESS;
 579     FILE *metadata = NULL;
 580     char * token = NULL;
 581     char * value = NULL;
 582     orte_process_name_t proc;
 583     opal_list_item_t* item = NULL;
 584     orte_sstore_base_local_snapshot_info_t *vpid_snapshot = NULL;
 585 
 586     /*
 587      * Cleanup the structure a bit, so we can refresh it below
 588      */
 589     while (NULL != (item = opal_list_remove_first(&global_snapshot->local_snapshots))) {
 590         OBJ_RELEASE(item);
 591     }
 592 
 593     if( NULL != global_snapshot->start_time ) {
 594         free( global_snapshot->start_time );
 595         global_snapshot->start_time = NULL;
 596     }
 597 
 598     if( NULL != global_snapshot->end_time ) {
 599         free( global_snapshot->end_time );
 600         global_snapshot->end_time = NULL;
 601     }
 602 
 603     /*
 604      * Open the metadata file
 605      */
 606     if (NULL == (metadata = fopen(global_snapshot->metadata_filename, "r")) ) {
 607         opal_output(orte_sstore_base_framework.framework_output,
 608                     "sstore:base:extract_global_metadata() Unable to open the file (%s)\n",
 609                     global_snapshot->metadata_filename);
 610         ORTE_ERROR_LOG(ORTE_ERROR);
 611         exit_status = ORTE_ERROR;
 612         goto cleanup;
 613     }
 614 
 615     /*
 616      * Seek to the sequence number requested
 617      */
 618     if( ORTE_SUCCESS != (ret = orte_sstore_base_metadata_seek_to_seq_num(metadata, global_snapshot->seq_num))) {
 619         ORTE_ERROR_LOG(ORTE_ERROR);
 620         exit_status = ORTE_ERROR;
 621         goto cleanup;
 622     }
 623 
 624     /*
 625      * Extract each token and make the records
 626      */
 627     do {
 628         if( ORTE_SUCCESS != orte_sstore_base_metadata_read_next_token(metadata, &token, &value) ) {
 629             break;
 630         }
 631 
 632         if(0 == strncmp(token, SSTORE_METADATA_GLOBAL_SNAP_SEQ_STR,  strlen(SSTORE_METADATA_GLOBAL_SNAP_SEQ_STR)) ||
 633            0 == strncmp(token, SSTORE_METADATA_INTERNAL_MIG_SEQ_STR, strlen(SSTORE_METADATA_INTERNAL_MIG_SEQ_STR)) ) {
 634             break;
 635         }
 636 
 637         if( 0 == strncmp(token, SSTORE_METADATA_INTERNAL_PROCESS_STR, strlen(SSTORE_METADATA_INTERNAL_PROCESS_STR)) ) {
 638             orte_util_convert_string_to_process_name(&proc, value);
 639 
 640             /* Not the first process, so append it to the list */
 641             if( NULL != vpid_snapshot) {
 642                 opal_list_append(&global_snapshot->local_snapshots, &(vpid_snapshot->super));
 643             }
 644 
 645             vpid_snapshot = OBJ_NEW(orte_sstore_base_local_snapshot_info_t);
 646             vpid_snapshot->ss_handle = global_snapshot->ss_handle;
 647 
 648             vpid_snapshot->process_name.jobid  = proc.jobid;
 649             vpid_snapshot->process_name.vpid   = proc.vpid;
 650         }
 651         else if(0 == strncmp(token, SSTORE_METADATA_LOCAL_CRS_COMP_STR, strlen(SSTORE_METADATA_LOCAL_CRS_COMP_STR))) {
 652             vpid_snapshot->crs_comp = strdup(value);
 653         }
 654         else if(0 == strncmp(token, SSTORE_METADATA_LOCAL_COMPRESS_COMP_STR, strlen(SSTORE_METADATA_LOCAL_COMPRESS_COMP_STR))) {
 655             vpid_snapshot->compress_comp = strdup(value);
 656         }
 657         else if(0 == strncmp(token, SSTORE_METADATA_LOCAL_COMPRESS_POSTFIX_STR, strlen(SSTORE_METADATA_LOCAL_COMPRESS_POSTFIX_STR))) {
 658             vpid_snapshot->compress_postfix = strdup(value);
 659         }
 660         else if(0 == strncmp(token, SSTORE_METADATA_INTERNAL_TIME_STR, strlen(SSTORE_METADATA_INTERNAL_TIME_STR)) ) {
 661             if( NULL == global_snapshot->start_time) {
 662                 global_snapshot->start_time = strdup(value);
 663             }
 664             else {
 665                 global_snapshot->end_time   = strdup(value);
 666             }
 667         }
 668         else if(0 == strncmp(token, SSTORE_METADATA_GLOBAL_AMCA_PARAM_STR, strlen(SSTORE_METADATA_GLOBAL_AMCA_PARAM_STR))) {
 669             global_snapshot->amca_param  = strdup(value);
 670         }
 671         else if(0 == strncmp(token, SSTORE_METADATA_GLOBAL_TUNE_PARAM_STR, strlen(SSTORE_METADATA_GLOBAL_TUNE_PARAM_STR))) {
 672             global_snapshot->tune_param  = strdup(value);
 673         }
 674     } while(0 == feof(metadata) );
 675 
 676     /* Append the last item */
 677     if( NULL != vpid_snapshot) {
 678         opal_list_append(&global_snapshot->local_snapshots, &(vpid_snapshot->super));
 679     }
 680 
 681  cleanup:
 682     if( NULL != metadata ) {
 683         fclose(metadata);
 684         metadata = NULL;
 685     }
 686     if( NULL != value ) {
 687         free(value);
 688         value = NULL;
 689     }
 690     if( NULL != token ) {
 691         free(token);
 692         token = NULL;
 693     }
 694 
 695     return exit_status;
 696 }
 697 
 698 int orte_sstore_base_find_largest_seq_num(orte_sstore_base_global_snapshot_info_t *global_snapshot, int *seq_num)
 699 {
 700     int exit_status = ORTE_SUCCESS;
 701     FILE *metadata = NULL;
 702     int tmp_seq_num = -1;
 703 
 704     *seq_num = -1;
 705 
 706     /*
 707      * Open the metadata file
 708      */
 709     if (NULL == (metadata = fopen(global_snapshot->metadata_filename, "r")) ) {
 710         opal_output(orte_sstore_base_framework.framework_output,
 711                     "sstore:base:find_largest_seq_num() Unable to open the file (%s)\n",
 712                     global_snapshot->metadata_filename);
 713         ORTE_ERROR_LOG(ORTE_ERROR);
 714         exit_status = ORTE_ERROR;
 715         goto cleanup;
 716     }
 717 
 718     while(0 <= (tmp_seq_num = orte_sstore_base_metadata_read_next_seq_num(metadata)) ) {
 719         if( tmp_seq_num > *seq_num ) {
 720             *seq_num = tmp_seq_num;
 721         }
 722     }
 723 
 724     if( *seq_num < 0 ) {
 725         exit_status = ORTE_ERROR;
 726     }
 727 
 728  cleanup:
 729     if( NULL != metadata ) {
 730         fclose(metadata);
 731         metadata = NULL;
 732     }
 733 
 734     return exit_status;
 735 }
 736 
 737 int orte_sstore_base_find_all_seq_nums(orte_sstore_base_global_snapshot_info_t *global_snapshot, int *num_seq, char ***seq_list)
 738 {
 739     int exit_status = ORTE_SUCCESS;
 740     FILE *metadata = NULL;
 741     int tmp_seq_num = -1;
 742     char * tmp_str = NULL;
 743 
 744     *num_seq = 0;
 745     *seq_list = NULL;
 746 
 747     /*
 748      * Open the metadata file
 749      */
 750     if (NULL == (metadata = fopen(global_snapshot->metadata_filename, "r")) ) {
 751         opal_output(orte_sstore_base_framework.framework_output,
 752                     "sstore:base:find_all_seq_nums() Unable to open the file (%s)\n",
 753                     global_snapshot->metadata_filename);
 754         ORTE_ERROR_LOG(ORTE_ERROR);
 755         exit_status = ORTE_ERROR;
 756         goto cleanup;
 757     }
 758 
 759     while(0 <= (tmp_seq_num = orte_sstore_base_metadata_read_next_seq_num(metadata)) ) {
 760         opal_asprintf(&tmp_str, "%d", tmp_seq_num);
 761 
 762         if( NULL != tmp_str ) {
 763             opal_argv_append(num_seq, seq_list, tmp_str);
 764             free(tmp_str);
 765             tmp_str = NULL;
 766         }
 767     }
 768 
 769  cleanup:
 770     if( NULL != metadata ) {
 771         fclose(metadata);
 772         metadata = NULL;
 773     }
 774 
 775     if( NULL != tmp_str ) {
 776         free(tmp_str);
 777         tmp_str = NULL;
 778     }
 779 
 780     return exit_status;
 781 }
 782 
 783 
 784 /*
 785  * Extract the next sequence number from the file
 786  */
 787 int orte_sstore_base_metadata_read_next_seq_num(FILE *file)
 788 {
 789     char *token = NULL;
 790     char *value = NULL;
 791     int seq_int = -1;
 792 
 793     do {
 794         if( ORTE_SUCCESS != orte_sstore_base_metadata_read_next_token(file, &token, &value) ) {
 795             seq_int = -1;
 796             goto cleanup;
 797         }
 798     } while(0 != strncmp(token, SSTORE_METADATA_INTERNAL_DONE_SEQ_STR, strlen(SSTORE_METADATA_INTERNAL_DONE_SEQ_STR)));
 799 
 800     seq_int = atoi(value);
 801 
 802  cleanup:
 803     if( NULL != token) {
 804         free(token);
 805         token = NULL;
 806     }
 807 
 808     if( NULL != value) {
 809         free(value);
 810         value = NULL;
 811     }
 812 
 813     return seq_int;
 814 }
 815 
 816 int orte_sstore_base_metadata_seek_to_seq_num(FILE *file, int seq_num)
 817 {
 818     char *token = NULL;
 819     char *value = NULL;
 820     int seq_int = -1;
 821 
 822     rewind(file);
 823 
 824     do {
 825         do {
 826             if( ORTE_SUCCESS != orte_sstore_base_metadata_read_next_token(file, &token, &value) ) {
 827                 seq_int = -1;
 828                 goto cleanup;
 829             }
 830         } while(0 != strncmp(token, SSTORE_METADATA_GLOBAL_SNAP_SEQ_STR, strlen(SSTORE_METADATA_GLOBAL_SNAP_SEQ_STR)) );
 831         seq_int = atoi(value);
 832     } while(seq_num != seq_int );
 833 
 834  cleanup:
 835     if( NULL != token) {
 836         free(token);
 837         token = NULL;
 838     }
 839 
 840     if( NULL != value) {
 841         free(value);
 842         value = NULL;
 843     }
 844 
 845     if( seq_num != seq_int ) {
 846         return ORTE_ERROR;
 847     } else {
 848         return ORTE_SUCCESS;
 849     }
 850 }
 851 
 852 int orte_sstore_base_metadata_read_next_token(FILE *file, char **token, char **value)
 853 {
 854     int exit_status = ORTE_SUCCESS;
 855     int max_len = 256;
 856     char * line = NULL;
 857     int line_len = 0;
 858     int c = 0, s = 0, v = 0;
 859     char *local_token = NULL;
 860     char *local_value = NULL;
 861     bool end_of_line = false;
 862 
 863     line = (char *) malloc(sizeof(char) * max_len);
 864 
 865  try_again:
 866     /*
 867      * If we are at the end of the file, then just return
 868      */
 869     if(0 != feof(file) ) {
 870         exit_status = ORTE_ERROR;
 871         goto cleanup;
 872     }
 873 
 874     /*
 875      * Other wise grab the next token/value pair
 876      */
 877     if (NULL == fgets(line, max_len, file) ) {
 878         exit_status = ORTE_ERROR;
 879         goto cleanup;
 880     }
 881     line_len = strlen(line);
 882     /* Strip off the new line if it it there */
 883     if('\n' == line[line_len-1]) {
 884         line[line_len-1] = '\0';
 885         line_len--;
 886         end_of_line = true;
 887     }
 888     else {
 889         end_of_line = false;
 890     }
 891 
 892     /* Ignore lines with just '#' too */
 893     if(2 >= line_len)
 894         goto try_again;
 895 
 896     /*
 897      * Extract the token from the set
 898      */
 899     for(c = 0;
 900         line[c] != ':' &&
 901             c < line_len;
 902         ++c) {
 903         ;
 904     }
 905     c += 2; /* For the ' ' and the '\0' */
 906     local_token = (char *)malloc(sizeof(char) * (c + 1));
 907 
 908     for(s = 0; s < c; ++s) {
 909         local_token[s] = line[s];
 910     }
 911 
 912     local_token[s] = '\0';
 913     *token = strdup(local_token);
 914 
 915     if( NULL != local_token) {
 916         free(local_token);
 917         local_token = NULL;
 918     }
 919 
 920     /*
 921      * Extract the value from the set
 922      */
 923     local_value = (char *)malloc(sizeof(char) * (line_len - c + 1));
 924     for(v = 0, s = c;
 925         s < line_len;
 926         ++s, ++v) {
 927         local_value[v] = line[s];
 928     }
 929 
 930     while(!end_of_line) {
 931         if (NULL == fgets(line, max_len, file) ) {
 932             exit_status = ORTE_ERROR;
 933             goto cleanup;
 934         }
 935         line_len = strlen(line);
 936         /* Strip off the new line if it it there */
 937         if('\n' == line[line_len-1]) {
 938             line[line_len-1] = '\0';
 939             line_len--;
 940             end_of_line = true;
 941         }
 942         else {
 943             end_of_line = false;
 944         }
 945 
 946         local_value = (char *)realloc(local_value, sizeof(char) * line_len);
 947         for(s = 0;
 948             s < line_len;
 949             ++s, ++v) {
 950             local_value[v] = line[s];
 951         }
 952     }
 953 
 954     local_value[v] = '\0';
 955     *value = strdup(local_value);
 956 
 957  cleanup:
 958     if( NULL != local_token) {
 959         free(local_token);
 960         local_token = NULL;
 961     }
 962 
 963     if( NULL != local_value) {
 964         free(local_value);
 965         local_value = NULL;
 966     }
 967 
 968     if( NULL != line) {
 969         free(line);
 970         line = NULL;
 971     }
 972 
 973     return exit_status;
 974 }

/* [<][>][^][v][top][bottom][index][help] */