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 */
013package org.jikesrvm.mm.mmtk.gcspy;
014
015import static org.jikesrvm.objectmodel.JavaHeaderConstants.JAVA_HEADER_BYTES;
016import static org.jikesrvm.objectmodel.JavaHeaderConstants.OTHER_HEADER_BYTES;
017import static org.jikesrvm.runtime.SysCall.sysCall;
018
019import org.mmtk.utility.Log;
020import org.mmtk.utility.gcspy.GCspy;
021import org.mmtk.vm.VM;
022import org.vmmagic.pragma.Interruptible;
023import org.vmmagic.pragma.Uninterruptible;
024import org.vmmagic.unboxed.Address;
025
026/**
027 * Generic GCspy Server Interpreter.
028 * <p>
029 * This class implements the GCspy server.
030 * The server runs as a separate pthread and communicates with GCspy
031 * clients. It handles commands from the client and passes data to it.
032 * Mostly it forwards calls to the C gcspy library.
033 */
034@Uninterruptible public class ServerInterpreter extends org.mmtk.vm.gcspy.ServerInterpreter {
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) {
091        Log.write("Starting GCSpy server, wait=");
092        Log.writeln(wait);
093      }
094
095      Address serverOuterLoop = sysCall.gcspyMainServerOuterLoop();
096      sysCall.gcspyStartserver(server, wait ? 1 : 0, serverOuterLoop);
097    }
098  }
099
100  @Override
101  public boolean isConnected(int event) {
102    if (org.jikesrvm.VM.BuildWithGCSpy) {
103      if (DEBUG)
104        Log.writeln("ServerInterpreter.isConnected, server=", server);
105
106      if (!initialised)
107        return false;
108      int res = sysCall.gcspyMainServerIsConnected(server, event);
109      return (res != 0);
110    } else {
111      return false;
112    }
113  }
114
115  @Override
116  public void startCompensationTimer() {
117    if (org.jikesrvm.VM.BuildWithGCSpy) {
118      if (VM.VERIFY_ASSERTIONS)
119        VM.assertions._assert(initialised,
120                       "ServerInterpreter.startCompensationTimer: server not initiialised");
121
122      sysCall.gcspyMainServerStartCompensationTimer(server);
123    }
124  }
125
126  @Override
127  public void stopCompensationTimer() {
128    if (org.jikesrvm.VM.BuildWithGCSpy) {
129      if (VM.VERIFY_ASSERTIONS)
130        VM.assertions._assert(initialised,
131                       "ServerInterpreter.stopCompensationTimer: server not initiialised");
132
133      sysCall.gcspyMainServerStopCompensationTimer(server);
134    }
135  }
136
137  @Override
138  public void serverSafepoint(int event) {
139    if (org.jikesrvm.VM.BuildWithGCSpy) {
140      if (DEBUG)
141        Log.writeln("ServerInterpreter.serverSafepoint, server=", server);
142
143      if (!initialised)
144        return;
145      sysCall.gcspyMainServerSafepoint(server, event);
146    }
147  }
148
149  @Override
150  public int computeHeaderSize() {
151    return JAVA_HEADER_BYTES + OTHER_HEADER_BYTES;
152  }
153}