File: NonVolatileMemoryAccessWithLongOffset.java

package info (click to toggle)
openjdk-11 11.0.28%2B6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,124 kB
  • sloc: java: 5,208,481; xml: 1,192,267; cpp: 1,138,346; ansic: 461,925; javascript: 162,416; sh: 16,738; objc: 13,729; python: 4,757; asm: 3,570; makefile: 2,965; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (546 lines) | stat: -rw-r--r-- 20,779 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


import sun.misc.Unsafe;

import java.util.Arrays;
import java.util.Random;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.round;
import static java.math.BigInteger.TEN;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.nio.ByteOrder;
import static java.lang.String.format;
import static java.lang.System.arraycopy;

/* @test
 * @bug 8235385 8287508
 * @summary Verifies non-volatile memory access with long offset
 * @requires os.arch == "aarch64"
 */
public class NonVolatileMemoryAccessWithLongOffset {
    private static final Unsafe unsafe;
    static Random random = new Random();

    static {
        Field f = null;
        try {
            f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            unsafe = (Unsafe) f.get(null);
        } catch (ReflectiveOperationException e) {
            throw new Error(e);
        }
    }
    private static final int MAX_LENGTH = 10000;

    private static byte[] input0 = new byte[MAX_LENGTH];
    private static byte[] input1 = new byte[MAX_LENGTH];
    private static final int MAX_VALUE_LENGTH = random.nextInt(200) + 20;
    private static final byte[] maxValue = new byte[MAX_VALUE_LENGTH];
    private static final byte[] minValue = new byte[MAX_VALUE_LENGTH];
    private static final int DECIMAL_MAX_VALUE_LENGTH = MAX_VALUE_LENGTH - 15;
    private static byte[] byteArray;

    private static final    int valueType = random.nextInt(100);
    private static final    int numNulls = random.nextInt(100);
    private static final    int numRows = random.nextInt(100);
    private static final    int countDistinct = random.nextInt(100);
    private static final    long rawDataSize = (long)random.nextInt(100);
    private static final    long sum = (long)random.nextInt(1000);
    private static final    int version = random.nextInt(100);


    private static final    int dictOffset = random.nextInt(100);
    private static final    int dictLength = random.nextInt(100);
    private static final    int histOffset = random.nextInt(100);
    private static final    int histLength = random.nextInt(100);
    private static final    int dpnOffset = random.nextInt(100);
    private static final    int dpnCount = random.nextInt(100);

    private static final    long maxRowCount = (long)random.nextInt(1000);
    private static final    long minRowCount = (long)random.nextInt(1000);
    private static final    long totalRowCount = (long)random.nextInt(100);
    private static final    long maxMemSize = (long)random.nextInt(1000);
    private static final    long minMemSize = (long)random.nextInt(100);
    private static final    long totalMemSize = (long)random.nextInt(1000);

    private static final    long toastOffset = (long)random.nextInt(1000);
    private static final    boolean hasToast = random.nextInt(100) > 50;


    private static final int MAX_STRING_LENGTH = random.nextInt(300) + 20;
    private static final byte[] maxString = new byte[MAX_STRING_LENGTH];
    private static final byte[] minString = new byte[MAX_STRING_LENGTH];
    private static int maxStringLength = random.nextInt(100);
    private static int minStringLength = random.nextInt(100);
    private static final boolean maxStringIsNull = random.nextInt(100) > 20;
    private static final boolean minStringIsNull = random.nextInt(100) > 60;

    private static final short precision = (short)random.nextInt(100);
    private static final short scale = (short)random.nextInt(100);
    private static final boolean useShortCompressFloat = random.nextInt(100) > 60;
    private static final int SHORT_SIZE = 2;
    private static final int CHAR_SIZE = 2;
    private static final int INT_SIZE = 4;
    private static final int LONG_SIZE = 8;
    private static final int FLOAT_SIZE = 4;
    private static final int DOUBLE_SIZE = 8;
    private static final int BOOLEAN_SIZE = 1;

    private static void premitiveAssert(boolean flag) {
        if (flag == false) {
            throw new RuntimeException("overflow!");
        }
    }

    private static long BYTE_ARRAY_OFFSET = unsafe.ARRAY_BYTE_BASE_OFFSET;

    private static final void toBytes(short obj, byte[] rawBytes, int start) {
        premitiveAssert(rawBytes.length >= (start +         SHORT_SIZE));
        unsafe.putShort(rawBytes, (long) BYTE_ARRAY_OFFSET + start, obj);
    }

    private static final short bytes2short(byte[] rawBytes, int start){
        premitiveAssert(rawBytes.length >= (start + SHORT_SIZE));
        return unsafe.getShort(rawBytes, (long) BYTE_ARRAY_OFFSET + start);
    }

