SURFEX v8.1
General documentation of Surfex
getstk.c
Go to the documentation of this file.
1 #if defined(__64BIT__) && defined(RS6K)
2 #include <pthread.h> /* requires xlc_r or cc_r */
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 
9 typedef long long int ll_t;
10 
11 static ll_t maxstack = 0;
12 
13 #ifdef RS6K
14 
15 #if defined(__64BIT__)
16 /* Assume AIX >= 5.1 with 64-bit addressing */
17 
18 #include <fcntl.h>
19 #include <sys/procfs.h>
20 
21 static pthread_t master_tid;
22 
23 ll_t
25 {
26  static int fd = -9999;
27  static ll_t stackbase = 0;
28  ll_t stackused = 0;
29 
30  if (fd == -9999) {
31  pstatus_t pstatus;
32  char procfile[80];
33  int pid = getpid();
34  master_tid = pthread_self();
35  snprintf(procfile,sizeof(procfile),"/proc/%d/status",pid);
36  fd = open(procfile, O_RDONLY);
37  if (read(fd, &pstatus, sizeof(pstatus)) == sizeof(pstatus)) {
38  stackbase = (ll_t)pstatus.pr_stkbase;
39  close(fd);
40  fd = 0;
41  }
42  }
43 
44  if (fd == 0 && stackbase > 0) {
45  int tid = pthread_self();
46  if (pthread_equal(tid, master_tid)) { /* the master thread */
47  char addr[1];
48  stackused = stackbase - (ll_t)addr;
49  if (stackused > maxstack) maxstack = stackused;
50  }
51  else {
52  stackused = 0; /* at the moment we cannot figure out
53  stack base address for slave thread(s) */
54  }
55  }
56 
57  return stackused;
58 }
59 
60 #else
61 
62 ll_t
64 {
65  extern ll_t getstackusage_();
66  static ll_t init_stack = -1;
67  ll_t stackused = 0;
68  if (init_stack == -1) init_stack = getstackusage_();
69  stackused = getstackusage_() - init_stack;
70  if (stackused > maxstack) maxstack = stackused;
71  return stackused;
72 }
73 
74 #endif /* defined(__64BIT__) */
75 
76 #else /* non-RS6K */
77 
78 #if defined(LINUX)
79 
80 ll_t getstk_()
81 {
82  extern ll_t getstackusage_();
83  ll_t stackused = getstackusage_();
84  if (stackused > maxstack) maxstack = stackused;
85  return stackused;
86 }
87 
88 #else
89 ll_t
90 getstk_()
91 {
92 #if defined(CRAY)
93  return 0;
94 #else
95  extern ll_t getstackusage_();
96  static ll_t init_stack = -1;
97  ll_t stackused = 0;
98  if (init_stack == -1) init_stack = getstackusage_();
99  stackused = getstackusage_() - init_stack;
100  if (stackused > maxstack) maxstack = stackused;
101  return stackused;
102 #endif
103 }
104 
105 #endif
106 #endif
107 
108 /* Maximum stacksize encountered */
109 
110 ll_t
112 {
113  (void) getstk_();
114  return maxstack;
115 }
static pthread_t master_tid
Definition: getstk.c:21
long long int ll_t
Definition: privpub.h:293
static ll_t maxstack
Definition: getstk.c:11
ll_t getstackusage_()
Definition: getstackusage.c:12
int snprintf(char *str, size_t size, const char *format,...)
Definition: endian.c:108
subroutine init_stack(K)
Definition: stack_mix.F90:18
ll_t getstk_()
Definition: getstk.c:24
ll_t getmaxstk_()
Definition: getstk.c:111
long long int ll_t
Definition: getstk.c:9
pid_t pid
Definition: opfla_perfmon.c:22