netcdf-c/nc_perf/tst_mem1.c
Dennis Heimbigner d2316f866c Additional Fixes to NCZarr
Primary Fixes:
* Add a whole variable optimization -- used in the rare case that nc_get/put_vara covers the whole of a variable and the variable has a single chunk.
* Fix chunking error when stride causes whole chunks to be skipped.
* Fix some memory leaks
* Add test cases
* Add one performance test to nczarr_test/. This uses the timer utils from unit_test: timer_utils.[ch].
* Move ncdumpchunks utility from ncdump to nczarr_test

Misc. Other Changes:
* Make check for aws libraries conditional on --enable-nczarr-s3
* Remove all but one bm tests from nczarr_test until they are working.
* Remove another dependency on HDF5 from supposedly non-HDF5 specific code; specifically hdf5_log_hdf5.
* Make the BAIL2 macro be hdf5 specific and replace elsewhere with an HDF5 independent equivalent.
* Move hdf5cache.c to libsrc4/nc4cache.c because it is used by nczarr.
* Modify unit_tests so that some of them are run even if using Windows.
* Misc. small bug fixes and refactors and memory leaks.
* Rename some conflicting tests for cmake.
* Attempted to make nc_perf work with cmake and failed.
2020-12-16 20:48:02 -07:00

78 lines
2.2 KiB
C

/* This is part of the netCDF package. Copyright 2020 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use.
Test internal netcdf-4 file code for memory leaks. This test was
suggest by Jeff Whitaker. See
https://github.com/Unidata/netcdf-c/issues/1575.
Ed Hartnett 2/9/20
*/
#include <config.h>
#include <nc_tests.h>
#include <err_macros.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#else
#error "cannot use rusage"
#endif
#define FILE_NAME "tst_mem1.nc"
#define NUM_FILE_OPENS 10000
int main()
{
int ncid, varid, idx;
struct rusage r_usage;
printf("\n*** Testing netcdf-4 memory use.\n");
printf("*** testing mem use opening/closing file...");
{
long my_rss = 0;
int my_idx = -1;
int at_max = 0;
NC_UNUSED(my_idx); NC_UNUSED(at_max);
if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR;
/* if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR; */
if (nc_def_var(ncid, "dummy", NC_DOUBLE, 0, NULL, &varid)) ERR;
if (nc_close(ncid)) ERR;
for (idx = 0; idx < NUM_FILE_OPENS; idx++)
{
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_close(ncid)) ERR;
getrusage(RUSAGE_SELF, &r_usage);
/* if (!(idx % 100)) */
/* printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss); */
/* Memory usage goes up in the first couple of opens, but
* should then remain steady. Check that it does not
* change after some number of iterations; the expected number is 10. */
#if 1
if (!my_rss || idx < 10)
my_rss = r_usage.ru_maxrss;
else
if (my_rss != r_usage.ru_maxrss) ERR;
#else
/* Locate the monotonic maximum */
if(my_rss < r_usage.ru_maxrss) {
my_rss = r_usage.ru_maxrss;
my_idx = idx;
} else if(my_rss > r_usage.ru_maxrss) {
fprintf(stderr,"decrease: from: [%d] %ld to [%d] %ld\n",my_idx,my_rss,idx,r_usage.ru_maxrss);
ERR;
} else {
if(!at_max)
fprintf(stderr,"maximum: [%d] %ld\n",idx,r_usage.ru_maxrss);
at_max = 1;
}
#endif
};
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}