Skip to content

Commit

Permalink
Merge pull request #42 from PoojaDurgad/ver_bose
Browse files Browse the repository at this point in the history
 add verbose option to the tool
  • Loading branch information
mpirvu authored Mar 8, 2021
2 parents 20eefb6 + 13f2697 commit 51ad4a4
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 37 deletions.
4 changes: 4 additions & 0 deletions perf-tool/include/infra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ extern int portNo;
extern std::string commandsPath;
extern std::string logPath;

enum Verbose { NONE, ERROR, WARN, INFO};

extern int verbose;

class EventConfig
{
public:
Expand Down
57 changes: 43 additions & 14 deletions perf-tool/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ JNIEnv *jni_env = NULL;
int portNo = ServerConstants::DEFAULT_PORT;
std::string commandsPath = "";
std::string logPath = "logs.json";

int verbose = NONE;

void parseAgentOptions(const char *options)
{
Expand All @@ -58,10 +58,12 @@ void parseAgentOptions(const char *options)
std::string oIn(options);
int pos1, pos2 = 0;

/* there is a max of three options the user can supply here
/* there is a max of four options the user can supply here
* "commands" is followed by a path to the commands file
* "log" is followed by the path to the location for the log file
* "portNo" is followed by a number indicating the port to run the server on
* "portNo" is followed by a number indicating the port to run the server on
* "verbose" is used to indicate the degree of verbosity to show the
* additional information
*/
while ((pos1 = oIn.find(optionsDelim)) != std::string::npos)
{
Expand All @@ -83,7 +85,13 @@ void parseAgentOptions(const char *options)
token.erase(0, pos2 + pathDelim.length());
portNo = stoi(token);
}
else if (!token.substr(0, pos2).compare("verbose"))
{
token.erase(0, pos2 + pathDelim.length());
verbose = static_cast<Verbose>(stoi(token));
}
}

oIn.erase(0, pos1 + optionsDelim.length());
}
if (!oIn.empty())
Expand All @@ -106,12 +114,27 @@ void parseAgentOptions(const char *options)
token.erase(0, pos2 + pathDelim.length());
portNo = stoi(token);
}
else if (!token.substr(0, pos2).compare("verbose"))
{
token.erase(0, pos2 + pathDelim.length());
verbose = static_cast<Verbose>(stoi(token));

}
}
}

printf("%s\n", commandsPath.c_str());
printf("%s\n", logPath.c_str());
printf("%i\n", portNo);
if (verbose != ERROR && verbose != WARN && verbose != INFO)
{
verbose = NONE;
}

if (verbose == INFO)
{
printf("verbosity: %i\n", verbose);
printf("path to command: %s\n", commandsPath.c_str());
printf("logpath: %s\n", logPath.c_str());
printf("port number: %i\n", portNo);
}
}


Expand Down Expand Up @@ -140,19 +163,22 @@ JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char *options, void *reserved)
if (!server)
{
jvmtiError error;
printf("On attach initiated with options: %s\n", options);
parseAgentOptions(options);

if (verbose >= WARN)
printf("On attach initiated with options: %s\n", options);

rc = vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_2);
if (rc != JNI_OK)
{
fprintf(stderr, "Cannot get JVMTI env\n");
if (verbose >= ERROR)
fprintf(stderr, "Cannot get JVMTI env\n");
return rc;
}
rc = vm->GetEnv((void **)&jni_env, JNI_VERSION_1_8);
if (rc != JNI_OK)
{
fprintf(stderr, "Cannot get JNI env\n");
if (verbose >= ERROR)
fprintf(stderr, "Cannot get JNI env\n");
return rc;
}

