OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
mon_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
26#if CPP_mach == CPP_w95 || CPP_mach == CPP_win64_spmd || CPP_mach == CPP_p4win64_spmd || CPP_mach == CPP_wnt || CPP_mach == CPP_wmr || CPP_mach == CPP_p4win64 || CPP_mach == CPP_p4win32
27
28#define _FCALL
29
30#elif 1
31#define _FCALL
32#endif
33
34
35#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
36
37/* Linux : X86, X86-64, IA64*/
38
39#include <stdio.h>
40#include <string.h>
41#include <stdlib.h>
42#include <sys/sysinfo.h>
43#include <unistd.h>
44#include <sys/utsname.h>
45
46
47void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
48{
49
50 FILE * stream = NULL;
51 char ligne[256];
52 char freq[256];
53 int size,i,j;
54 int flag = 0;
55 int debut;
56 float fre;
57 struct sysinfo info;
58 struct utsname unam;
59#if CPP_mach==CPP_il || CPP_mach==CPP_il_spmd
60 char * compstr="family :";
61#elif 1
62 char * compstr="model name";
63#endif
64 char * compMHZ="cpu MHz";
65 int cpMHZ=strlen(compMHZ);
66 int cmplen=strlen(compstr);
67 int unknown;
68 int lenmach;
69
70 unknown = 1;
71 j= 0;
72
73 stream=fopen("/proc/cpuinfo","r");
74
75 /* ------------ */
76 /* find cputype */
77 /* ------------ */
78 uname(&unam);
79 lenmach=strlen(unam.machine);
80
81 while (fgets(ligne,255,stream) && unknown){
82 if (strncmp(compstr,ligne,cmplen)==0){
83 unknown = 0;
84 break;
85 }
86 }
87
88 if (unknown == 0){
89 size = strlen(ligne);
90 debut = 12;
91 /*Elimination de blanc au debut*/
92 do
93 {
94 debut=debut+1;
95 }
96 while (ligne[debut]==' ' );
97
98 for (i=debut; i<size-1;i++) {
99 if (ligne[i]>31){
100 cputype[j] = ligne[i];
101 j=j+1 ;
102 }
103 }
104 cputype[j] = ' ' ;
105 j=j+1 ;
106 cputype[j] = '(' ;
107 j=j+1 ;
108 for (i=0; i<lenmach;i++) {
109 cputype[j] = unam.machine[i];
110 j=j+1 ;
111 }
112 cputype[j] = ')' ;
113 j=j+1 ;
114 cputype[j]='\0';
115
116 }else{
117 sprintf(cputype,"Unknown");
118 }
119 *lencputype = strlen(cputype);
120
121 /* -------------- */
122 /* find Frequence */
123 /* -------------- */
124
125 unknown = 1;
126 while (fgets(ligne,255,stream) && unknown){
127 if (strncmp(compMHZ,ligne,cpMHZ)==0){
128 unknown = 0;
129 break;
130 }
131 }
132 if ( unknown == 0){
133#if CPP_mach==CPP_il || CPP_mach==CPP_il_spmd
134 debut = 13;
135#elif 1
136 debut = 10;
137#endif
138 j=0;
139 size = strlen(ligne);
140 for (i=debut; i<size-1;i++) {
141 freq[j] = ligne[i];
142 j=j+1 ;
143 }
144 freq[j]='\0';
145#if CPP_mach==CPP_il || CPP_mach==CPP_il_spmd
146 fre = atof(freq);
147 *frequence = fre;
148#elif 1
149 *frequence = atoi(freq);
150#endif
151 }
152 else{
153 *frequence = -1;
154 }
155
156 fclose(stream);
157
158 gethostname(hostname, 256);
159 *lenhost = strlen(hostname);
160
161 /*memory - swap */
162 sysinfo ( &info );
163 *memory = info.totalram*info.mem_unit/(1024*1024);
164 *swap = info.freeswap*info.mem_unit/(1024*1024);
165
166
167}
168
169#elif CPP_mach == CPP_wmr || CPP_mach==CPP_wnt || CPP_mach==CPP_p4win64_spmd || CPP_mach==CPP_win64_spmd || CPP_mach==CPP_p4win64 || CPP_mach==CPP_p4win32
170/* WIN XP - WIN SERVER 2003 - WIN SERVER 2003 CCE */
171
172#include <windows.h>
173#include <winbase.h>
174#include <stdio.h>
175#include <string.h>
176#include <stdlib.h>
177#include <math.h>
178#include <process.h>
179#include <io.h>
180#include <sys\types.h>
181
182#if CPP_mach == CPP_p4win64_spmd || CPP_mach==CPP_p4win64 || CPP_mach==CPP_p4win32 || CPP_mach==CPP_wnt && CPP_rel==10
183#define popen _popen
184#define pclose _pclose
185#endif
186
187void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
188{
189 FILE * stream = NULL;
190 char * hname2;
191 int len,j,error;
192 char hname1[512];
193 int nRet;
194 WSADATA wsaData;
195 WORD version = MAKEWORD(1,1);
196
197 HKEY handle;
198 char RegKey_Entry[]="HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
199 char query1[] = "ProcessorNameString";
200 SYSTEM_INFO info;
201 int mxlen1=512;
202 DWORD mhz;
203 int l = sizeof(mhz);
204#if CPP_mach==CPP_wnt
205 MEMORYSTATUS statex;
206#elif 1
207 MEMORYSTATUSEX statex;
208#endif
209
210/*hostname*/
211 nRet = WSAStartup(version, &wsaData);
212 len=gethostname(hostname,256);
213 len=WSAGetLastError();
214 *lenhost = strlen(hostname);
215
216/* CPU type*/
217 /* registry based access */
218 if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE,RegKey_Entry,0, KEY_QUERY_VALUE, &handle ) != ERROR_SUCCESS ) goto FAIL1;
219 if ( RegQueryValueEx (handle,query1, NULL, NULL,(LPBYTE) hname1,(LPDWORD) &mxlen1 ) != ERROR_SUCCESS ) goto FAIL1;
220 RegCloseKey ( handle );
221
222 GetSystemInfo(&info);
223 if (info.dwProcessorType < 8664) {
224 hname2="x86";
225 }
226 else { hname2="x86_64"; }
227
228 sprintf(cputype,"%s (%s)",hname1,hname2);
229 goto CONTINUE;
230
231FAIL1:
232 sprintf(cputype,"UNKOWN");
233
234CONTINUE:
235
236 *lencputype = strlen(cputype);
237
238/*Frequence*/
239
240 if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE,
241 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
242 0, KEY_QUERY_VALUE, &handle ) != ERROR_SUCCESS )
243 return;
244 if ( RegQueryValueEx ( handle, "~MHz", NULL, NULL,
245 (LPBYTE )(&mhz) ,(LPDWORD) &l ) != ERROR_SUCCESS )
246 return;
247 RegCloseKey ( handle );
248 *frequence = (int)(mhz/50.0 +0.5)*50;
249
250/*memory,swap */
251 *memory=-1;
252 *swap=-1;
253#if CPP_mach==CPP_wnt
254 statex.dwLength = sizeof (statex);
255
256 GlobalMemoryStatus (&statex);
257 *memory = statex.dwAvailPhys/(1024*1024);
258
259 *swap=statex.dwTotalPageFile/(1024*1024);
260#elif 1
261 statex.dwLength = sizeof (statex);
262 GlobalMemoryStatusEx (&statex);
263
264 *memory = statex.ullAvailPhys/(1024*1024);
265 *swap=statex.ullTotalPageFile/(1024*1024);
266
267#endif
268
269}
270
271
272#elif CPP_mach ==CPP_hp11 || CPP_mach == CPP_hp11_spmd
273
274/* HP-UX */
275
276#include <stdio.h>
277#include <string.h>
278#include <stdlib.h>
279#include <sys/pstat.h>
280#include <sys/param.h>
281#include <unistd.h>
282
283
284void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
285{
286 char ligne[256];
287 char freq[256];
288 char cpt[256];
289 char cmdline[512];
290 char *ret;
291 int size,i,j,sz;
292 int debut;
293
294/*utilise pour frequence*/
295 struct pst_processor psp;
296 struct pst_dynamic psd;
297 struct pst_static pst;
298
299 unsigned long int clock_speed, scclktick;
300
301 FILE * stream = NULL;
302
303 stream = popen ("hostname","r");
304 fgets(ligne,256, stream);
305
306/* hostname */
307 fgets(ligne,256, stream);
308 size = strlen(ligne);
309 for ( i=0 ; i< size-1; i++) {
310 hostname[i] = ligne[i] ;
311 }
312 hostname[size-1] = '\0';
313 *lenhost = strlen(hostname);
314 pclose(stream);
315
316/* cputype */
317 stream = popen ("model","r");
318 fgets(ligne,256, stream);
319 size = strlen(ligne);
320 for ( i=0 ; i< size-1; i++) {
321 cputype[i] = ligne[i] ;
322 }
323
324 cputype[size-1] = '\0';
325 pclose(stream);
326
327/* a faire sur HP PA ONLY */
328#if CPP_mach==CPP_hp11_spmd && CPP_rel != 600|| CPP_mach==CPP_hp11 && CPP_rel != 600
329 debut=0;
330 j = 0;
331 do {
332 debut ++;
333 } while (ligne[debut] != '/');
334
335 do {
336 debut++;
337 cpt[j] = ligne[debut];
338 j++;
339 } while (ligne[debut] != '/');
340 cpt[j-1] = '\0';
341
342 sprintf(cmdline," grep -i %s /usr/sam/lib/mo/sched.models | awk '{print $3}'",cpt);
343 stream = popen (cmdline,"r");
344 fgets(ligne,256, stream);
345 sz = strlen(ligne);
346
347 cputype[size-1] = ' ';
348 cputype[size] = '-';
349 size++;
350 cputype[size] = ' ';
351 size++;
352
353 for (i=0;i<sz-1;i++) {
354 cputype[size]=ligne[i];
355 size++;
356 }
357 cputype[size]='\0';
358 pclose(stream);
359#endif
360 *lencputype = strlen(cputype);
361
362
363/* frequence */
364 pstat_getprocessor(&psp, sizeof(psp), 1, 0);
365 scclktick=sysconf(_SC_CLK_TCK);
366 clock_speed = psp.psp_iticksperclktick * scclktick;
367 clock_speed = (clock_speed/1000000);
368 *frequence = clock_speed;
369
370/*memory */
371 pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0);
372 *memory=pst.physical_memory*pst.page_size/(1024*1024);
373
374/*swap*/
375 pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0);
376 *swap = ((psd.psd_vm*pst.page_size - pst.physical_memory*pst.page_size)>0)?(psd.psd_vm*pst.page_size - pst.physical_memory*pst.page_size)/(1024*1024):0;
377
378}
379
380
381/* IBM */
382
383#elif CPP_mach == CPP_pwr4_spmd || CPP_mach ==CPP_pwr4
384
385#include <stdio.h>
386#include <string.h>
387#include <stdlib.h>
388#include <unistd.h>
389
390
391void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
392
393{
394 char ligne[256],swaps[256];
395 char freq[256];
396 char cpt[256];
397 char cmdline[512];
398 char *ret;
399 int size,i,j,sz;
400 int debut, lswaps;
401 FILE * stream = NULL;
402
403 stream = popen ("hostname","r");
404 fgets(ligne,256, stream);
405
406/* hostname */
407 fgets(ligne,256, stream);
408 size = strlen(ligne);
409 for ( i=0 ; i< size-1; i++) {
410 hostname[i] = ligne[i] ;
411 }
412 hostname[size-1] = '\0';
413 pclose(stream);
414
415 *lenhost = strlen(hostname);
416
417 stream = popen ("lscfg | grep proc | awk '{print $2}'","r");
418
419 fgets(ligne,256, stream);
420 size = strlen(ligne);
421 for (i=0;i<size-1;i++) cpt[i]=ligne[i];
422 cpt[size-1]='\0';
423 pclose(stream);
424
425 sprintf(cmdline,"lsattr -El %s | grep frequency | awk '{print $2}'",cpt);
426
427 stream = popen (cmdline,"r");
428 fgets(ligne,256, stream);
429 pclose(stream);
430/* frequence */
431 *frequence = atoi(ligne) / 1000000;
432
433
434/* cputype */
435 sprintf(cmdline,"lsattr -El %s | grep type",cpt);
436
437 stream = popen (cmdline,"r");
438 fgets(ligne,256, stream);
439 pclose(stream);
440
441 debut = 0;
442 do {
443 debut++;
444 } while (ligne[debut]!=' ');
445 debut++;
446 do {
447 debut++;
448 } while (ligne[debut]==' ');
449
450 i=0;
451 do {
452 cputype[i] = ligne[debut] ;
453 i++;
454 debut++;
455 } while (ligne[debut]!=' ');
456 pclose(stream);
457 cputype[i] = '\0';
458
459 *lencputype=strlen(cputype);
460
461/*memory */
462 *memory=-1;
463 sprintf(cmdline,"lsattr -El mem0 | awk '{print $2}' ");
464 stream = popen (cmdline,"r");
465 fgets(ligne,256, stream);
466 pclose(stream);
467
468 *memory = atoi(ligne);
469/*swap*/
470 *swap=-1;
471 sprintf(cmdline,"lsps -s | awk '{print $1}' ");
472 stream = popen (cmdline,"r");
473 fgets(ligne,256, stream);
474 fgets(ligne,256, stream);
475 pclose(stream);
476 lswaps = 0;
477 for (i=0;i<strlen(ligne);i++){
478 if (ligne[i]>='0' && ligne[i]<='9'){
479 swaps[lswaps]=ligne[i];
480 lswaps++;
481 }
482 }
483 swaps[lswaps]='\0';
484 *swap = atoi(swaps);
485 /*lsps -a*/
486}
487
488
489#elif CPP_mach==CPP_sun25 || CPP_mach==CPP_sol10x64_spmd
490#include <stdlib.h>
491#include <unistd.h>
492#include <strings.h>
493#include <stdio.h>
494#include <sys/types.h>
495#include <sys/processor.h>
496#include <sys/systeminfo.h>
497
498#if CPP_rel == 1000 || CPP_mach==CPP_sol10x64_spmd
499#include <kstat.h>
500#include <inttypes.h>
501#endif
502
503
504void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
505{
506 processor_info_t infop;
507 char ligne[256],swaps[256];
508 int i,resus,size;
509 int proc_id=0;
510 int me,debut;
511 int lswap=0;
512 int count=0;
513 FILE * stream = NULL;
514#if CPP_rel == 1000 || CPP_mach==CPP_sol10x64_spmd
515 kstat_ctl_t *kc;
516 kstat_t *ksp;
517 kstat_named_t *knp;
518#endif
519
520 stream = popen ("hostname","r");
521 fgets(ligne,256, stream);
522
523/* hostname */
524 fgets(ligne,256, stream);
525 size = strlen(ligne);
526 for ( i=0 ; i< size-1; i++) {
527 hostname[i] = ligne[i] ;
528 }
529 hostname[size-1] = '\0';
530 *lenhost = strlen(hostname);
531 pclose(stream);
532
533/* cputype, frequency */
534 processor_info(proc_id, &infop);
535#if CPP_rel == 1000 || CPP_mach==CPP_sol10x64_spmd
536 kc = kstat_open();
537 ksp = kstat_lookup(kc, "cpu_info", -1, NULL);
538 kstat_read(kc, ksp, NULL);
539 knp = kstat_data_lookup(ksp, "brand");
540 sprintf(cputype,"%s",knp->value);
541#elif 1
542 strcpy(cputype,infop.pi_processor_type);
543#endif
544 *lencputype = strlen(cputype);
545
546 *frequence = infop.pi_clock;
547
548/*memory */
549 *memory=-1;
550
551*memory = (int)sysconf(_SC_PHYS_PAGES)/1024 * sysconf(_SC_PAGESIZE)/1024;
552
553/*swap*/
554 stream = popen("swap -s","r");
555 fgets(ligne,256, stream);
556 pclose(stream);
557 *swap=-1;
558
559 swaps[0]='\0';
560 debut=0;
561 for (i=0;i<strlen(ligne);i++)
562 {
563 if (ligne[i]>='0' && ligne[i]<='9')
564 {
565 swaps[lswap]=ligne[i];
566 lswap++;
567 debut=1;
568 }
569 else {
570 if (debut==1){
571 if (count<3){
572 lswap=0;
573 debut=0;
574 count++;
575 }
576 else
577 { break;}
578 }
579 }
580 }
581 *swap = atoi(swaps)/1024;
582
583}
584
585#elif CPP_mach==CPP_macosx64
586
587#include <stdio.h>
588#include <stdlib.h>
589#include <sys/types.h>
590#include <sys/sysctl.h>
591#include <unistd.h>
592#include <strings.h>
593
594
595void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
596{
597 FILE * stream = NULL;
598 long freq,frq,mem;
599 size_t ilen,lenh;
600 char swapstring[256],s1[256],s2[256],s3[256],s4[256];
601 ilen=8;
602 lenh=256;
603 mem=0;
604 /*----------*/
605 /* Hostname */
606 /*----------*/
607
608 gethostname(hostname,lenh);
609 *lenhost = strlen(hostname);
610
611 /*-----------*/
612 /* CPU brand */
613 /*-----------*/
614
615 sysctlbyname("machdep.cpu.brand_string", cputype, &lenh, NULL, 0);
616 *lencputype = strlen(cputype);
617
618 /*---------------*/
619 /* CPU Frequency */
620 /*---------------*/
621
622 sysctlbyname("hw.cpufrequency",&freq,&ilen, NULL, 0);
623 frq = freq/1000000;
624 *frequence = (int)frq;
625
626 /*---------------*/
627 /* System Memory */
628 /*---------------*/
629
630 sysctlbyname("hw.physmem",&mem,&ilen, NULL, 0);
631 mem = mem / 1048576 ;
632 *memory = (int) mem;
633
634 /*---------------*/
635 /* Swap */
636 /*---------------*/
637 stream = popen ("df -k /","r");
638 fgets(swapstring,256, stream);
639 fgets(swapstring,256, stream);
640
641 sscanf(swapstring,"%s %s %s %s",s1,s2,s3,s4);
642 *swap = atoi(s4)/1024;
643
644}
645
646
647#elif 1
648
649#include <stdio.h>
650#include <string.h>
651#include <stdlib.h>
652
653
654void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
655{
656 sprintf(hostname,"UNKNOWN");
657 *lenhost = strlen(hostname);
658 sprintf(cputype,"UNKNOWN");
659 *lencputype = strlen(cputype);
660 *frequence = 0;
661
662/*memory */
663 *memory=-1;
664/*swap*/
665 *swap=-1;
666}
667
668#endif
669
670
671void cpuinfo(char *hostname,int *lenhost, char *cputype, int *lencputype,int *frequence,int *memory, int *swap )
672{
673 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
674}
675
676void cpuinfo_(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
677{
678 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
679}
680
681void cpuinfo__(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
682{
683 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
684}
685
686void _FCALL CPUINFO (char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
687{
688 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
689}
690
691
void * handle
void cpuinfo_(char *hostname, int *lenhost, char *cputype, int *lencputype, int *frequence, int *memory, int *swap)
Definition mon_c.c:678
void _FCALL CPUINFO(char *hostname, int *lenhost, char *cputype, int *lencputype, int *frequence, int *memory, int *swap)
Definition mon_c.c:688
void cpuinfo__(char *hostname, int *lenhost, char *cputype, int *lencputype, int *frequence, int *memory, int *swap)
Definition mon_c.c:683
void cpuinfo_c(char *hostname, int *lenhost, char *cputype, int *lencputype, int *frequence, int *memory, int *swap)
Definition mon_c.c:47
void cpuinfo(char *hostname, int *lenhost, char *cputype, int *lencputype, int *frequence, int *memory, int *swap)
Definition mon_c.c:673
#define swap(a, b, tmp)
Definition macros.h:40
#define _FCALL
static int proc_id
Definition rad2rad_c.c:126
*fortran !University of Stuttgart All rights reserved Inc All rights reserved ! $COPYRIGHT$ !Additional copyrights may follow ! $HEADER$ !WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING !Do ***not ***copy this file to the directory where your Fortran !fortran application is compiled unless it is absolutely necessary !Most !modern Fortran compilers now support the I command line flag
Definition mpif.h:26