|
| 1 | +/* |
| 2 | + Copyright 2011, UCAR/Unidata |
| 3 | + See COPYRIGHT file for copying and redistribution conditions. |
| 4 | +
|
| 5 | + This is part of netCDF. |
| 6 | + |
| 7 | + This program tests for a bug discovered with nofill mode that failed |
| 8 | + only on file systems with block size in a particular range. It fails |
| 9 | + when invoked with the blksize argument between 2091953 and 2150032, |
| 10 | + inclusive, and succeeds for other blksizes. |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <config.h> |
| 14 | +#include <nc_tests.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <limits.h> |
| 17 | +#include <stdlib.h> |
| 18 | +#include <errno.h> |
| 19 | +#include <netcdf.h> |
| 20 | + |
| 21 | +#define FILE_NAME "tst_nofill2.nc" |
| 22 | +#define LON_LEN 240 |
| 23 | +#define LAT_LEN 121 |
| 24 | +#define LVL_LEN 31 |
| 25 | +#define TIME_LEN 1 |
| 26 | + |
| 27 | +int |
| 28 | +create_file(char *file_name, int fill_mode, size_t* sizehintp) |
| 29 | +{ |
| 30 | + int ncid; |
| 31 | + int lon_dim, lat_dim, lvl_dim, time_dim; |
| 32 | + int time_id, zonal_wnd_id; |
| 33 | + int i; |
| 34 | + |
| 35 | + /* rank (number of dimensions) for each variable */ |
| 36 | +# define RANK_time 1 |
| 37 | +# define RANK_lat 1 |
| 38 | +# define RANK_lon 1 |
| 39 | +# define RANK_lvl 1 |
| 40 | +# define RANK_zonal_wnd 4 |
| 41 | + |
| 42 | + /* variable shapes */ |
| 43 | + int zonal_wnd_dims[RANK_zonal_wnd]; |
| 44 | + size_t zonal_wnd_start[RANK_zonal_wnd]; |
| 45 | + size_t zonal_wnd_count[RANK_zonal_wnd]; |
| 46 | + float zonal_wnd[LON_LEN*LAT_LEN*TIME_LEN]; |
| 47 | + size_t default_initialsize = 0; |
| 48 | + double time[TIME_LEN] = {1.}; |
| 49 | + |
| 50 | + /* To test bug on filesystem without large block size, we can get |
| 51 | + * the same effect by providing the desired value as sizehint to |
| 52 | + * nc__create() instead of calling nc_create() and getting the |
| 53 | + * block size reported by fstat */ |
| 54 | + if (nc__create(file_name, NC_CLOBBER, default_initialsize, sizehintp, &ncid)) ERR; |
| 55 | + if (nc_set_fill(ncid, fill_mode, NULL)) ERR; |
| 56 | + |
| 57 | + /* define dimensions */ |
| 58 | + if (nc_def_dim(ncid, "lon", LON_LEN, &lon_dim)) ERR; |
| 59 | + if (nc_def_dim(ncid, "lat", LAT_LEN, &lat_dim)) ERR; |
| 60 | + if (nc_def_dim(ncid, "lvl", LVL_LEN, &lvl_dim)) ERR; |
| 61 | + if (nc_def_dim(ncid, "time", TIME_LEN, &time_dim)) ERR; |
| 62 | + |
| 63 | + /* define variables */ |
| 64 | + if (nc_def_var(ncid, "time", NC_DOUBLE, RANK_time, &time_dim, &time_id)) ERR; |
| 65 | + |
| 66 | + zonal_wnd_dims[0] = time_dim; |
| 67 | + zonal_wnd_dims[1] = lvl_dim; |
| 68 | + zonal_wnd_dims[2] = lat_dim; |
| 69 | + zonal_wnd_dims[3] = lon_dim; |
| 70 | + if (nc_def_var(ncid, "zonal_wnd", NC_FLOAT, RANK_zonal_wnd, zonal_wnd_dims, &zonal_wnd_id)) ERR; |
| 71 | + |
| 72 | + if (nc_enddef (ncid)) ERR; |
| 73 | + if (nc_put_var_double(ncid, time_id, time)) ERR; |
| 74 | + /* Bug exposed when written in reverse order. */ |
| 75 | + for(i = LVL_LEN - 1; i>=0; i--) |
| 76 | + /* for(i = 0; i < LVL_LEN; i++) */ |
| 77 | + { |
| 78 | + int izw; |
| 79 | + for(izw = 0; izw < TIME_LEN * LAT_LEN * LON_LEN; izw++) { |
| 80 | + zonal_wnd[izw] = 100 + i; |
| 81 | + } |
| 82 | + zonal_wnd_start[0] = 0; |
| 83 | + zonal_wnd_start[1] = i; |
| 84 | + zonal_wnd_start[2] = 0; |
| 85 | + zonal_wnd_start[3] = 0; |
| 86 | + zonal_wnd_count[0] = TIME_LEN; |
| 87 | + zonal_wnd_count[1] = 1; |
| 88 | + zonal_wnd_count[2] = LAT_LEN; |
| 89 | + zonal_wnd_count[3] = LON_LEN; |
| 90 | + if (nc_put_vara_float(ncid, zonal_wnd_id, zonal_wnd_start, zonal_wnd_count, zonal_wnd)) ERR; |
| 91 | + } |
| 92 | + if (nc_close(ncid)) ERR; |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +int |
| 97 | +main(int argc, char **argv) |
| 98 | +{ |
| 99 | + size_t sizehint = (1750000); /* default if not set on command line, |
| 100 | + * exposes bug. It turns out any |
| 101 | + * value between 2091953 and 2150032 |
| 102 | + * triggers bug, whereas all other |
| 103 | + * values work fine. */ |
| 104 | + |
| 105 | + printf("\n*** Testing nofill mode.\n"); |
| 106 | + printf("*** Create file in nofill mode, writing all values..."); |
| 107 | + { |
| 108 | +#define NUM_TRIES 4000 |
| 109 | + int ncid; |
| 110 | + size_t idx[1] = {0}; |
| 111 | + double data; |
| 112 | + int i; |
| 113 | + |
| 114 | + for (i = 0; i < NUM_TRIES; i++) |
| 115 | + { |
| 116 | + printf("...trying sizehint of %ld\n", sizehint); |
| 117 | + if (create_file(FILE_NAME, NC_NOFILL, &sizehint)) ERR; |
| 118 | + if (nc_open(FILE_NAME, 0, &ncid)) ERR; |
| 119 | + if (nc_get_var1(ncid, 0, idx, &data)) ERR; |
| 120 | + if (!data) ERR; |
| 121 | + if (nc_close(ncid)) ERR; |
| 122 | + sizehint += 10000; |
| 123 | + } |
| 124 | + } |
| 125 | + SUMMARIZE_ERR; |
| 126 | + FINAL_RESULTS; |
| 127 | +} |
0 commit comments