001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.mm.mmtk.gcspy;
014    
015    import org.mmtk.utility.Log;
016    import org.mmtk.vm.VM;
017    import org.mmtk.utility.gcspy.GCspy;
018    
019    import static org.jikesrvm.runtime.SysCall.sysCall;
020    import org.jikesrvm.objectmodel.JavaHeaderConstants;
021    
022    import org.vmmagic.unboxed.*;
023    import org.vmmagic.pragma.*;
024    
025    /**
026     * Generic GCspy Server Interpreter.
027     * <p>
028     * This class implements the GCspy server.
029     * The server runs as a separate pthread and communicates with GCspy
030     * clients. It handles commands from the client and passes data to it.
031     * Mostly it forwards calls to the C gcspy library.
032     */
033    @Uninterruptible public class ServerInterpreter extends org.mmtk.vm.gcspy.ServerInterpreter
034      implements JavaHeaderConstants {
035    
036    
037      @Override
038      @Interruptible
039      public void init(String name, int port, boolean verbose) {
040        if (org.jikesrvm.VM.BuildWithGCSpy) {
041          if (VM.VERIFY_ASSERTIONS)
042            VM.assertions._assert(!initialised, "Tried to re-init server interpreter");
043          initialised = true;
044    
045          if (DEBUG)
046            Log.writeln("-- Initialising main server on port ",port);
047    
048          Address tmp = GCspy.util.getBytes(name);
049          server = sysCall.gcspyMainServerInit(port, MAX_LEN, tmp, verbose?1:0);
050    
051          if (DEBUG) {
052            Log.writeln("gcspy_main_server_t address = "); Log.writeln(server);
053          }
054    
055          GCspy.util.free(tmp);
056          // Set up the list of ServerSpaces
057          spaces = new org.jikesrvm.mm.mmtk.gcspy.ServerSpace[MAX_SPACES];
058        }
059      }
060    
061      @Override
062      public void addEvent(int num, String name) {
063        if (org.jikesrvm.VM.BuildWithGCSpy) {
064          if (VM.VERIFY_ASSERTIONS)
065            VM.assertions._assert(initialised,
066                           "ServerInterpreter.addEvent: server not initiialised");
067    
068          Address tmp = GCspy.util.getBytes(name);
069          sysCall.gcspyMainServerAddEvent(server, num, tmp);
070          GCspy.util.free(tmp);
071        }
072      }
073    
074      @Override
075      public void setGeneralInfo(String info) {
076        if (org.jikesrvm.VM.BuildWithGCSpy) {
077          if (VM.VERIFY_ASSERTIONS)
078            VM.assertions._assert(initialised,
079                           "ServerInterpreter.setGeneralInfo: server not initiialised");
080    
081          Address tmp = GCspy.util.getBytes(info);
082          sysCall.gcspyMainServerSetGeneralInfo(server, tmp);
083          GCspy.util.free(tmp);
084        }
085      }
086    
087      @Override
088      public void startServer(boolean wait) {
089        if (org.jikesrvm.VM.BuildWithGCSpy) {
090          if (DEBUG) { Log.write("Starting GCSpy server, wait="); Log.writeln(wait); }
091    
092          Address serverOuterLoop = sysCall.gcspyMainServerOuterLoop();
093          sysCall.gcspyStartserver(server, wait?1:0, serverOuterLoop);
094        }
095      }
096    
097      @Override
098      public boolean isConnected(int event) {
099        if (org.jikesrvm.VM.BuildWithGCSpy) {
100          if (DEBUG)
101            Log.writeln("ServerInterpreter.isConnected, server=", server);
102    
103          if (!initialised)
104            return false;
105          int res = sysCall.gcspyMainServerIsConnected(server, event);
106          return (res != 0);
107        } else {
108          return false;
109        }
110      }
111    
112      @Override
113      public void startCompensationTimer() {
114        if (org.jikesrvm.VM.BuildWithGCSpy) {
115          if (VM.VERIFY_ASSERTIONS)
116            VM.assertions._assert(initialised,
117                           "ServerInterpreter.startCompensationTimer: server not initiialised");
118    
119          sysCall.gcspyMainServerStartCompensationTimer(server);
120        }
121      }
122    
123      @Override
124      public void stopCompensationTimer() {
125        if (org.jikesrvm.VM.BuildWithGCSpy) {
126          if (VM.VERIFY_ASSERTIONS)
127            VM.assertions._assert(initialised,
128                           "ServerInterpreter.stopCompensationTimer: server not initiialised");
129    
130          sysCall.gcspyMainServerStopCompensationTimer(server);
131        }
132      }
133    
134      @Override
135      public void serverSafepoint(int event) {
136        if (org.jikesrvm.VM.BuildWithGCSpy) {
137          if (DEBUG)
138            Log.writeln("ServerInterpreter.serverSafepoint, server=", server);
139    
140          if (!initialised)
141            return;
142          sysCall.gcspyMainServerSafepoint(server, event);
143        }
144      }
145    
146      @Override
147      public int computeHeaderSize() {
148        return JAVA_HEADER_BYTES+OTHER_HEADER_BYTES;
149      }
150    }