    private static final void toBytes(int obj, byte[] rawBytes, int start) {
        premitiveAssert(rawBytes.length >= (start +         INT_SIZE));
        unsafe.putInt(rawBytes, (long) BYTE_ARRAY_OFFSET + start, obj);
    }

    private static final int bytes2int(byte[] rawBytes, int start){
        premitiveAssert(rawBytes.length >= (start + INT_SIZE));
        return unsafe.getInt(rawBytes, (long) BYTE_ARRAY_OFFSET + start);
    }

    private static final void toBytes(long obj, byte[] rawBytes, int start) {
        premitiveAssert(rawBytes.length >= (start +         LONG_SIZE));
        unsafe.putLong(rawBytes, (long) BYTE_ARRAY_OFFSET + start, obj);
    }

    private static final long bytes2long(byte[] rawBytes, int start){
        premitiveAssert(rawBytes.length >= (start + LONG_SIZE));
        return unsafe.getLong(rawBytes, (long) BYTE_ARRAY_OFFSET + start);
    }

    private static final void toBytes(boolean obj, byte[] rawBytes, int start) {
        premitiveAssert(rawBytes.length >= (start +         1));
        unsafe.putBoolean(rawBytes, (long) BYTE_ARRAY_OFFSET + start, obj);
    }

    private static final boolean bytes2boolen(byte[] rawBytes, int start) {
        premitiveAssert(rawBytes.length >= (start + 1));
        return unsafe.getBoolean(rawBytes, (long) BYTE_ARRAY_OFFSET + start);
    }

