Skip to content

Instantly share code, notes, and snippets.

View apangin's full-sized avatar

Andrei Pangin apangin

View GitHub Profile
import java.util.ArrayList;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.StreamSupport;
public class ParallelStream {
public static int[] process(Iterable<Integer> iterable) {
return StreamSupport.stream(iterable.spliterator(), true)
.mapToInt(i -> {
LockSupport.parkNanos(1_000_000);
@apangin
apangin / VMFlags.java
Created January 27, 2021 23:23
Read and modify non-manageable JVM flags in runtime: macOS version
import sun.misc.Unsafe;
import javax.management.ObjectName;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
@apangin
apangin / LogSafepoints.java
Last active January 15, 2021 19:34
Java safepoint event listener
import one.log.event.ILoggingEvent;
import one.log.event.OperationLogEvent;
import one.log.stat.StatCollector;
import one.log.stat.Statistics;
import one.nio.mgt.DiagnosticCommand;
import one.nio.os.BatchThread;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Matcher;
@apangin
apangin / vmoption.c
Created January 15, 2021 11:23
Change non-manageable JVM option in runtime
#include <jvmti.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
JNIEXPORT jint JNICALL
Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
if (options != NULL) {
int* addr = (int*)strtol(options, NULL, 0);
printf("vmoption [%p] = %d\n", addr, *addr); fflush(stdout);
@apangin
apangin / unmalloc.c
Created January 11, 2021 17:45
Avoid long TTSP pauses caused by Unsafe.allocateMemory/freeMemory
// Replaces implementation of Unsafe.allocateMemory/freeMemory
// to avoid long time-to-safepoint pauses.
//
// Compile: gcc -O3 -fno-omit-frame-pointer -fPIC -shared -olibunmalloc.so unmalloc.c
//
// Usage: java -agentpath:/path/to/libunmalloc.so ...
#include <jvmti.h>
#include <stdint.h>
#include <stdio.h>
@apangin
apangin / ChangeVMFlag.java
Created December 11, 2020 01:26
Change MaxJavaStackTraceDepth JVM flag in runtime
import sun.jvm.hotspot.debugger.Address;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Changes -XX:MaxJavaStackTraceDepth flag to 1000000 in runtime
* without restarting the JVM.
@apangin
apangin / proccount.c
Created October 8, 2020 21:41
Sets the number of CPUs available for a JVM in a container (JDK < 8u191)
/*
* Copyright 2020 Andrei Pangin
*
* 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
package string;
import org.openjdk.jmh.annotations.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@State(Scope.Thread)
@apangin
apangin / NpsConverter.java
Created June 6, 2020 18:08
Converts .collapsed output produced by async-profiler to VisualVM format
/*
* Copyright 2020 Andrei Pangin
*
* 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
/*
* Copyright 2018 Andrei Pangin
*
* This is a specialized Java port of the FlameGraph script available at
* https://github.com/brendangregg/FlameGraph/blob/master/flamegraph.pl
*
* Copyright 2016 Netflix, Inc.
* Copyright 2011 Joyent, Inc. All rights reserved.
* Copyright 2011 Brendan Gregg. All rights reserved.
*