source: CPL/oasis3-mct/branches/OASIS3-MCT_5.0_branch/lib/psmile/src/GPTLget_memusage.c @ 6331

Last change on this file since 6331 was 6331, checked in by aclsce, 17 months ago

Moved oasis-mct_5.0 in oasis3-mct/branches directory.

File size: 4.0 KB
Line 
1/*
2** $Id: get_memusage.c,v 1.10 2010-11-09 19:08:53 rosinski Exp $
3**
4** Author: Jim Rosinski
5**   Credit to Chuck Bardeen for MACOS section (__APPLE__ ifdef)
6**
7** get_memusage:
8**
9**   Designed to be called from Fortran, returns information about memory
10**   usage in each of 5 input int* args.  On Linux read from the /proc
11**   filesystem because getrusage() returns placebos (zeros).  Return -1 for
12**   values which are unavailable or ambiguous on a particular architecture.
13**
14**   Return value: 0  = success
15**                 -1 = failure
16*/
17
18#include <sys/resource.h>
19
20/*#include "gptl.h" */   /* additional cpp defs and function prototypes */
21/* extern int gptlget_memusage_ (int *, int *, int *, int *, int *);  */
22
23/* _AIX is automatically defined when using the AIX C compilers */
24#ifdef _AIX
25#include <sys/times.h>
26#endif
27
28#ifdef IRIX64
29#include <sys/time.h>
30#endif
31
32#ifdef __APPLE__
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37
38#elif (defined HAVE_SLASHPROC)
39
40#include <sys/time.h>
41#include <sys/types.h>
42#include <stdio.h>
43#include <unistd.h>
44
45#endif
46
47#ifdef BGP
48
49#include <spi/kernel_interface.h>
50#include <common/bgp_personality.h>
51#include <common/bgp_personality_inlines.h>
52#include <malloc.h>
53#define   Personality                    _BGP_Personality_t
54
55#endif
56
57#ifdef BGQ
58
59#include <malloc.h>
60#include <spi/include/kernel/memory.h>
61
62#endif
63
64int gptlget_memusage (int *size, int *rss, int *share, int *text, int *datastack)
65{
66#if defined (BGP) || defined(BGQ)
67
68  long long alloc;
69  struct mallinfo m;
70#if defined (BGP)
71  Personality pers;
72#endif
73#if defined (BGQ)
74  uint64_t shared_mem_count;
75#endif
76  long long total;
77  int node_config;
78 
79 /* memory available */
80#if defined(BGP)
81  Kernel_GetPersonality(&pers, sizeof(pers));
82  total = BGP_Personality_DDRSizeMB(&pers);
83
84  node_config  = BGP_Personality_processConfig(&pers);
85  if (node_config == _BGP_PERS_PROCESSCONFIG_VNM) total /= 4;
86  else if (node_config == _BGP_PERS_PROCESSCONFIG_2x2) total /= 2;
87  total *= 1024*1024;
88
89  *size = total;
90#endif
91
92#if defined(BGQ)
93  Kernel_GetMemorySize(KERNEL_MEMSIZE_SHARED, &shared_mem_count);
94
95  shared_mem_count *= 1024*1024;
96  *size = shared_mem_count;
97
98#endif
99  /* total memory used  - heap only (not static memory)*/
100
101  m = mallinfo();
102  alloc = m.hblkhd + m.uordblks;
103
104  *rss = alloc;
105  *share     = -1;
106  *text     = -1;
107  *datastack = -1;
108
109#elif (defined __APPLE__)
110
111  FILE *fd;
112  char cmd[60]; 
113  int pid = (int) getpid ();
114 
115  sprintf (cmd, "ps -o vsz -o rss -o tsiz -p %d | grep -v RSS", pid);
116  fd = popen (cmd, "r");
117
118  if (fd) {
119    fscanf (fd, "%d %d %d", size, rss, text);
120    *share     = -1;
121    *datastack = -1;
122    (void) pclose (fd);
123  }
124
125  return 0;
126
127#elif (defined HAVE_SLASHPROC)
128  FILE *fd;                       /* file descriptor for fopen */
129  int pid;                        /* process id */
130  static char *head = "/proc/";   /* part of path */
131  static char *tail = "/statm";   /* part of path */
132  char file[19];                  /* full path to file in /proc */
133  int dum;                        /* placeholder for unused return arguments */
134  int ret;                        /* function return value */
135
136  /*
137  ** The file we want to open is /proc/<pid>/statm
138  */
139
140  pid = (int) getpid ();
141  if (pid > 999999) {
142    fprintf (stderr, "get_memusage: pid %d is too large\n", pid);
143    return -1;
144  }
145
146  sprintf (file, "%s%d%s", head, pid, tail);
147  if ((fd = fopen (file, "r")) < 0) {
148    fprintf (stderr, "get_memusage: bad attempt to open %s\n", file);
149    return -1;
150  }
151
152  /*
153  ** Read the desired data from the /proc filesystem directly into the output
154  ** arguments, close the file and return.
155  */
156
157  ret = fscanf (fd, "%d %d %d %d %d %d %d", 
158                size, rss, share, text, datastack, &dum, &dum);
159  ret = fclose (fd);
160  return 0;
161
162#else
163
164  struct rusage usage;         /* structure filled in by getrusage */
165
166  if (getrusage (RUSAGE_SELF, &usage) < 0)
167    return -1;
168 
169  *size      = -1;
170  *rss       = usage.ru_maxrss;
171  *share     = -1;
172  *text      = -1;
173  *datastack = -1;
174#ifdef IRIX64
175  *datastack = usage.ru_idrss + usage.ru_isrss;
176#endif
177  return 0;
178
179#endif
180}
Note: See TracBrowser for help on using the repository browser.