Nitin Verma’s Blog

Archive for December 2008

I am thinking of starting with AspectJ.

A presentation by Gregor Kiczales

http://video.google.com/videoplay?docid=8566923311315412414

Some links :

  1. http://www.eclipse.org/aspectj/downloads.php
  2. http://www.eclipse.org/aspectj/doc/released/progguide/index.html
  3. http://www.eclipse.org/aspectj/doc/next/index.html
  4. http://yakafokon.wordpress.com/2008/12/02/beans-binding-jsr-295-with-pojo-and-aop/
  5. http://wordpress.com/tag/aspectj/
  6. http://www.ibm.com/developerworks/library/j-aopwork1/
  7. http://www.java2s.com/Code/Java/Spring/AspectJExamplefromProSpring.htm

I would post something soon if I really do something with it 🙂

Advertisements
// Refer to my previous post
// https://vermanitin.wordpress.com/2008/12/25/helloworld-flex/
$ ps -eafwww | grep hw | grep -v grep
nitinv   11703  6642  0 16:42 pts/7    00:00:00 ./hw

$ gcore -o hw.core 11703
(no debugging symbols found)
Using host libthread_db library "/lib/i686/nosegneg/libthread_db.so.1".
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
0x00fac402 in __kernel_vsyscall ()
Saved corefile hw.core.11703

