Skip to content

Instantly share code, notes, and snippets.

View serkan-ozal's full-sized avatar

Serkan ÖZAL serkan-ozal

View GitHub Profile
@headius
headius / meltdown_in_a_nutshell.md
Last active July 27, 2018 13:43
How Meltdown Works

Algorithm

  1. A secret byte you want to read is stored at inaccessible memory location priv_mem.
  2. The sender triggers an access exception by attempting to read priv_mem.
  3. Due to CPU optimization (out-of-order execution), the load of secret from priv_mem and the use of its value in (4) and (5) below may execute before the exception is triggered.
  4. Calculate an offset into a known array probe by multiplying secret by the width of a cache line (or whatever block size the CPU typically fetches, like a 4096-byte page). This guarantees each of those 256 possible offsets will cache separately.
  5. Load probe[offset], which causes the CPU to cache exactly one chunk of of our array, populating one cache line.
  6. The exception finally triggers, clearing the modified registers...but cached data is not excised.
  7. Iterate over all 256 offsets into probe to find out which one loads fast. You've determined the value of secret.

Quick Tips for Fast Code on the JVM

I was talking to a coworker recently about general techniques that almost always form the core of any effort to write very fast, down-to-the-metal hot path code on the JVM, and they pointed out that there really isn't a particularly good place to go for this information. It occurred to me that, really, I had more or less picked up all of it by word of mouth and experience, and there just aren't any good reference sources on the topic. So… here's my word of mouth.

This is by no means a comprehensive gist. It's also important to understand that the techniques that I outline in here are not 100% absolute either. Performance on the JVM is an incredibly complicated subject, and while there are rules that almost always hold true, the "almost" remains very salient. Also, for many or even most applications, there will be other techniques that I'm not mentioning which will have a greater impact. JMH, Java Flight Recorder, and a good profiler are your very best friend! Mea

@apangin
apangin / HighResolutionTimer.java
Created October 3, 2016 23:04
Java HighResolutionTimer hack for Windows
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.spi.MidiDeviceProvider;
public class HighResolutionTimer {
private static final MidiDevice midiDevice = getMidiOutDevice();
private static MidiDevice getMidiOutDevice() {
MidiDeviceProvider provider = new com.sun.media.sound.MidiOutDeviceProvider();
MidiDevice.Info[] info = provider.getDeviceInfo();
@ursuad
ursuad / kafka-cheat-sheet.md
Last active June 17, 2025 10:00
Quick command reference for Apache Kafka

Kafka Topics

List existing topics

bin/kafka-topics.sh --zookeeper localhost:2181 --list

Describe a topic

bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic mytopic

Purge a topic

bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic mytopic --config retention.ms=1000

... wait a minute ...

How to setup AWS lambda function to talk to the internet and VPC

I'm going to walk you through the steps for setting up a AWS Lambda to talk to the internet and a VPC. Let's dive in.

So it might be really unintuitive at first but lambda functions have three states.

  1. No VPC, where it can talk openly to the web, but can't talk to any of your AWS services.
  2. VPC, the default setting where the lambda function can talk to your AWS services but can't talk to the web.
  3. VPC with NAT, The best of both worlds, AWS services and web.
@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);
@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>
DELETE test
PUT test
{
"mappings": {
"test": {
"properties": {
"start": {
"type": "date"
},
@apangin
apangin / HotSpot JVM intrinsics
Last active February 26, 2025 11:43
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
@shihpeng
shihpeng / Main.java
Last active November 8, 2022 18:23
LogStash-Forwarder (Lumberjack) client Java implementation
import javax.net.ssl.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.Deflater;
/*
* This is a simple implementation of the Lumberjack client.