Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_MemoryUtils.C

raw
1 // Copyright(C) 1999-2023 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_MemoryUtils.h"
8
9 // For memory utilities...
10 #if defined(__IOSS_WINDOWS__)
11 #include <windows.h>
12 #elif defined(__unix__) || defined(__unix) || defined(unix) || \
13 (defined(__APPLE__) && defined(__MACH__))
14 #include <sys/resource.h>
15 #include <unistd.h>
16
17 #if defined(__APPLE__) && defined(__MACH__) && (defined(__arm__) || defined(__arm64__))
18 #include <mach/arm/kern_return.h>
19 #endif
20
21 #if defined(__APPLE__) && defined(__MACH__)
22 #include <mach/kern_return.h>
23 #include <mach/mach_init.h>
24 #include <mach/message.h>
25 #include <mach/task.h>
26 #include <mach/task_info.h>
27
28 #elif (defined(_AIX) || defined(__TOS__AIX__)) || \
29 (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
30 #include <fcntl.h>
31 #include <procfs.h>
32
33 #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
34 #include <stdio.h>
35 #endif
36 #endif
37
38 #if defined(BGQ_LWK) && defined(__linux__)
39 #include <spi/include/kernel/location.h>
40 #include <spi/include/kernel/memory.h>
41 #endif
42
43 size_t Ioss::MemoryUtils::get_memory_info()
44 {
45 // Code from http://nadeausoftware.com/sites/NadeauSoftware.com/files/getRSS.c
46 size_t memory_usage = 0;
47 #if defined(__IOSS_WINDOWS__)
48 #if 0
49 /* Windows -------------------------------------------------- */
50 PROCESS_MEMORY_COUNTERS info;
51 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
52 memory_usage = (size_t)info.WorkingSetSize;
53 #else
54 memory_usage = 0;
55 #endif
56
57 #elif defined(__APPLE__) && defined(__MACH__)
58 kern_return_t error;
59 mach_msg_type_number_t outCount;
60 mach_task_basic_info_data_t taskinfo{};
61
62 taskinfo.virtual_size = 0;
63 outCount = MACH_TASK_BASIC_INFO_COUNT;
64 error = task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
65 reinterpret_cast<task_info_t>(&taskinfo), &outCount);
66 if (error == KERN_SUCCESS) {
67 memory_usage = taskinfo.resident_size;
68 }
69 #elif __linux__
70 #if defined(BGQ_LWK)
71 uint64_t heap;
72 Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &heap);
73 memory_usage = heap;
74 #else
75 // On Linux, the /proc pseudo-file system contains a directory for
76 // each running or zombie process. The /proc/[pid]/stat,
77 // /proc/[pid]/statm, and /proc/[pid]/status pseudo-files for the
78 // process with id [pid] all contain a process's current resident
79 // set size, among other things. But the /proc/[pid]/statm
80 // pseudo-file is the easiest to read since it contains a single
81 // line of text with white-space delimited values:
82 //
83 // * total program size
84 // * resident set size
85 // * shared pages
86 // * text (code) size
87 // * library size
88 // * data size (heap + stack)
89 // * dirty pages
90 //
91 // The second value provides the process's current resident set size
92 // in pages. To get the field for the current process, open
93 // /proc/self/statm and parse the second integer value. Multiply the
94 // field by the page size from sysconf( ).
95
96 long rss = 0L;
97 FILE *fp = NULL;
98 if ((fp = fopen("/proc/self/statm", "r")) == NULL)
99 return (size_t)0L; // Can't open? */
100 if (fscanf(fp, "%*s%ld", &rss) != 1) {
101 fclose(fp);
102 return (size_t)0L; // Can't read? */
103 }
104 fclose(fp);
105 memory_usage = (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
106 #endif
107 #endif
108 return memory_usage;
109 }
110
111 size_t Ioss::MemoryUtils::get_hwm_memory_info()
112 {
113 // Code from http://nadeausoftware.com/sites/NadeauSoftware.com/files/getRSS.c
114 size_t memory_usage = 0;
115 #if defined(__IOSS_WINDOWS__)
116 #if 0
117 /* Windows -------------------------------------------------- */
118 PROCESS_MEMORY_COUNTERS info;
119 GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
120 memory_usage = (size_t)info.PeakWorkingSetSize;
121 #else
122 memory_usage = 0;
123 #endif
124
125 #elif (defined(_AIX) || defined(__TOS__AIX__)) || \
126 (defined(__sun__) || defined(__sun) || defined(sun) && (defined(__SVR4) || defined(__svr4__)))
127 /* AIX and Solaris ------------------------------------------ */
128 struct psinfo psinfo;
129 int fd = -1;
130 if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
131 return (size_t)0L; /* Can't open? */
132 if (read(fd, &psinfo, sizeof(psinfo)) != sizeof(psinfo)) {
133 close(fd);
134 return (size_t)0L; /* Can't read? */
135 }
136 close(fd);
137 memory_usage = (size_t)(psinfo.pr_rssize * 1024L);
138
139 #elif (defined(__APPLE__) && defined(__MACH__)) || (defined(__linux__) && !defined(BGQ_LWK))
140 /* BSD, Linux, and OSX -------------------------------------- */
141 struct rusage rusage;
142 getrusage(RUSAGE_SELF, &rusage);
143 #if defined(__APPLE__) && defined(__MACH__)
144 memory_usage = (size_t)rusage.ru_maxrss;
145 #else
146 memory_usage = (size_t)(rusage.ru_maxrss * 1024L);
147 #endif
148 #endif
149 return memory_usage;
150 }