Skip to content

Instantly share code, notes, and snippets.

View apangin's full-sized avatar

Andrei Pangin apangin

View GitHub Profile
@apangin
apangin / patcher.cpp
Created July 2, 2019 16:03
Example for the presentation about JVM TI
#include <jvmti.h>
#include <stdio.h>
static jvmtiIterationControl JNICALL
heap_callback(jlong class_tag, jlong size, jlong* tag_ptr, void* user_data) {
*tag_ptr = 1;
return JVMTI_ITERATION_CONTINUE;
}
JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
@apangin
apangin / HeapIterator.java
Created February 6, 2016 14:02
Dump all Java strings using JVMTI
public class HeapIterator {
static {
System.loadLibrary("heapIterator");
}
private static native void printStrings();
public static void main(String[] args) throws Exception {
printStrings();
@apangin
apangin / JDK 9 intrinsics
Last active May 11, 2023 18:32
JDK 9 intrinsics
_hashCode java/lang/Object.hashCode()I
_getClass java/lang/Object.getClass()Ljava/lang/Class;
_clone java/lang/Object.clone()Ljava/lang/Object;
_notify java/lang/Object.notify()V
_notifyAll java/lang/Object.notifyAll()V
_dabs java/lang/Math.abs(D)D
_dsin java/lang/Math.sin(D)D
_dcos java/lang/Math.cos(D)D
_dtan java/lang/Math.tan(D)D
_datan2 java/lang/Math.atan2(DD)D
@apangin
apangin / HotSpot JVM intrinsics
Last active May 11, 2023 18:32
HotSpot JVM intrinsics
_hashCode java/lang/Object.hashCode()I
_getClass java/lang/Object.getClass()Ljava/lang/Class;
_clone java/lang/Object.clone()Ljava/lang/Object;
_dabs java/lang/Math.abs(D)D
_dsin java/lang/Math.sin(D)D
_dcos java/lang/Math.cos(D)D
_dtan java/lang/Math.tan(D)D
_datan2 java/lang/Math.atan2(DD)D
_dsqrt java/lang/Math.sqrt(D)D
_dlog java/lang/Math.log(D)D
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// See https://twitter.com/tagir_valeev/status/1402089474805354499
public class StringBuilderHash {
import java.util.Random;
public abstract class PowerAlgorithm {
public abstract long power(int x, int y);
static class NaivePower extends PowerAlgorithm {
@Override
public long power(int x, int y) {
long result = 1;
@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
@apangin
apangin / rafpatch.c
Last active March 14, 2023 23:46
Patch to enable O_SYNC on RandomAccessFile
/*
* Compile: gcc -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -shared -fPIC -O3 -olibrafpatch.so -Wl,-soname,librafpatch.so rafpatch.c $JAVA_HOME/lib/amd64/libjava.so
* Run: java -agentpath:/path/to/librafpatch.so MainClass
*/
#include <jvmti.h>
#include <jni.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
@apangin
apangin / proccount.c
Last active March 14, 2023 23:46
Fool JVM about the number of available processors according to /sys/devices/system/cpu/online
// Compile: gcc -O2 -fPIC -shared -o libproccount.so proccount.c
// Run: LD_PRELOAD=/path/to/libproccount.so java args
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
static int online_cpus;
__attribute__((constructor))
@apangin
apangin / Bytecodes.java
Created January 31, 2016 19:16
Using JVMTI to obtain bytecodes of a Java method
import java.lang.reflect.Method;
import java.util.Arrays;
public class Bytecodes {
static {
System.loadLibrary("bytecodes");
}
private static native byte[] getBytecodes(Method method);