Friday, 10 February 2017

Java native library memory profiling using Valgrind

Our Java application  interacts lot with our native libraries for various computation purposes, for those native we wanted to conduct the memory profile using “Valgrind”.
When I first started JVM under Valgrind which never took off because of memory usage and huge code instrumentation was needed to done by Valgrind.


Finally I had to serialize all JNI interactions using java.lang.reflect.InvocationHandler during the Java application run and later on replayed JNI interactions under Valgrind environment to get the memory profile of native libraries.


Following the diagram describing our approach:



Note: It is an invasive approach (I had to change application code for recording purpose) because of the use of java.lang.reflect.InvocationHandler for recording, however Spring AOP could also be used in cases where non-invasive approach is needed.
For recording I used JBOSS COT to avoid implementing serializable interface within all our model classes, alternatively serializable interface can be implemented within all classes.

Code snipped of recorder:
public class JNIInteractionRecorder implements InvocationHandler {
  static public String OUTPUT_FILE = "jni_interactions.output";

   // Application JNI wrapper which needs to be recorded.
    private final JNIWrapper OriginalWrapper;

    /**
     * output stream parameter; used JBOSS because then we do not need to have
    * serializable interface implemented.
    */
    private JBossObjectOutputStream output;

       /**
         * Input stream parameter; used JBOSS library because then we do not need to have
         * serializable interface implemented.
       */
    Private JBossObjectInputStream input

 JNIInteractionRecorder() {
   OutputStream outFile = new FileOutputStream(dirName  +  "/"   + OUTPUT_FILE);
            OutputStream buffer = new BufferedOutputStream( outFile );
            output = new JBossObjectOutputStream( buffer );
}

@Override
    protected void finalize() throws Throwable {
               output.flush();
        output.close();
        super.finalize();
    }

@Override
    public synchronized Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
               try {
                              Object result = method.invoke(wrapper, args);
                              output.writeUTF(method.getName());
                              output.writeObject(args);
                              output.flush();

                              if (currentDataLen > MAX_FILE_SIZE) {
                                             rollover();
                              }
                              return result;
               }
               catch (InvocationTargetException e) {
                              e.printStackTrace();
                              throw e.getCause();
               }
    }
}


/**
     * Replay the test under Valgrind environment.
     */
    public void runIt() {
        try {
            while (input.available() > 0) {
                methodName = input.readUTF();
                array = (Object[])input.readObject();

                Method method = null;
                try {
                    method = getWrapperType().getMethod(methodName, convertToClassArray(array));
                }
                catch (NoSuchMethodException e) {
                                             e.printStackTrace();
                }

                if (method == null) {
                    // try with the collection class
                    try {
                        method = getWrapperType().getMethod(methodName, convertToClassArrayWithCollecion(array));
                    }
                    catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }

                if (method != null) {

                    try {
                        method.invoke(wrapper, array);
                    }
                    catch (InvocationTargetException e) {
                        System.out.println("cause:"  e.getCause().getMessage());
                        e.printStackTrace();
                    }
                }
            }
        }
        catch (EOFException e) {
               System.out.println(">>>> EOF is reached");
        }
    }




Sunday, 5 February 2017

8 bit microprocessor code refresher (assembly programming)

It has been long since I looked at the lowest level programming, probably first since 2000.
Problem / assignment
int i =  14
int j = 28
printf (“%d”, i + j)
result -> 42

4 bit mnemonic table used on 8 bit processor:
0
0
0
0
NOP
No operation
0
0
0
1
LDA
Load data from given memory address into register A
0
0
1
0
ADD
Add register A with given address and store the result in register A
0
0
1
1
SUB
Subtract A with given address and store the result in register A
0
1
0
0
STA
Store register A data into given memory
0
1
0
1
OUT
Copy register A to output (display)
0
1
1
0
JMP
Jump to PC to given address
0
1
1
1
LDI
Load value into the register A
1
0
0
0
JC
Jump is carry is set
1
1
1
1
HLT
Halt the execution

Binary code version:

Address
Instruction (mnemonic)
Data

0
LDA
4
Copy address 4 data into A
1
ADD
5
Add address 5 data with A and store in A
2
OUT

Display the A data [ replacement of ‘printf’ ]
3
HLT

Halt the execution
4
NOP
14
14 value at address 4  [replacement of ‘int i = 14’]
5
NOP
28
28 value at address 5 [replacement of  ‘int j = 28’]


Thanks for Ben Eater  for sharing this video:


Screenshot of same from video.


Total Pageviews

Popular Posts