    private static final byte[] toBytesDup() {
        byte[] rawBytes = input1;

        int offset = 0;

        toBytes(valueType, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(numNulls, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(numRows, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(countDistinct, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(rawDataSize, rawBytes, offset);
        offset +=         LONG_SIZE;

        toBytes(sum, rawBytes, offset);
        offset +=         LONG_SIZE;
        if (version > 50) {
            System.arraycopy(maxValue, 0, rawBytes, offset, MAX_VALUE_LENGTH);
            offset += MAX_VALUE_LENGTH;

            System.arraycopy(minValue, 0, rawBytes, offset, MAX_VALUE_LENGTH);
            offset += MAX_VALUE_LENGTH;
        } else {
            System.arraycopy(maxValue, 0, rawBytes, offset, DECIMAL_MAX_VALUE_LENGTH);
            offset += DECIMAL_MAX_VALUE_LENGTH;

            System.arraycopy(minValue, 0, rawBytes, offset, DECIMAL_MAX_VALUE_LENGTH);
            offset += DECIMAL_MAX_VALUE_LENGTH;
        }

        toBytes(dictOffset, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(dictLength, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(histOffset, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(histLength, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(dpnOffset, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(dpnCount, rawBytes, offset);
        offset +=         INT_SIZE;
        if (version >= 60) {
            toBytes(maxRowCount, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(minRowCount, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(totalRowCount, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(maxMemSize, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(minMemSize, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(totalMemSize, rawBytes, offset);
            offset +=         LONG_SIZE;
        }

       if (version >= 65) {
           toBytes(toastOffset, rawBytes, offset);
           offset +=         LONG_SIZE;
           toBytes(hasToast, rawBytes, offset);
           offset +=         BOOLEAN_SIZE;
       }
       if (version >= 70) {
          System.arraycopy(maxString, 0, rawBytes, offset, MAX_STRING_LENGTH);
          offset += MAX_STRING_LENGTH;

          System.arraycopy(minString, 0, rawBytes, offset, MAX_STRING_LENGTH);
          offset += MAX_STRING_LENGTH;

          toBytes(maxStringLength, rawBytes, offset);
          offset +=         INT_SIZE;

          toBytes(minStringLength, rawBytes, offset);
          offset +=         INT_SIZE;

          toBytes(maxStringIsNull, rawBytes, offset);
          offset +=         BOOLEAN_SIZE;

          toBytes(minStringIsNull, rawBytes, offset);
          offset +=         BOOLEAN_SIZE;
       }

       if (version >= 75) {
           toBytes(precision, rawBytes, offset);
           offset +=         SHORT_SIZE;

           toBytes(scale, rawBytes, offset);
           offset +=         SHORT_SIZE;
       }

       if (version >= 80) {
           toBytes(useShortCompressFloat, rawBytes, offset);
           offset +=         BOOLEAN_SIZE;
       }
        return rawBytes;
    }


    private static final byte[] toBytes() {
        byte[] rawBytes = input0;

        int offset = 0;

        toBytes(valueType, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(numNulls, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(numRows, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(countDistinct, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(rawDataSize, rawBytes, offset);
        offset +=         LONG_SIZE;

        toBytes(sum, rawBytes, offset);
        offset +=         LONG_SIZE;

        if (version > 50) {
            System.arraycopy(maxValue, 0, rawBytes, offset, MAX_VALUE_LENGTH);
            offset += MAX_VALUE_LENGTH;

            System.arraycopy(minValue, 0, rawBytes, offset, MAX_VALUE_LENGTH);
            offset += MAX_VALUE_LENGTH;
        } else {
            System.arraycopy(maxValue, 0, rawBytes, offset, DECIMAL_MAX_VALUE_LENGTH);
            offset += DECIMAL_MAX_VALUE_LENGTH;

            System.arraycopy(minValue, 0, rawBytes, offset, DECIMAL_MAX_VALUE_LENGTH);
            offset += DECIMAL_MAX_VALUE_LENGTH;
        }

        toBytes(dictOffset, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(dictLength, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(histOffset, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(histLength, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(dpnOffset, rawBytes, offset);
        offset +=         INT_SIZE;

        toBytes(dpnCount, rawBytes, offset);
        offset +=         INT_SIZE;

        if (version >= 60) {
            toBytes(maxRowCount, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(minRowCount, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(totalRowCount, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(maxMemSize, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(minMemSize, rawBytes, offset);
            offset +=         LONG_SIZE;

            toBytes(totalMemSize, rawBytes, offset);
            offset +=         LONG_SIZE;
        }

       if (version >= 65) {
           toBytes(toastOffset, rawBytes, offset);
           offset +=         LONG_SIZE;
           toBytes(hasToast, rawBytes, offset);
           offset +=         BOOLEAN_SIZE;
       }

       if (version >= 70) {
          System.arraycopy(maxString, 0, rawBytes, offset, MAX_STRING_LENGTH);
          offset += MAX_STRING_LENGTH;

          System.arraycopy(minString, 0, rawBytes, offset, MAX_STRING_LENGTH);
          offset += MAX_STRING_LENGTH;

          toBytes(maxStringLength, rawBytes, offset);
          offset +=         INT_SIZE;

          toBytes(minStringLength, rawBytes, offset);
          offset +=         INT_SIZE;

          toBytes(maxStringIsNull, rawBytes, offset);
          offset +=         BOOLEAN_SIZE;

          toBytes(minStringIsNull, rawBytes, offset);
          offset +=         BOOLEAN_SIZE;
       }

       if (version >= 75) {
           toBytes(precision, rawBytes, offset);
           offset +=         SHORT_SIZE;

           toBytes(scale, rawBytes, offset);
           offset +=         SHORT_SIZE;
       }

       if (version >= 80) {
           toBytes(useShortCompressFloat, rawBytes, offset);
           offset +=         BOOLEAN_SIZE;
       }
        return rawBytes;
    }

    private static final void fromBytes(byte[] rawBytes) throws Throwable {
        int offset = 0;

        if (valueType != bytes2int(rawBytes, offset)) throw new RuntimeException("valueType does not match");
        offset += INT_SIZE;

        if (numNulls != bytes2int(rawBytes, offset)) throw new RuntimeException("numNulls does not match");
        offset += INT_SIZE;

        if (numRows != bytes2int(rawBytes, offset)) throw new RuntimeException("numRows does not match");
        offset += INT_SIZE;

        if (countDistinct != bytes2int(rawBytes, offset)) throw new RuntimeException("countDistinct does not match");
        offset += INT_SIZE;

        if (rawDataSize != bytes2long(rawBytes, offset)) throw new RuntimeException("rawDataSize does not match");
        offset += LONG_SIZE;

        if (sum != bytes2long(rawBytes, offset)) throw new RuntimeException("sum does not match");
        offset += LONG_SIZE;

        byte[] maxValue_ = new byte[MAX_VALUE_LENGTH];
        byte[] minValue_ = new byte[MAX_VALUE_LENGTH];
        if (version > 50) {
            System.arraycopy(rawBytes, offset, maxValue_, 0, MAX_VALUE_LENGTH);
            offset += MAX_VALUE_LENGTH;
            if (!Arrays.equals(maxValue, maxValue_)) throw new RuntimeException("maxValue does not match");

            System.arraycopy(rawBytes, offset, minValue_, 0, MAX_VALUE_LENGTH);
            offset += MAX_VALUE_LENGTH;
            if (!Arrays.equals(minValue, minValue_)) throw new RuntimeException("minValue does not match");
        } else {
            System.arraycopy(rawBytes, offset, maxValue_, 0, DECIMAL_MAX_VALUE_LENGTH);
            offset += DECIMAL_MAX_VALUE_LENGTH;
            if (!Arrays.equals(maxValue, maxValue_)) throw new RuntimeException("maxValue does not match");

            System.arraycopy(rawBytes, offset, minValue_, 0, DECIMAL_MAX_VALUE_LENGTH);
            offset += DECIMAL_MAX_VALUE_LENGTH;
            if (!Arrays.equals(minValue, minValue_)) throw new RuntimeException("minValue does not match");
        }

        if (dictOffset != bytes2int(rawBytes, offset)) throw new RuntimeException("dictOffset does not match");
        offset += INT_SIZE;

        if (dictLength != bytes2int(rawBytes, offset)) throw new RuntimeException("dictLength does not match");
        offset += INT_SIZE;

        if (histOffset != bytes2int(rawBytes, offset)) throw new RuntimeException("histOffset does not match");
        offset += INT_SIZE;

        if (histLength != bytes2int(rawBytes, offset)) throw new RuntimeException("histLength does not match");
        offset += INT_SIZE;

        if (dpnOffset != bytes2int(rawBytes, offset)) throw new RuntimeException("dpnOffset does not match");
        offset += INT_SIZE;

        if (dpnCount != bytes2int(rawBytes, offset)) throw new RuntimeException("dpnCount does not match");
        offset +=         INT_SIZE;

        if (version >= 60) {
            if (maxRowCount != bytes2long(rawBytes, offset)) throw new RuntimeException("maxRowCount does not match");
            offset += LONG_SIZE;

            if (minRowCount != bytes2long(rawBytes, offset)) throw new RuntimeException("minRowCount does not match");
            offset += LONG_SIZE;

            if (totalRowCount != bytes2long(rawBytes, offset)) throw new RuntimeException("totalRowCount does not match");
            offset += LONG_SIZE;

            if (maxMemSize != bytes2long(rawBytes, offset)) throw new RuntimeException("maxMemSize does not match");
            offset += LONG_SIZE;

            if (minMemSize != bytes2long(rawBytes, offset)) throw new RuntimeException("minMemSize does not match");
            offset += LONG_SIZE;

            if (totalMemSize != bytes2long(rawBytes, offset)) throw new RuntimeException("totalMemSize does not match");
            offset += LONG_SIZE;
        }

        if (version >= 65) {
            if (toastOffset != bytes2long(rawBytes, offset)) throw new RuntimeException("toastOffset does not match");
            offset += LONG_SIZE;

            if (hasToast != bytes2boolen(rawBytes, offset)) throw new RuntimeException("hasToast does not match");
            offset += BOOLEAN_SIZE;
        }

        if (version >= 70) {
            byte[] maxString_ = new byte[MAX_STRING_LENGTH];
            System.arraycopy(rawBytes, offset, maxString_, 0, MAX_STRING_LENGTH);
            offset += MAX_STRING_LENGTH;
            if (!Arrays.equals(maxString, maxString_)) throw new RuntimeException("maxString does not match");

            byte[] minString_ = new byte[MAX_STRING_LENGTH];
            System.arraycopy(rawBytes, offset, minString_, 0, MAX_STRING_LENGTH);
            offset += MAX_STRING_LENGTH;
            if (!Arrays.equals(minString, minString_)) throw new RuntimeException("minString does not match");

            if (maxStringLength != bytes2int(rawBytes, offset)) throw new RuntimeException("maxStringLength does not match");
            offset += INT_SIZE;

            if (minStringLength != bytes2int(rawBytes, offset)) throw new RuntimeException("minStringLength does not match");
            offset += INT_SIZE;

            if (maxStringIsNull != bytes2boolen(rawBytes, offset)) throw new RuntimeException("maxStringIsNull does not match");
            offset += BOOLEAN_SIZE;

            if (minStringIsNull != bytes2boolen(rawBytes, offset)) throw new RuntimeException("minStringIsNull does not match");
            offset += BOOLEAN_SIZE;
        }

        if (version >= 75) {
            if (precision != bytes2short(rawBytes, offset)) throw new RuntimeException("precision does not match");
            offset += SHORT_SIZE;

            if (scale != bytes2short(rawBytes, offset)) throw new RuntimeException("scale does not match");
            offset += SHORT_SIZE;
        }

        if (version >= 80) {
            if (useShortCompressFloat != bytes2boolen(rawBytes, offset)) throw new RuntimeException("useShortCompressFloat does not match");
            offset += BOOLEAN_SIZE;
        }
    }

    public static void main(String[] args) throws Throwable {
        long s = 0, s1 = 0;
        for (int i = 0; i < input0.length; i++) {
            input0[i] = 0;
            input1[i] = 0;
        }
        for (int i = 0; i < 100000; i++) {
            s += toBytes()[0];
            s1 += toBytesDup()[0];
            for (int j = 0; j < input0.length; j++) {
                if (input0[j] != input1[j]) {
                    throw new RuntimeException("not match!");
                }
            }
            fromBytes(input0);
            fromBytes(input1);
        }
    }
}