static bool util_addr2line_lookup__(const void * bt_addr , char ** func_name , char ** file_name , int * line_nr, bool subtract_base_adress) { *func_name = NULL; // If dladdr() succeeds, but addr2line fails the func_name pointer will be set, but the function will return false anyway. *file_name = NULL; *line_nr = 0; { bool address_found = false; Dl_info dl_info; #if defined(__APPLE__) return false; #else if (dladdr(bt_addr , &dl_info)) { const char * executable = dl_info.dli_fname; *func_name = util_alloc_string_copy( dl_info.dli_sname ); if (util_file_exists( executable )) { char *stdout_file = util_alloc_tmp_file("/tmp" , "addr2line" , true); /* 1: Run addr2line application */ { char ** argv = util_calloc(3 , sizeof * argv ); argv[0] = util_alloc_string_copy("--functions"); argv[1] = util_alloc_sprintf("--exe=%s" , executable ); { char * rel_address = (char *) bt_addr; if (subtract_base_adress) rel_address -= (size_t) dl_info.dli_fbase; argv[2] = util_alloc_sprintf("%p" , (void *) rel_address); } util_spawn_blocking("addr2line", 3, (const char **) argv, stdout_file, NULL); util_free_stringlist(argv , 3); } /* 2: Parse stdout output */ { bool at_eof; FILE * stream = util_fopen(stdout_file , "r"); char * tmp_fname = util_fscanf_alloc_line(stream , &at_eof); if (strcmp(tmp_fname , UNDEFINED_FUNCTION) != 0) { char * stdout_file_name = util_fscanf_alloc_line(stream , &at_eof); char * line_string = NULL; util_binary_split_string( stdout_file_name , ":" , false , file_name , &line_string); if (line_string && util_sscanf_int( line_string , line_nr)) address_found = true; free( stdout_file_name ); util_safe_free( line_string ); } free( tmp_fname ); fclose(stream); } util_unlink_existing(stdout_file); free( stdout_file ); } } return address_found; #endif } }
void test_copy( const char* argv0 ) { ERT::TestArea ta("test/copy"); test_assert_throw( ta.copyFile( "does/not/exist" ) , std::invalid_argument ); test_assert_throw( ta.copyDirectory( argv0 ) , std::invalid_argument ); ta.copyFile( argv0 ); test_assert_true( util_file_exists( LOCAL_ARGV0 )); util_unlink_existing( LOCAL_ARGV0 ); test_assert_false( util_file_exists( LOCAL_ARGV0)); ta.copyParentContent( argv0 ); test_assert_true( util_file_exists( LOCAL_ARGV0)); { ERT::TestArea ta2("test2/copy"); ta2.copyFile( LOCAL_ARGV0 ); test_assert_true( util_file_exists( LOCAL_ARGV0)); } }
static void util_addr2line_lookup(const char * executable , const char * bt_symbol , char ** func_name , char ** file_line) { char *tmp_file = util_alloc_tmp_file("/tmp" , "addr2line" , true); char * adress; { int start_pos = 0; int end_pos; while ( bt_symbol[start_pos] != '[') start_pos++; end_pos = start_pos; while ( bt_symbol[end_pos] != ']') end_pos++; adress = util_alloc_substring_copy( bt_symbol , start_pos + 1 , end_pos - start_pos - 1 ); } { char ** argv; argv = util_calloc(3 , sizeof * argv ); argv[0] = util_alloc_string_copy("--functions"); argv[1] = util_alloc_sprintf("--exe=%s" , executable); argv[2] = util_alloc_string_copy(adress); util_fork_exec("addr2line" , 3 , (const char **) argv , true , NULL , NULL , NULL , tmp_file , NULL); util_free_stringlist(argv , 3); } { bool at_eof; FILE * stream = util_fopen(tmp_file , "r"); *func_name = util_fscanf_alloc_line(stream , &at_eof); *file_line = util_fscanf_alloc_line(stream , &at_eof); fclose(stream); } util_unlink_existing(tmp_file); free(adress); free(tmp_file); }
static void enkf_fs_umount( enkf_fs_type * fs ) { if (!fs->read_only) { enkf_fs_fsync( fs ); enkf_fs_fwrite_misfit( fs ); } if (fs->lock_fd > 0) { close( fs->lock_fd ); // Closing the lock_file file descriptor - and releasing the lock. util_unlink_existing( fs->lock_file ); } { int refcount = fs->refcount; if (refcount == 0) { enkf_fs_free_driver( fs->dynamic_forecast ); enkf_fs_free_driver( fs->dynamic_analyzed ); enkf_fs_free_driver( fs->parameter ); enkf_fs_free_driver( fs->eclipse_static ); enkf_fs_free_driver( fs->index ); util_safe_free( fs->case_name ); util_safe_free( fs->root_path ); util_safe_free(fs->lock_file); util_safe_free( fs->mount_point ); path_fmt_free( fs->case_fmt ); path_fmt_free( fs->case_member_fmt ); path_fmt_free( fs->case_tstep_fmt ); path_fmt_free( fs->case_tstep_member_fmt ); state_map_free( fs->state_map ); time_map_free( fs->time_map ); cases_config_free( fs->cases_config ); free( fs ); } else util_abort("%s: internal fuckup - tried to umount a filesystem with refcount:%d\n",__func__ , refcount); } }
int main(int argc , char ** argv) { enkf_main_install_SIGNALS(); const char * root_path = argv[1]; const char * config_file = argv[2]; const char * forward_init_string = argv[3]; test_work_area_type * work_area = test_work_area_alloc(config_file , false); test_work_area_copy_directory_content( work_area , root_path ); { bool forward_init; bool strict = true; enkf_main_type * enkf_main; test_assert_true( util_sscanf_bool( forward_init_string , &forward_init)); util_clear_directory( "Storage" , true , true ); enkf_main = enkf_main_bootstrap( NULL , config_file , strict , true ); { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_node_type * gen_param_node = enkf_state_get_node( state , "PARAM" ); { const enkf_config_node_type * gen_param_config_node = enkf_node_get_config( gen_param_node ); char * init_file1 = enkf_config_node_alloc_initfile( gen_param_config_node , NULL , 0); char * init_file2 = enkf_config_node_alloc_initfile( gen_param_config_node , "/tmp", 0); test_assert_bool_equal( enkf_config_node_use_forward_init( gen_param_config_node ) , forward_init ); test_assert_string_equal( init_file1 , "PARAM_INIT"); test_assert_string_equal( init_file2 , "/tmp/PARAM_INIT"); free( init_file1 ); free( init_file2 ); } test_assert_bool_equal( enkf_node_use_forward_init( gen_param_node ) , forward_init ); if (forward_init) test_assert_bool_not_equal( enkf_node_initialize( gen_param_node , 0 , enkf_state_get_rng( state )) , forward_init); // else hard_failure() } test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main ))); if (forward_init) { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); enkf_node_type * gen_param_node = enkf_state_get_node( state , "PARAM" ); node_id_type node_id = {.report_step = 0 , .iens = 0, .state = ANALYZED }; create_runpath( enkf_main ); test_assert_true( util_is_directory( "simulations/run0" )); { run_mode_type run_mode = ENSEMBLE_EXPERIMENT; enkf_main_init_run(enkf_main , run_mode); /* This is ugly */ test_assert_false( enkf_node_has_data( gen_param_node , fs, node_id )); util_unlink_existing( "simulations/run0/PARAM_INIT" ); } { FILE * stream = util_fopen("simulations/run0/PARAM_INIT" , "w"); fprintf(stream , "0\n1\n2\n3\n" ); fclose( stream ); } { bool loadOK = true; stringlist_type * msg_list = stringlist_alloc_new(); { run_mode_type run_mode = ENSEMBLE_EXPERIMENT; enkf_main_init_run(enkf_main , run_mode); /* This is ugly */ } test_assert_true( enkf_node_forward_init( gen_param_node , "simulations/run0" , 0 )); enkf_state_forward_init( state , fs , &loadOK ); test_assert_true( loadOK ); enkf_state_load_from_forward_model( state , fs , &loadOK , false , msg_list ); stringlist_free( msg_list ); test_assert_true( loadOK ); { double value; test_assert_true( enkf_node_user_get( gen_param_node , fs , "0" , node_id , &value)); test_assert_double_equal( 0 , value); test_assert_true( enkf_node_user_get( gen_param_node , fs , "1" , node_id , &value)); test_assert_double_equal( 1 , value); test_assert_true( enkf_node_user_get( gen_param_node , fs , "2" , node_id , &value)); test_assert_double_equal( 2 , value); } } util_clear_directory( "simulations" , true , true ); create_runpath( enkf_main ); test_assert_true( util_is_directory( "simulations/run0" )); test_assert_true( util_is_file( "simulations/run0/PARAM.INC" )); { FILE * stream = util_fopen("simulations/run0/PARAM.INC" , "r"); double v0,v1,v2,v3; fscanf(stream , "%lg %lg %lg %lg" , &v0,&v1,&v2,&v3); fclose( stream ); test_assert_double_equal( 0 , v0); test_assert_double_equal( 1 , v1); test_assert_double_equal( 2 , v2); test_assert_double_equal( 3 , v3); } util_clear_directory( "simulations" , true , true ); } enkf_main_free( enkf_main ); } test_work_area_free( work_area ); }
int main(int argc , char ** argv) { enkf_main_install_SIGNALS(); const char * root_path = argv[1]; const char * config_file = argv[2]; const char * init_file = argv[3]; const char * forward_init_string = argv[4]; test_work_area_type * work_area = test_work_area_alloc(config_file ); test_work_area_copy_directory_content( work_area , root_path ); test_work_area_install_file( work_area , init_file ); { bool forward_init; bool strict = true; enkf_main_type * enkf_main; test_assert_true( util_sscanf_bool( forward_init_string , &forward_init)); util_clear_directory( "Storage" , true , true ); enkf_main = enkf_main_bootstrap( config_file , strict , true ); { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" ); { const enkf_config_node_type * surface_config_node = enkf_node_get_config( surface_node ); char * init_file1 = enkf_config_node_alloc_initfile( surface_config_node , NULL , 0); char * init_file2 = enkf_config_node_alloc_initfile( surface_config_node , "/tmp", 0); test_assert_bool_equal( enkf_config_node_use_forward_init( surface_config_node ) , forward_init ); test_assert_string_equal( init_file1 , "Surface.irap"); test_assert_string_equal( init_file2 , "/tmp/Surface.irap"); free( init_file1 ); free( init_file2 ); } test_assert_bool_equal( enkf_node_use_forward_init( surface_node ) , forward_init ); if (forward_init) test_assert_bool_not_equal( enkf_node_initialize( surface_node , 0 , enkf_state_get_rng( state )) , forward_init); // else hard_failure() } test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main ))); if (forward_init) { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); run_arg_type * run_arg = run_arg_alloc_ENSEMBLE_EXPERIMENT( fs , 0 ,0 , "simulations/run0"); enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" ); node_id_type node_id = {.report_step = 0 , .iens = 0, .state = ANALYZED }; create_runpath( enkf_main ); test_assert_true( util_is_directory( "simulations/run0" )); { int error = 0; stringlist_type * msg_list = stringlist_alloc_new(); test_assert_false( enkf_node_has_data( surface_node , fs, node_id )); util_unlink_existing( "simulations/run0/Surface.irap" ); test_assert_false( enkf_node_forward_init( surface_node , "simulations/run0" , 0 )); enkf_state_forward_init( state , run_arg , &error ); test_assert_true(LOAD_FAILURE & error); error = 0; { enkf_fs_type * fs = enkf_main_get_fs(enkf_main); state_map_type * state_map = enkf_fs_get_state_map(fs); state_map_iset(state_map, 0, STATE_INITIALIZED); } enkf_state_load_from_forward_model(state, run_arg , &error, false, msg_list); stringlist_free( msg_list ); test_assert_true(LOAD_FAILURE & error); } util_copy_file( init_file , "simulations/run0/Surface.irap"); { int error = 0; stringlist_type * msg_list = stringlist_alloc_new(); test_assert_true( enkf_node_forward_init( surface_node , "simulations/run0" , 0 )); enkf_state_forward_init( state , run_arg , &error ); test_assert_int_equal(0, error); enkf_state_load_from_forward_model( state , run_arg , &error , false , msg_list ); stringlist_free( msg_list ); test_assert_int_equal(0, error); { double value; test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); test_assert_double_equal( 2735.7461 , value); test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); test_assert_double_equal( 2737.0122 , value); } } util_clear_directory( "simulations" , true , true ); create_runpath( enkf_main ); test_assert_true( util_is_directory( "simulations/run0" )); test_assert_true( util_is_file( "simulations/run0/SURFACE.INC" )); test_assert_true( enkf_node_fload( surface_node , "simulations/run0/SURFACE.INC")); { double value; test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); test_assert_double_equal( 2735.7461 , value); test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); test_assert_double_equal( 2737.0122 , value); } util_clear_directory( "simulations" , true , true ); } enkf_main_free( enkf_main ); } }
int main(int argc , char ** argv) { enkf_main_install_SIGNALS(); const char * root_path = argv[1]; const char * config_file = argv[2]; const char * forward_init_string = argv[3]; test_work_area_type * work_area = test_work_area_alloc(config_file ); test_work_area_copy_directory_content( work_area , root_path ); { bool forward_init; bool strict = true; enkf_main_type * enkf_main; test_assert_true( util_sscanf_bool( forward_init_string , &forward_init)); util_clear_directory( "Storage" , true , true ); enkf_main = enkf_main_bootstrap( NULL , config_file , strict , true ); { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_node_type * gen_kw_node = enkf_state_get_node( state , "MULTFLT" ); { const enkf_config_node_type * gen_kw_config_node = enkf_node_get_config( gen_kw_node ); char * init_file1 = enkf_config_node_alloc_initfile( gen_kw_config_node , NULL , 0); char * init_file2 = enkf_config_node_alloc_initfile( gen_kw_config_node , "/tmp", 0); test_assert_bool_equal( enkf_config_node_use_forward_init( gen_kw_config_node ) , forward_init ); test_assert_string_equal( init_file1 , "MULTFLT_INIT"); test_assert_string_equal( init_file2 , "/tmp/MULTFLT_INIT"); free( init_file1 ); free( init_file2 ); } test_assert_bool_equal( enkf_node_use_forward_init( gen_kw_node ) , forward_init ); if (forward_init) test_assert_bool_not_equal( enkf_node_initialize( gen_kw_node , 0 , enkf_state_get_rng( state )) , forward_init); // else hard_failure() } test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main ))); if (forward_init) { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); run_arg_type * run_arg = run_arg_alloc_ENSEMBLE_EXPERIMENT( fs , 0 , 0 , "simulations/run0"); enkf_node_type * gen_kw_node = enkf_state_get_node( state , "MULTFLT" ); node_id_type node_id = {.report_step = 0 , .iens = 0, .state = ANALYZED }; create_runpath( enkf_main ); test_assert_true( util_is_directory( "simulations/run0" )); { int error = 0; stringlist_type * msg_list = stringlist_alloc_new(); bool_vector_type * iactive = bool_vector_alloc( enkf_main_get_ensemble_size( enkf_main ) , true); test_assert_false( enkf_node_has_data( gen_kw_node , fs, node_id )); util_unlink_existing( "simulations/run0/MULTFLT_INIT" ); test_assert_false( enkf_node_forward_init( gen_kw_node , "simulations/run0" , 0 )); enkf_state_forward_init( state , run_arg , &error ); test_assert_true(LOAD_FAILURE & error); error = 0; { enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); state_map_type * state_map = enkf_fs_get_state_map(fs); state_map_iset(state_map , 0 , STATE_INITIALIZED); } enkf_state_load_from_forward_model( state , run_arg , &error , false , msg_list ); stringlist_free( msg_list ); bool_vector_free( iactive ); test_assert_true(LOAD_FAILURE & error); } { FILE * stream = util_fopen("simulations/run0/MULTFLT_INIT" , "w"); fprintf(stream , "123456.0\n" ); fclose( stream ); } { int error = 0; stringlist_type * msg_list = stringlist_alloc_new(); test_assert_true( enkf_node_forward_init( gen_kw_node , "simulations/run0" , 0 )); enkf_state_forward_init( state , run_arg , &error ); test_assert_int_equal(0, error); enkf_state_load_from_forward_model( state , run_arg , &error , false , msg_list ); stringlist_free( msg_list ); test_assert_int_equal(0, error); { double value; test_assert_true( enkf_node_user_get( gen_kw_node , fs , "MULTFLT" , node_id , &value)); test_assert_double_equal( 123456.0 , value); } } test_assert_true( util_is_file ("simulations/run0/parameters.txt")); //Export of gen kw params util_clear_directory( "simulations" , true , true ); create_runpath( enkf_main ); test_assert_true( util_is_directory( "simulations/run0" )); test_assert_true( util_is_file( "simulations/run0/MULTFLT.INC" )); { FILE * stream = util_fopen("simulations/run0/MULTFLT.INC" , "r"); double value; fscanf(stream , "%lg" , &value); fclose( stream ); test_assert_double_equal( 123456.0 , value); } util_clear_directory( "simulations" , true , true ); run_arg_free( run_arg ); } enkf_main_free( enkf_main ); } test_work_area_free( work_area ); }
int main(int argc , char ** argv) { enkf_main_install_SIGNALS(); { const char * config_file = argv[1]; const char * init_file = argv[2]; const char * forward_init_string = argv[3]; bool forward_init; bool strict = true; enkf_main_type * enkf_main; test_assert_true( util_sscanf_bool( forward_init_string , &forward_init)); util_clear_directory( "/tmp/Storage" , true , true ); enkf_main = enkf_main_bootstrap( NULL , config_file , strict , true ); { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" ); { const enkf_config_node_type * surface_config_node = enkf_node_get_config( surface_node ); char * init_file1 = enkf_config_node_alloc_initfile( surface_config_node , NULL , 0); char * init_file2 = enkf_config_node_alloc_initfile( surface_config_node , "/tmp", 0); test_assert_bool_equal( enkf_config_node_use_forward_init( surface_config_node ) , forward_init ); test_assert_string_equal( init_file1 , "Surface.irap"); test_assert_string_equal( init_file2 , "/tmp/Surface.irap"); free( init_file1 ); free( init_file2 ); } test_assert_bool_equal( enkf_node_use_forward_init( surface_node ) , forward_init ); if (forward_init) test_assert_bool_not_equal( enkf_node_initialize( surface_node , 0 , enkf_state_get_rng( state )) , forward_init); // else hard_failure() } test_assert_bool_equal( forward_init, ensemble_config_have_forward_init( enkf_main_get_ensemble_config( enkf_main ))); if (forward_init) { enkf_state_type * state = enkf_main_iget_state( enkf_main , 0 ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); enkf_node_type * surface_node = enkf_state_get_node( state , "SURFACE" ); node_id_type node_id = {.report_step = 0 , .iens = 0, .state = ANALYZED }; create_runpath( enkf_main ); test_assert_true( util_is_directory( "/tmp/simulations/run0" )); { bool loadOK = true; stringlist_type * msg_list = stringlist_alloc_new(); { run_mode_type run_mode = ENSEMBLE_EXPERIMENT; enkf_main_init_run(enkf_main , run_mode); /* This is ugly */ } test_assert_false( enkf_node_has_data( surface_node , fs, node_id )); util_unlink_existing( "/tmp/simulations/run0/Surface.irap" ); test_assert_false( enkf_node_forward_init( surface_node , "/tmp/simulations/run0" , 0 )); enkf_state_forward_init( state , fs , &loadOK ); test_assert_false( loadOK ); loadOK = true; enkf_state_load_from_forward_model( state , fs , &loadOK , false , msg_list ); stringlist_free( msg_list ); test_assert_false( loadOK ); } util_copy_file( init_file , "/tmp/simulations/run0/Surface.irap"); { bool loadOK = true; stringlist_type * msg_list = stringlist_alloc_new(); { run_mode_type run_mode = ENSEMBLE_EXPERIMENT; enkf_main_init_run(enkf_main , run_mode); /* This is ugly */ } test_assert_true( enkf_node_forward_init( surface_node , "/tmp/simulations/run0" , 0 )); enkf_state_forward_init( state , fs , &loadOK ); test_assert_true( loadOK ); enkf_state_load_from_forward_model( state , fs , &loadOK , false , msg_list ); stringlist_free( msg_list ); test_assert_true( loadOK ); { double value; test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); test_assert_double_equal( 2735.7461 , value); test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); test_assert_double_equal( 2737.0122 , value); } } util_clear_directory( "/tmp/simulations" , true , true ); create_runpath( enkf_main ); test_assert_true( util_is_directory( "/tmp/simulations/run0" )); test_assert_true( util_is_file( "/tmp/simulations/run0/SURFACE.INC" )); test_assert_true( enkf_node_fload( surface_node , "/tmp/simulations/run0/SURFACE.INC")); { double value; test_assert_true( enkf_node_user_get( surface_node , fs , "0" , node_id , &value)); test_assert_double_equal( 2735.7461 , value); test_assert_true( enkf_node_user_get( surface_node , fs , "5" , node_id , &value)); test_assert_double_equal( 2737.0122 , value); } util_clear_directory( "/tmp/simulations" , true , true ); } enkf_main_free( enkf_main ); } }