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
187
188void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
189{
190 FILE * stream = NULL;
191 char * hname2;
192 int len,j,error;
193 char hname1[512];
194 int nRet;
195 WSADATA wsaData;
196 WORD version = MAKEWORD(1,1);
197
198 HKEY handle;
199 char RegKey_Entry[]="HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
200 char query1[] = "ProcessorNameString";
201 SYSTEM_INFO info;
202 int mxlen1=512;
203 DWORD mhz;
204 int l = sizeof(mhz);
205#if CPP_mach==CPP_wnt
206 MEMORYSTATUS statex;
207#elif 1
208 MEMORYSTATUSEX statex;
209#endif
210
211/*hostname*/
212 nRet = WSAStartup(version, &wsaData);
213 len=gethostname(hostname,256);
214 len=WSAGetLastError();
215 *lenhost = strlen(hostname);
216
217/* CPU type*/
218 /* registry based access */
219 if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE,RegKey_Entry,0, KEY_QUERY_VALUE, &handle ) != ERROR_SUCCESS ) goto FAIL1;
220 if ( RegQueryValueEx (handle,query1, NULL, NULL,(LPBYTE) hname1,(LPDWORD) &mxlen1 ) != ERROR_SUCCESS ) goto FAIL1;
221 RegCloseKey ( handle );
222
223 GetSystemInfo(&info);
224 if (info.dwProcessorType < 8664) {
225 hname2="x86";
226 }
227 else { hname2="x86_64"; }
228
229 sprintf(cputype,"%s (%s)",hname1,hname2);
230 goto CONTINUE;
231
232FAIL1:
233 sprintf(cputype,"UNKOWN");
234
235CONTINUE:
236
237 *lencputype = strlen(cputype);
238
239/*Frequence*/
240
241 if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE,
242 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
243 0, KEY_QUERY_VALUE, &handle ) != ERROR_SUCCESS )
244 return;
245 if ( RegQueryValueEx ( handle, "~MHz", NULL, NULL,
246 (LPBYTE )(&mhz) ,(LPDWORD) &l ) != ERROR_SUCCESS )
247 return;
248 RegCloseKey ( handle );
249 *frequence = (int)(mhz/50.0 +0.5)*50;
250
251/*memory,swap */
252 *memory=-1;
253 *swap=-1;
254#if CPP_mach==CPP_wnt
255 statex.dwLength = sizeof (statex);
256
257 GlobalMemoryStatus (&statex);
258 *memory = statex.dwAvailPhys/(1024*1024);
259
260 *swap=statex.dwTotalPageFile/(1024*1024);
261#elif 1
262 statex.dwLength = sizeof (statex);
263 GlobalMemoryStatusEx (&statex);
264
265 *memory = statex.ullAvailPhys/(1024*1024);
266 *swap=statex.ullTotalPageFile/(1024*1024);
267
268#endif
269
270}
271
272
273#elif CPP_mach ==CPP_hp11 || CPP_mach == CPP_hp11_spmd
274
275/* HP-UX */
276
277#include <stdio.h>
278#include <string.h>
279#include <stdlib.h>
280#include <sys/pstat.h>
281#include <sys/param.h>
282#include <unistd.h>
283
284
285void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
286{
287 char ligne[256];
288 char freq[256];
289 char cpt[256];
290 char cmdline[512];
291 char *ret;
292 int size,i,j,sz;
293 int debut;
294
295/*utilise pour frequence*/
296 struct pst_processor psp;
297 struct pst_dynamic psd;
298 struct pst_static pst;
299
300 unsigned long int clock_speed, scclktick;
301
302 FILE * stream = NULL;
303
304 stream = popen ("hostname","r");
305 fgets(ligne,256, stream);
306
307/* hostname */
308 fgets(ligne,256, stream);
309 size = strlen(ligne);
310 for ( i=0 ; i< size-1; i++) {
311 hostname[i] = ligne[i] ;
312 }
313 hostname[size-1] = '\0';
314 *lenhost = strlen(hostname);
315 pclose(stream);
316
317/* cputype */
318 stream = popen ("model","r");
319 fgets(ligne,256, stream);
320 size = strlen(ligne);
321 for ( i=0 ; i< size-1; i++) {
322 cputype[i] = ligne[i] ;
323 }
324
325 cputype[size-1] = '\0';
326 pclose(stream);
327
328/* a faire sur HP PA ONLY */
329#if CPP_mach==CPP_hp11_spmd && CPP_rel != 600|| CPP_mach==CPP_hp11 && CPP_rel != 600
330 debut=0;
331 j = 0;
332 do {
333 debut ++;
334 } while (ligne[debut] != '/');
335
336 do {
337 debut++;
338 cpt[j] = ligne[debut];
339 j++;
340 } while (ligne[debut] != '/');
341 cpt[j-1] = '\0';
342
343 sprintf(cmdline," grep -i %s /usr/sam/lib/mo/sched.models | awk '{print $3}'",cpt);
344 stream = popen (cmdline,"r");
345 fgets(ligne,256, stream);
346 sz = strlen(ligne);
347
348 cputype[size-1] = ' ';
349 cputype[size] = '-';
350 size++;
351 cputype[size] = ' ';
352 size++;
353
354 for (i=0;i<sz-1;i++) {
355 cputype[size]=ligne[i];
356 size++;
357 }
358 cputype[size]='\0';
359 pclose(stream);
360#endif
361 *lencputype = strlen(cputype);
362
363
364/* frequence */
365 pstat_getprocessor(&psp, sizeof(psp), 1, 0);
366 scclktick=sysconf(_SC_CLK_TCK);
367 clock_speed = psp.psp_iticksperclktick * scclktick;
368 clock_speed = (clock_speed/1000000);
369 *frequence = clock_speed;
370
371/*memory */
372 pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0);
373 *memory=pst.physical_memory*pst.page_size/(1024*1024);
374
375/*swap*/
376 pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0);
377 *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;
378
379}
380
381
382/* IBM */
383
384#elif CPP_mach == CPP_pwr4_spmd || CPP_mach ==CPP_pwr4
385
386#include <stdio.h>
387#include <string.h>
388#include <stdlib.h>
389#include <unistd.h>
390
391
392
393void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
394
395{
396 char ligne[256],swaps[256];
397 char freq[256];
398 char cpt[256];
399 char cmdline[512];
400 char *ret;
401 int size,i,j,sz;
402 int debut, lswaps;
403 FILE * stream = NULL;
404
405 stream = popen ("hostname","r");
406 fgets(ligne,256, stream);
407
408/* hostname */
409 fgets(ligne,256, stream);
410 size = strlen(ligne);
411 for ( i=0 ; i< size-1; i++) {
412 hostname[i] = ligne[i] ;
413 }
414 hostname[size-1] = '\0';
415 pclose(stream);
416
417 *lenhost = strlen(hostname);
418
419 stream = popen ("lscfg | grep proc | awk '{print $2}'","r");
420
421 fgets(ligne,256, stream);
422 size = strlen(ligne);
423 for (i=0;i<size-1;i++) cpt[i]=ligne[i];
424 cpt[size-1]='\0';
425 pclose(stream);
426
427 sprintf(cmdline,"lsattr -El %s | grep frequency | awk '{print $2}'",cpt);
428
429 stream = popen (cmdline,"r");
430 fgets(ligne,256, stream);
431 pclose(stream);
432/* frequence */
433 *frequence = atoi(ligne) / 1000000;
434
435
436/* cputype */
437 sprintf(cmdline,"lsattr -El %s | grep type",cpt);
438
439 stream = popen (cmdline,"r");
440 fgets(ligne,256, stream);
441 pclose(stream);
442
443 debut = 0;
444 do {
445 debut++;
446 } while (ligne[debut]!=' ');
447 debut++;
448 do {
449 debut++;
450 } while (ligne[debut]==' ');
451
452 i=0;
453 do {
454 cputype[i] = ligne[debut] ;
455 i++;
456 debut++;
457 } while (ligne[debut]!=' ');
458 pclose(stream);
459 cputype[i] = '\0';
460
461 *lencputype=strlen(cputype);
462
463/*memory */
464 *memory=-1;
465 sprintf(cmdline,"lsattr -El mem0 | awk '{print $2}' ");
466 stream = popen (cmdline,"r");
467 fgets(ligne,256, stream);
468 pclose(stream);
469
470 *memory = atoi(ligne);
471/*swap*/
472 *swap=-1;
473 sprintf(cmdline,"lsps -s | awk '{print $1}' ");
474 stream = popen (cmdline,"r");
475 fgets(ligne,256, stream);
476 fgets(ligne,256, stream);
477 pclose(stream);
478 lswaps = 0;
479 for (i=0;i<strlen(ligne);i++){
480 if (ligne[i]>='0' && ligne[i]<='9'){
481 swaps[lswaps]=ligne[i];
482 lswaps++;
483 }
484 }
485 swaps[lswaps]='\0';
486 *swap = atoi(swaps);
487 /*lsps -a*/
488}
489
490
491#elif CPP_mach==CPP_sun25 || CPP_mach==CPP_sol10x64_spmd
492#include <stdlib.h>
493#include <unistd.h>
494#include <strings.h>
495#include <stdio.h>
496#include <sys/types.h>
497#include <sys/processor.h>
498#include <sys/systeminfo.h>
499
500#if CPP_rel == 1000 || CPP_mach==CPP_sol10x64_spmd
501#include <kstat.h>
502#include <inttypes.h>
503#endif
504
505
506void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
507{
508 processor_info_t infop;
509 char ligne[256],swaps[256];
510 int i,resus,size;
511 int proc_id=0;
512 int me,debut;
513 int lswap=0;
514 int count=0;
515 FILE * stream = NULL;
516#if CPP_rel == 1000 || CPP_mach==CPP_sol10x64_spmd
517 kstat_ctl_t *kc;
518 kstat_t *ksp;
519 kstat_named_t *knp;
520#endif
521
522 stream = popen ("hostname","r");
523 fgets(ligne,256, stream);
524
525/* hostname */
526 fgets(ligne,256, stream);
527 size = strlen(ligne);
528 for ( i=0 ; i< size-1; i++) {
529 hostname[i] = ligne[i] ;
530 }
531 hostname[size-1] = '\0';
532 *lenhost = strlen(hostname);
533 pclose(stream);
534
535/* cputype, frequency */
536 processor_info(proc_id, &infop);
537#if CPP_rel == 1000 || CPP_mach==CPP_sol10x64_spmd
538 kc = kstat_open();
539 ksp = kstat_lookup(kc, "cpu_info", -1, NULL);
540 kstat_read(kc, ksp, NULL);
541 knp = kstat_data_lookup(ksp, "brand");
542 sprintf(cputype,"%s",knp->value);
543#elif 1
544 strcpy(cputype,infop.pi_processor_type);
545#endif
546 *lencputype = strlen(cputype);
547
548 *frequence = infop.pi_clock;
549
550/*memory */
551 *memory=-1;
552
553*memory = (int)sysconf(_SC_PHYS_PAGES)/1024 * sysconf(_SC_PAGESIZE)/1024;
554
555/*swap*/
556 stream = popen("swap -s","r");
557 fgets(ligne,256, stream);
558 pclose(stream);
559 *swap=-1;
560
561 swaps[0]='\0';
562 debut=0;
563 for (i=0;i<strlen(ligne);i++)
564 {
565 if (ligne[i]>='0' && ligne[i]<='9')
566 {
567 swaps[lswap]=ligne[i];
568 lswap++;
569 debut=1;
570 }
571 else {
572 if (debut==1){
573 if (count<3){
574 lswap=0;
575 debut=0;
576 count++;
577 }
578 else
579 { break;}
580 }
581 }
582 }
583 *swap = atoi(swaps)/1024;
584
585}
586
587#elif CPP_mach==CPP_macosx64
588
589#include <stdio.h>
590#include <stdlib.h>
591#include <sys/types.h>
592#include <sys/sysctl.h>
593#include <unistd.h>
594#include <strings.h>
595
596
597void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
598{
599 FILE * stream = NULL;
600 long freq,frq,mem;
601 size_t ilen,lenh;
602 char swapstring[256],s1[256],s2[256],s3[256],s4[256];
603 ilen=8;
604 lenh=256;
605 mem=0;
606 /*----------*/
607 /* Hostname */
608 /*----------*/
609
610 gethostname(hostname,lenh);
611 *lenhost = strlen(hostname);
612
613 /*-----------*/
614 /* CPU brand */
615 /*-----------*/
616
617 sysctlbyname("machdep.cpu.brand_string", cputype, &lenh, NULL, 0);
618 *lencputype = strlen(cputype);
619
620 /*---------------*/
621 /* CPU Frequency */
622 /*---------------*/
623
624 sysctlbyname("hw.cpufrequency",&freq,&ilen, NULL, 0);
625 frq = freq/1000000;
626 *frequence = (int)frq;
627
628 /*---------------*/
629 /* System Memory */
630 /*---------------*/
631
632 sysctlbyname("hw.physmem",&mem,&ilen, NULL, 0);
633 mem = mem / 1048576 ;
634 *memory = (int) mem;
635
636 /*---------------*/
637 /* Swap */
638 /*---------------*/
639 stream = popen ("df -k /","r");
640 fgets(swapstring,256, stream);
641 fgets(swapstring,256, stream);
642
643 sscanf(swapstring,"%s %s %s %s",s1,s2,s3,s4);
644 *swap = atoi(s4)/1024;
645
646}
647
648
649#elif 1
650
651#include <stdio.h>
652#include <string.h>
653#include <stdlib.h>
654
655
656void cpuinfo_c(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
657{
658 sprintf(hostname,"UNKNOWN");
659 *lenhost = strlen(hostname);
660 sprintf(cputype,"UNKNOWN");
661 *lencputype = strlen(cputype);
662 *frequence = 0;
663
664/*memory */
665 *memory=-1;
666/*swap*/
667 *swap=-1;
668}
669
670#endif
671
672
673void cpuinfo(char *hostname,int *lenhost, char *cputype, int *lencputype,int *frequence,int *memory, int *swap )
674{
675 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
676}
677
678void cpuinfo_(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
679{
680 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
681}
682
683void cpuinfo__(char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
684{
685 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
686}
687
688void _FCALL CPUINFO (char *hostname,int *lenhost, char *cputype, int *lencputype, int *frequence,int *memory, int *swap )
689{
690 cpuinfo_c(hostname,lenhost, cputype,lencputype, frequence,memory, swap );
691}
692
693
694
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