SURFEX v8.1
General documentation of Surfex
gethwm.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include "getstatm.h"
5 
6 typedef long long int ll_t;
7 
8 static ll_t maxhwm = 0;
9 
10 #if defined(CRAY) && !defined(SV2)
11 #define gethwm GETHWM
12 #else
13 #define gethwm gethwm_
14 #endif
15 
16 #ifdef RS6K
17 
18 #if defined(__64BIT__)
19 /* Assume AIX >= 5.1 with 64-bit addressing */
20 #include <fcntl.h>
21 #include <sys/procfs.h>
22 ll_t
24 {
25  static int fd = -9999;
26  static char *heapbase = NULL;
27  ll_t heapsize = 0;
28 
29  if (fd == -9999) {
30  pstatus_t pstatus;
31  char procfile[80];
32  int pid = getpid();
33  sprintf(procfile,"/proc/%d/status",pid);
34  fd = open(procfile, O_RDONLY);
35  if (read(fd, &pstatus, sizeof(pstatus)) == sizeof(pstatus)) {
36  heapbase = (char *)pstatus.pr_brkbase;
37  close(fd);
38  fd = 0;
39  }
40  }
41 
42  if (fd == 0 && heapbase != NULL) {
43  heapsize = (ll_t)((char *)sbrk(0) - heapbase);
44  }
45 
46  return heapsize;
47 }
48 
49 #else
50 
51 ll_t
52 gethwm()
53 {
54  extern ll_t getrss_();
55  return getrss_();
56 }
57 
58 #endif /* defined(__64BIT__) */
59 
60 #else /* non-RS6K */
61 
62 #if defined(_CRAYC)
63 ll_t
64 gethwm()
65 {
67  return get_tcmalloc_heap_size_();
68 }
69 #elif defined(LINUX)
70 static ll_t basesize = -1;
71 static size_t pagesize = 4096;
72 ll_t gethwm()
73 {
74  struct statm sm;
75  ll_t rc = 0;
76  if (getstatm(&sm) == 0) {
77  if (basesize < 0) { /* the very first time */
78  basesize = sm.size;
79  pagesize = getpagesize();
80  if (pagesize <= 0) pagesize = 4096;
81  }
82  rc = (sm.size - basesize) * pagesize;
83  if (rc > maxhwm) maxhwm = rc;
84  }
85  return rc;
86 }
87 
88 #elif defined(NECSX)
89 
90 ll_t
91 gethwm()
92 {
93  extern ll_t getrss_();
94  return getrss_();
95 }
96 
97 #else
98 ll_t gethwm()
99 {
100  ll_t rc = (ll_t)((char *)sbrk(0) - (char *)0);
101  return rc;
102 }
103 #endif
104 
105 #endif
106 
107 #if defined(SV2)
108 int getpid_()
109 {
110  return getpid();
111 }
112 
113 unsigned int sleep_(unsigned int seconds)
114 {
115  return sleep(seconds);
116 }
117 
118 #endif
119 
121 {
122  (void) gethwm_();
123  return maxhwm;
124 }
int getstatm(struct statm *sm)
Definition: getstatm.c:7
static ll_t basesize
Definition: gethwm.c:70
int getpid_()
Definition: gethwm.c:108
long long int ll_t
Definition: gethwm.c:6
long long int ll_t
Definition: privpub.h:293
ll_t gethwm()
Definition: gethwm.c:23
long long int gethwm_()
static size_t pagesize
Definition: gethwm.c:71
Definition: getstatm.h:16
size_t get_tcmalloc_heap_size_()
unsigned int sleep_(unsigned int seconds)
Definition: gethwm.c:113
int size
Definition: getstatm.h:18
static ll_t maxhwm
Definition: gethwm.c:8
ll_t getmaxhwm_()
Definition: gethwm.c:120
pid_t pid
Definition: opfla_perfmon.c:22
long long int getrss_()