File: tst_mem.c

package info (click to toggle)
netcdf 1:4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (82 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* This is part of the netCDF package.
   Copyright 2018 University Corporation for Atmospheric Research/Unidata
   See COPYRIGHT file for conditions of use.

   Test internal netcdf-4 file code.
   $Id$
*/

#include <config.h>
#include <stdio.h>
#include <nc_tests.h>
#include <err_macros.h>
#include "netcdf.h"
#include <hdf5.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h> /* Extra high precision time info. */
#include <string.h>

#define NDIMS 1
#define FILE_NAME "tst_mem.nc"
#define NUM_TRIES 20000
#define TIME_NAME "time"
#define SFC_TEMP_NAME "sfc_temp"

void
get_mem_used2(int *mem_used)
{
   char buf[30];
   FILE *pf;

   snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid());
   pf = fopen(buf, "r");
   if (pf) {
      unsigned size; /*       total program size */
      unsigned resident;/*   resident set size */
      unsigned share;/*      shared pages */
      unsigned text;/*       text (code) */
      unsigned lib;/*        library */
      unsigned data;/*       data/stack */
      /*unsigned dt;          dirty pages (unused in Linux 2.6)*/
      fscanf(pf, "%u %u %u %u %u %u", &size, &resident, &share,
	     &text, &lib, &data);
      *mem_used = data;
   }
   else
      *mem_used = -1;
  fclose(pf);
}

int main(void)
{
   int ncid, sfc_tempid;
   float data;
   int dimid;
   size_t l_index[NDIMS] = {10000};
   int mem_used, mem_used1;
   int i;

   printf("\n*** Testing netcdf-4 memory use with unlimited dimensions.\n");
   printf("*** testing with user-contributed code...");

   if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
   if (nc_def_dim(ncid, TIME_NAME, NC_UNLIMITED, &dimid)) ERR;
   if (nc_def_var(ncid, SFC_TEMP_NAME, NC_FLOAT, NDIMS, &dimid, &sfc_tempid)) ERR;

   /* Write data each 100ms*/
   get_mem_used2(&mem_used);
   for (i = 0; i < NUM_TRIES; i++)
   {
      data = 25.5 + l_index[0];
      if (nc_put_var1_float(ncid, sfc_tempid, l_index, (const float*) &data)) ERR;
      l_index[0]++;
      get_mem_used2(&mem_used1);
      if (!(i%100) && mem_used1 - mem_used)
	 printf("delta %d bytes of memory for try %d\n", mem_used1 - mem_used, i);
   }

   if (nc_close(ncid)) ERR;
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}