From 26c3e261b7648d33d28dcfc2ee7d86078b6626f7 Mon Sep 17 00:00:00 2001 From: Greg Eisenhauer Date: Fri, 10 Nov 2023 11:59:25 -0500 Subject: [PATCH] Add Remote "-status" command to see if a server is running and where --- .../adios2/toolkit/remote/remote_common.cpp | 22 +++++ source/adios2/toolkit/remote/remote_common.h | 14 +++ .../adios2/toolkit/remote/remote_server.cpp | 97 ++++++++++++++++--- 3 files changed, 117 insertions(+), 16 deletions(-) diff --git a/source/adios2/toolkit/remote/remote_common.cpp b/source/adios2/toolkit/remote/remote_common.cpp index b7391e76c4..bab73b0462 100644 --- a/source/adios2/toolkit/remote/remote_common.cpp +++ b/source/adios2/toolkit/remote/remote_common.cpp @@ -115,6 +115,25 @@ FMStructDescRec KillResponseStructs[] = { {"KillResponse", KillResponseList, sizeof(struct _KillResponseMsg), NULL}, {NULL, NULL, 0, NULL}}; +FMField StatusServerList[] = {{"StatusResponseCondition", "integer", sizeof(long), + FMOffset(StatusServerMsg, StatusResponseCondition)}, + {NULL, NULL, 0, 0}}; + +FMStructDescRec StatusServerStructs[] = { + {"StatusServer", StatusServerList, sizeof(struct _StatusServerMsg), NULL}, + {NULL, NULL, 0, NULL}}; + +FMField StatusResponseList[] = { + {"StatusResponseCondition", "integer", sizeof(long), + FMOffset(StatusResponseMsg, StatusResponseCondition)}, + {"Hostname", "string", sizeof(char *), FMOffset(StatusResponseMsg, Hostname)}, + {"Status", "string", sizeof(char *), FMOffset(StatusResponseMsg, Status)}, + {NULL, NULL, 0, 0}}; + +FMStructDescRec StatusResponseStructs[] = { + {"StatusResponse", StatusResponseList, sizeof(struct _StatusResponseMsg), NULL}, + {NULL, NULL, 0, NULL}}; + void RegisterFormats(RemoteCommon::Remote_evpath_state &ev_state) { ev_state.OpenFileFormat = CMregister_format(ev_state.cm, RemoteCommon::OpenFileStructs); @@ -129,6 +148,9 @@ void RegisterFormats(RemoteCommon::Remote_evpath_state &ev_state) ev_state.CloseFileFormat = CMregister_format(ev_state.cm, RemoteCommon::CloseFileStructs); ev_state.KillServerFormat = CMregister_format(ev_state.cm, RemoteCommon::KillServerStructs); ev_state.KillResponseFormat = CMregister_format(ev_state.cm, RemoteCommon::KillResponseStructs); + ev_state.StatusServerFormat = CMregister_format(ev_state.cm, RemoteCommon::StatusServerStructs); + ev_state.StatusResponseFormat = + CMregister_format(ev_state.cm, RemoteCommon::StatusResponseStructs); } } } diff --git a/source/adios2/toolkit/remote/remote_common.h b/source/adios2/toolkit/remote/remote_common.h index 53f84aaf61..fc75b4a1f0 100644 --- a/source/adios2/toolkit/remote/remote_common.h +++ b/source/adios2/toolkit/remote/remote_common.h @@ -103,6 +103,18 @@ typedef struct _KillResponseMsg char *Status; } *KillResponseMsg; +typedef struct _StatusServerMsg +{ + int StatusResponseCondition; +} *StatusServerMsg; + +typedef struct _StatusResponseMsg +{ + int StatusResponseCondition; + char *Hostname; + char *Status; +} *StatusResponseMsg; + enum VerbosityLevel { NoVerbose = 0, // Generally no output (but not absolutely quiet?) @@ -129,6 +141,8 @@ struct Remote_evpath_state CMFormat CloseFileFormat; CMFormat KillServerFormat; CMFormat KillResponseFormat; + CMFormat StatusServerFormat; + CMFormat StatusResponseFormat; }; void RegisterFormats(struct Remote_evpath_state &ev_state); diff --git a/source/adios2/toolkit/remote/remote_server.cpp b/source/adios2/toolkit/remote/remote_server.cpp index 202d0be8c7..47240ec03e 100644 --- a/source/adios2/toolkit/remote/remote_server.cpp +++ b/source/adios2/toolkit/remote/remote_server.cpp @@ -349,6 +349,38 @@ static void KillResponseHandler(CManager cm, CMConnection conn, void *vevent, vo exit(0); } +static void StatusServerHandler(CManager cm, CMConnection conn, void *vevent, void *client_data, + attr_list attrs) +{ + StatusServerMsg status_msg = static_cast(vevent); + struct Remote_evpath_state *ev_state = static_cast(client_data); + _StatusResponseMsg status_response_msg; + char hostbuffer[256]; + + // To retrieve hostname + gethostname(hostbuffer, sizeof(hostbuffer)); + memset(&status_response_msg, 0, sizeof(status_response_msg)); + status_response_msg.StatusResponseCondition = status_msg->StatusResponseCondition; + status_response_msg.Hostname = &hostbuffer[0]; + std::stringstream Status; + Status << "ADIOS files Opened: " << ADIOSFilesOpened << " (" << TotalGets << " gets for " + << readable_size(TotalGetBytesSent) << ") Simple files opened: " << SimpleFilesOpened + << " (" << TotalSimpleReads << " reads for " << readable_size(TotalSimpleBytesSent) + << ")"; + status_response_msg.Status = strdup(Status.str().c_str()); + CMwrite(conn, ev_state->StatusResponseFormat, &status_response_msg); + free(status_response_msg.Status); +} + +static void StatusResponseHandler(CManager cm, CMConnection conn, void *vevent, void *client_data, + attr_list attrs) +{ + StatusResponseMsg status_response_msg = static_cast(vevent); + std::cout << "Server running on " << status_response_msg->Hostname + << " current status: " << status_response_msg->Status << std::endl; + exit(0); +} + void ServerRegisterHandlers(struct Remote_evpath_state &ev_state) { CMregister_handler(ev_state.OpenFileFormat, OpenHandler, &ev_state); @@ -357,6 +389,8 @@ void ServerRegisterHandlers(struct Remote_evpath_state &ev_state) CMregister_handler(ev_state.ReadRequestFormat, ReadRequestHandler, &ev_state); CMregister_handler(ev_state.KillServerFormat, KillServerHandler, &ev_state); CMregister_handler(ev_state.KillResponseFormat, KillResponseHandler, &ev_state); + CMregister_handler(ev_state.StatusServerFormat, StatusServerHandler, &ev_state); + CMregister_handler(ev_state.StatusResponseFormat, StatusResponseHandler, &ev_state); } static const char *hostname = "localhost"; @@ -390,6 +424,35 @@ void connect_and_kill(int ServerPort) exit(0); } +void connect_and_get_status(int ServerPort) +{ + CManager cm = CManager_create(); + _StatusServerMsg status_msg; + struct Remote_evpath_state ev_state; + attr_list contact_list = create_attr_list(); + atom_t CM_IP_PORT = -1; + atom_t CM_IP_HOSTNAME = -1; + CM_IP_HOSTNAME = attr_atom_from_string("IP_HOST"); + CM_IP_PORT = attr_atom_from_string("IP_PORT"); + add_attr(contact_list, CM_IP_HOSTNAME, Attr_String, (attr_value)hostname); + add_attr(contact_list, CM_IP_PORT, Attr_Int4, (attr_value)ServerPort); + CMConnection conn = CMinitiate_conn(cm, contact_list); + if (!conn) + return; + + ev_state.cm = cm; + + RegisterFormats(ev_state); + + ServerRegisterHandlers(ev_state); + + memset(&status_msg, 0, sizeof(status_msg)); + status_msg.StatusResponseCondition = CMCondition_get(ev_state.cm, conn); + CMwrite(conn, ev_state.StatusServerFormat, &status_msg); + CMCondition_wait(ev_state.cm, status_msg.StatusResponseCondition); + exit(0); +} + static atom_t CM_IP_PORT = -1; static bool server_timeout(void *CMvoid, int time_since_service) @@ -430,6 +493,7 @@ int main(int argc, char **argv) struct Remote_evpath_state ev_state; int background = 0; int kill_server = 0; + int status_server = 0; int no_timeout = 0; // default to timeout for (int i = 1; i < argc; i++) @@ -442,31 +506,27 @@ int main(int argc, char **argv) { kill_server++; } + else if (strcmp(argv[i], "-status") == 0) + { + status_server++; + } else if (strcmp(argv[i], "-no_timeout") == 0) { no_timeout++; } - if (argv[i][0] == '-') + else if (strcmp(argv[i], "-v") == 0) { - size_t j = 1; - while (argv[i][j] != 0) - { - if (argv[i][j] == 'v') - { - verbose++; - } - else if (argv[i][j] == 'q') - { - verbose--; - } - j++; - } + verbose++; + } + else if (strcmp(argv[i], "-q") == 0) + { + verbose--; } else { fprintf(stderr, "Unknown argument \"%s\"\n", argv[i]); - fprintf(stderr, - "Usage: remote_server [-background] [-kill_server] [-no_timeout] [-v] [-q]\n"); + fprintf(stderr, "Usage: remote_server [-background] [-kill_server] [-no_timeout] " + "[-status] [-v] [-q]\n"); exit(1); } } @@ -476,6 +536,11 @@ int main(int argc, char **argv) connect_and_kill(ServerPort); exit(0); } + if (status_server) + { + connect_and_get_status(ServerPort); + exit(0); + } if (background) { if (verbose)