OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
memory_use_c.c
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23#include "hardware.inc"
24
25#define _FCALL
26
27
28#if CPP_mach==CPP_linux || CPP_mach == CPP_linux64_spmd || CPP_mach == CPP_p4linux964_spmd || CPP_mach == CPP_il_spmd || CPP_mach == CPP_il || CPP_mach == CPP_linux_spmd || CPP_mach == CPP_linux || CPP_mach == CPP_linux964 || CPP_mach == CPP_p4linux964 || CPP_mach == CPP_p4linux932
29
30/* ----- */
31/* LINUX */
32/* ----- */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <sys/types.h>
37#include <unistd.h>
38#include <string.h>
39
40void map_memory(int * VmPeak, int *VmSize,int *VmRSS,int *VmHWM,int *VmStk )
41{
42 int pid;
43 FILE * stream;
44 char fil[512],cmd_line[512],line[512],str1[128],str2[128],size[128],size2[128],size3[128],size4[128],size5[128];
45 char * rd;
46 int i,slen,non_convertible,nreadi,VmP,VmS;
47
48 pid = getpid();
49
50 sprintf(fil,"/proc/%i/status",pid);
51 stream = fopen(fil,"r");
52
53 *VmPeak=-1;
54 *VmSize=-1;
55 *VmRSS=-1;
56 *VmHWM=-1;
57 *VmStk=-1;
58
59 while (fgets(line,512,stream)){
60 if (strncmp(line,"VmPeak",6)==0){
61 sscanf(line,"%s %s %s",str1,size,str2);
62 *VmPeak=atoi(size)/1024;
63 }
64
65 if (strncmp(line,"VmSize",6)==0){
66 sscanf(line,"%s %s %s",str1,size2,str2);
67 *VmSize=atoi(size2)/1024;
68 }
69
70 if (strncmp(line,"VmRSS:",6)==0){
71 sscanf(line,"%s %s %s",str1,size3,str2);
72 *VmRSS=atoi(size3)/1024;
73 }
74
75 if (strncmp(line,"VmHWM:",6)==0){
76 sscanf(line,"%s %s %s",str1,size4,str2);
77 *VmHWM=atoi(size4)/1024;
78 }
79
80 if (strncmp(line,"VmStk:",6)==0){
81 sscanf(line,"%s %s %s",str1,size5,str2);
82 *VmStk=atoi(size5)/1024;
83 }
84 }
85 fflush(stdout);
86 fclose(stream);
87}
88
89
91{
92 int pid;
93 FILE * stream;
94 char fil[512],cmd_line[512],line[512],str1[128],str2[128],size[128],size2[128],size3[128],size4[128];
95 char * rd;
96 int i,slen,non_convertible,nreadi,VmP,VmS;
97
98 pid = getpid();
99
100 sprintf(fil,"/proc/%i/status",pid);
101 stream = fopen(fil,"r");
102 VmP=0;
103 VmS=0;
104 while (fgets(line,512,stream)){
105 if (strncmp(line,"VmPeak",6)==0){
106 sscanf(line,"%s %s %s",str1,size,str2);
107 printf(" %s %s %s \n",str1,size,str2);
108
109 }
110 if (strncmp(line,"VmSize",6)==0){
111 sscanf(line,"%s %s %s",str1,size2,str2);
112 printf(" %s %s %s \n",str1,size2,str2);
113 }
114 if (strncmp(line,"VmRSS:",6)==0){
115 sscanf(line,"%s %s %s",str1,size3,str2);
116 printf(" %s %s %s \n",str1,size3,str2);
117 }
118 if (strncmp(line,"VmHWM:",6)==0){
119 sscanf(line,"%s %s %s",str1,size4,str2);
120 printf(" %s %s %s \n",str1,size4,str2);
121 }
122 if (strncmp(line,"VmStk:",6)==0){
123 sscanf(line,"%s %s %s",str1,size4,str2);
124 printf(" %s %s %s \n",str1,size4,str2);
125 }
126
127 }
128 fflush(stdout);
129 fclose(stream);
130}
131
132void memory_use_c(int *memsize)
133{
134 int pid;
135 FILE * stream;
136 char fil[512],cmd_line[512],line[512],str1[128],str2[128],size[128],size2[128];
137 char * rd;
138 int i,slen,non_convertible,nreadi,VmP,VmS;
139 *memsize=-1;
140
141 pid = getpid();
142
143 sprintf(fil,"/proc/%i/status",pid);
144 stream = fopen(fil,"r");
145 VmP=0;
146 VmS=0;
147 while (fgets(line,512,stream)){
148 if (strncmp(line,"VmPeak",6)==0){
149 VmP=1;
150 sscanf(line,"%s %s %s",str1,size,str2);
151 break;
152 }
153 if (strncmp(line,"VmSize",6)==0){
154 VmS=1;
155 sscanf(line,"%s %s %s",str1,size2,str2);
156 }
157 }
158
159 non_convertible=0;
160 if (VmP==1){
161 slen = strlen(size);
162 for (i=0;i<slen;i++){if (size[i] < '0' && size[i] > '9') non_convertible=1;}
163 if (non_convertible==0){*memsize = atoi(size)/1024;}else{*memsize = -1;}
164 }
165 else{
166 if (VmS==1){
167 slen = strlen(size2);
168 for (i=0;i<slen;i++){
169 if (size2[i] < '0' && size2[i] > '9') non_convertible=1;
170 }
171 if (non_convertible==0){*memsize = atoi(size2)/1024;}else{*memsize = -1;}
172 }else {*memsize = -1;}
173 }
174 fclose(stream);
175}
176
177
178#elif CPP_mach==CPP_p4win64_spmd || CPP_mach==CPP_win64_spmd || CPP_mach==CPP_p4win64 || CPP_mach==CPP_p4win32
179
180/* ------- */
181/* WINDOWS */
182/* ------- */
183#include <stdio.h>
184#include <Windows.h>
185#include <Psapi.h>
186
187void memory_use_c(int *memsize)
188{
189 PROCESS_MEMORY_COUNTERS MemInfo;
190 *memsize=-1;
191 GetProcessMemoryInfo(GetCurrentProcess(), &MemInfo, sizeof(MemInfo));
192 *memsize = MemInfo.PeakWorkingSetSize / 1048576;
193}
194
195
196void map_memory(int * VmPeak, int *VmSize,int *VmRSS,int *VmHWM,int *VmStk )
197{
198 int pid;
199 FILE * stream;
200 char fil[512],cmd_line[512],line[512],str1[128],str2[128],size[128],size2[128],size3[128],size4[128],size5[128];
201 char * rd;
202 int i,slen,non_convertible,nreadi,VmP,VmS;
203 PROCESS_MEMORY_COUNTERS MemInfo;
204
205
206 *VmPeak=-1;
207 *VmSize=-1;
208 *VmRSS=-1;
209 *VmHWM=-1;
210 *VmStk=-1;
211 GetProcessMemoryInfo(GetCurrentProcess(), &MemInfo, sizeof(MemInfo));
212
213 *VmPeak = MemInfo.PeakWorkingSetSize / 1048576;
214 *VmSize = MemInfo.WorkingSetSize / 1048576;
215
216 }
217
218
219
220
221#elif 1
222/* --------------- */
223/* Default Routine */
224/* --------------- */
225void memory_use_c(int *memsize)
226{
227 *memsize=-1;
228}
229
230void map_memory(int * VmPeak, int *VmSize,int *VmRSS,int *VmHWM,int *VmStk )
231{
232 *VmPeak=-1;
233 *VmSize=-1;
234 *VmRSS=-1;
235 *VmHWM=-1;
236 *VmStk=-1;
237}
238
239#endif
240
241
242
243void memory_use_c_(int *memsize)
244{
245 memory_use_c(memsize);
246}
247
248void memory_use_c__(int *memsize)
249{
250 memory_use_c(memsize);
251}
252
253
254void _FCALL MEMORY_USE_C (int *memsize)
255{
256 memory_use_c(memsize);
257}
258
260{
261 printf("Not Available under Windows\n");
262}
263
264
265/* ---------------------- */
266void map_memory_(int * VmPeak, int *VmSize,int *VmRSS,int *VmHWM,int *VmStk )
267{
268 map_memory(VmPeak,VmSize,VmRSS,VmHWM,VmStk );
269}
270
271void map_memory__(int * VmPeak, int *VmSize,int *VmRSS,int *VmHWM,int *VmStk )
272{
273 map_memory(VmPeak,VmSize,VmRSS,VmHWM,VmStk );
274}
275
276void _FCALL MAP_MEMORY(int * VmPeak, int *VmSize,int *VmRSS,int *VmHWM,int *VmStk )
277{
278 map_memory(VmPeak,VmSize,VmRSS,VmHWM,VmStk );
279}
280
void _FCALL MEMORY_STATS_C()
void map_memory_(int *VmPeak, int *VmSize, int *VmRSS, int *VmHWM, int *VmStk)
void memory_use_c(int *memsize)
void memory_use_c__(int *memsize)
void map_memory__(int *VmPeak, int *VmSize, int *VmRSS, int *VmHWM, int *VmStk)
void memory_use_c_(int *memsize)
void _FCALL MAP_MEMORY(int *VmPeak, int *VmSize, int *VmRSS, int *VmHWM, int *VmStk)
void map_memory(int *VmPeak, int *VmSize, int *VmRSS, int *VmHWM, int *VmStk)
void memory_stats_c_()
#define _FCALL
void _FCALL MEMORY_USE_C(int *memsize)
subroutine slen(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4, j, area, aream)
Definition slen.F:31