now detect memory leak open/closing files with tst_mem1.c

This commit is contained in:
Edward Hartnett 2020-02-09 08:51:11 -07:00
parent 8ffa18c385
commit 2d3f4be2ea

View File

@ -14,9 +14,12 @@
#include <err_macros.h> #include <err_macros.h>
#include <sys/resource.h> #include <sys/resource.h>
#define FILE_NAME "tst_mem1.nc"
#define NUM_FILE_OPENS 100
int main() int main()
{ {
int dataset_id, time_id, dummyvar_id, ret, idx; int dataset_id, time_id, dummyvar_id, idx;
size_t start[1] = {0}; size_t start[1] = {0};
size_t count[1] = {100}; size_t count[1] = {100};
double data[100]; double data[100];
@ -27,29 +30,27 @@ int main()
data[idx]=-99; data[idx]=-99;
}; };
printf("\n*** Testing netcdf-4 memory use with unlimited dimensions.\n"); printf("\n*** Testing netcdf-4 memory use.\n");
printf("*** testing with user-contributed code..."); printf("*** testing mem use opening/closing file...");
{ {
if (nc_create("test.nc", NC_CLOBBER | NC_NETCDF4, &dataset_id)) ERR; long my_rss = 0;
/*ret=nc_create("test.nc", NC_CLOBBER | NC_64BIT_OFFSET, &dataset_id);*/
if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
ret=nc_def_dim(dataset_id, "time", NC_UNLIMITED, &time_id);
if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
ret=nc_def_var(dataset_id, "dummy", NC_DOUBLE, 1, &time_id, &dummyvar_id);
if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
/*ret=nc_enddef(dataset_id);*/
ret=nc_put_vara(dataset_id, dummyvar_id, start, count, data);
if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
ret=nc_close(dataset_id);
if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;}
for (idx = 0; idx < 100000; idx++) { if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &dataset_id)) ERR;
ret=nc_open("test.nc", NC_NOWRITE, &dataset_id); if (nc_def_dim(dataset_id, "time", NC_UNLIMITED, &time_id)) ERR;
if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;} if (nc_def_var(dataset_id, "dummy", NC_DOUBLE, 1, &time_id, &dummyvar_id)) ERR;
ret=nc_close(dataset_id); if (nc_put_vara(dataset_id, dummyvar_id, start, count, data)) ERR;
if(ret!=NC_NOERR) {printf("%s\n",nc_strerror(ret)); return 1;} if (nc_close(dataset_id)) ERR;
getrusage(RUSAGE_SELF,&r_usage);
/* printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss); */ for (idx = 0; idx < NUM_FILE_OPENS; idx++)
{
if (nc_open(FILE_NAME, NC_NOWRITE, &dataset_id)) ERR;
if (nc_close(dataset_id)) ERR;
getrusage(RUSAGE_SELF, &r_usage);
printf("Memory usage: %ld kilobytes\n",r_usage.ru_maxrss);
if (!my_rss)
my_rss = r_usage.ru_maxrss;
else
if (my_rss != r_usage.ru_maxrss) ERR;
}; };
} }
SUMMARIZE_ERR; SUMMARIZE_ERR;