/*
 * Copyright (c) 2022, Azul Systems, Inc. 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.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * 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.
 */

package jdk.jfr.tool;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.text.ParseException;
import java.util.Random;

import jdk.jfr.Event;
import jdk.jfr.Recording;
import jdk.jfr.StackTrace;
import jdk.test.lib.process.OutputAnalyzer;

/**
 * @test
 * @bug 8297402
 * @summary Test huge stack trace pool
 * @key jfr
 * @requires vm.hasJFR
 * @library /test/lib /test/jdk
 * @run main/othervm/timeout=600 -XX:FlightRecorderOptions=stackdepth=2048 jdk.jfr.tool.TestHugeStackTracePool
 */
public class TestHugeStackTracePool {
    private static final Random random = new Random();

    public static void main(String... args) throws Throwable {
        String recordingFileName = "recording.jfr";
        Path path = Path.of(recordingFileName);
        Recording r = new Recording();
        r.setToDisk(true);
        r.start();

        int eventCount = 26_000;
        for (int i = 0; i < eventCount; i++) {
            method(0);
        }

        r.stop();
        r.dump(path);
        r.close();

        OutputAnalyzer output = ExecuteHelper.jfr("-J-Xmx8g", "print", "--events", "jdk.JVMInformation", recordingFileName);
        output.shouldHaveExitValue(0);
    }

    @StackTrace(true)
    static class StackTraceEvent extends Event {
        public StackTraceEvent() {
        }
    }

    static void method(int depth) {
        if (depth < 512) {
            int methodIndex = random.nextInt(10);
            try {
                Method m = TestHugeStackTracePool.class.getMethod("method" + methodIndex, int.class);
                m.invoke(null, depth + 1);
            } catch (Exception e) {
            }
        }
        else {
            StackTraceEvent event = new StackTraceEvent();
            event.commit();
        }
    }

    public static void method0(int depth) {
        method(depth + 1);
    }

    public static void method1(int depth) {
        method(depth + 1);
    }

    public static void method2(int depth) {
        method(depth + 1);
    }

    public static void method3(int depth) {
        method(depth + 1);
    }

    public static void method4(int depth) {
        method(depth + 1);
    }

    public static void method5(int depth) {
        method(depth + 1);
    }

    public static void method6(int depth) {
        method(depth + 1);
    }

    public static void method7(int depth) {
        method(depth + 1);
    }

    public static void method8(int depth) {
        method(depth + 1);
    }

    public static void method9(int depth) {
        method(depth + 1);
    }
}
