/dev/tonne

Java VM Thread Dump

If a Java Swing application freezes without an exception, a JVM thread dump can reveal the code position that causes the freeze. Simply press CTRL-\ in the calling terminal (not in the app window) to create the dump.
Alternatively you can send the JVM process a QUIT signal: kill -QUIT process-id.

Mac Users: The shortcut is CTRL+ALT+SHIFT+7

Example Output:

^\2011-02-24 21:40:14
Full thread dump Java HotSpot(TM) 64-Bit Server VM (17.1-b03-307 mixed mode):

"DestroyJavaVM" prio=5 tid=103000800 nid=0x100501000 waiting on condition [00000000]
   java.lang.Thread.State: RUNNABLE

"Poller SunPKCS11-Darwin" daemon prio=1 tid=10358a800 nid=0x12b004000 waiting on condition [12b003000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at sun.security.pkcs11.SunPKCS11$TokenPoller.run(SunPKCS11.java:692)
	at java.lang.Thread.run(Thread.java:680)

"AWT-EventQueue-0" prio=6 tid=101a12000 nid=0x12aeb8000 runnable [12aeb6000]
   java.lang.Thread.State: RUNNABLE
	at test.MyClass.init(MyClass.java:36)
	at javax.swing.JComponent.paint(JComponent.java:1029)
	at javax.swing.JComponent.paintChildren(JComponent.java:862)
	- locked <107e24db8> (a java.awt.Component$AWTTreeLock)
	at ... (shortened)

java memory pools

wrong memory pool ratios can lead to swap-like performance issues in javavms (due to gc)
profiling can be done with jconsole, amount of new space is set with -Xmn
config for jmx profiling interface:
JAVA_OPTS="-server -Xmx400M -Xmn350M \
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 \
-Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.password.file=/etc/tomcat-5.5/jmxremote.password \
-Dcom.sun.management.jmxremote.access.file=/etc/tomcat-5.5/jmxremote.access"
password format:
user password

access format:
user readwrite/readonly

chmod password and access files to 600

reset mysql password

Stop mysqld and restart it with the --skip-grant-tables option.
normally: mysqld_safe --skip-grant-tables
login without password
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;

Apache startup fails due to 'no space left on device'

After a lot of apache restarts the daemon sometimes refuses to come up with some obscure error message like (28)No space left on device: mod_python: Failed to create global mutex or (28)No space left on device: Couldn't create accept lock.

This can be caused by large numbers of orphaned semaphores of the apache user. To check for those semaphores simply run

# ipcs -s | grep apache

To remove all semaphores you can do

# for semid in `ipcs -s | grep apache | cut -f2 -d" "`; do ipcrm -s $semid; done

Please make sure that the daemon is stopped while running this command.

mysql migrate 4.0 to 4.1+

causes strange table not found errors on multitable delete. table must be referenced by alias instead of tablename:

delete mytable from mytable m join mytable2;
must be changed to
delete m from mytable m join mytable2;

Switching Values

How to switch the values of two int variables without using a temp variable?

Pseudocode

a = 5
b = 3

a = a + b
b = a - b
a = a - b

Java, Groovy

int a = 5;
int b = 3;

b = a + b - (a = b);

Python

a = 5
b = 3

a, b = b, a

webapp reload

strange error on webapp reload, but not on tomcat start:
java.lang.NoClassDefFoundError at org.apache.log4j.Logger.getLogger(Logger.java:105)
is caused by log4js PatternLayout

crontab refresh

if cron complains about "root", "command not found":
backup /etc/crontab
run crontab -r as root to refresh

spamassassin for exim

#router

spamcheck_router:
no_verify
check_local_user
# When to scan a message :
# - it isn't already flagged as spam
# - it isn't already scanned
condition = "${if and { {!def:h_X-Spam-Flag:} {!eq {$received_protocol}{spam-scanned}}} {1}{0$
driver = accept
transport = spamcheck

#transports 
 
spamcheck:
debug_print = "T: spamassassin_pipe for $local_part@$domain"
driver = pipe
command = /usr/sbin/exim -oMr spam-scanned -bS
use_bsmtp
transport_filter = /usr/bin/spamc
home_directory = "/tmp"
current_directory = "/tmp"
user = exim
group = exim
return_fail_output
message_prefix =
message_suffix =

userforward:
driver = redirect
check_local_user
file = $home/.forward
allow_filter
no_verify
no_expn
check_ancestor
file_transport = address_file
pipe_transport = address_pipe
reply_transport = address_reply
directory_transport = address_directory
skip_syntax_errors
 
address_directory:
driver = appendfile
maildir_format
delivery_date_add
envelope_to_add
return_path_add

# .forward in user home, must be owned by user

# Exim filter

if
        $h_X-Spam-Status: CONTAINS "Yes"
then
        save /home/$USER/Maildir/.Spam/
        finish
endif

show open ports

Show list of listening programs:

# netstat --tcp --numeric --listening --programs

Show list of both listening programs and established connections:

# netstat --tcp --numeric --all --programs

The --numeric option prevents netstat from resolving ip addresses and port numbers.