File: PrintMemory.c

package info (click to toggle)
openmx 3.2.4.dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 62,572 kB
  • ctags: 2,684
  • sloc: ansic: 130,666; python: 876; makefile: 560; xml: 63; perl: 18; sh: 4
file content (163 lines) | stat: -rw-r--r-- 2,887 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* print used memory 

 2003.03.13 H. Kino
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define fp_bsize         1048576     /* buffer size for setvbuf */

static FILE *fp=NULL;
static char *filename=NULL;

typedef struct slist_{
  char *name;
  float size;
  struct slist_ *next;
} slist; 

static slist *top=NULL;



void PrintMemory(char *name, size_t size0, char *mode)
{
  slist *now, *last;
  float size;
  char buf[fp_bsize];          /* setvbuf */

  size=(float)size0;

/*  printf("PrintMemory called, %s %d %s\n",name,size,mode); */
  fflush(stdout);

  if (mode) {
    if (strcmp(mode,"init")==0) {

      /* try opening a file */
      if (filename) { free(filename); filename=NULL;}
      filename = strdup(name);
      fp=fopen(filename,"w");

#ifdef xt3
      setvbuf(fp,buf,_IOFBF,fp_bsize);  /* setvbuf */
#endif

      if (fp==NULL) {
	printf("PrintMemory: fail to open a file: %s\n",filename);
	printf("PrintMemory: write to stdout\n"); 
	fp=stdout;
      }
      else {
	/* printf("PrintMemory: write to %s\n",filename); */
	fclose(fp);
      }


      return;
    }

    if (strcmp(mode,"sum")==0) {
      float total=0;
      for (now=top; now; now=now->next ) {
	total+= now->size;
      }

      if (fp==NULL) fp=stdout;
      if (fp!=stdout) {
	fp=fopen(filename,"a");

#ifdef xt3
        setvbuf(fp,buf,_IOFBF,fp_bsize);  /* setvbuf */
#endif

	if (fp==NULL) {
	  fp=stdout;
	}
      }

      fprintf(fp,"Memory: %-35s %10.2f MBytes\n","total",total);

      if (fp!=stdout) {
	fclose(fp);
      }

      return;
    }

  }

  size /= 1024*1024;

  /* find iterator */
  for (last=now=top; now; now=now->next ) {
    if ( strcmp(now->name,name)==0 ) {
      /* found it*/
      break;
    }
    last=now;
  }
  if (now) { /* found */
    /* in this case, it is already written */
    return; 
  }
  else  { /* not found */
    if (last==NULL) { /* first time */
      last=top = (slist*) malloc(sizeof(slist));
    } 
    else { /* add this to the last */
      last=last->next=(slist*) malloc(sizeof(slist));
    }
    last->name=strdup(name);
    last->size=size;
    last->next=NULL;
  }

  /* write it to the file */

  if (fp==NULL) fp=stdout;
  if (fp!=stdout) {
    fp=fopen(filename,"a");

#ifdef xt3
    setvbuf(fp,buf,_IOFBF,fp_bsize);  /* setvbuf */
#endif

    if (fp==NULL) {
      fp=stdout;
    }
  }

  fprintf(fp,"Memory: %-35s %10.2f MBytes\n",name,size);

  if (fp!=stdout) {
    fclose(fp);
  }
 
}

#if 0

main()
{
/*   PrintMemory("test.memory",0,"init"); */
   PrintMemory("main: a",100000000,"add");
   PrintMemory("main: b",100000000,"add");
   PrintMemory("main: c",100000000,"add");
   PrintMemory("main: d",200000000,"add");
   PrintMemory("main: d",100000000,"add");
   PrintMemory("main: a",500000000,"add");
   PrintMemory("main: a",300000000,"add");
   PrintMemory("main: f",200000000,"add");

}
#endif