-
Notifications
You must be signed in to change notification settings - Fork 397
/
omrsysinfo.c
2058 lines (1824 loc) · 69 KB
/
omrsysinfo.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
/*******************************************************************************
* Copyright IBM Corp. and others 2015
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
/**
* @file
* @ingroup Port
* @brief System information
*/
#include <pdh.h>
#include <pdhmsg.h>
#include <stdio.h>
#include <windows.h>
#include <WinSDKVer.h>
/* Undefine the winsockapi because winsock2 defines it. Removes warnings. */
#if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
#undef _WINSOCKAPI_
#endif
#include <winsock2.h>
#if defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE)
#include <VersionHelpers.h>
#endif
#include "omrportpriv.h"
#include "omrportpg.h"
#include "omrportptb.h"
#include "ut_omrport.h"
#include "omrsysinfo_helpers.h"
#define OMRPORT_SYSINFO_WINDOWS_TICK 10000000ULL
#define OMRPORT_SYSINFO_SEC_TO_UNIX_EPOCH 11644473600ULL
#define OMRPORT_SYSINFO_NS100_PER_SEC 10000000ULL
static int32_t copyEnvToBuffer(struct OMRPortLibrary *portLibrary, void *args);
typedef struct CopyEnvToBufferArgs {
uintptr_t bufferSizeBytes;
void *buffer;
uintptr_t numElements;
} CopyEnvToBufferArgs;
/* Missing from the ALPHA include files */
#ifndef VER_PLATFORM_WIN32_WINDOWS
#define VER_PLATFORM_WIN32_WINDOWS 1
#endif
void
omrsysinfo_set_number_user_specified_CPUs(struct OMRPortLibrary *portLibrary, uintptr_t number)
{
Trc_PRT_sysinfo_set_number_user_specified_CPUs_Entered();
portLibrary->portGlobals->userSpecifiedCPUs = number;
Trc_PRT_sysinfo_set_number_user_specified_CPUs_Exit(number);
}
/**
* Determine the CPU architecture.
*
* @param[in] portLibrary The port library.
*
* @return A null-terminated string describing the CPU architecture of the hardware, NULL on error.
*
* @note portLibrary is responsible for allocation/deallocation of returned buffer.
* @note See http://www.tolstoy.com/samizdat/sysprops.html for good values to return.
*/
const char *
omrsysinfo_get_CPU_architecture(struct OMRPortLibrary *portLibrary)
{
#if defined(_PPC_)
return OMRPORT_ARCH_PPC;
#elif defined(_X86_)
return OMRPORT_ARCH_X86;
#elif defined( _AMD64_)
return OMRPORT_ARCH_HAMMER;
#else
return "unknown";
#endif
}
intptr_t
omrsysinfo_get_processor_description(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc)
{
intptr_t rc = -1;
Trc_PRT_sysinfo_get_processor_description_Entered(desc);
if (NULL != desc) {
memset(desc, 0, sizeof(OMRProcessorDesc));
rc = omrsysinfo_get_x86_description(portLibrary, desc);
}
Trc_PRT_sysinfo_get_processor_description_Exit(rc);
return rc;
}
BOOLEAN
omrsysinfo_processor_has_feature(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc, uint32_t feature)
{
BOOLEAN rc = FALSE;
Trc_PRT_sysinfo_processor_has_feature_Entered(desc, feature);
if ((NULL != desc) && (feature < (OMRPORT_SYSINFO_FEATURES_SIZE * 32))) {
uint32_t featureIndex = feature / 32;
uint32_t featureShift = feature % 32;
rc = OMR_ARE_ALL_BITS_SET(desc->features[featureIndex], 1u << featureShift);
}
Trc_PRT_sysinfo_processor_has_feature_Exit((uintptr_t)rc);
return rc;
}
intptr_t
omrsysinfo_processor_set_feature(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc, uint32_t feature, BOOLEAN enable)
{
intptr_t rc = -1;
Trc_PRT_sysinfo_processor_set_feature_Entered(desc, feature, enable);
if ((NULL != desc) && (feature < (OMRPORT_SYSINFO_FEATURES_SIZE * 32))) {
uint32_t featureIndex = feature / 32;
uint32_t featureShift = feature % 32;
if (enable) {
desc->features[featureIndex] |= (1u << featureShift);
}
else {
desc->features[featureIndex] &= ~(1u << featureShift);
}
rc = 0;
}
Trc_PRT_sysinfo_processor_set_feature_Exit(rc);
return rc;
}
const char*
omrsysinfo_get_processor_feature_name(struct OMRPortLibrary *portLibrary, uint32_t feature)
{
const char* rc = "null";
Trc_PRT_sysinfo_get_processor_feature_name_Entered(feature);
rc = omrsysinfo_get_x86_processor_feature_name(feature);
Trc_PRT_sysinfo_get_processor_feature_name_Exit(rc);
return rc;
}
/**
* Generate the corresponding string literals for the provided OMRProcessorDesc. The buffer will be zero
* initialized and overwritten with the processor feature output string.
*
* @param[in] portLibrary The port library.
* @param[in] desc The struct that contains the list of processor features to be converted to string.
* @param[out] buffer The processor feature output string.
* @param[in] length The size of the buffer in number of bytes.
*
* @return 0 on success, -1 if output string size exceeds input length.
*/
intptr_t
omrsysinfo_get_processor_feature_string(struct OMRPortLibrary *portLibrary, OMRProcessorDesc *desc, char * buffer, const size_t length)
{
BOOLEAN start = TRUE;
size_t i = 0;
size_t j = 0;
size_t numberOfBits = 0;
size_t bufferLength = 0;
memset(buffer, 0, length);
for (i = 0; i < OMRPORT_SYSINFO_FEATURES_SIZE; i++) {
numberOfBits = CHAR_BIT * sizeof(desc->features[i]);
for (j = 0; j < numberOfBits; j++) {
if (desc->features[i] & (1 << j)) {
uint32_t feature = (uint32_t)(i * numberOfBits + j);
const char * featureName = omrsysinfo_get_processor_feature_name(portLibrary, feature);
size_t featureLength = strlen(featureName);
if (start == FALSE) {
strncat(buffer, " ", length - bufferLength - 1);
bufferLength += 1;
} else {
start = FALSE;
}
if (length - bufferLength - 1 < featureLength) {
return -1;
}
strncat(buffer, featureName, length - bufferLength - 1);
bufferLength += featureLength;
}
}
}
return 0;
}
#define ENVVAR_VALUE_BUFFER_LENGTH 512
#define ENVVAR_NAME_BUFFER_LENGTH 128
intptr_t
omrsysinfo_get_env(struct OMRPortLibrary *portLibrary, const char *envVar, char *infoString, uintptr_t bufSize)
{
DWORD rc = 0;
intptr_t result = -1;
wchar_t envVarWideCharValueBuffer[ENVVAR_VALUE_BUFFER_LENGTH];
wchar_t *envVarWideCharValue = envVarWideCharValueBuffer;
wchar_t *envVarWideCharName = NULL;
wchar_t envVarNameConversionBuffer[ENVVAR_NAME_BUFFER_LENGTH];
/*
* Convert the envvar from modified UTF-8 to UTF-16 (wide character).
* Stack-allocate a buffer large enough to hold typical envvar names.
* If the name is larger than the buffer, a new buffer is dynamically allocated.
*/
envVarWideCharName = port_convertFromUTF8(portLibrary, envVar, envVarNameConversionBuffer, ENVVAR_NAME_BUFFER_LENGTH);
if (NULL == envVarWideCharName) {
return -1;
}
rc = GetEnvironmentVariableW(envVarWideCharName, envVarWideCharValue, ENVVAR_VALUE_BUFFER_LENGTH); /* Try with the stack buffer first */
if ((ENVVAR_VALUE_BUFFER_LENGTH <= rc) && (0 != rc)) {
/* if the value fit, rc, which does not include the null, is at most ENVVAR_VALUE_BUFFER_LENGTH-1. */
DWORD envVarValueLength = rc;
envVarWideCharValue = (wchar_t *)portLibrary->mem_allocate_memory(portLibrary, envVarValueLength * sizeof(wchar_t), OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL != envVarWideCharValue) {
rc = GetEnvironmentVariableW(envVarWideCharName, envVarWideCharValue, envVarValueLength);
} else {
rc = 0; /* Memory allocation failure */
}
}
if (envVarNameConversionBuffer != envVarWideCharName) {
/* port_convertFromUTF8 allocated a buffer for us */
portLibrary->mem_free_memory(portLibrary, envVarWideCharName);
}
if (0 == rc) {
/*
* Possible causes:
* - envvar does not exist or is empty
* - memory allocation failure
*/
result = -1;
} else {
/* Calculate the number of bytes required for the conversion */
rc = WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS, envVarWideCharValue, -1, NULL, 0, NULL, NULL);
if (0 == rc) { /* error, probably bogus Unicode */
result = -1;
} else if (rc > bufSize) {
/* Caller-supplied buffer is too small. Return the actual number of bytes required. */
result = rc;
} else {
/* Do the conversion for real. */
rc = WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS, envVarWideCharValue, -1, infoString, (int)bufSize, NULL, NULL);
Assert_PRT_true(0 != rc); /* Bogus Unicode or buffer too small should be caught by previous tests */
result = 0;
}
}
if (envVarWideCharValue != envVarWideCharValueBuffer) {
/* We had to allocate a large buffer */
portLibrary->mem_free_memory(portLibrary, envVarWideCharValue);
}
return result;
}
/**
* Determine the OS type.
*
* @param[in] portLibrary The port library.
*
* @return OS type string (NULL terminated) on success, NULL on error.
*
* @note portLibrary is responsible for allocation/deallocation of returned buffer.
*/
const char *
omrsysinfo_get_OS_type(struct OMRPortLibrary *portLibrary)
{
BOOLEAN isClientMajorVersion10 = FALSE;
BOOLEAN isServerMajorVersion10 = FALSE;
/*
WIN32_WINNT version constants :
#define _WIN32_WINNT_NT4 0x0400
#define _WIN32_WINNT_WIN2K 0x0500
#define _WIN32_WINNT_WINXP 0x0501
#define _WIN32_WINNT_WS03 0x0502
#define _WIN32_WINNT_WIN6 0x0600
#define _WIN32_WINNT_VISTA 0x0600
#define _WIN32_WINNT_WS08 0x0600
#define _WIN32_WINNT_LONGHORN 0x0600
#define _WIN32_WINNT_WIN7 0x0601
#define _WIN32_WINNT_WIN8 0x0602
#define _WIN32_WINNT_WINBLUE 0x0603
#define _WIN32_WINNT_WINTHRESHOLD 0x0A00 / * ABRACADABRA_THRESHOLD * /
#define _WIN32_WINNT_WIN10 0x0A00 / * ABRACADABRA_THRESHOLD * /
*/
if (NULL == PPG_si_osType) {
char *defaultTypeName = "Windows";
#if !defined(_WIN32_WINNT_WIN10) || (_WIN32_WINNT_MAXVER < _WIN32_WINNT_WIN10)
OSVERSIONINFOEX versionInfo;
#endif /* !defined(_WIN32_WINNT_WIN10) || (_WIN32_WINNT_MAXVER < _WIN32_WINNT_WIN10) */
PPG_si_osType = defaultTypeName; /* by default, use the "unrecognized version" string */
PPG_si_osTypeOnHeap = NULL;
#if defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE)
/* Windows 8.1 or later */
/* OS Versions: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx */
if (IsWindowsServer()) {
#if defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10)
if (IsWindows10OrGreater()) {
/* Starting with major version 10, use the registry to get the version */
PPG_si_osType = defaultTypeName;
isServerMajorVersion10 = TRUE;
} else
#else /* defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10) */
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
/* GetVersionEx() is deprecated, but still needed when using older compilers. Suppress the warning. */
#pragma warning( suppress : 4996 )
if (GetVersionEx((OSVERSIONINFO *) &versionInfo)) {
if (10 <= versionInfo.dwMajorVersion) {
isServerMajorVersion10 = TRUE;
}
}
if (isServerMajorVersion10) {
PPG_si_osType = defaultTypeName;
/* Windows 10+ is detected, don't check the following cases. */
} else
#endif /* defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10) */
if (IsWindows8Point1OrGreater()) {
PPG_si_osType = "Windows Server 2012 R2";
} else if (IsWindows8OrGreater()) {
PPG_si_osType = "Windows Server 2012";
} else if (IsWindows7OrGreater()) {
PPG_si_osType = "Windows Server 2008 R2";
} else if (IsWindowsVistaOrGreater()) {
PPG_si_osType = "Windows Server 2008";
} else if (IsWindowsXPOrGreater()) {
PPG_si_osType = "Windows Server 2003";
}
} else {
#if defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10)
if (IsWindows10OrGreater()) {
/* Starting with major version 10, use the registry to get the version */
PPG_si_osType = defaultTypeName;
isClientMajorVersion10 = TRUE;
} else
#else /* defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10) */
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
/* GetVersionEx() is deprecated, but still needed when using older compilers. Suppress the warning. */
#pragma warning( suppress : 4996 )
if (GetVersionEx((OSVERSIONINFO *) &versionInfo)) {
if ((VER_PLATFORM_WIN32_NT == versionInfo.dwPlatformId) && (10 <= versionInfo.dwMajorVersion)) {
PPG_si_osType = NULL;
if (VER_NT_WORKSTATION == versionInfo.wProductType) {
if ((10 == versionInfo.dwMajorVersion) && (0 == versionInfo.dwMinorVersion)) {
/* build number cutoff for Windows 11 is 22000 */
if (versionInfo.dwBuildNumber >= 22000) {
PPG_si_osType = "Windows 11";
} else {
PPG_si_osType = "Windows 10";
}
}
} else {
isServerMajorVersion10 = TRUE;
}
}
}
if ((PPG_si_osType != defaultTypeName) || isServerMajorVersion10) {
if (NULL == PPG_si_osType) {
PPG_si_osType = defaultTypeName;
}
/* Windows 10+ is detected, don't check the following cases. */
} else
#endif /* defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10) */
if (IsWindows8Point1OrGreater()) {
PPG_si_osType = "Windows 8.1";
} else if (IsWindows8OrGreater()) {
PPG_si_osType = "Windows 8";
} else if (IsWindows7OrGreater()) {
PPG_si_osType = "Windows 7";
} else if (IsWindowsVistaOrGreater()) {
PPG_si_osType = "Windows Vista";
} else if (IsWindowsXPOrGreater()) {
PPG_si_osType = "Windows XP";
}
}
#else /* defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE) */
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
if (!GetVersionEx((OSVERSIONINFO *) &versionInfo)) {
return NULL;
}
if (VER_PLATFORM_WIN32_NT == versionInfo.dwPlatformId) {
/*
* Java 8 and later support Windows 7 and later.
* Include legacy versions out of sympathy.
*/
switch (versionInfo.dwMajorVersion) {
case 5: {
switch (versionInfo.dwMinorVersion) {
case 0:
PPG_si_osType = "Windows 2000";
break;
case 1:
PPG_si_osType = "Windows XP"; /* 32-bit */
break;
case 2:
switch (versionInfo.wProductType) {
case VER_NT_WORKSTATION:
PPG_si_osType = "Windows XP"; /* 64-bit */
break;
case VER_NT_DOMAIN_CONTROLLER: /* FALLTHROUGH */
case VER_NT_SERVER:
default :
PPG_si_osType = "Windows Server 2003";
break;
}
break;
default:
PPG_si_osType = defaultTypeName;
break;
}
break;
}
case 6: {
switch (versionInfo.wProductType) {
case VER_NT_WORKSTATION: {
switch (versionInfo.dwMinorVersion) {
case 0:
PPG_si_osType = "Windows Vista";
break;
case 1:
PPG_si_osType = "Windows 7";
break;
case 2:
PPG_si_osType = "Windows 8";
break;
case 3:
PPG_si_osType = "Windows 8.1";
break;
default:
PPG_si_osType = defaultTypeName;
break;
} /* VER_NT_WORKSTATION */
break;
}
default: {
switch (versionInfo.dwMinorVersion) {
case 0:
PPG_si_osType = "Windows Server 2008";
break;
case 1:
PPG_si_osType = "Windows Server 2008 R2";
break;
case 2:
PPG_si_osType = "Windows Server 2012";
break;
case 3:
PPG_si_osType = "Windows Server 2012 R2";
break;
default:
PPG_si_osType = defaultTypeName;
break;
}
break;
}
} /* switch (versionInfo.wProductType) */
/* (versionInfo.dwMajorVersion == 6) */
break;
}
case 10: {
switch (versionInfo.wProductType) {
case VER_NT_WORKSTATION: {
switch (versionInfo.dwMinorVersion) {
case 0:
if (versionInfo.dwBuildNumber >= 22000) {
PPG_si_osType = "Windows 11";
} else {
PPG_si_osType = "Windows 10";
}
break;
default:
PPG_si_osType = defaultTypeName;
break;
}
}
break;
default: {
/* Starting with major version 10, use the registry to get the version */
PPG_si_osType = defaultTypeName;
isServerMajorVersion10 = TRUE;
}
break;
}
}
break;
default:
PPG_si_osType = defaultTypeName;
break;
}
}
#endif /* defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE) */
#define PRODUCT_NAME_KEY "ProductName"
#define CURRENT_BUILD_KEY "CurrentBuild"
#define WINDOWS_SERVER_PREFIX "Windows Server version "
if (defaultTypeName == PPG_si_osType) {
HKEY hKey;
if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey)) {
DWORD valueSize = 0;
/* determine Windows 11 or 10 or server equivalent since they share major and minor version numbers */
if ((isServerMajorVersion10 || isClientMajorVersion10)
&& (ERROR_SUCCESS == RegQueryValueExA(hKey, CURRENT_BUILD_KEY, NULL, NULL, NULL, &valueSize))) {
char currentbuildBuffer[10]; /* CurrentBuild values have been 5 digits so far, buffer created with extra space */
if (sizeof(currentbuildBuffer) >= valueSize) {
if (ERROR_SUCCESS == RegQueryValueExA(hKey, CURRENT_BUILD_KEY, NULL, NULL, (LPBYTE)currentbuildBuffer, &valueSize)) {
int currentBuildNumber = atoi(currentbuildBuffer);
if (isClientMajorVersion10) {
/* Windows 11 build number cutoff is 22000 */
if (currentBuildNumber >= 22000) {
PPG_si_osType = "Windows 11";
} else {
PPG_si_osType = "Windows 10";
}
}
else if (isServerMajorVersion10) {
/* Windows Server 2022 build number cutoff is 20348, Server 2019 cutoff is 17763
* and Server 2016 cutoff is 14393. These versions are currently ones with major
* version 10.
*/
if (currentBuildNumber >= 20348) {
PPG_si_osType = "Windows Server 2022";
} else if (currentBuildNumber >= 17763) {
PPG_si_osType = "Windows Server 2019";
} else if (currentBuildNumber >= 14393) {
PPG_si_osType = "Windows Server 2016";
}
}
}
}
}
if (defaultTypeName == PPG_si_osType) {
/* query the first time to get the content size of the value. */
if (ERROR_SUCCESS == RegQueryValueExA(hKey, PRODUCT_NAME_KEY, NULL, NULL, NULL, &valueSize)) {
char *productNameBuffer = portLibrary->mem_allocate_memory(portLibrary, valueSize, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL != productNameBuffer) {
/* query the second time to get the value */
if (ERROR_SUCCESS == RegQueryValueExA(hKey, PRODUCT_NAME_KEY, NULL, NULL, (LPBYTE)productNameBuffer, &valueSize)) {
PPG_si_osType = PPG_si_osTypeOnHeap = productNameBuffer;
} else {
portLibrary->mem_free_memory(portLibrary, productNameBuffer);
}
}
}
}
}
}
#undef PRODUCT_NAME_KEY
#undef CURRENT_BUILD_KEY
#undef WINDOWS_SERVER_PREFIX
if (defaultTypeName == PPG_si_osType) {
#if defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE)
Trc_PRT_sysinfo_failed_to_get_os_type();
#else
Trc_PRT_sysinfo_unrecognized_Windows_version(versionInfo.wProductType, versionInfo.dwMinorVersion, versionInfo.dwMajorVersion);
#endif
}
}
return PPG_si_osType;
}
/**
* Determine version information from the operating system.
*
* @param[in] portLibrary The port library.
*
* @return OS version string (NULL terminated) on success, NULL on error.
*
* @note portLibrary is responsible for allocation/deallocation of returned buffer.
*/
const char *
omrsysinfo_get_OS_version(struct OMRPortLibrary *portLibrary)
{
/* OS Version format : <CurrentVersion> build <CurrentBuildNumber> <CSDVersion>
* Sample : 6.1 build 7601 Service Pack 1
* */
#if defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE)
if (NULL == PPG_si_osVersion) {
#if defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10)
if (IsWindows10OrGreater()) {
/* Build information for Windows 10 can't be hard coded, use GetVersionEx() below. */
PPG_si_osVersion = NULL;
} else
#else /* defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10) */
OSVERSIONINFOW versionInfo;
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
/* GetVersionEx() is deprecated, but still needed to detect Windows 10 when using older compilers. Suppress the warning. */
#pragma warning( suppress : 4996 )
if (GetVersionExW(&versionInfo) && (10 <= versionInfo.dwMajorVersion)) {
/* Build information for Windows 10 can't be hard coded, use GetVersionEx() below. */
PPG_si_osVersion = NULL;
} else
#endif /* defined(_WIN32_WINNT_WIN10) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WIN10) */
if (IsWindows8Point1OrGreater()) {
PPG_si_osVersion = "6.3 build 9600";
} else if (IsWindows8OrGreater()) {
PPG_si_osVersion = "6.2 build 9200";
} else if (IsWindows7SP1OrGreater()) {
PPG_si_osVersion = "6.1 build 7601 Sevice Pack 1";
} else if (IsWindows7OrGreater()) {
PPG_si_osVersion = "6.1 build 7600";
} else if (IsWindowsVistaSP2OrGreater()) {
PPG_si_osVersion = "6.0 build 6002 Sevice Pack 2";
} else if (IsWindowsVistaSP1OrGreater()) {
PPG_si_osVersion = "6.0 build 6001 Sevice Pack 1";
} else if (IsWindowsVistaOrGreater()) {
PPG_si_osVersion = "6.0 build 6000";
/* The Windows XP Service Packs do not update the version number. */
} else if (IsWindowsXPSP3OrGreater()) {
PPG_si_osVersion = "5.1 build 2600 Sevice Pack 3";
} else if (IsWindowsXPSP2OrGreater()) {
PPG_si_osVersion = "5.1 build 2600 Sevice Pack 2";
} else if (IsWindowsXPSP1OrGreater()) {
PPG_si_osVersion = "5.1 build 2600 Sevice Pack 1";
} else if (IsWindowsXPOrGreater()) {
PPG_si_osVersion = "5.1 build 2600";
}
}
#endif /* defined(_WIN32_WINNT_WINBLUE) && (_WIN32_WINNT_MAXVER >= _WIN32_WINNT_WINBLUE) */
if (NULL == PPG_si_osVersion) {
OSVERSIONINFOW versionInfo;
int len = sizeof("0123456789.0123456789 build 0123456789 ") + 1;
char *buffer;
uintptr_t position;
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
/* GetVersionEx() is deprecated, but still useful to get the Windows 10 build information. Suppress the warning. */
#pragma warning( suppress : 4996 )
if (!GetVersionExW(&versionInfo)) {
return NULL;
}
if (NULL != versionInfo.szCSDVersion) {
len += WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS, versionInfo.szCSDVersion, -1, NULL, 0, NULL, NULL);
}
buffer = portLibrary->mem_allocate_memory(portLibrary, len, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == buffer) {
return NULL;
}
position = portLibrary->str_printf(portLibrary, buffer, len, "%d.%d build %d",
versionInfo.dwMajorVersion,
versionInfo.dwMinorVersion,
versionInfo.dwBuildNumber & 0x0000FFFF);
if ((NULL != versionInfo.szCSDVersion) && ('\0' != versionInfo.szCSDVersion[0])) {
buffer[position++] = ' ';
WideCharToMultiByte(OS_ENCODING_CODE_PAGE, OS_ENCODING_WC_FLAGS, versionInfo.szCSDVersion, -1, &buffer[position], (int)(len - position - 1), NULL, NULL);
}
PPG_si_osVersion = buffer;
PPG_si_osVersionOnHeap = buffer;
}
return PPG_si_osVersion;
}
intptr_t
omrsysinfo_process_exists(struct OMRPortLibrary *portLibrary, uintptr_t pid)
{
HANDLE hProcess;
intptr_t rc = 0;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, (DWORD)pid);
/* OpenProcess returns NULL if the process does not exist or if the call fails for other reasons */
if (NULL == hProcess) {
DWORD lastError = GetLastError();
/*
* if we are querying another user's process, we get ERROR_ACCESS_DENIED.
* For dead processes we get ERROR_INVALID_PARAMETER
*/
if (ERROR_ACCESS_DENIED == lastError) {
rc = 1;
}
} else {
DWORD exitCode;
uintptr_t callSucceeded;
callSucceeded = GetExitCodeProcess(hProcess, (LPDWORD)&exitCode);
if ((0 != callSucceeded) && (STILL_ACTIVE == exitCode)) {
rc = 1;
}
CloseHandle(hProcess);
}
return rc;
}
/**
* Determine the process ID of the calling process.
*
* @param[in] portLibrary The port library.
*
* @return the PID.
*/
uintptr_t
omrsysinfo_get_pid(struct OMRPortLibrary *portLibrary)
{
return GetCurrentProcessId();
}
uintptr_t
omrsysinfo_get_ppid(struct OMRPortLibrary *portLibrary)
{
return 0;
}
uintptr_t
omrsysinfo_get_euid(struct OMRPortLibrary *portLibrary)
{
return 0;
}
uintptr_t
omrsysinfo_get_egid(struct OMRPortLibrary *portLibrary)
{
return 0;
}
intptr_t
omrsysinfo_get_groups(struct OMRPortLibrary *portLibrary, uint32_t **gidList, uint32_t categoryCode)
{
return -1;
}
/**
* @internal Helper routine that determines the full path of the current executable
* that launched the JVM instance.
*/
static intptr_t
find_executable_name(struct OMRPortLibrary *portLibrary, char **result)
{
DWORD length;
wchar_t unicodeBuffer[UNICODE_BUFFER_SIZE];
char *utf8Result;
length = GetModuleFileNameW(NULL, unicodeBuffer, UNICODE_BUFFER_SIZE);
if (!length || (length >= UNICODE_BUFFER_SIZE)) {
return -1;
}
unicodeBuffer[length] = '\0';
utf8Result = portLibrary->mem_allocate_memory(portLibrary, (length + 1) * 3, OMR_GET_CALLSITE(), OMRMEM_CATEGORY_PORT_LIBRARY);
if (NULL == utf8Result) {
return -1;
}
port_convertToUTF8(portLibrary, unicodeBuffer, utf8Result, length * 3);
*result = utf8Result;
return 0;
}
/**
* Determines an absolute pathname for the executable.
*
* @param[in] portLibrary The port library.
* @param[in] argv0 argv[0] value
* @param[out] result Null terminated pathname string
*
* @return 0 on success, -1 on error (or information is not available).
*
* @note Caller should /not/ de-allocate memory in the result buffer, as string containing
* the executable name is system-owned (managed internally by the port library).
*/
intptr_t
omrsysinfo_get_executable_name(struct OMRPortLibrary *portLibrary, const char *argv0, char **result)
{
(void) argv0; /* @args used */
/* Clear any pending error conditions. */
portLibrary->error_set_last_error(portLibrary, 0, 0);
if (PPG_si_executableName) {
*result = PPG_si_executableName;
return 0;
}
*result = NULL;
return (intptr_t)-1;
}
uintptr_t
omrsysinfo_get_number_CPUs_by_type(struct OMRPortLibrary *portLibrary, uintptr_t type)
{
uintptr_t toReturn = 0;
Trc_PRT_sysinfo_get_number_CPUs_by_type_Entered();
switch (type) {
case OMRPORT_CPU_PHYSICAL:
case OMRPORT_CPU_ONLINE: {
SYSTEM_INFO aSysInfo;
GetSystemInfo(&aSysInfo);
toReturn = aSysInfo.dwNumberOfProcessors;
if (0 == toReturn) {
Trc_PRT_sysinfo_get_number_CPUs_by_type_failedPhysical("(no errno) ", 0);
}
break;
}
case OMRPORT_CPU_BOUND: {
uintptr_t processAffinity = 0;
uintptr_t systemAffinity = 0;
HANDLE currentProcess = GetCurrentProcess();
uintptr_t count = 0;
int32_t i = 0;
uintptr_t mask = 0x1;
GetProcessAffinityMask(currentProcess, (PDWORD_PTR) &processAffinity, (PDWORD_PTR) &systemAffinity);
/*
* Count the number of bound CPU's
*/
for (i = 0; i < (sizeof(DWORD64) * 8); i++) {
count += (0 == (processAffinity & mask)) ? 0 : 1;
mask <<= 1;
}
toReturn = count;
if (0 == toReturn) {
Trc_PRT_sysinfo_get_number_CPUs_by_type_failedBound("errno: ", GetLastError());
}
break;
}
case OMRPORT_CPU_TARGET: {
uintptr_t specified = portLibrary->portGlobals->userSpecifiedCPUs;
if (0 < specified) {
toReturn = specified;
} else {
toReturn = portLibrary->sysinfo_get_number_CPUs_by_type(portLibrary, OMRPORT_CPU_BOUND);
}
break;
}
default:
/* Invalid argument */
toReturn = 0;
Trc_PRT_sysinfo_get_number_CPUs_by_type_invalidType();
break;
}
Trc_PRT_sysinfo_get_number_CPUs_by_type_Exit(type, toReturn);
return toReturn;
}
/* Paths for pdh memory counters. */
#define MEMORY_COMMIT_LIMIT_COUNTER_PATH "\\Memory\\Commit Limit"
#define MEMORY_COMMITTED_BYTES_COUNTER_PATH "\\Memory\\Committed Bytes"
#define MEMORY_CACHE_BYTES_COUNTER_PATH "\\Memory\\Cache Bytes"
int32_t
omrsysinfo_get_memory_info(struct OMRPortLibrary *portLibrary, struct J9MemoryInfo *memInfo, ...)
{
int32_t rc = -1;
MEMORYSTATUSEX aMemStatusEx = {0};
/* Handles for pdh memory counters. */
PDH_HCOUNTER memoryCommitLimitCounter = NULL;
PDH_HCOUNTER memoryCommittedBytesCounter = NULL;
PDH_HCOUNTER memoryCacheBytesCounter = NULL;
/* Handle for querying pdh performance data. */
PDH_HQUERY statsHandle = NULL;
PDH_STATUS status = ERROR_SUCCESS;
PDH_RAW_COUNTER counterValue = {PDH_CSTATUS_INVALID_DATA, {0, 0}, 0, 0, 0};
Trc_PRT_sysinfo_get_memory_info_Entered();
if (NULL == memInfo) {
Trc_PRT_sysinfo_get_memory_info_Exit(OMRPORT_ERROR_SYSINFO_NULL_OBJECT_RECEIVED);
return OMRPORT_ERROR_SYSINFO_NULL_OBJECT_RECEIVED;
}
memInfo->totalPhysical = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->availPhysical = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->totalVirtual = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->availVirtual = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->totalSwap = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->availSwap = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->cached = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->buffered = OMRPORT_MEMINFO_NOT_AVAILABLE;
memInfo->swappiness = OMRPORT_MEMINFO_NOT_AVAILABLE;
aMemStatusEx.dwLength = sizeof(aMemStatusEx);
rc = GlobalMemoryStatusEx(&aMemStatusEx);
/* Win32 API GlobalMemoryStatusEx() returns 0 on failure. */
if (0 == rc) {
Trc_PRT_sysinfo_get_memory_info_memStatFailed(GetLastError());
Trc_PRT_sysinfo_get_memory_info_Exit(OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO);
return OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO;
}
memInfo->totalPhysical = (uint64_t)aMemStatusEx.ullTotalPhys;
memInfo->availPhysical = (uint64_t)aMemStatusEx.ullAvailPhys;
memInfo->totalVirtual = (uint64_t)aMemStatusEx.ullTotalVirtual;
memInfo->availVirtual = (uint64_t)aMemStatusEx.ullAvailVirtual;
/* Create pdh handle for managing the performance data collection. */
status = PdhOpenQuery(NULL, (DWORD_PTR)NULL, (PDH_HQUERY *)&statsHandle);
if (ERROR_SUCCESS != status) {
Trc_PRT_sysinfo_get_memory_info_pdhOpenQueryFailed(status);
Trc_PRT_sysinfo_get_memory_info_Exit(OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO);
return OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO;
}
status = PdhAddCounter(statsHandle,
MEMORY_COMMIT_LIMIT_COUNTER_PATH,
(DWORD_PTR)NULL,
&memoryCommitLimitCounter);
if (ERROR_SUCCESS != status) {
Trc_PRT_sysinfo_get_memory_info_failedAddingCounter("Commit Limit", status);
Trc_PRT_sysinfo_get_memory_info_Exit(OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO);
PdhCloseQuery(statsHandle);
return OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO;
}
status = PdhAddCounter(statsHandle,
MEMORY_COMMITTED_BYTES_COUNTER_PATH,
(DWORD_PTR)NULL,
&memoryCommittedBytesCounter);
if (ERROR_SUCCESS != status) {
Trc_PRT_sysinfo_get_memory_info_failedAddingCounter("Committed Bytes", status);
Trc_PRT_sysinfo_get_memory_info_Exit(OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO);
PdhCloseQuery(statsHandle);
return OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO;
}
status = PdhAddCounter(statsHandle,
MEMORY_CACHE_BYTES_COUNTER_PATH,
(DWORD_PTR)NULL,
&memoryCacheBytesCounter);
if (ERROR_SUCCESS != status) {
Trc_PRT_sysinfo_get_memory_info_failedAddingCounter("Cache Bytes", status);
Trc_PRT_sysinfo_get_memory_info_Exit(OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO);
PdhCloseQuery(statsHandle);
return OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO;
}
/* Collect the current raw data value for all counters in the usage stats query. */
status = PdhCollectQueryData(statsHandle);
if (ERROR_SUCCESS != status) {
Trc_PRT_sysinfo_get_memory_info_dataQueryFailed();
Trc_PRT_sysinfo_get_memory_info_Exit(OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO);
PdhCloseQuery(statsHandle);
return OMRPORT_ERROR_SYSINFO_ERROR_READING_MEMORY_INFO;
}
status = PdhGetRawCounterValue(memoryCommitLimitCounter, (LPDWORD)NULL, &counterValue);
if ((ERROR_SUCCESS == status) && ((PDH_CSTATUS_VALID_DATA == counterValue.CStatus) ||
(PDH_CSTATUS_NEW_DATA == counterValue.CStatus))) {
memInfo->totalSwap = counterValue.FirstValue;
}
status = PdhGetRawCounterValue(memoryCommittedBytesCounter, (LPDWORD)NULL, &counterValue);
if ((ERROR_SUCCESS == status) && ((PDH_CSTATUS_VALID_DATA == counterValue.CStatus) ||
(PDH_CSTATUS_NEW_DATA == counterValue.CStatus))) {
memInfo->availSwap = (memInfo->totalSwap - counterValue.FirstValue);
}
status = PdhGetRawCounterValue(memoryCacheBytesCounter, (LPDWORD)NULL, &counterValue);
if ((ERROR_SUCCESS == status) && ((PDH_CSTATUS_VALID_DATA == counterValue.CStatus) ||
(PDH_CSTATUS_NEW_DATA == counterValue.CStatus))) {
memInfo->cached = counterValue.FirstValue;
}
/* Note that Windows does not have 'buffered memory' and hence, memInfo->buffered remains -1. */
memInfo->timestamp = (portLibrary->time_nano_time(portLibrary) / NANOSECS_PER_USEC);
memInfo->hostAvailPhysical = memInfo->availPhysical;
memInfo->hostCached = memInfo->cached;
memInfo->hostBuffered = memInfo->buffered;
Trc_PRT_sysinfo_get_memory_info_Exit(0);
PdhCloseQuery(statsHandle);