-
Notifications
You must be signed in to change notification settings - Fork 5
/
sysinfofuncs.c
1202 lines (1045 loc) · 39.9 KB
/
sysinfofuncs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ----------------------------------------------------------------------
*
* coNCePTuaL run-time library:
* internal functions for acquiring system information
*
* By Scott Pakin <[email protected]>
*
* ----------------------------------------------------------------------
*
*
* Copyright (C) 2003, Triad National Security, LLC
* All rights reserved.
*
* Copyright (2003). Triad National Security, LLC. This software
* was produced under U.S. Government contract 89233218CNA000001 for
* Los Alamos National Laboratory (LANL), which is operated by Los
* Alamos National Security, LLC (Triad) for the U.S. Department
* of Energy. The U.S. Government has rights to use, reproduce,
* and distribute this software. NEITHER THE GOVERNMENT NOR TRIAD
* MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY
* FOR THE USE OF THIS SOFTWARE. If software is modified to produce
* derivative works, such modified software should be clearly marked,
* so as not to confuse it with the version available from LANL.
*
* Additionally, redistribution and use in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Triad National Security, LLC, Los Alamos
* National Laboratory, the U.S. Government, nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TRIAD AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TRIAD OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* ----------------------------------------------------------------------
*/
#include "runtimelib.h"
/**********
* Macros *
**********/
/* For convenience, combine multiple #if checks into one. */
#if defined(HAVE_INVENT_H) && defined(HAVE_GETINVENT)
# define GETINVENT_OKAY
#endif
#if defined(HAVE_SYS_SYSMP_H) && defined(HAVE_SYSMP)
# define SYSMP_OKAY
#endif
/* Assign a variable unless it's already been assigned. */
#define ASSIGN(LHS,RHS) LHS = (LHS) ? (LHS) : (RHS)
/************************************
* Imported variables and functions *
************************************/
extern char *ncptl_concatenate_strings (ncptl_int numstrings, ...);
extern int ncptl_fork_works;
/************************************
* Internal variables and functions *
************************************/
#ifdef HAVE_BGLPERSONALITY
/* Store the personality of the BG/L partition. */
BGLPersonality ncptl_bgl_personality;
#endif
#ifdef HAVE_BGPPERSONALITY
/* Store the personality of the BG/P partition. */
_BGP_Personality_t ncptl_bgp_personality;
#endif
#ifdef HAVE_PCIUTILS
/* Note if an error occurred while processing the PCI bus. */
int pciutils_error = 0;
#endif
/* Given a "key : value\n" string, copy and return key. */
static char *extract_key (char *key_value)
{
char *eos;
char *result = ncptl_strdup (key_value);
for (eos=strchr(result, ':'); eos>=result && isspace((int)eos[-1]); eos--)
eos[-1] = '\0';
return result;
}
/* Given a "key : value\n" string, copy and return value. */
static char *extract_value (char *key_value)
{
char *colonptr = strchr (key_value, ':');
char *result = ncptl_strdup (colonptr+2);
result[strlen(result)-1] = '\0'; /* Remove the newline character */
return result;
}
/* Read and return the first NCPTL_MAX_LINE_LEN bytes of a given file
* or NULL on failure. If is_binary is set, do no extra processing.
* If is_binary is not set, truncate the text at the first newline
* character. */
static char *read_first_line (char *filename, int is_binary)
{
FILE *fh;
char *oneline;
char *c;
if (!(fh = fopen(filename, is_binary ? "rb" : "r")))
return NULL;
oneline = ncptl_malloc(NCPTL_MAX_LINE_LEN+1, 0);
memset(oneline, 0, NCPTL_MAX_LINE_LEN+1);
if (fread(oneline, 1, NCPTL_MAX_LINE_LEN, fh) == 0) {
/* Treat a read of zero bytes as a failure. */
fclose(fh);
ncptl_free(oneline);
return NULL;
}
fclose(fh);
if (!is_binary) {
for (c=oneline; *c != '\n' && *c != '\r' && *c != '\0'; c++)
;
*c = '\0'; /* Safe because we allocated N+1 bytes but read only N */
oneline = ncptl_realloc (oneline, c-oneline+1, 0);
}
return oneline;
}
#ifdef HAVE_SYSCTL
# ifdef CTL_HW
/* Return the value of an integer sysctl variable. */
static unsigned int get_sysctl_int (int category, int variable)
{
int mib[2];
unsigned int result = 0;
size_t intsize = sizeof(unsigned int);
mib[0] = category;
mib[1] = variable;
(void) sysctl (mib, 2, (void *)&result, &intsize, NULL, 0);
return result;
}
#endif
# if defined(CTL_KERN) || defined(CTL_HW)
/* Return the value of a string sysctl variable. The caller must
* ncptl_free() the result. */
static char *get_sysctl_string (int category, int variable)
{
int mib[2];
char resultstr[NCPTL_MAX_LINE_LEN];
size_t stringsize = NCPTL_MAX_LINE_LEN;
mib[0] = category;
mib[1] = variable;
if (!sysctl (mib, 2, (void *)resultstr, &stringsize, NULL, 0))
return ncptl_strdup (resultstr);
else
return NULL;
}
# endif
#endif
#ifdef HAVE_HAL
/* Connect to the HAL daemon and read a property string from it. The
* caller must ncptl_free() the result. NULL is returned if the UDI
* or property is unavailable. */
static char *get_hal_property_string (const char *udi, const char *propname)
{
LibHalContext *hal_ctx; /* HAL context */
DBusConnection *conn; /* Connection to the DBus */
DBusError errval; /* Error value returned from the dbus library */
char *propvalue; /* Value corresponding to {uid, propname} */
char *propvaluecopy; /* Copy of the above to return to the user */
/* Connect to the DBus. */
dbus_error_init (&errval);
if (!(conn=dbus_bus_get(DBUS_BUS_SYSTEM, &errval))) {
if (dbus_error_is_set (&errval))
dbus_error_free (&errval);
return NULL;
}
/* Create a HAL context (which implies connecting to the HAL daemon). */
if (!(hal_ctx=libhal_ctx_new()))
return NULL;
if (!libhal_ctx_set_dbus_connection(hal_ctx, conn))
return NULL;
if (!libhal_ctx_init(hal_ctx, &errval)) {
if (dbus_error_is_set (&errval))
dbus_error_free (&errval);
return NULL;
}
/* Query HAL for the property. */
propvalue = libhal_device_get_property_string (hal_ctx, udi, propname, &errval);
if (!propvalue) {
if (dbus_error_is_set (&errval))
dbus_error_free (&errval);
return NULL;
}
propvaluecopy = ncptl_strdup (propvalue);
libhal_free_string (propvalue);
/* Destroy our HAL context. */
libhal_ctx_shutdown (hal_ctx, &errval);
libhal_ctx_free (hal_ctx);
/* Disconnect from the DBus. */
dbus_connection_unref (conn);
dbus_error_free (&errval);
/* Return the coNCePTuaL-allocated string. */
return propvaluecopy;
}
/* Fill in various pieces of system information using HAL. */
static void fill_in_sys_desc_hal (SYSTEM_INFORMATION *info)
{
const char *computer_udi = "/org/freedesktop/Hal/devices/computer";
/* Try to read the computer make and model information. */
if (!info->computer) {
char *vendor;
char *product;
/* Read the computer vendor and product. */
vendor = get_hal_property_string(computer_udi, "system.vendor");
product = get_hal_property_string(computer_udi, "system.product");
info->computer = ncptl_concatenate_strings (2, vendor, product);
ncptl_free (vendor);
ncptl_free (product);
}
/* Try to read the BIOS vendor and version. */
if (!info->bios) {
char *vendor;
char *version;
char *release_date;
vendor = get_hal_property_string(computer_udi, "smbios.bios.vendor");
version = get_hal_property_string(computer_udi, "smbios.bios.version");
release_date = get_hal_property_string(computer_udi, "smbios.bios.release_date");
info->bios = ncptl_concatenate_strings (3, vendor, version, release_date);
ncptl_free (vendor);
ncptl_free (version);
ncptl_free (release_date);
}
}
#endif
/* Fill in the name of the OS distribution if possible. */
static void fill_in_osdist (SYSTEM_INFORMATION *info)
{
/* Run the lsb_release script to determine the OS distribution. */
#if defined(HAVE_POPEN)
/* popen() uses fork() which may not work properly. */
if (ncptl_fork_works) {
FILE *lsb_pipe; /* Pipe from "lsb_release" */
char oneline[NCPTL_MAX_LINE_LEN]; /* One line read from lsb_pipe */
if ((lsb_pipe=popen("lsb_release -d 2>&1", "r"))) {
while (fgets (oneline, NCPTL_MAX_LINE_LEN-1, lsb_pipe))
if (!strncmp (oneline, "Description:", 12)) {
ASSIGN (info->osdist, ncptl_strdup (oneline+13));
break;
}
pclose(lsb_pipe);
}
}
if (info->osdist)
return;
#endif
/* Read the distribution from *-release (fedora-release,
* redhat-release, system-release, etc.). This is of course a bit
* risky because there may happen to be a binary *-release file in
* /etc. We simply take our chances and hope for the best. */
#ifdef HAVE_GLOB
if (1) {
glob_t globinfo; /* Information about the expanded pathname */
unsigned int i;
if (glob("/etc/*-release", 0, NULL, &globinfo) == 0) {
/* Iterate over each filename in turn. */
for (i=0; i<globinfo.gl_pathc && !info->osdist; i++)
ASSIGN (info->osdist, read_first_line(globinfo.gl_pathv[i], 0));
globfree(&globinfo);
}
}
#endif
}
/* Fill in the host, arch, os, osdist, and computer fields. */
static void fill_in_sys_desc (SYSTEM_INFORMATION *info)
{
#if defined(HAVE_UNAME) && defined(HAVE_SYS_UTSNAME_H)
struct utsname hostinfo; /* Various bit of description */
#endif
#ifdef HOST_NAME_MAX_VAR
char thishostname[HOST_NAME_MAX_VAR+1]; /* Host name only */
#endif
/* If we have /proc/version, try reading the OS version from there. */
if (!info->os)
info->os = read_first_line ("/proc/version", 0);
/* Try to determine the name of the OS distribution. */
fill_in_osdist(info);
#if defined(HAVE_SYSCTL) && defined(CTL_KERN) && defined(KERN_VERSION)
/* sysctl() can return the full OS name at times when uname() can't. */
ASSIGN (info->os, get_sysctl_string (CTL_KERN, KERN_VERSION));
#endif
#if defined(HAVE_UNAME) && defined(HAVE_SYS_UTSNAME_H)
if (uname (&hostinfo) != -1) {
ASSIGN (info->hostname, ncptl_strdup (hostinfo.nodename));
ASSIGN (info->arch, ncptl_strdup (hostinfo.machine));
if (!info->os) {
info->os = (char *) ncptl_malloc (strlen(hostinfo.sysname) + 1 +
strlen(hostinfo.release) + 1 +
strlen(hostinfo.version) + 1,
0);
sprintf (info->os, "%s %s %s",
hostinfo.sysname, hostinfo.release, hostinfo.version);
}
}
#endif
#ifdef HOST_NAME_MAX_VAR
if (!gethostname (thishostname, HOST_NAME_MAX_VAR))
ASSIGN (info->hostname, ncptl_strdup (thishostname));
#endif
if (info->hostname && info->hostname[0]=='\0') {
/* At the time of this writing, BlueGene/L returns an empty hostname. */
ncptl_free (info->hostname);
info->hostname = NULL;
ASSIGN (info->hostname, ncptl_strdup ("unknown"));
}
/* Try to replace the host name with a more "official" host name. */
if (info->hostname) {
#ifdef HAVE_GETADDRINFO
struct addrinfo hints;
struct addrinfo *thishostinfo;
memset (&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_CANONNAME;
if (getaddrinfo(info->hostname, NULL, &hints, &thishostinfo) == 0 &&
thishostinfo->ai_canonname && thishostinfo->ai_canonname[0] != '\0') {
ncptl_free (info->hostname);
info->hostname = ncptl_strdup (thishostinfo->ai_canonname);
}
#elif defined(HAVE_GETHOSTBYNAME)
struct hostent *thishostinfo = gethostbyname (info->hostname);
if (thishostinfo && thishostinfo->h_name && thishostinfo->h_name[0] != '\0') {
ncptl_free (info->hostname);
info->hostname = ncptl_strdup (thishostinfo->h_name);
}
#endif
}
#ifdef HAVE_HAL
fill_in_sys_desc_hal (info);
#endif
}
#ifdef USE_PAPI
/* Fill in the CPU-related fields using PAPI. */
static void fill_in_cpu_info_PAPI (SYSTEM_INFORMATION *info)
{
const PAPI_hw_info_t *hardwareinfo; /* Hardware (mostly CPU) information */
if ((hardwareinfo=PAPI_get_hardware_info())) {
ASSIGN (info->contexts_per_node, hardwareinfo->ncpu);
ASSIGN (info->cpu_vendor, ncptl_strdup (hardwareinfo->vendor_string));
ASSIGN (info->cpu_model, ncptl_strdup (hardwareinfo->model_string));
ASSIGN (info->cpu_freq, 1.0e6 * hardwareinfo->mhz);
}
}
#endif
#if defined(HAVE_SYSCTL) && defined(CTL_HW)
/* Fill in the CPU-related fields using sysctl(). */
static void fill_in_cpu_info_sysctl (SYSTEM_INFORMATION *info)
{
/* CPU speed */
#ifdef HW_NCPU
ASSIGN (info->contexts_per_node, (int) get_sysctl_int (CTL_HW, HW_NCPU));
#endif
#ifdef HW_MODEL
ASSIGN (info->cpu_model, get_sysctl_string (CTL_HW, HW_MODEL));
#endif
#ifdef HW_CPU_FREQ
ASSIGN (info->cpu_freq, (double) get_sysctl_int (CTL_HW, HW_CPU_FREQ));
#endif
#ifdef HW_CPUSPEED
ASSIGN (info->cpu_freq, 1.0e6 * get_sysctl_int (CTL_HW, HW_CPUSPEED));
#endif
/* Cycle-counter speed */
#ifdef HW_TB_FREQ
ASSIGN (info->timer_freq, (double) get_sysctl_int (CTL_HW, HW_TB_FREQ));
#endif
/* Keep compilers from complaining about unused info */
if (0)
printf ("%p", info);
}
#endif
/* Fill in the CPU-related fields using sysconf(). */
#ifdef HAVE_SYSCONF
static void fill_in_cpu_info_sysconf (SYSTEM_INFORMATION *info)
{
# ifdef _SC_NPROCESSORS_ONLN
/* Note that we store the number of processors online instead of the
* number of processors configured. The idea is to help detect poor
* performance caused by unexpected multiprogramming when a CPU is
* offline. */
if (sysconf(_SC_NPROCESSORS_ONLN) > 0)
ASSIGN (info->contexts_per_node, (int) sysconf(_SC_NPROCESSORS_ONLN));
#else
/* Keep compilers from complaining about unused info */
if (0)
printf ("%p", info);
# endif
}
#endif
#ifdef HAVE_KSTAT_DATA_LOOKUP
/* Fill in the CPU-related fields using kstat_data_lookup(). */
static void fill_in_cpu_info_kstat (SYSTEM_INFORMATION *info)
{
kstat_ctl_t *kcontrol; /* kstat control */
kstat_t *thekstat; /* The kstat itself */
kstat_named_t *kstatdata; /* Value encountered */
/* Open the kernel statistics. */
if (!(kcontrol=kstat_open()))
return;
/* CPU model and frequency */
if (!info->cpu_model &&
(thekstat=kstat_lookup(kcontrol, "cpu_info", -1, "cpu_info0")) &&
(kstat_read(kcontrol, thekstat, NULL) != -1)) {
/* CPU model */
if ((kstatdata=(kstat_named_t *)kstat_data_lookup (thekstat, "cpu_type")) &&
kstatdata->data_type == KSTAT_DATA_CHAR)
ASSIGN (info->cpu_model, ncptl_strdup (kstatdata->value.c));
/* CPU frequency */
if ((kstatdata=(kstat_named_t *)kstat_data_lookup (thekstat, "clock_MHz")) &&
kstatdata->data_type == KSTAT_DATA_INT32)
ASSIGN (info->cpu_freq, 1.0e6 * kstatdata->value.i32);
}
/* Number of CPUs */
if (!info->contexts_per_node &&
(thekstat=kstat_lookup(kcontrol, "unix", -1, "system_misc")) &&
(kstat_read(kcontrol, thekstat, NULL) != -1)) {
if ((kstatdata=(kstat_named_t *)kstat_data_lookup (thekstat, "ncpus")) &&
kstatdata->data_type == KSTAT_DATA_UINT32)
ASSIGN (info->contexts_per_node, (int)kstatdata->value.ui32);
}
/* Close the kernel statistics. */
(void) kstat_close (kcontrol);
}
#endif
#ifdef HAVE_BGPPERSONALITY
/* Fill in the CPU-related fields using BG/P's Kernel_GetPersonality(). */
static void fill_in_cpu_info_bgp (SYSTEM_INFORMATION *info)
{
ASSIGN (info->cpu_freq, ncptl_bgp_personality.Kernel_Config.FreqMHz*1e6);
}
#endif
#ifdef HAVE_BGLPERSONALITY
/* Fill in the CPU-related fields using BG/L's rts_get_personality(). */
static void fill_in_cpu_info_bgl (SYSTEM_INFORMATION *info)
{
ASSIGN (info->cpu_freq, (double) BGLPersonality_clockHz(&ncptl_bgl_personality));
ASSIGN (info->contexts_per_node,
BGLPersonality_virtualNodeMode(&ncptl_bgl_personality) ? 2 : 1);
}
#endif
#ifdef HAVE___CPU_MHZ
/* Fill in the clock speed using the Cray XT's __cpu_mhz variable. */
static void fill_in_cpu_info_xt (SYSTEM_INFORMATION *info)
{
extern uint32_t __cpu_mhz;
ASSIGN (info->cpu_freq, 1e6 * __cpu_mhz);
}
#endif
#if defined(SYSMP_OKAY) && defined(MP_NPROCS)
/* Fill in the CPU-related fields using IRIX and UNICOS's sysmp(). */
static void fill_in_cpu_info_sysmp (SYSTEM_INFORMATION *info)
{
int64_t numcpus = (int64_t) sysmp (MP_NPROCS);
if (numcpus > 0)
ASSIGN (info->contexts_per_node, numcpus);
}
#endif
#ifdef GETINVENT_OKAY
/* Fill in the CPU-related fields using SGI and Cray's getinvent(). */
static void fill_in_cpu_info_getinvent (SYSTEM_INFORMATION *info)
{
inventory_t *invitem;
setinvent();
while ((invitem=getinvent()))
if (invitem->inv_class==INV_PROCESSOR && invitem->inv_type==INV_CPUBOARD)
ASSIGN (info->cpu_freq, 1e6 * (double) invitem->inv_controller);
}
#endif
#if defined(HAVE_SYS_SYSINFO_H) && defined(GSI_CPU_INFO)
/* Fill in the CPU-related fields using OSF1's getsysinfo(). */
static void fill_in_cpu_info_getsysinfo (SYSTEM_INFORMATION *info)
{
struct cpu_info CPU_information;
int startloc = 0;
if (getsysinfo (GSI_CPU_INFO, (caddr_t)&CPU_information,
sizeof(CPU_information), &startloc, NULL, NULL) >= -1) {
ASSIGN (info->contexts_per_node, CPU_information.cpus_in_box);
ASSIGN (info->cpu_freq, 1e6 * (double) CPU_information.mhz);
}
}
#endif
#ifdef ODM_IS_SUPPORTED
/* Fill in the CPU-related fields using AIX's Object Data Manager. */
static void fill_in_cpu_info_odm (SYSTEM_INFORMATION *info)
{
struct CuAt *cuat_info;
int num_instances;
if (odm_initialize())
return;
if (!info->cpu_freq && (cuat_info=getattr ("proc0", "frequency", 0, &num_instances)))
sscanf (cuat_info->value, "%lf", &info->cpu_freq);
if ((cuat_info=getattr ("proc0", "type", 0, &num_instances)))
ASSIGN (info->cpu_model, ncptl_strdup (cuat_info->value));
if (!info->threads_per_core && (cuat_info=getattr ("proc0", "smt_threads", 0, &num_instances)))
sscanf (cuat_info->value, "%d", &info->threads_per_core);
(void) odm_terminate();
}
#endif
#ifdef _WIN32
/* Fill in the CPU-related fields using Win32 functions. */
static void fill_in_cpu_info_win32 (SYSTEM_INFORMATION *info)
{
LARGE_INTEGER timerfreq;
SYSTEM_INFO sysinfo;
MEMORYSTATUSEX meminfo;
OSVERSIONINFOEX osinfo;
HKEY cpukey;
/* Read the timer frequency. */
if (QueryPerformanceFrequency(&timerfreq))
ASSIGN (info->timer_freq, (double)timerfreq.QuadPart);
/* Store both the fully-qualified DNS name and the NetBIOS name. */
if (!info->hostname) {
char hostname[NCPTL_MAX_LINE_LEN];
DWORD namelen;
namelen = NCPTL_MAX_LINE_LEN;
if (GetComputerNameA (hostname, &namelen)) {
info->hostname = (char *) ncptl_malloc (NCPTL_MAX_LINE_LEN, 0);
strcpy (info->hostname, hostname);
namelen = NCPTL_MAX_LINE_LEN;
if (GetComputerNameExA (ComputerNamePhysicalDnsFullyQualified, hostname, &namelen))
sprintf (info->hostname + strlen(info->hostname), " (%s)", hostname);
info->hostname = ncptl_realloc (info->hostname, strlen(info->hostname)+1, 0);
}
}
/* Get information about the CPU and memory system. */
GetSystemInfo (&sysinfo);
ASSIGN (info->pagesize, (uint64_t)sysinfo.dwPageSize);
ASSIGN (info->contexts_per_node, (uint64_t)sysinfo.dwNumberOfProcessors);
if (!info->arch)
switch (sysinfo.wProcessorArchitecture) {
case PROCESSOR_ARCHITECTURE_INTEL:
info->arch = (char *) ncptl_malloc (NCPTL_MAX_LINE_LEN, 0);
if (sysinfo.wProcessorLevel < 10)
sprintf (info->arch, "i%d86", sysinfo.wProcessorLevel);
else if (sysinfo.wProcessorLevel == 15)
sprintf (info->arch, "i686");
else
sprintf (info->arch, "Intel processor level %d", sysinfo.wProcessorLevel);
info->arch = ncptl_realloc (info->arch, strlen(info->arch)+1, 0);
break;
case PROCESSOR_ARCHITECTURE_IA64:
info->arch = "ia64";
break;
case PROCESSOR_ARCHITECTURE_AMD64:
info->arch = "x86_64";
break;
default:
break;
}
meminfo.dwLength = sizeof (MEMORYSTATUSEX);
GlobalMemoryStatusEx (&meminfo);
ASSIGN (info->physmem, (uint64_t)meminfo.ullTotalPhys);
/* Get more information about the CPU by reading the Windows registry. */
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"Hardware\\Description\\System\\CentralProcessor\\0",
0, KEY_READ, &cpukey) == ERROR_SUCCESS) {
char buffer[NCPTL_MAX_LINE_LEN];
DWORD bufferlen;
/* Processor description */
bufferlen = NCPTL_MAX_LINE_LEN;
if (!info->cpu_model &&
RegQueryValueEx (cpukey, "ProcessorNameString", NULL, NULL,
buffer, &bufferlen) == ERROR_SUCCESS)
ASSIGN (info->cpu_model, ncptl_strdup(buffer));
/* Vendor identifier */
bufferlen = NCPTL_MAX_LINE_LEN;
if (!info->cpu_vendor &&
RegQueryValueEx (cpukey, "VendorIdentifier", NULL, NULL,
buffer, &bufferlen) == ERROR_SUCCESS)
ASSIGN (info->cpu_vendor, ncptl_strdup(buffer));
/* CPU frequency */
bufferlen = NCPTL_MAX_LINE_LEN;
if (!info->cpu_freq &&
RegQueryValueEx (cpukey, "~MHz", NULL, NULL,
buffer, &bufferlen) == ERROR_SUCCESS)
ASSIGN (info->cpu_freq, 1.0e6 * (double)*(DWORD *)buffer);
/* We're finished with the CPU registry key. */
(void) RegCloseKey (cpukey);
}
/* Pretty-print the version of Windows that the user is running. */
if (!info->os) {
osinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
if (GetVersionEx ((LPOSVERSIONINFO) &osinfo)) {
DWORD majver = osinfo.dwMajorVersion;
DWORD minver = osinfo.dwMinorVersion;
char *osstring = NULL;
info->os = (char *) ncptl_malloc (NCPTL_MAX_LINE_LEN, 0);
sprintf (info->os, "Microsoft Windows ");
if (majver == 6)
osstring = "Vista";
else if (majver == 5) {
if (minver == 2)
osstring = "Server 2003";
else if (minver == 1)
osstring = "XP";
else if (minver == 0)
osstring = "2000";
}
else if (majver == 4)
osstring = "NT 4";
if (osstring)
strcat (info->os, osstring);
else
sprintf (info->os + strlen(info->os),
"(unrecognized version %d.%d)",
majver, minver);
if (osinfo.szCSDVersion[0]) {
strcat (info->os, " ");
strcat (info->os, osinfo.szCSDVersion);
}
sprintf (info->os + strlen(info->os), " (Build %u)", osinfo.dwBuildNumber);
info->os = ncptl_realloc (info->os, strlen(info->os)+1, 0);
}
}
}
#endif
/* Fill in the CPU-related fields by reading /proc/cpuinfo. */
static void fill_in_cpu_info_cpuinfo (SYSTEM_INFORMATION *info)
{
FILE *procinfo; /* Handle to /proc/cpuinfo */
char oneline[NCPTL_MAX_LINE_LEN+1]; /* One line read from /proc/cpuinfo */
double frequency; /* CPU or timer frequency */
int cpucount; /* Number of active CPU cores */
int have_ncpus = info->contexts_per_node!=0; /* 1=already assigned the total number of CPU cores */
int cpu_cores; /* Number of CPU cores per socket */
int physical_id; /* Physical ID of the current socket */
int min_physical_id = 1<<30; /* Minimum physical ID encountered (typically 0, sometimes 1, have yet to see anything else */
char *cpu_family = NULL; /* CPU family name */
char *cpu_model = NULL; /* CPU model number */
char *cpu_revision = NULL; /* CPU revision number */
/* Read interesting information from /proc/cpuinfo. */
if (!(procinfo=fopen ("/proc/cpuinfo", "r")))
return;
while (fgets (oneline, NCPTL_MAX_LINE_LEN, procinfo)) {
char *keyname = extract_key (oneline);
/* CPU speed */
if (!info->cpu_freq) {
/* IA-32 and IA-64 */
if (sscanf (oneline, "cpu MHz : %lf", &frequency))
ASSIGN (info->cpu_freq, 1.0e6 * frequency);
/* PowerPC */
else if (sscanf (oneline, "clock : %lfMHz", &frequency))
ASSIGN (info->cpu_freq, 1.0e6 * frequency);
else if (sscanf (oneline, "clock : %lfGHz", &frequency))
ASSIGN (info->cpu_freq, 1.0e9 * frequency);
/* Alpha */
else if (sscanf (oneline, "cycle frequency [Hz] : %lf", &frequency))
ASSIGN (info->cpu_freq, frequency);
}
/* CPU model */
if (!info->cpu_model) {
/* IA-32 */
if (!strcmp (keyname, "model name"))
ASSIGN (info->cpu_model, extract_value (oneline));
/* PowerPC */
else if (!strcmp (keyname, "cpu"))
ASSIGN (info->cpu_model, extract_value (oneline));
/* IA-64 */
else if (!strcmp (keyname, "family")) {
char *firstbad; /* Pointer to first nonnumeric character */
cpu_family = extract_value (oneline);
errno = 0;
(void) strtoull (cpu_family, &firstbad, 10);
if (*firstbad == '\0') {
/* Family is entirely numeric -- prefix with the word "Family". */
char cpu_family_string[NCPTL_MAX_LINE_LEN];
sprintf (cpu_family_string, "family %s", cpu_family);
ncptl_free (cpu_family);
cpu_family = ncptl_strdup (cpu_family_string);
}
}
else if (!strcmp (keyname, "model"))
cpu_model = extract_value (oneline);
else if (!strcmp (keyname, "revision"))
cpu_revision = extract_value (oneline);
if (cpu_family && cpu_model && cpu_revision) {
char cpu_description[NCPTL_MAX_LINE_LEN];
sprintf (cpu_description, "%s, model %s, revision %s",
cpu_family, cpu_model, cpu_revision);
info->cpu_model = ncptl_strdup (cpu_description);
}
}
else
/* Alpha defines both "cpu" and "cpu model"; we want only the latter. */
if (!strcmp (keyname, "cpu model")) {
if (info->cpu_model) {
ncptl_free (info->cpu_model);
info->cpu_model = NULL;
}
ASSIGN (info->cpu_model, extract_value (oneline));
}
/* CPU vendor */
if (!info->cpu_vendor) {
/* IA-32 */
if (!strcmp (keyname, "vendor_id"))
ASSIGN (info->cpu_vendor, extract_value (oneline));
/* IA-64 */
else if (!strcmp (keyname, "vendor"))
ASSIGN (info->cpu_vendor, extract_value (oneline));
}
/* Total number of CPU compute contexts (threads*cores*dies*sockets) */
/* IA-32, IA-64, and PowerPC */
if (!have_ncpus && !strcmp (keyname, "processor"))
info->contexts_per_node++;
/* Alpha */
else if (sscanf (oneline, "cpus active : %d", &cpucount))
ASSIGN (info->contexts_per_node, cpucount);
/* Cycle-counter speed */
/* IA-64 */
if (sscanf (oneline, "itc MHz : %lf", &frequency))
ASSIGN (info->timer_freq, 1.0e6 * frequency);
/* PowerPC */
else if (sscanf (oneline, "timebase : %lf", &frequency))
ASSIGN (info->timer_freq, frequency);
/* Number of sockets and cores */
if (sscanf (oneline, "cpu cores : %d", &cpu_cores))
ASSIGN(info->cores_per_socket, cpu_cores);
if (sscanf (oneline, "physical id : %d", &physical_id)) {
if (physical_id + 1 > info->sockets_per_node)
info->sockets_per_node = physical_id + 1;
if (physical_id < min_physical_id)
min_physical_id = physical_id;
}
/* CPU flags */
if (!strcmp (keyname, "flags"))
ASSIGN (info->cpu_flags, extract_value (oneline));
/* Clean up before the next iteration. */
ncptl_free (keyname);
}
fclose (procinfo);
/* Adjust the socket count based on the minimum ID encountered. */
if (info->sockets_per_node)
info->sockets_per_node -= min_physical_id;
/* Clean up any memory we may have allocated. */
ncptl_free (cpu_family);
ncptl_free (cpu_model);
ncptl_free (cpu_revision);
}
/* Fill in the CPU-related fields by reading files under /sys. */
static void fill_in_cpu_info_sysfs (SYSTEM_INFORMATION *info)
{
char *maxfreq_str; /* Maximum CPU frequency in KHz as a text string */
double maxfreq; /* Maximum CPU frequency in KHz */
/* As a last-ditch effort to find the timebase frequency, use the
* highest frequency at which the CPU can run. This is probably a
* decent guess but is not guaranteed to return the correct
* timebase. Alas, reading the correct value from the x86 MSRs can
* currently be done only from kernel mode. */
if (info->timer_freq)
return;
maxfreq_str = read_first_line ("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", 0);
if (!maxfreq_str)
return;
if (sscanf (maxfreq_str, "%lf", &maxfreq) == 1)
ASSIGN (info->timer_freq, maxfreq*1000.0);
ncptl_free (maxfreq_str);
}
/* Fill in all of the the CPU-related fields. */
static void fill_in_cpu_info (SYSTEM_INFORMATION *info)
{
#ifdef CYCLES_PER_USEC
ASSIGN (info->timer_freq, 1.0e6 * CYCLES_PER_USEC);
#endif
fill_in_cpu_info_cpuinfo (info);
#ifdef USE_PAPI
fill_in_cpu_info_PAPI (info);
#endif
#ifdef HAVE_BGPPERSONALITY
fill_in_cpu_info_bgp (info);
#endif
#ifdef HAVE_BGLPERSONALITY
fill_in_cpu_info_bgl (info);
#endif
#ifdef HAVE___CPU_MHZ
fill_in_cpu_info_xt (info);
#endif
#ifdef ODM_IS_SUPPORTED
fill_in_cpu_info_odm (info);
#endif
#ifdef SYSMP_OKAY
fill_in_cpu_info_sysmp (info);
#endif
#ifdef GETINVENT_OKAY
fill_in_cpu_info_getinvent (info);
#endif
#if defined(HAVE_SYS_SYSINFO_H) && defined(GSI_CPU_INFO)
fill_in_cpu_info_getsysinfo (info);
#endif
#if defined(HAVE_SYSCTL) && defined(CTL_HW)
fill_in_cpu_info_sysctl (info);
#endif
#ifdef HAVE_SYSCONF
fill_in_cpu_info_sysconf (info);
#endif
#ifdef HAVE_KSTAT_DATA_LOOKUP
fill_in_cpu_info_kstat (info);
#endif
#ifdef _WIN32
fill_in_cpu_info_win32 (info);
#endif
#if defined(TIMEBASE_FREQUENCY_FILENAME)
if (!info->timer_freq) {
char *timebase_bits;
timebase_bits = read_first_line (TIMEBASE_FREQUENCY_FILENAME, 1);
if (timebase_bits) {
ASSIGN (info->timer_freq, *(double *)timebase_bits);
ncptl_free (timebase_bits);
}
}
#endif
fill_in_cpu_info_sysfs (info);
/* If contexts_per_node is 1, then cores_per_socket,
* sockets_per_node, and threads_per_core must all also be 1. */
if (info->contexts_per_node == 1) {
ASSIGN(info->cores_per_socket, 1);
ASSIGN(info->sockets_per_node, 1);
ASSIGN(info->threads_per_core, 1);
}
/* If we know contexts_per_node, cores_per_socket, and
* sockets_per_node, we can easily compute threads_per_core if
* necessary. */
if (info->contexts_per_node
&& info->cores_per_socket
&& info->sockets_per_node
&& !info->threads_per_core)
info->threads_per_core =
info->contexts_per_node / (info->cores_per_socket * info->sockets_per_node);
}
/* Fill in all of the memory-related fields. */
static void fill_in_mem_info (SYSTEM_INFORMATION *info)
{
/* OS page size */
#ifdef OS_PAGE_SIZE
ASSIGN (info->pagesize, (uint64_t) OS_PAGE_SIZE);
#else
# if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
if (sysconf(_SC_PAGESIZE) > 0)
ASSIGN (info->pagesize, (uint64_t) sysconf(_SC_PAGESIZE));
# endif
# if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
if (sysconf(_SC_PAGE_SIZE) > 0)
ASSIGN (info->pagesize, (uint64_t) sysconf(_SC_PAGE_SIZE));
# endif
# if defined(HAVE_GETPAGESIZE)
ASSIGN (info->pagesize, (uint64_t) getpagesize());
# endif
#endif
/* Physical memory */
#if defined(HAVE_BGPPERSONALITY)
ASSIGN (info->physmem, (uint64_t) ncptl_bgp_personality.DDR_Config.DDRSizeMB*1048576);
#endif
#if defined(HAVE_BGLPERSONALITY)
ASSIGN (info->physmem, (uint64_t) BGLPersonality_DDRSize(&ncptl_bgl_personality));
#endif