Line data Source code
1 : /*
2 : ** This file was downloaded and modified from https://github.com/jmrosinski/GPTL
3 : ** with the following Copyright permission,
4 : **
5 : ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 : ** of this software and associated documentation files (the “Software”), to deal
7 : ** in the Software for any noncommercial purposes without restriction, including
8 : ** without limitation the rights to use, copy, modify, merge, publish,
9 : ** distribute, sublicense, and/or sell copies of the Software, and to permit
10 : ** persons to whom the Software is furnished to do so, subject to the following
11 : ** conditions: The above copyright notice and this permission notice shall be
12 : ** included in all copies or substantial portions of the Software. Any
13 : ** commercial use (including sale) of the software, and derivative development
14 : ** towards commercial use, requires written permission of the copyright
15 : ** holder. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
16 : ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 : ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18 : ** EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 : ** OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 : ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 : ** DEALINGS IN THE SOFTWARE.
22 : **
23 : */
24 :
25 : /*
26 : ** $Id: get_memusage.c,v 1.10 2010-11-09 19:08:53 rosinski Exp $
27 : **
28 : ** Author: Jim Rosinski
29 : ** Credit to Chuck Bardeen for MACOS section (__APPLE__ ifdef)
30 : **
31 : ** get_memusage:
32 : **
33 : ** Designed to be called from Fortran, returns information about memory
34 : ** usage in each of 5 input int* args. On Linux read from the /proc
35 : ** filesystem because getrusage() returns placebos (zeros). Return -1 for
36 : ** values which are unavailable or ambiguous on a particular architecture.
37 : **
38 : ** Return value: 0 = success
39 : ** -1 = failure
40 : */
41 :
42 : #define _NO_CHANGE 0
43 : #define _UPPER_CASE 1
44 : #define _ADD_UNDERSCORE 2
45 : #define _ADD_TWO_UNDERSCORES 3
46 :
47 : #ifdef FORTRANUNDERSCORE
48 : #define NAMING _ADD_UNDERSCORE
49 : #endif
50 :
51 : #ifdef FORTRANDOUBLEUNDERSCORE
52 : #define NAMING _ADD_TWO_UNDERSCORES
53 : #endif
54 :
55 : #ifdef FORTRANCAPS
56 : #define NAMING _UPPER_CASE
57 : #endif
58 :
59 : #ifndef NAMING
60 : #define NAMING _NO_CHANGE
61 : #endif
62 :
63 : #if (NAMING == _ADD_UNDERSCORE)
64 : #define ice_memusage_gptl ice_memusage_gptl_
65 : #endif
66 :
67 : #if (NAMING == _ADD_TWO_UNDERSCORES)
68 : #define ice_memusage_gptl ice_memusage_gptl__
69 : #endif
70 :
71 : #if (NAMING == _UPPER_CASE)
72 : #define ice_memusage_gptl ICE_MEMUSAGE_GPTL
73 : #endif
74 :
75 :
76 : #include <sys/resource.h>
77 :
78 : /*#include "gptl.h" */ /* additional cpp defs and function prototypes */
79 : /* extern int ice_memusage_gptl (int *, int *, int *, int *, int *); */
80 :
81 : /* _AIX is automatically defined when using the AIX C compilers */
82 : #ifdef _AIX
83 : #include <sys/times.h>
84 : #endif
85 :
86 : #ifdef IRIX64
87 : #include <sys/time.h>
88 : #endif
89 :
90 : #ifdef HAVE_SLASHPROC
91 :
92 : #include <sys/time.h>
93 : #include <sys/types.h>
94 : #include <stdio.h>
95 : #include <unistd.h>
96 :
97 : #elif (defined __APPLE__)
98 :
99 : #include <stdio.h>
100 : #include <stdlib.h>
101 : #include <unistd.h>
102 :
103 : #endif
104 :
105 : #ifdef BGP
106 :
107 : #include <spi/kernel_interface.h>
108 : #include <common/bgp_personality.h>
109 : #include <common/bgp_personality_inlines.h>
110 : #include <malloc.h>
111 : #define Personality _BGP_Personality_t
112 :
113 : #endif
114 :
115 : #ifdef BGQ
116 :
117 : #include <malloc.h>
118 : #include <spi/include/kernel/memory.h>
119 :
120 : #endif
121 :
122 0 : int ice_memusage_gptl (int *size, int *rss, int *share, int *text, int *datastack)
123 : {
124 : #if defined (BGP) || defined(BGQ)
125 :
126 : long long alloc;
127 : struct mallinfo m;
128 : #if defined (BGP)
129 : Personality pers;
130 : #endif
131 : #if defined (BGQ)
132 : uint64_t shared_mem_count;
133 : #endif
134 : long long total;
135 : int node_config;
136 :
137 : /* memory available */
138 : #if defined(BGP)
139 : Kernel_GetPersonality(&pers, sizeof(pers));
140 : total = BGP_Personality_DDRSizeMB(&pers);
141 :
142 : node_config = BGP_Personality_processConfig(&pers);
143 : if (node_config == _BGP_PERS_PROCESSCONFIG_VNM) total /= 4;
144 : else if (node_config == _BGP_PERS_PROCESSCONFIG_2x2) total /= 2;
145 : total *= 1024*1024;
146 :
147 : *size = total;
148 : #endif
149 :
150 : #if defined(BGQ)
151 : Kernel_GetMemorySize(KERNEL_MEMSIZE_SHARED, &shared_mem_count);
152 :
153 : shared_mem_count *= 1024*1024;
154 : *size = shared_mem_count;
155 :
156 : #endif
157 : /* total memory used - heap only (not static memory)*/
158 :
159 : m = mallinfo();
160 : alloc = m.hblkhd + m.uordblks;
161 :
162 : *rss = alloc;
163 : *share = -1;
164 : *text = -1;
165 : *datastack = -1;
166 :
167 :
168 : #elif (defined HAVE_SLASHPROC)
169 : FILE *fd; /* file descriptor for fopen */
170 : int pid; /* process id */
171 : static char *head = "/proc/"; /* part of path */
172 : static char *tail = "/statm"; /* part of path */
173 : char file[19]; /* full path to file in /proc */
174 : int dum; /* placeholder for unused return arguments */
175 : int ret; /* function return value */
176 :
177 : /*
178 : ** The file we want to open is /proc/<pid>/statm
179 : */
180 :
181 : pid = (int) getpid ();
182 : if (pid > 999999) {
183 : fprintf (stderr, "get_memusage: pid %d is too large\n", pid);
184 : return -1;
185 : }
186 :
187 : sprintf (file, "%s%d%s", head, pid, tail);
188 : if ((fd = fopen (file, "r")) < 0) {
189 : fprintf (stderr, "get_memusage: bad attempt to open %s\n", file);
190 : return -1;
191 : }
192 :
193 : /*
194 : ** Read the desired data from the /proc filesystem directly into the output
195 : ** arguments, close the file and return.
196 : */
197 :
198 : ret = fscanf (fd, "%d %d %d %d %d %d %d",
199 : size, rss, share, text, datastack, &dum, &dum);
200 : ret = fclose (fd);
201 : return 0;
202 :
203 : #elif (defined __APPLE__)
204 :
205 : FILE *fd;
206 : char cmd[60];
207 : int pid = (int) getpid ();
208 :
209 : sprintf (cmd, "ps -o vsz -o rss -o tsiz -p %d | grep -v RSS", pid);
210 : fd = popen (cmd, "r");
211 :
212 : if (fd) {
213 : fscanf (fd, "%d %d %d", size, rss, text);
214 : *share = -1;
215 : *datastack = -1;
216 : (void) pclose (fd);
217 : }
218 :
219 : return 0;
220 :
221 : #else
222 :
223 : struct rusage usage; /* structure filled in by getrusage */
224 :
225 0 : if (getrusage (RUSAGE_SELF, &usage) < 0)
226 0 : return -1;
227 :
228 0 : *size = -1;
229 0 : *rss = usage.ru_maxrss;
230 0 : *share = -1;
231 0 : *text = -1;
232 0 : *datastack = -1;
233 : #ifdef IRIX64
234 : *datastack = usage.ru_idrss + usage.ru_isrss;
235 : #endif
236 0 : return 0;
237 :
238 : #endif
239 : }
|