Expand All @@ -169,7 +195,8 @@ JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char *options, void *reserved)
error = jvmti->RunAgentThread( createNewThread(jni_env), &startServer, NULL, JVMTI_THREAD_NORM_PRIORITY );
if (error != JVMTI_ERROR_NONE)
{
fprintf(stderr, "Error starting agent thread\n");
if (verbose >= ERROR)
fprintf(stderr, "Error starting agent thread\n");
delete server;
server = NULL;
return JNI_ERR;
Expand All @@ -180,12 +207,14 @@ JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char *options, void *reserved)

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)
{
printf("Options passed in: %s\n", options);
parseAgentOptions(options);
if (verbose >= WARN)
printf("Options passed in: %s\n", options);

jint rest = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_2);
if (rest != JNI_OK || jvmti == NULL) {


if (verbose >= ERROR)
printf("Unable to get access to JVMTI version 1.2");
return JNI_ERR;
}
Expand All @@ -201,4 +230,4 @@ JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)
check_jvmti_error(jvmti, error, "Unable to init VM death event.");

return JNI_OK;
}
}
26 changes: 17 additions & 9 deletions perf-tool/src/agentOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,26 @@ extern EventConfig monitorConfig;

void invalidCommand(const std::string& function, const std::string& command)
{
printf("Invalid command with parameters: {functionality: %s, command: %s}\n", function.c_str(), command.c_str());
if (verbose >= ERROR)
fprintf(stderr, "Invalid command with parameters: {functionality: %s, command: %s}\n", function.c_str(), command.c_str());
}

void invalidFunction(const std::string& function, const std::string& command)
{
printf("Invalid function with parameters: {functionality: %s, command: %s}\n", function.c_str(), command.c_str());
if (verbose >= ERROR)
fprintf(stderr, "Invalid function with parameters: {functionality: %s, command: %s}\n", function.c_str(), command.c_str());
}

void invalidRate(const std::string& function, const std::string& command, int sampleRate)
{
printf("Invalid rate with parameters: {functionality: %s, command: %s, sampleRate: %i}\n", function.c_str(), command.c_str(), sampleRate);
if (verbose >= ERROR)
fprintf(stderr, "Invalid rate with parameters: {functionality: %s, command: %s, sampleRate: %i}\n", function.c_str(), command.c_str(), sampleRate);
}

void invalidStackTraceDepth(const std::string& function, const std::string& command, int stackTraceDepth)
{
printf("Invalid stackTraceDepth with parameters: {functionality: %s, command: %s, stackTraceDepth: %i}\n", function.c_str(), command.c_str(), stackTraceDepth);
if (verbose >= ERROR)
fprintf(stderr, "Invalid stackTraceDepth with parameters: {functionality: %s, command: %s, stackTraceDepth: %i}\n", function.c_str(), command.c_str(), stackTraceDepth);
}

void modifyMonitorEvents(const std::string& function, const std::string& command, int rate, int stackTraceDepth,
Expand Down Expand Up @@ -88,7 +92,8 @@ void modifyMonitorEvents(const std::string& function, const std::string& command
}
else if (!command.compare("start"))
{
printf("Monitor Events Capability already enabled\n");
if (verbose == INFO)
printf("Monitor Events Capability already enabled\n");
error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, (jthread)NULL);
check_jvmti_error(jvmti, error, "Unable to enable MonitorContendedEntered event notifications.");
} else {
Expand All @@ -110,7 +115,8 @@ void modifyMonitorEvents(const std::string& function, const std::string& command
}
else if (!command.compare("stop"))
{
printf("Monitor Events Capability already disabled.");
if (verbose == INFO)
printf("Monitor Events Capability already disabled.");
error = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, (jthread)NULL);
check_jvmti_error(jvmti, error, "Unable to disable MonitorContendedEntered event.");
} else {
Expand Down Expand Up @@ -168,7 +174,8 @@ void modifyObjectAllocEvents(const std::string& function,const std::string& comm
}
else if (!command.compare("stop"))
{
printf("Obect Alloc Capability already disabled");
if (verbose == INFO)
printf("Obect Alloc Capability already disabled");
error = jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, (jthread)NULL);
check_jvmti_error(jvmti, error, "Unable to disable ObjectAlloc event.");
}
Expand Down Expand Up @@ -307,7 +314,8 @@ void agentCommand(const json& jCommand)
callbackClass = jcallback["class"].get<std::string>();
callbackMethod = jcallback["method"].get<std::string>();
callbackSignature = jcallback["signature"].get<std::string>();
printf("Found callback: %s.%s%s\n", callbackClass.c_str(), callbackMethod.c_str(), callbackSignature.c_str());
if (verbose == INFO)
printf("Found callback: %s.%s%s\n", callbackClass.c_str(), callbackMethod.c_str(), callbackSignature.c_str());
}