$ gdb ./hw hw.core.11703
GNU gdb Red Hat Linux (6.5-37.el5_2.2rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...(no debugging symbols found)
Using host libthread_db library "/lib/i686/nosegneg/libthread_db.so.1".


warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/i686/nosegneg/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/i686/nosegneg/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
(no debugging symbols found)
Core was generated by `/home/nitinv/codes/grammar/hw'.
#0  0x00fac402 in __kernel_vsyscall ()
(gdb) bt
#0  0x00fac402 in __kernel_vsyscall ()
#1  0x00b1d1d3 in __read_nocancel () from /lib/i686/nosegneg/libc.so.6
#2  0x00abd588 in _IO_file_read_internal () from /lib/i686/nosegneg/libc.so.6
#3  0x00abe930 in _IO_new_file_underflow () from /lib/i686/nosegneg/libc.so.6
#4  0x00abf02b in _IO_default_uflow_internal () from /lib/i686/nosegneg/libc.so.6
#5  0x00ac039d in __uflow () from /lib/i686/nosegneg/libc.so.6
#6  0x00aba10c in getc () from /lib/i686/nosegneg/libc.so.6
#7  0x08048fad in yy_get_next_buffer ()
#8  0x08048c84 in yylex ()
#9  0x08048655 in main ()

This even works with ulimit not set 😉

$ ulimit -a | grep core
core file size          (blocks, -c) 0

Neat way to stop and restart your process to take a dump. This can get handy sometime while getting a good snapshot.
Michael B. Trausch gcore: Obtain core dump of current running application — see comment

flex – fast lexical analyzer generator

// This is the flex version I have.
$ flex -V
flex version 2.5.4

// A Hello World lexical scanner.
$ cat -n hw.lex
     1  %{
     2  // Hello World!!!
     3  %}
     4  %%
     5
     6  [\t ]+  ;
     7
     8  a { printf( "' %s '", yytext); }
     9  hw { printf( "' -%s- '", yytext); }
    10  ^hw$ { printf( "' %s '", yytext); }
    11  ^exit$ { printf( "%s :have a nice day!!!\n", yytext); exit(0); }
    12
    13  %%
    14
    15  int yywrap()
    16  {
    17          return 1;
    18  }

// Generate your C code, with lex debug messages on. ( remove -d if you do not need those ).
$ flex -d -ohw.yy.c  hw.lex

$ more hw.yy.c
#line 2 "hw.yy.c"
/* A lexical scanner generated by flex*/

/* Scanner skeleton version:
 * $Header: /home/daffy/u0/vern/flex/RCS/flex.skl,v 2.91 96/09/10 16:58:48 vern Exp $
 */

#define FLEX_SCANNER
#define YY_FLEX_MAJOR_VERSION 2
#define YY_FLEX_MINOR_VERSION 5

#include <stdio .h>
#include <unistd .h>


/* cfront 1.2 defines "c_plusplus" instead of "__cplusplus" */
#ifdef c_plusplus
#ifndef __cplusplus
#define __cplusplus
#endif
#endif


#ifdef __cplusplus

#include <stdlib .h>

/* Use prototypes in function declarations. */
#define YY_USE_PROTOS

/* The "const" storage-class-modifier is valid. */
#define YY_USE_CONST

#else   /* ! __cplusplus */

#if __STDC__

#define YY_USE_PROTOS
#define YY_USE_CONST

#endif  /* __STDC__ */
#endif  /* ! __cplusplus */

#ifdef __TURBOC__
 #pragma warn -rch
 #pragma warn -use
#include <io .h>
#include <stdlib .h>
#define YY_USE_CONST
#define YY_USE_PROTOS
#endif
...

// Compile 

$ gcc -o hw -ll hw.yy.c

// Run your lexical scanner binary!!!

$ ./hw
--(end of buffer or a NUL)
hw
--accepting rule at line 10 ("hw
")
--accepting default rule ("
")
' hw '
--(end of buffer or a NUL)
hwhwa
--accepting rule at line 9 ("hw")
--accepting rule at line 9 ("hw")
--accepting rule at line 8 ("a")
--accepting default rule ("
")
' -hw- '' -hw- '' a '
--(end of buffer or a NUL)
hw hw a
--accepting rule at line 9 ("hw")
--accepting rule at line 6 (" ")
--accepting rule at line 9 ("hw")
--accepting rule at line 6 (" ")
--accepting rule at line 8 ("a")
--accepting default rule ("
")
' -hw- '' -hw- '' a '
--(end of buffer or a NUL)
exit
--accepting rule at line 11 ("exit
")
exit :have a nice day!!!


Using jdk 1.6 the cost of blocking synchronization is nearly same as
non-blocking counter part. This does not say 1.6 makes it liveness
hazard free or deadlock free.

But makes it quite fast.

// We have three methods
// -1: Thread unsafe
    public static void addUnsafe() {
        b++;
    }
// 0: Thread safe, non-blocking synchronization
    public static void addNBSync() {
        try {
            lock.lock();
            b++;
        } finally {
            lock.unlock();
        }
    }
// 1: Thread safe, blocking synchronization
    public static synchronized void addSync() {
        b++;
    }

// Log format
//Use:<method>:<end value of b>:<time taken>
$ ~/usr/local/jdk1.5.0_17/bin/java -classpath classes1.5  edu.nitin.Main
Use:-1:63929500:156367000
Use:0:100000000:5360473000
Use:1:100000000:24290461000

$ echo "scale=10; ( 5360473000 - 156367000 ) / 156367000 " | bc
33.2813573196
$ echo "scale=10; ( 24290461000 - 156367000 ) / 156367000 " | bc
154.3426298387

$ ~/usr/local/jdk1.6.0_11/bin/java -classpath classes1.6  edu.nitin.Main
Use:-1:59050465:190538454
Use:0:100000000:5350310972
Use:1:100000000:6404364288

$ echo "scale=10; ( 5350310972 - 190538454 ) / 156367000 " | bc
32.9978353361
$ echo "scale=10; ( 6404364288 - 190538454 ) / 156367000 " | bc
39.7387289773

A test running 1000 threads doing 100,000 atomic operations each,

all the three ways.

...

package edu.nitin;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @author Nitin Verma
 */
public class Main {

    private final static Lock lock = new ReentrantLock();

    private static long b = 0;
    private final static long n = 100000;
    private final static int a = 1000;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException {
        test(-1);
        test(0);
        test(1);
    }

    private static void test(final int use) throws InterruptedException {
        b = 0;
        final long start = System.nanoTime();
        long end = 0;
        try {
            final Thread[] ta = new Thread[a];
            for (int i = 0; i < a; i++) {                 ta[i] = new Thread(new MyRunnable(use));                 ta[i].start();             }             for (int i = 0; i < a; i++) {                 if (ta[i] != null && ta[i].isAlive()) {                     ta[i].join();                 }             }         } finally {             end = System.nanoTime();         }         System.out.println("Use:" + use + ":" + b + ":" + (end - start));     }     public static void addN(final long n, final int use) {         if (use == 0) {             for (long i = 0; i < n; i++) {                 addNBSync();             }         }         else if (use == 1)  {             for (long i = 0; i < n; i++) {                 addSync();             }         }         else {             for (long i = 0; i < n; i++) {                 addUnsafe();             }         }     }     public static void addNBSync() {         try {             lock.lock();             b++;         } finally {             lock.unlock();         }     }     public static synchronized void addSync() {         b++;     }     public static void addUnsafe() {         b++;     }     private static class MyRunnable implements Runnable {         private int use;         public MyRunnable(final int use) {             this.use = use;         }         public void run() {             addN(n, use);         }     } } [/sourcecode]

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!