Observability of the Java Virtual Machine

Image
The JVM is one of the most observable runtimes. It provides us lots of tools for troubleshooting a JVM application in production. 1. Thread observability Threads are how the JVM actually does work. When something is wrong in production, the symptom is almost always a thread: stopped, blocked, leaking etc. Thread dumps work on any JVM with no  instrumentation, no agents, no restarts. <Example project link with /threaddump endpoint>         // (1) Deadlock — two threads grab the same pair of locks in opposite order.         new Thread(() -> grab(LOCK_A, LOCK_B), "deadlock-A-then-B").start();         new Thread(() -> grab(LOCK_B, LOCK_A), "deadlock-B-then-A").start(); http://localhost:8080/actuator/threaddump To list the JVMS, we can use the command below. PS C:\observe-jvm> jps -lv 25296 jdk.jcmd/sun.tools.jps.Jps -Dapplication.home=C:\Program Files\Microsoft\jdk-21.0.3.9-hotspot -Xms8m -Djdk.module.main=...

Notes on Bit Manipulation

Finding nth bit of an integer:

If you want to get the nth bit of a number, use this mask:

1 << n

So if you left shift 1 n times, you get the mask.

Below is the expression to find the nth bit:

number & (1 << n) 

Setting the nth bit to 1:

number | (1 << n)

Clearing the nth bit (setting it to zero):

We want to apply & operator with 0 to the nth bit. The mask we need is:

~(1 << n)

So the expression to clear the nth bit is:

number & ~(1 << n)

Flipping the nth bit:

We want to apply XOR with 1. Below is the expression:

number ^ (1 << n)


Finding if a bit in a position is set:

One way to do that is to right shift the number n times. Than the nth bit is the first bit. The following expression is the answer:

(number >> n) & 1


Finding number of 1 bits in unsigned integer

Loop through the bits and check. (Look at hammingWeight() method code below)

Finding Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. (Look at hammingDistance() method below)

https://www.youtube.com/watch?v=7jkIUgLC29I

TODO:

  • Modify Bit
  • Write a func that counts the number of bits different between two numbers

Summary:

public class Main {

    static int getNthBit(int value, int n) {


        int mask = 1 << n;


        return (value & mask) == 0 ? 0 : 1;


    }


    static int setNthBit(int value, int n) {


        int mask = 1 << n;


        return (value | mask);

    }

    static int clearNthBit(int value, int n) {


        int mask = ~(1 << n);


        return (value & mask);

    }

    static int flipNthBit(int value, int n) {


        int mask = (1 << n);


        return (value ^ mask);


    }

    static boolean isBitSet(int value, int n) {


        int mask = 1 << n;


        return (value & mask) != 0;

    }

    static boolean isBitSet2(int value, int n) {


        int shifted = value >> n;


        return (shifted & 1) != 0;


    }

    public static int hammingWeight(int n) {


        int count = 0;


        for (int i = 0; i < 32; ++i) {


            if ((n & (1 << i)) != 0) {

                ++count;
            }

        }


        return count;


    }

    public static int hammingDistance(int x, int y) {


        int d = 0;


        for (int i = 0; i < 32; ++i) {


            if ((x & (1 << i)) != ((y & (1 << i)))) {

                ++d;
            }

        }


        return d;


    }


    public static void main(String[] args) {


        int x = 5;


        System.out.println(getNthBit(x, 1));


        System.out.println(setNthBit(x, 1));


        System.out.println(clearNthBit(x, 1));

    }
}






Comments

Popular posts from this blog

The WeakReference class, monitoring memory leak and garbage collection in a Java application

Simplescalar Simulator - Part 2: sim-outorder.c

Notes on Java Performance