Expand Down Expand Up @@ -348,4 +356,4 @@ void agentCommand(const json& jCommand)
}
}
return;
}
}
18 changes: 12 additions & 6 deletions perf-tool/src/infra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void check_jvmti_error_throw(jvmtiEnv *jvmti, jvmtiError errnum, const char *str
if (errnum != JVMTI_ERROR_NONE) {
char *errnum_str = NULL;
jvmti->GetErrorName(errnum, &errnum_str);
printf("ERROR: JVMTI: [%d] %s - %s\n", errnum, (errnum_str == NULL ? "Unknown": errnum_str), (str == NULL ? "" : str));
if (verbose >= ERROR)
fprintf(stderr, "ERROR: JVMTI: [%d] %s - %s\n", errnum, (errnum_str == NULL ? "Unknown": errnum_str), (str == NULL ? "" : str));
throw "Oops!";
}
}
Expand All @@ -43,7 +44,8 @@ bool check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char *str) {
char *errnum_str;
errnum_str = NULL;
jvmti->GetErrorName(errnum, &errnum_str);
printf("ERROR: JVMTI: [%d] %s - %s\n", errnum, (errnum_str == NULL ? "Unknown": errnum_str), (str == NULL ? "" : str));
if (verbose >= ERROR)
fprintf(stderr, "ERROR: JVMTI: [%d] %s - %s\n", errnum, (errnum_str == NULL ? "Unknown": errnum_str), (str == NULL ? "" : str));
return false;
}
return true;
Expand Down Expand Up @@ -84,7 +86,8 @@ EventConfig::CallbackIDs EventConfig::getCallBackIDs(JNIEnv *env)
{
if (env->ExceptionCheck() == JNI_TRUE)
env->ExceptionClear();
fprintf(stderr, "Cannot find callback method %s%s\n", getCallbackMethod().c_str(), getCallbackSignature().c_str());
if (verbose >= ERROR)
fprintf(stderr, "Cannot find callback method %s%s\n", getCallbackMethod().c_str(), getCallbackSignature().c_str());
/* Delete the name of the method, so that we don't search over and over */
callbackMethod.clear();
}
Expand All @@ -93,7 +96,8 @@ EventConfig::CallbackIDs EventConfig::getCallBackIDs(JNIEnv *env)
{
if (env->ExceptionCheck() == JNI_TRUE)
env->ExceptionClear();
fprintf(stderr, "Cannot find callback class %s\n", getCallbackClass().c_str());
if (verbose >= ERROR)
fprintf(stderr, "Cannot find callback class %s\n", getCallbackClass().c_str());
/* Delete the name of the class, so that we don't search over and over */
callbackClass.clear();
}
Expand Down Expand Up @@ -130,7 +134,8 @@ JNIEXPORT void JNICALL VMInit(jvmtiEnv *jvmtiEnv, JNIEnv* jni_env, jthread threa

error = jvmtiEnv -> RunAgentThread( createNewThread(jni_env), &startServer, NULL, JVMTI_THREAD_NORM_PRIORITY );
check_jvmti_error_throw(jvmtiEnv, error, "Error starting agent thread.");
printf("VM starting up.\n");
if (verbose >= WARN)
printf("VM starting up.\n");
}

