2008-03-16 10:03:52 +01:00
// Threaddump_p.java
// -----------------------
// part of YACY
2008-07-20 19:14:51 +02:00
// (C) by Michael Peter Christen; mc@yacy.net
2008-03-16 10:03:52 +01:00
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
//
// This File is contributed by Alexander Fieger
//
2009-09-29 23:28:49 +02:00
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
2008-03-16 10:03:52 +01:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-06-06 18:01:27 +02:00
import java.io.File ;
2011-03-14 21:46:04 +01:00
import java.io.IOException ;
2008-06-06 18:01:27 +02:00
import java.util.Date ;
import java.util.Map ;
2008-12-03 16:38:29 +01:00
import java.util.ArrayList ;
2008-06-06 18:01:27 +02:00
2010-08-23 14:32:02 +02:00
import net.yacy.cora.protocol.RequestHeader ;
2011-03-07 16:29:45 +01:00
import net.yacy.kelondro.logging.ThreadDump ;
2011-03-15 09:39:05 +01:00
import net.yacy.kelondro.util.OS ;
2011-09-25 18:59:06 +02:00
import net.yacy.peers.operation.yacyBuildProperties ;
import net.yacy.search.Switchboard ;
2009-10-10 03:14:19 +02:00
2008-03-16 10:03:52 +01:00
import de.anomic.server.serverObjects ;
import de.anomic.server.serverSwitch ;
public class Threaddump_p {
2009-07-19 22:37:44 +02:00
public static serverObjects respond ( final RequestHeader header , final serverObjects post , final serverSwitch env ) {
2009-02-10 21:57:04 +01:00
serverObjects prop = new serverObjects ( ) ;
2009-07-19 22:37:44 +02:00
Switchboard sb = ( Switchboard ) env ;
2009-02-10 21:57:04 +01:00
2008-12-04 13:54:16 +01:00
final StringBuilder buffer = new StringBuilder ( 1000 ) ;
2008-03-16 10:03:52 +01:00
2011-03-15 02:03:35 +01:00
final boolean plain = post ! = null & & post . getBoolean ( " plain " , false ) ;
2009-05-12 00:26:21 +02:00
final int sleep = ( post = = null ) ? 0 : post . getInt ( " sleep " , 0 ) ; // a sleep before creation of a thread dump can be used for profiling
2008-12-04 00:26:25 +01:00
if ( sleep > 0 ) try { Thread . sleep ( sleep ) ; } catch ( final InterruptedException e ) { }
prop . put ( " dump " , " 1 " ) ;
// Thread dump
final Date dt = new Date ( ) ;
2009-07-11 19:03:22 +02:00
final String versionstring = yacyBuildProperties . getVersion ( ) + " / " + yacyBuildProperties . getSVNRevision ( ) ;
2009-09-18 11:19:52 +02:00
Runtime runtime = Runtime . getRuntime ( ) ;
2008-12-04 00:26:25 +01:00
2011-03-07 16:29:45 +01:00
ThreadDump . bufferappend ( buffer , plain , " ************* Start Thread Dump " + dt + " ******************* " ) ;
ThreadDump . bufferappend ( buffer , plain , " " ) ;
ThreadDump . bufferappend ( buffer , plain , " YaCy Version: " + versionstring ) ;
ThreadDump . bufferappend ( buffer , plain , " Assigned Memory = " + ( runtime . maxMemory ( ) ) ) ;
ThreadDump . bufferappend ( buffer , plain , " Used Memory = " + ( runtime . totalMemory ( ) - runtime . freeMemory ( ) ) ) ;
ThreadDump . bufferappend ( buffer , plain , " Available Memory = " + ( runtime . maxMemory ( ) - runtime . totalMemory ( ) + runtime . freeMemory ( ) ) ) ;
ThreadDump . bufferappend ( buffer , plain , " " ) ;
ThreadDump . bufferappend ( buffer , plain , " " ) ;
2008-12-04 00:26:25 +01:00
2009-09-09 22:49:16 +02:00
int multipleCount = 100 ;
2010-09-02 21:24:22 +02:00
File appPath = sb . getAppPath ( ) ;
2008-12-04 00:26:25 +01:00
if ( post ! = null & & post . containsKey ( " multipleThreaddump " ) ) {
2009-02-10 21:57:04 +01:00
multipleCount = post . getInt ( " count " , multipleCount ) ;
2008-12-04 00:26:25 +01:00
final ArrayList < Map < Thread , StackTraceElement [ ] > > traces = new ArrayList < Map < Thread , StackTraceElement [ ] > > ( ) ;
2009-02-10 21:57:04 +01:00
for ( int i = 0 ; i < multipleCount ; i + + ) {
2011-03-14 21:46:04 +01:00
try {
traces . add ( ThreadDump . getAllStackTraces ( ) ) ;
} catch ( OutOfMemoryError e ) {
break ;
}
2008-12-04 00:26:25 +01:00
}
2011-03-17 21:48:39 +01:00
ThreadDump . appendStackTraceStats ( appPath , buffer , traces , plain ) ;
2008-12-04 00:26:25 +01:00
} else {
2011-03-14 21:46:04 +01:00
// write a thread dump to standard error output
2011-03-15 09:39:05 +01:00
File logFile = new File ( " yacy.log " ) ;
if ( ThreadDump . canProduceLockedBy ( logFile ) ) {
try {
new ThreadDump ( logFile ) . appendBlockTraces ( buffer , plain ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
} else if ( OS . canExecUnix ) {
ThreadDump . bufferappend ( buffer , plain , " this thread dump function can find threads that lock others, to enable this function start YaCy with 'startYACY.sh -l' " ) ;
ThreadDump . bufferappend ( buffer , plain , " " ) ;
2011-03-15 02:03:35 +01:00
}
2011-03-14 21:46:04 +01:00
2008-12-04 00:26:25 +01:00
// generate a single thread dump
2011-03-14 21:46:04 +01:00
final Map < Thread , StackTraceElement [ ] > stackTraces = ThreadDump . getAllStackTraces ( ) ;
new ThreadDump ( appPath , stackTraces , plain , Thread . State . BLOCKED ) . appendStackTraces ( buffer , plain , Thread . State . BLOCKED ) ;
new ThreadDump ( appPath , stackTraces , plain , Thread . State . RUNNABLE ) . appendStackTraces ( buffer , plain , Thread . State . RUNNABLE ) ;
new ThreadDump ( appPath , stackTraces , plain , Thread . State . TIMED_WAITING ) . appendStackTraces ( buffer , plain , Thread . State . TIMED_WAITING ) ;
new ThreadDump ( appPath , stackTraces , plain , Thread . State . WAITING ) . appendStackTraces ( buffer , plain , Thread . State . WAITING ) ;
new ThreadDump ( appPath , stackTraces , plain , Thread . State . NEW ) . appendStackTraces ( buffer , plain , Thread . State . NEW ) ;
new ThreadDump ( appPath , stackTraces , plain , Thread . State . TERMINATED ) . appendStackTraces ( buffer , plain , Thread . State . TERMINATED ) ;
2008-12-04 00:26:25 +01:00
}
2008-03-16 10:03:52 +01:00
2011-03-07 16:29:45 +01:00
ThreadDump . bufferappend ( buffer , plain , " ************* End Thread Dump " + dt + " ******************* " ) ;
2008-12-04 00:26:25 +01:00
2009-02-10 21:57:04 +01:00
prop . put ( " plain_count " , multipleCount ) ;
2008-12-05 14:55:48 +01:00
prop . put ( " plain_content " , buffer . toString ( ) ) ;
2008-12-04 00:26:25 +01:00
prop . put ( " plain " , ( plain ) ? 1 : 0 ) ;
2008-03-16 10:03:52 +01:00
return prop ; // return from serverObjects respond()
}
2008-03-20 15:21:58 +01:00
2008-04-24 15:25:38 +02:00
2008-03-16 10:03:52 +01:00
}