From d970a37bd92a1509524e4653544965b19651ee03 Mon Sep 17 00:00:00 2001 From: Petrov Igor Date: Thu, 13 Oct 2022 11:27:57 +0300 Subject: [PATCH] [MM] Tests for js with gc intrinsics Signed-off-by: Petrov Igor --- tests/runtime/mem/gc/common.js | 43 ++++++++++++++++++++++ tests/runtime/mem/gc/fullGC.js | 44 +++++++++++++++++++++++ tests/runtime/mem/gc/gcMarkTasksStress.js | 16 +++++++++ tests/runtime/mem/gc/gcStringTableTest.js | 32 +++++++++++++++++ tests/runtime/mem/gc/gcTaskQueueStress.js | 40 +++++++++++++++++++++ tests/runtime/mem/gc/oom.js | 31 ++++++++++++++++ 6 files changed, 206 insertions(+) create mode 100644 tests/runtime/mem/gc/common.js create mode 100644 tests/runtime/mem/gc/fullGC.js create mode 100644 tests/runtime/mem/gc/gcMarkTasksStress.js create mode 100644 tests/runtime/mem/gc/gcStringTableTest.js create mode 100644 tests/runtime/mem/gc/gcTaskQueueStress.js create mode 100644 tests/runtime/mem/gc/oom.js diff --git a/tests/runtime/mem/gc/common.js b/tests/runtime/mem/gc/common.js new file mode 100644 index 000000000..1bad2f24f --- /dev/null +++ b/tests/runtime/mem/gc/common.js @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Need to implement +export function getFreeSpace() { + return 130000001 +} + +// Need to implement +export function GetMaxYoungSpace() { + return 41964304 +} + +export function newWeakRefInYoung() { + // use a separate function because temporary object 'Object' + // are kept in vreg and GC treat it as a root. + return new WeakRef(new Object()); +} + +// allocate an object of size in bytes at least 'sizeInBytes' +export function allocLargeObject() { + // Ctor Array(length) doesn't allocates the internal JSTaggedValue array. + // Only length setter does this. + let big = new Array() + for (let i = 0; i < 10; ++i) { + let a = new Array() + a.length = 1024 + big[i] = a + } + return big; +} diff --git a/tests/runtime/mem/gc/fullGC.js b/tests/runtime/mem/gc/fullGC.js new file mode 100644 index 000000000..be15abeea --- /dev/null +++ b/tests/runtime/mem/gc/fullGC.js @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import "./common" + +const ALLOCATE_COUNT = 1024 +const FINISH_THRESHOLD = 130000000 +i = 0 +while(getFreeSpace() > FINISH_THRESHOLD) { + + // allocate alive objects + for (let i = 0; i < ALLOCATE_COUNT; ++i) { + let ref = newWeakRefInYoung(); + // add ref to mark queue => alive object + markObject(ref); + } + // allocate garbage + for (let i = 0; i < ALLOCATE_COUNT; ++i) { + let ref = newWeakRefInYoung(); + if (ref.deref() == undefined) { + throw Error(); + } + } + + // periodically run Full GC + i = (i + 1) % 20; + if (i == 0) { + startGC("full") + } else { + startGC("young") + } +} diff --git a/tests/runtime/mem/gc/gcMarkTasksStress.js b/tests/runtime/mem/gc/gcMarkTasksStress.js new file mode 100644 index 000000000..bf9bca45f --- /dev/null +++ b/tests/runtime/mem/gc/gcMarkTasksStress.js @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// in progress diff --git a/tests/runtime/mem/gc/gcStringTableTest.js b/tests/runtime/mem/gc/gcStringTableTest.js new file mode 100644 index 000000000..21a36e228 --- /dev/null +++ b/tests/runtime/mem/gc/gcStringTableTest.js @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const MIN_BIG_STRING_SIZE = 256 * 1024 * 1024 + 1 +const START_STR = "a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9" +function newBigString() { + let newString = START_STR + START_STR + while (newString.length < MIN_BIG_STRING_SIZE) { + newString += newString + } + return newString +} + +let string_count = 0 +let string_array = new Array() +string_array.length = 256 +while (string_count < string_array.length) { + string_array[string_count] = newBigString() + string_count++ +} \ No newline at end of file diff --git a/tests/runtime/mem/gc/gcTaskQueueStress.js b/tests/runtime/mem/gc/gcTaskQueueStress.js new file mode 100644 index 000000000..72bb16da7 --- /dev/null +++ b/tests/runtime/mem/gc/gcTaskQueueStress.js @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function getRandomGC() { + let num = Math.floor(Math.random() * 4) + if (num == 0) { + return "young" + } else if (num == 1) { + return "mixed" + } else if (num == 2) { + return "threshold" + } else { + return "full" + } +} + +const GC_COUNT = 400; +const GARBAGE_COUNT = 50 +for (let i = 0; i < GC_COUNT; ++i) +{ + for (let j = 0; j < GARBAGE_COUNT; ++j) { + let ref = newWeakRefInYoung(); + if (ref.deref() == undefined) { + throw Error(); + } + } + startGC(getRandomGC()); +} diff --git a/tests/runtime/mem/gc/oom.js b/tests/runtime/mem/gc/oom.js new file mode 100644 index 000000000..a82fc04a3 --- /dev/null +++ b/tests/runtime/mem/gc/oom.js @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import "./common" + +const FEW_GARBAGE_COUNT = 10 +while (getFreeSpace() > GetMaxYoungSpace()) { + let ref = allocLargeObject() + markObject(ref); + for (let i = 0; i < FEW_GARBAGE_COUNT; ++i) { + let garbage = newWeakRefInYoung(); + if (garbage.deref() == undefined) { + throw Error(); + } + } +} + +startGC("mixed") -- Gitee