270
270
@ ExportLibrary (value = NativeTypeLibrary .class , useForAOT = false )
271
271
public class GraalHPyContext extends CExtContext implements TruffleObject {
272
272
273
- private static final boolean TRACE ;
274
- private static final int TRACE_SLEEP_TIME ;
275
- static {
276
- String prop = System .getProperty ("HPyTraceUpcalls" );
277
- boolean doTrace = false ;
278
- int sleepTime = 5000 ;
279
- if (prop != null ) {
280
- if (prop .equals ("true" )) {
281
- doTrace = true ;
282
- } else {
283
- try {
284
- sleepTime = Integer .parseInt (prop );
285
- doTrace = true ;
286
- } catch (NumberFormatException e ) {
287
- // pass
288
- }
289
- }
290
- }
291
- TRACE = doTrace ;
292
- TRACE_SLEEP_TIME = sleepTime ;
293
- }
273
+ private final boolean traceJNIUpcalls ;
294
274
295
275
private static final TruffleLogger LOGGER = PythonLanguage .getLogger (GraalHPyContext .class );
296
276
@@ -857,8 +837,12 @@ public static GraalHPyNativeSymbol getGetterFunctionName(LLVMType llvmType) {
857
837
858
838
public GraalHPyContext (PythonContext context , Object hpyLibrary ) {
859
839
super (context , hpyLibrary , GraalHPyConversionNodeSupplier .HANDLE );
840
+ PythonLanguage language = context .getLanguage ();
841
+ useNativeFastPaths = language .getEngineOption (PythonOptions .HPyEnableJNIFastPaths );
842
+ int traceJNISleepTime = language .getEngineOption (PythonOptions .HPyTraceUpcalls );
843
+ traceJNIUpcalls = traceJNISleepTime != 0 ;
860
844
this .slowPathFactory = context .factory ();
861
- this .hpyContextMembers = createMembers (context , getName ());
845
+ this .hpyContextMembers = createMembers (context , getName (), traceJNIUpcalls );
862
846
for (Object member : hpyContextMembers ) {
863
847
if (member instanceof GraalHPyHandle ) {
864
848
GraalHPyHandle handle = (GraalHPyHandle ) member ;
@@ -869,7 +853,9 @@ public GraalHPyContext(PythonContext context, Object hpyLibrary) {
869
853
}
870
854
hpyHandleTable = Arrays .copyOf (hpyHandleTable , IMMUTABLE_HANDLE_COUNT * 2 );
871
855
nextHandle = IMMUTABLE_HANDLE_COUNT ;
872
- this .useNativeFastPaths = context .getLanguage ().getEngineOption (PythonOptions .HPyEnableJNIFastPaths );
856
+ if (traceJNIUpcalls ) {
857
+ startUpcallsDaemon (traceJNISleepTime );
858
+ }
873
859
}
874
860
875
861
protected String getName () {
@@ -1394,45 +1380,23 @@ public enum Counter {
1394
1380
UpcallListNew ,
1395
1381
UpcallTupleFromArray ;
1396
1382
1397
- long count ;
1398
-
1399
- void increment () {
1400
- if (TRACE ) {
1401
- count ++;
1402
- }
1403
- }
1383
+ @ CompilationFinal (dimensions = 1 ) private static final Counter [] VALUES = values ();
1404
1384
}
1405
1385
1406
- static {
1407
- if (TRACE ) {
1408
- Thread thread = new Thread (() -> {
1409
-
1410
- while (true ) {
1411
- try {
1412
- Thread .sleep (TRACE_SLEEP_TIME );
1413
- } catch (InterruptedException e ) {
1414
- // fall through
1415
- }
1416
- System .out .println ("==== HPy counts" );
1417
- for (Counter c : Counter .values ()) {
1418
- System .out .printf (" %20s: %8d\n " , c .name (), c .count );
1419
- }
1420
- }
1421
-
1422
- });
1423
- thread .setDaemon (true );
1424
- thread .start ();
1386
+ private void increment (Counter upcall ) {
1387
+ if (traceJNIUpcalls ) {
1388
+ jniCounts [upcall .ordinal ()]++;
1425
1389
}
1426
1390
}
1427
1391
1428
1392
@ SuppressWarnings ("static-method" )
1429
1393
public final long ctxFloatFromDouble (double value ) {
1430
- Counter .UpcallFloatFromDouble . increment ( );
1394
+ increment ( Counter .UpcallFloatFromDouble );
1431
1395
return GraalHPyBoxing .boxDouble (value );
1432
1396
}
1433
1397
1434
1398
public final double ctxFloatAsDouble (long handle ) {
1435
- Counter .UpcallFloatAsDouble . increment ( );
1399
+ increment ( Counter .UpcallFloatAsDouble );
1436
1400
1437
1401
if (GraalHPyBoxing .isBoxedDouble (handle )) {
1438
1402
return GraalHPyBoxing .unboxDouble (handle );
@@ -1446,7 +1410,7 @@ public final double ctxFloatAsDouble(long handle) {
1446
1410
}
1447
1411
1448
1412
public final long ctxLongAsLong (long handle ) {
1449
- Counter .UpcallLongAsLong . increment ( );
1413
+ increment ( Counter .UpcallLongAsLong );
1450
1414
1451
1415
if (GraalHPyBoxing .isBoxedInt (handle )) {
1452
1416
return GraalHPyBoxing .unboxInt (handle );
@@ -1462,7 +1426,7 @@ public final long ctxLongAsLong(long handle) {
1462
1426
}
1463
1427
1464
1428
public final double ctxLongAsDouble (long handle ) {
1465
- Counter .UpcallLongAsDouble . increment ( );
1429
+ increment ( Counter .UpcallLongAsDouble );
1466
1430
1467
1431
if (GraalHPyBoxing .isBoxedInt (handle )) {
1468
1432
return GraalHPyBoxing .unboxInt (handle );
@@ -1478,7 +1442,7 @@ public final double ctxLongAsDouble(long handle) {
1478
1442
}
1479
1443
1480
1444
public final long ctxLongFromLong (long l ) {
1481
- Counter .UpcallLongFromLong . increment ( );
1445
+ increment ( Counter .UpcallLongFromLong );
1482
1446
1483
1447
if (com .oracle .graal .python .builtins .objects .ints .PInt .isIntRange (l )) {
1484
1448
return GraalHPyBoxing .boxInt ((int ) l );
@@ -1487,14 +1451,14 @@ public final long ctxLongFromLong(long l) {
1487
1451
}
1488
1452
1489
1453
public final long ctxAsStruct (long handle ) {
1490
- Counter .UpcallCast . increment ( );
1454
+ increment ( Counter .UpcallCast );
1491
1455
1492
1456
Object receiver = getObjectForHPyHandle (GraalHPyBoxing .unboxHandle (handle )).getDelegate ();
1493
1457
return (long ) HPyGetNativeSpacePointerNodeGen .getUncached ().execute (receiver );
1494
1458
}
1495
1459
1496
1460
public final long ctxNew (long typeHandle , long dataOutVar ) {
1497
- Counter .UpcallNew . increment ( );
1461
+ increment ( Counter .UpcallNew );
1498
1462
1499
1463
Object type = getObjectForHPyHandle (GraalHPyBoxing .unboxHandle (typeHandle )).getDelegate ();
1500
1464
PythonObject pythonObject ;
@@ -1536,7 +1500,7 @@ public final long ctxNew(long typeHandle, long dataOutVar) {
1536
1500
}
1537
1501
1538
1502
public final long ctxTypeGenericNew (long typeHandle ) {
1539
- Counter .UpcallTypeGenericNew . increment ( );
1503
+ increment ( Counter .UpcallTypeGenericNew );
1540
1504
1541
1505
Object type = getObjectForHPyHandle (GraalHPyBoxing .unboxHandle (typeHandle )).getDelegate ();
1542
1506
@@ -1568,12 +1532,12 @@ private void closeNativeHandle(long handle) {
1568
1532
}
1569
1533
1570
1534
public final void ctxClose (long handle ) {
1571
- Counter .UpcallClose . increment ( );
1535
+ increment ( Counter .UpcallClose );
1572
1536
closeNativeHandle (handle );
1573
1537
}
1574
1538
1575
1539
public final void ctxBulkClose (long unclosedHandlePtr , int size ) {
1576
- Counter .UpcallBulkClose . increment ( );
1540
+ increment ( Counter .UpcallBulkClose );
1577
1541
for (int i = 0 ; i < size ; i ++) {
1578
1542
long handle = unsafe .getLong (unclosedHandlePtr );
1579
1543
unclosedHandlePtr += 8 ;
@@ -1584,7 +1548,7 @@ public final void ctxBulkClose(long unclosedHandlePtr, int size) {
1584
1548
}
1585
1549
1586
1550
public final long ctxDup (long handle ) {
1587
- Counter .UpcallDup . increment ( );
1551
+ increment ( Counter .UpcallDup );
1588
1552
if (GraalHPyBoxing .isBoxedHandle (handle )) {
1589
1553
GraalHPyHandle pyHandle = getObjectForHPyHandle (GraalHPyBoxing .unboxHandle (handle ));
1590
1554
return GraalHPyBoxing .boxHandle (createHandle (pyHandle .getDelegate ()).getId (this , ConditionProfile .getUncached ()));
@@ -1594,7 +1558,7 @@ public final long ctxDup(long handle) {
1594
1558
}
1595
1559
1596
1560
public final long ctxGetItemi (long hCollection , long lidx ) {
1597
- Counter .UpcallGetItemI . increment ( );
1561
+ increment ( Counter .UpcallGetItemI );
1598
1562
try {
1599
1563
// If handle 'hCollection' is a boxed int or double, the object is not subscriptable.
1600
1564
if (!GraalHPyBoxing .isBoxedHandle (hCollection )) {
@@ -1648,7 +1612,7 @@ public final long ctxGetItemi(long hCollection, long lidx) {
1648
1612
* @return {@code 0} on success; {@code -1} on error
1649
1613
*/
1650
1614
public final int ctxSetItem (long hSequence , long hKey , long hValue ) {
1651
- Counter .UpcallSetItem . increment ( );
1615
+ increment ( Counter .UpcallSetItem );
1652
1616
try {
1653
1617
// If handle 'hSequence' is a boxed int or double, the object is not a sequence.
1654
1618
if (!GraalHPyBoxing .isBoxedHandle (hSequence )) {
@@ -1691,7 +1655,7 @@ public final int ctxSetItem(long hSequence, long hKey, long hValue) {
1691
1655
}
1692
1656
1693
1657
public final int ctxSetItemi (long hSequence , long lidx , long hValue ) {
1694
- Counter .UpcallSetItemI . increment ( );
1658
+ increment ( Counter .UpcallSetItemI );
1695
1659
try {
1696
1660
// If handle 'hSequence' is a boxed int or double, the object is not a sequence.
1697
1661
if (!GraalHPyBoxing .isBoxedHandle (hSequence )) {
@@ -1748,7 +1712,7 @@ private static int setItemGeneric(Object receiver, Object clazz, Object key, Obj
1748
1712
}
1749
1713
1750
1714
public final int ctxNumberCheck (long handle ) {
1751
- Counter .UpcallNumberCheck . increment ( );
1715
+ increment ( Counter .UpcallNumberCheck );
1752
1716
if (GraalHPyBoxing .isBoxedDouble (handle ) || GraalHPyBoxing .isBoxedInt (handle )) {
1753
1717
return 1 ;
1754
1718
}
@@ -1777,7 +1741,7 @@ private static PythonBuiltinClassType getBuiltinClass(Object cls) {
1777
1741
}
1778
1742
1779
1743
public final int ctxTypeCheck (long handle , long typeHandle ) {
1780
- Counter .UpcallTypeCheck . increment ( );
1744
+ increment ( Counter .UpcallTypeCheck );
1781
1745
Object receiver ;
1782
1746
if (GraalHPyBoxing .isBoxedDouble (handle )) {
1783
1747
receiver = PythonBuiltinClassType .PFloat ;
@@ -1820,7 +1784,7 @@ public final int ctxTypeCheck(long handle, long typeHandle) {
1820
1784
}
1821
1785
1822
1786
public final long ctxLength (long handle ) {
1823
- Counter .UpcallLength . increment ( );
1787
+ increment ( Counter .UpcallLength );
1824
1788
assert GraalHPyBoxing .isBoxedHandle (handle );
1825
1789
1826
1790
Object receiver = getObjectForHPyHandle (GraalHPyBoxing .unboxHandle (handle )).getDelegate ();
@@ -1840,7 +1804,7 @@ public final long ctxLength(long handle) {
1840
1804
}
1841
1805
1842
1806
public final int ctxListCheck (long handle ) {
1843
- Counter .UpcallListCheck . increment ( );
1807
+ increment ( Counter .UpcallListCheck );
1844
1808
if (GraalHPyBoxing .isBoxedHandle (handle )) {
1845
1809
Object obj = getObjectForHPyHandle (GraalHPyBoxing .unboxHandle (handle )).getDelegate ();
1846
1810
Object clazz = GetClassNode .getUncached ().execute (obj );
@@ -1851,7 +1815,7 @@ public final int ctxListCheck(long handle) {
1851
1815
}
1852
1816
1853
1817
public final long ctxUnicodeFromWideChar (long wcharArrayPtr , long size ) {
1854
- Counter .UpcallUnicodeFromWideChar . increment ( );
1818
+ increment ( Counter .UpcallUnicodeFromWideChar );
1855
1819
1856
1820
if (!PInt .isIntRange (size )) {
1857
1821
// NULL handle
@@ -1873,19 +1837,19 @@ public final long ctxUnicodeFromWideChar(long wcharArrayPtr, long size) {
1873
1837
}
1874
1838
1875
1839
public final long ctxUnicodeFromJCharArray (char [] arr ) {
1876
- Counter .UpcallUnicodeFromJCharArray . increment ( );
1840
+ increment ( Counter .UpcallUnicodeFromJCharArray );
1877
1841
return createHandle (new String (arr , 0 , arr .length )).getId (this , ConditionProfile .getUncached ());
1878
1842
}
1879
1843
1880
1844
public final long ctxDictNew () {
1881
- Counter .UpcallDictNew . increment ( );
1845
+ increment ( Counter .UpcallDictNew );
1882
1846
PDict dict = PythonObjectFactory .getUncached ().createDict ();
1883
1847
return createHandle (dict ).getId (this , ConditionProfile .getUncached ());
1884
1848
}
1885
1849
1886
1850
public final long ctxListNew (long llen ) {
1887
1851
try {
1888
- Counter .UpcallListNew . increment ( );
1852
+ increment ( Counter .UpcallListNew );
1889
1853
int len = CastToJavaIntExactNode .getUncached ().execute (llen );
1890
1854
Object [] data = new Object [len ];
1891
1855
Arrays .fill (data , PNone .NONE );
@@ -1904,7 +1868,7 @@ public final long ctxListNew(long llen) {
1904
1868
* is useful to implement, e.g., tuple builder.
1905
1869
*/
1906
1870
public final long ctxTupleFromArray (long [] hItems , boolean steal ) {
1907
- Counter .UpcallTupleFromArray . increment ( );
1871
+ increment ( Counter .UpcallTupleFromArray );
1908
1872
1909
1873
Object [] objects = new Object [hItems .length ];
1910
1874
for (int i = 0 ; i < hItems .length ; i ++) {
@@ -2014,7 +1978,7 @@ final Object invokeMember(String key, Object[] args,
2014
1978
return memberInvokeLib .execute (member , args );
2015
1979
}
2016
1980
2017
- private static Object [] createMembers (PythonContext context , String name ) {
1981
+ private static Object [] createMembers (PythonContext context , String name , boolean traceJNIUpcalls ) {
2018
1982
Object [] members = new Object [HPyContextMember .VALUES .length ];
2019
1983
2020
1984
members [HPyContextMember .NAME .ordinal ()] = new CStringWrapper (name );
@@ -2276,7 +2240,7 @@ private static Object[] createMembers(PythonContext context, String name) {
2276
2240
members [HPyContextMember .CTX_GLOBAL_LOAD .ordinal ()] = new GraalHPyGlobalLoad ();
2277
2241
members [HPyContextMember .CTX_DUMP .ordinal ()] = new GraalHPyDump ();
2278
2242
2279
- if (TRACE ) {
2243
+ if (traceJNIUpcalls ) {
2280
2244
for (int i = 0 ; i < members .length ; i ++) {
2281
2245
Object m = members [i ];
2282
2246
if (m != null && !(m instanceof Number || m instanceof GraalHPyHandle )) {
@@ -2290,30 +2254,32 @@ private static Object[] createMembers(PythonContext context, String name) {
2290
2254
}
2291
2255
2292
2256
static final int [] counts = new int [HPyContextMember .VALUES .length ];
2257
+ static final int [] jniCounts = new int [Counter .values ().length ];
2293
2258
2294
- static {
2295
- if (TRACE ) {
2296
- Thread thread = new Thread () {
2297
- @ Override
2298
- public void run () {
2299
- while (true ) {
2300
- try {
2301
- Thread .sleep (TRACE_SLEEP_TIME );
2302
- } catch (InterruptedException e ) {
2303
- e .printStackTrace ();
2304
- }
2305
- System .out .println ("========= stats" );
2306
- for (int i = 0 ; i < counts .length ; i ++) {
2307
- if (counts [i ] != 0 ) {
2308
- System .out .printf (" %40s[%3d]: %d\n " , HPyContextMember .VALUES [i ].name , i , counts [i ]);
2309
- }
2310
- }
2259
+ private static void startUpcallsDaemon (int traceJNISleepTime ) {
2260
+ Thread thread = new Thread (() -> {
2261
+ while (true ) {
2262
+ try {
2263
+ Thread .sleep (traceJNISleepTime );
2264
+ } catch (InterruptedException e ) {
2265
+ e .printStackTrace ();
2266
+ }
2267
+ System .out .println ("========= HPy LLVM/NFI upcall counts" );
2268
+ for (int i = 0 ; i < counts .length ; i ++) {
2269
+ if (counts [i ] != 0 ) {
2270
+ System .out .printf (" %40s[%3d]: %d\n " , HPyContextMember .VALUES [i ].name , i , counts [i ]);
2311
2271
}
2312
2272
}
2313
- };
2314
- thread .setDaemon (true );
2315
- thread .start ();
2316
- }
2273
+ System .out .println ("==== HPy JNI upcall counts" );
2274
+ for (int i = 0 ; i < jniCounts .length ; i ++) {
2275
+ if (jniCounts [i ] != 0 ) {
2276
+ System .out .printf (" %40s[%3d]: %d\n " , Counter .VALUES [i ].name (), i , jniCounts [i ]);
2277
+ }
2278
+ }
2279
+ }
2280
+ });
2281
+ thread .setDaemon (true );
2282
+ thread .start ();
2317
2283
}
2318
2284
2319
2285
@ ExportLibrary (value = InteropLibrary .class , delegateTo = "delegate" )
0 commit comments