JNIEXPORT void JNICALL VMDeath(jvmtiEnv *jvmtiEnv, JNIEnv* jni_env) {
Expand All @@ -140,5 +145,6 @@ JNIEXPORT void JNICALL VMDeath(jvmtiEnv *jvmtiEnv, JNIEnv* jni_env) {
delete server;
server = NULL;
}
printf("VM shutting down.\n");
if (verbose >= WARN)
printf("VM shutting down.\n");
}
5 changes: 3 additions & 2 deletions perf-tool/src/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ using json = nlohmann::json;
std::atomic<int> monitorSampleCount{0};
EventConfig monitorConfig;


JNIEXPORT void JNICALL MonitorContendedEntered(jvmtiEnv *jvmtiEnv, JNIEnv *env, jthread thread, jobject object){
json j;
jvmtiError error;
Expand Down Expand Up @@ -161,7 +160,8 @@ JNIEXPORT void JNICALL MonitorContendedEntered(jvmtiEnv *jvmtiEnv, JNIEnv *env,
}
else
{
printf ("Error calling GetMethodID for java/lang/Thread.getId()J\n");
if (verbose >= ERROR)
fprintf (stderr, "Error calling GetMethodID for java/lang/Thread.getId()J\n");
}
}
else
Expand Down Expand Up @@ -195,6 +195,7 @@ JNIEXPORT void JNICALL MonitorContendedEntered(jvmtiEnv *jvmtiEnv, JNIEnv *env,
sendToServer(j.dump());

/* Also call the callback */

EventConfig::CallbackIDs callbackIDs = monitorConfig.getCallBackIDs(env);
if (callbackIDs.cachedCallbackClass && callbackIDs.cachedCallbackMethodId)
{
Expand Down
11 changes: 7 additions & 4 deletions perf-tool/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "agentOptions.hpp"
#include "perf.hpp"
#include "utils.hpp"
#include "infra.hpp"

using namespace std;
using json = nlohmann::json;
Expand Down Expand Up @@ -106,8 +107,9 @@ void Server::handleServer()
pollFds[0].events = POLLIN;
pollFds[0].revents = 0;
}

printf("Server started.\n");

if (verbose >= WARN)
printf("Server started.\n");

/* Use polling to keep track of clients and keyboard input */
while (keepPolling)
Expand All @@ -133,8 +135,9 @@ void Server::handleServer()
perror("ERROR on accept");
else
networkClients[activeNetworkClients] = new NetworkClient(newsocketFd);

printf("server: got connection from %s port %d\n",

if (verbose == INFO)
printf("server: got connection from %s port %d\n",
inet_ntoa(cli_addr.sin_addr),
ntohs(cli_addr.sin_port));

Expand Down
4 changes: 3 additions & 1 deletion perf-tool/src/serverClients.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*******************************************************************************/

#include "serverClients.hpp"
#include "infra.hpp"

#include <chrono>
#include <ctime>
Expand Down Expand Up @@ -122,7 +123,8 @@ CommandClient::CommandClient(const string filename)
commandsFile.open(filename);
if (!commandsFile.is_open())
{
printf("filename: %s\n", filename.c_str());
if (verbose >= ERROR)
printf("filename: %s\n", filename.c_str());
perror("ERROR opening commands file");
}

Expand Down
3 changes: 2 additions & 1 deletion perf-tool/src/verboseLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ void VerboseLogSubscriber::Unsubscribe()
{
perror("ERROR deregistering verbose log subscriber failed: ");
}
printf("Calling JVMTI extension %s, rc=%i\n", COM_IBM_REGISTER_VERBOSEGC_SUBSCRIBER, rc);
if (verbose >= WARN)
printf("Calling JVMTI extension %s, rc=%i\n", COM_IBM_REGISTER_VERBOSEGC_SUBSCRIBER, rc);
break;
}
extensionFunctions++; /* move on to the next extension function */
Expand Down

0 comments on commit 51ad4a4

Please sign in to comment.