]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph tools: de-globalize CephToolContext
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 8 Jun 2011 22:42:46 +0000 (15:42 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 8 Jun 2011 23:23:33 +0000 (16:23 -0700)
De-globalize CephToolContext. It's important to do this now because the
constructor for CephToolContext references the configuration (via
CephContext.)

Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/tools/ceph.cc
src/tools/common.cc
src/tools/common.h
src/tools/gceph.cc
src/tools/gui.cc
src/tools/gui.h

index 5b760f3f094eec097e728f24ff7f55ad38946105..e3b12d6964b102a44109d353648116e8f798a15e 100644 (file)
@@ -52,14 +52,15 @@ static void usage()
 
 static void parse_cmd_args(const vector<const char*> &args,
                const char **in_file, const char ** out_file,
-               ceph_tool_mode_t *mode, vector<const char*> *nargs)
+               ceph_tool_mode_t *mode, vector<const char*> *nargs,
+               bool *concise)
 {
   DEFINE_CONF_VARS(usage);
   FOR_EACH_ARG(args) {
     if (CEPH_ARGPARSE_EQ("in_file", 'i')) {
       CEPH_ARGPARSE_SET_ARG_VAL(in_file, OPT_STR);
     } else if (CEPH_ARGPARSE_EQ("concise", '\0')) {
-      g.concise = true;
+      *concise = true;
     } else if (CEPH_ARGPARSE_EQ("out_file", 'o')) {
       CEPH_ARGPARSE_SET_ARG_VAL(out_file, OPT_STR);
     } else if (CEPH_ARGPARSE_EQ("status", 's')) {
@@ -126,7 +127,8 @@ int main(int argc, const char **argv)
   vector<const char*> nargs;
 
   // parse user input
-  parse_cmd_args(args, &in_file, &out_file, &mode, &nargs);
+  bool concise = false;
+  parse_cmd_args(args, &in_file, &out_file, &mode, &nargs, &concise);
 
   bufferlist indata;
 
@@ -137,7 +139,8 @@ int main(int argc, const char **argv)
     }
   }
 
-  if (ceph_tool_common_init(mode)) {
+  CephToolCtx *ctx = ceph_tool_common_init(mode, concise);
+  if (!ctx) {
     derr << "ceph_tool_common_init failed." << dendl;
     return 1;
   }
@@ -148,9 +151,9 @@ int main(int argc, const char **argv)
   switch (mode) {
     case CEPH_TOOL_MODE_ONE_SHOT_OBSERVER: // fall through
     case CEPH_TOOL_MODE_OBSERVER: {
-      g.lock.Lock();
-      send_observe_requests();
-      g.lock.Unlock();
+      ctx->lock.Lock();
+      send_observe_requests(ctx);
+      ctx->lock.Unlock();
       break;
     }
 
@@ -160,11 +163,11 @@ int main(int argc, const char **argv)
        cmd.push_back(string(nargs[i]));
       }
       if (cmd.empty()) {
-       if (ceph_tool_do_cli())
+       if (ceph_tool_do_cli(ctx))
          ret = 1;
       }
       else {
-       if (ceph_tool_cli_input(cmd, out_file, indata))
+       if (ceph_tool_cli_input(ctx, cmd, out_file, indata))
          ret = 1;
       }
       if (ceph_tool_messenger_shutdown())
@@ -179,7 +182,7 @@ int main(int argc, const char **argv)
     }
   }
 
-  if (ceph_tool_common_shutdown())
+  if (ceph_tool_common_shutdown(ctx))
     ret = 1;
   return ret;
 }
index 3319e696c0adc79aaba91e90d9946cbe007295da..b368965e128ab5c8bcaa1c7858981937b9c6d318 100644 (file)
@@ -43,11 +43,9 @@ extern "C" {
 #include <histedit.h>
 }
 
+// TODO: should move these into CephToolCtx for consistency
 static enum ceph_tool_mode_t
   ceph_tool_mode(CEPH_TOOL_MODE_CLI_INPUT);
-
-struct ceph_tool_data g;
-
 static Cond cmd_cond;
 static SimpleMessenger *messenger = 0;
 static Tokenizer *tok;
@@ -80,17 +78,17 @@ static set<int> registered, seen;
 
 version_t map_ver[PAXOS_NUM];
 
-static void handle_observe(MMonObserve *observe)
+static void handle_observe(CephToolCtx *ctx, MMonObserve *observe)
 {
   dout(1) << observe->get_source() << " -> " << get_paxos_name(observe->machine_id)
          << " registered" << dendl;
-  g.lock.Lock();
+  ctx->lock.Lock();
   registered.insert(observe->machine_id);  
-  g.lock.Unlock();
+  ctx->lock.Unlock();
   observe->put();
 }
 
-static void handle_notify(MMonObserveNotify *notify)
+static void handle_notify(CephToolCtx *ctx, MMonObserveNotify *notify)
 {
   utime_t now = g_clock.now();
 
@@ -99,8 +97,9 @@ static void handle_notify(MMonObserveNotify *notify)
          << (notify->is_latest ? " (latest)" : "")
          << dendl;
   
-  if (ceph_fsid_compare(&notify->fsid, &g.mc.monmap.fsid)) {
-    dout(0) << notify->get_source_inst() << " notify fsid " << notify->fsid << " != " << g.mc.monmap.fsid << dendl;
+  if (ceph_fsid_compare(&notify->fsid, &ctx->mc.monmap.fsid)) {
+    dout(0) << notify->get_source_inst() << " notify fsid " << notify->fsid << " != "
+           << ctx->mc.monmap.fsid << dendl;
     notify->put();
     return;
   }
@@ -113,34 +112,34 @@ static void handle_notify(MMonObserveNotify *notify)
     {
       bufferlist::iterator p = notify->bl.begin();
       if (notify->is_latest) {
-       g.pgmap.decode(p);
+       ctx->pgmap.decode(p);
       } else {
        PGMap::Incremental inc;
        inc.decode(p);
-       g.pgmap.apply_incremental(inc);
+       ctx->pgmap.apply_incremental(inc);
       }
-      *g.log << now << "    pg " << g.pgmap << std::endl;
-      g.updates |= PG_MON_UPDATE;
+      *ctx->log << now << "    pg " << ctx->pgmap << std::endl;
+      ctx->updates |= PG_MON_UPDATE;
       break;
     }
 
   case PAXOS_MDSMAP:
-    g.mdsmap.decode(notify->bl);
-    *g.log << now << "   mds " << g.mdsmap << std::endl;
-    g.updates |= MDS_MON_UPDATE;
+    ctx->mdsmap.decode(notify->bl);
+    *ctx->log << now << "   mds " << ctx->mdsmap << std::endl;
+    ctx->updates |= MDS_MON_UPDATE;
     break;
 
   case PAXOS_OSDMAP:
     {
       if (notify->is_latest) {
-       g.osdmap.decode(notify->bl);
+       ctx->osdmap.decode(notify->bl);
       } else {
        OSDMap::Incremental inc(notify->bl);
-       g.osdmap.apply_incremental(inc);
+       ctx->osdmap.apply_incremental(inc);
       }
-      *g.log << now << "   osd " << g.osdmap << std::endl;
+      *ctx->log << now << "   osd " << ctx->osdmap << std::endl;
     }
-    g.updates |= OSD_MON_UPDATE;
+    ctx->updates |= OSD_MON_UPDATE;
     break;
 
   case PAXOS_LOG:
@@ -151,14 +150,14 @@ static void handle_notify(MMonObserveNotify *notify)
        ::decode(summary, p);
        // show last log message
        if (!summary.tail.empty())
-         *g.log << now << "   log " << summary.tail.back() << std::endl;
+         *ctx->log << now << "   log " << summary.tail.back() << std::endl;
       } else {
        LogEntry le;
        __u8 v;
        ::decode(v, p);
        while (!p.end()) {
          le.decode(p);
-         *g.log << now << "   log " << le << std::endl;
+         *ctx->log << now << "   log " << le << std::endl;
        }
       }
       break;
@@ -169,14 +168,14 @@ static void handle_notify(MMonObserveNotify *notify)
 #if 0
       bufferlist::iterator p = notify->bl.begin();
       if (notify->is_latest) {
-       KeyServerData data;
-       ::decode(data, p);
-       *g.log << now << "   auth " << std::endl;
+       KeyServerData ctx;
+       ::decode(ctx, p);
+       *ctx->log << now << "   auth " << std::endl;
       } else {
        while (!p.end()) {
          AuthMonitor::Incremental inc;
           inc.decode(p);
-         *g.log << now << "   auth " << inc.name.to_str() << std::endl;
+         *ctx->log << now << "   auth " << inc.name.to_str() << std::endl;
        }
       }
 #endif
@@ -186,13 +185,13 @@ static void handle_notify(MMonObserveNotify *notify)
 
   case PAXOS_MONMAP:
     {
-      g.mc.monmap.decode(notify->bl);
-      *g.log << now << "   mon " << g.mc.monmap << std::endl;
+      ctx->mc.monmap.decode(notify->bl);
+      *ctx->log << now << "   mon " << ctx->mc.monmap << std::endl;
     }
     break;
 
   default:
-    *g.log << now << "  ignoring unknown machine id " << notify->machine_id << std::endl;
+    *ctx->log << now << "  ignoring unknown machine id " << notify->machine_id << std::endl;
   }
 
   map_ver[notify->machine_id] = notify->ver;
@@ -206,7 +205,7 @@ static void handle_notify(MMonObserveNotify *notify)
       }
       break;
     case CEPH_TOOL_MODE_GUI:
-      g.gui_cond.Signal();
+      ctx->gui_cond.Signal();
       break;
     default:
       // do nothing
@@ -219,33 +218,39 @@ static void handle_notify(MMonObserveNotify *notify)
 class C_ObserverRefresh : public Context {
 public:
   bool newmon;
-  C_ObserverRefresh(bool n) : newmon(n) {}
+  C_ObserverRefresh(bool n, CephToolCtx *ctx_) 
+    : newmon(n),
+      ctx(ctx_)
+  {
+  }
   void finish(int r) {
-    send_observe_requests();
+    send_observe_requests(ctx);
   }
+private:
+  CephToolCtx *ctx;
 };
 
-void send_observe_requests()
+void send_observe_requests(CephToolCtx *ctx)
 {
   dout(1) << "send_observe_requests " << dendl;
 
   bool sent = false;
   for (int i=0; i<PAXOS_NUM; i++) {
-    MMonObserve *m = new MMonObserve(g.mc.monmap.fsid, i, map_ver[i]);
+    MMonObserve *m = new MMonObserve(ctx->mc.monmap.fsid, i, map_ver[i]);
     dout(1) << "mon" << " <- observe " << get_paxos_name(i) << dendl;
-    g.mc.send_mon_message(m);
+    ctx->mc.send_mon_message(m);
     sent = true;
   }
 
   registered.clear();
   float seconds = g_conf->paxos_observer_timeout/2;
   dout(1) << " refresh after " << seconds << " with same mon" << dendl;
-  g.timer.add_event_after(seconds, new C_ObserverRefresh(false));
+  ctx->timer.add_event_after(seconds, new C_ObserverRefresh(ctx, false));
 }
 
-static void handle_ack(MMonCommandAck *ack)
+static void handle_ack(CephToolCtx *ctx, MMonCommandAck *ack)
 {
-  g.lock.Lock();
+  ctx->lock.Lock();
   reply = true;
   reply_from = ack->get_source_inst();
   reply_rs = ack->rs;
@@ -253,36 +258,42 @@ static void handle_ack(MMonCommandAck *ack)
   reply_bl = ack->get_data();
   cmd_cond.Signal();
   if (resend_event) {
-    g.timer.cancel_event(resend_event);
+    ctx->timer.cancel_event(resend_event);
     resend_event = 0;
   }
-  g.lock.Unlock();
+  ctx->lock.Unlock();
   ack->put();
 }
 
-static void send_command()
+static void send_command(CephToolCtx *ctx)
 {
   version_t last_seen_version = 0;
-  MMonCommand *m = new MMonCommand(g.mc.monmap.fsid, last_seen_version);
+  MMonCommand *m = new MMonCommand(ctx->mc.monmap.fsid, last_seen_version);
   m->cmd = pending_cmd;
   m->set_data(pending_bl);
 
-  if (!g.concise)
-    *g.log << g_clock.now() << " mon" << " <- " << pending_cmd << std::endl;
-  g.mc.send_mon_message(m);
+  if (!ctx->concise)
+    *ctx->log << g_clock.now() << " mon" << " <- " << pending_cmd << std::endl;
+  ctx->mc.send_mon_message(m);
 }
 
 class Admin : public Dispatcher {
+public:
+  Admin(CephToolCtx *ctx_)
+    : ctx(ctx_)
+  {
+  }
+
   bool ms_dispatch(Message *m) {
     switch (m->get_type()) {
     case MSG_MON_COMMAND_ACK:
-      handle_ack((MMonCommandAck*)m);
+      handle_ack(ctx, (MMonCommandAck*)m);
       break;
     case MSG_MON_OBSERVE_NOTIFY:
-      handle_notify((MMonObserveNotify *)m);
+      handle_notify(ctx, (MMonObserveNotify *)m);
       break;
     case MSG_MON_OBSERVE:
-      handle_observe((MMonObserve *)m);
+      handle_observe(ctx, (MMonObserve *)m);
       break;
     case CEPH_MSG_MON_MAP:
       m->put();
@@ -295,37 +306,40 @@ class Admin : public Dispatcher {
 
   void ms_handle_connect(Connection *con) {
     if (con->get_peer_type() == CEPH_ENTITY_TYPE_MON) {
-      g.lock.Lock();
+      ctx->lock.Lock();
       if (ceph_tool_mode != CEPH_TOOL_MODE_CLI_INPUT) {
-       send_observe_requests();
+       send_observe_requests(ctx);
       }
       if (pending_cmd.size())
-       send_command();
-      g.lock.Unlock();
+       send_command(ctx);
+      ctx->lock.Unlock();
     }
   }
   bool ms_handle_reset(Connection *con) { return false; }
   void ms_handle_remote_reset(Connection *con) {}
 
-} dispatcher;
+private:
+  CephToolCtx *ctx;
+};
 
-int do_command(vector<string>& cmd, bufferlist& bl, string& rs, bufferlist& rbl)
+static int do_command(CephToolCtx *ctx,
+              vector<string>& cmd, bufferlist& bl, string& rs, bufferlist& rbl)
 {
-  Mutex::Locker l(g.lock);
+  Mutex::Locker l(ctx->lock);
 
   pending_cmd = cmd;
   pending_bl = bl;
   reply = false;
   
-  send_command();
+  send_command(ctx);
 
   while (!reply)
-    cmd_cond.Wait(g.lock);
+    cmd_cond.Wait(ctx->lock);
 
   rs = rs;
   rbl = reply_bl;
-  if (!g.concise)
-    *g.log << g_clock.now() << " "
+  if (!ctx->concise)
+    *ctx->log << g_clock.now() << " "
           << reply_from.name << " -> '"
           << reply_rs << "' (" << reply_rc << ")"
           << std::endl;
@@ -340,7 +354,7 @@ static const char *cli_prompt(EditLine *e)
   return "ceph> ";
 }
 
-int ceph_tool_do_cli()
+int ceph_tool_do_cli(CephToolCtx *ctx)
 {
   /* emacs style */
   EditLine *el = el_init("ceph", stdin, stdout, stderr);
@@ -365,16 +379,16 @@ int ceph_tool_do_cli()
     int chars_read;
     const char *line = el_gets(el, &chars_read);
 
-    //*g.log << "typed '" << line << "'" << std::endl;
+    //*ctx->log << "typed '" << line << "'" << std::endl;
 
     if (chars_read == 0) {
-      *g.log << "quit" << std::endl;
+      *ctx->log << "quit" << std::endl;
       break;
     }
 
     history(myhistory, &ev, H_ENTER, line);
 
-    if (run_command(line))
+    if (run_command(ctx, line))
       break;
   }
 
@@ -384,7 +398,7 @@ int ceph_tool_do_cli()
   return 0;
 }
 
-int run_command(const char *line)
+int run_command(CephToolCtx *ctx, const char *line)
 {
   if (strcmp(line, "quit\n") == 0)
     return 1;
@@ -423,11 +437,11 @@ int run_command(const char *line)
 
   bufferlist in;
   if (cmd.size() == 1 && cmd[0] == "print") {
-    if (!g.concise)
-      *g.log << "----" << std::endl;
+    if (!ctx->concise)
+      *ctx->log << "----" << std::endl;
     fwrite(in.c_str(), in.length(), 1, stdout);
-    if (!g.concise)
-      *g.log << "---- (" << in.length() << " bytes)" << std::endl;
+    if (!ctx->concise)
+      *ctx->log << "---- (" << in.length() << " bytes)" << std::endl;
     return 0;
   }
 
@@ -436,51 +450,51 @@ int run_command(const char *line)
   bufferlist out;
   if (infile) {
     if (out.read_file(infile) == 0) {
-      if (!g.concise)
-       *g.log << "read " << out.length() << " from " << infile << std::endl;
+      if (!ctx->concise)
+       *ctx->log << "read " << out.length() << " from " << infile << std::endl;
     } else {
       char buf[80];
-      *g.log << "couldn't read from " << infile << ": " << strerror_r(errno, buf, sizeof(buf)) << std::endl;
+      *ctx->log << "couldn't read from " << infile << ": " << strerror_r(errno, buf, sizeof(buf)) << std::endl;
       return 0;
     }
   }
 
   in.clear();
   string rs;
-  do_command(cmd, out, rs, in);
+  do_command(ctx, cmd, out, rs, in);
 
   if (in.length() == 0)
     return 0;
 
   if (outfile) {
     if (strcmp(outfile, "-") == 0) {
-      if (!g.concise)
-       *g.log << "----" << std::endl;
+      if (!ctx->concise)
+       *ctx->log << "----" << std::endl;
       fwrite(in.c_str(), in.length(), 1, stdout);
-      if (!g.concise)
-       *g.log << "---- (" << in.length() << " bytes)" << std::endl;
+      if (!ctx->concise)
+       *ctx->log << "---- (" << in.length() << " bytes)" << std::endl;
     }
     else {
       in.write_file(outfile);
-      if (!g.concise)
-       *g.log << "wrote " << in.length() << " to "
+      if (!ctx->concise)
+       *ctx->log << "wrote " << in.length() << " to "
               << outfile << std::endl;
     }
   }
   else {
-    if (!g.concise)
-      *g.log << "got " << in.length() << " byte payload; 'print' "
+    if (!ctx->concise)
+      *ctx->log << "got " << in.length() << " byte payload; 'print' "
             << "to dump to terminal, or add '>-' to command." << std::endl;
   }
   return 0;
 }
 
-int ceph_tool_cli_input(std::vector<std::string> &cmd, const char *outfile,
-                       bufferlist &indata)
+int ceph_tool_cli_input(CephToolCtx *ctx, std::vector<std::string> &cmd, 
+                       const char *outfile, bufferlist &indata)
 {
   string rs;
   bufferlist odata;
-  int ret = do_command(cmd, indata, rs, odata);
+  int ret = do_command(ctx, cmd, indata, rs, odata);
   if (ret)
     return ret;
 
@@ -509,13 +523,16 @@ int ceph_tool_cli_input(std::vector<std::string> &cmd, const char *outfile,
   return 0;
 }
 
-int ceph_tool_common_init(ceph_tool_mode_t mode)
+CephToolCtx* ceph_tool_common_init(ceph_tool_mode_t mode, bool concise)
 {
   ceph_tool_mode = mode;
 
+  std::auto_ptr <CephToolCtx> ctx;
+  ctx.reset(new CephToolCtx(&g_ceph_context, concise));
+
   // get monmap
-  if (g.mc.build_initial_monmap() < 0)
-    return -1;
+  if (ctx->mc.build_initial_monmap() < 0)
+    return NULL;
   
   // initialize tokenizer
   tok = tok_init(NULL);
@@ -524,28 +541,29 @@ int ceph_tool_common_init(ceph_tool_mode_t mode)
   messenger = new SimpleMessenger();
   messenger->register_entity(entity_name_t::CLIENT());
   messenger->start_with_nonce(getpid());
-  messenger->add_dispatcher_head(&dispatcher);
+  ctx->dispatcher = new Admin(ctx.get());
+  messenger->add_dispatcher_head(ctx->dispatcher);
 
-  g.lock.Lock();
-  g.timer.init();
-  g.lock.Unlock();
+  ctx->lock.Lock();
+  ctx->timer.init();
+  ctx->lock.Unlock();
 
-  g.mc.set_messenger(messenger);
-  g.mc.init();
+  ctx->mc.set_messenger(messenger);
+  ctx->mc.init();
 
-  if (g.mc.authenticate() < 0) {
+  if (ctx->mc.authenticate() < 0) {
     derr << "unable to authenticate as " << g_conf->name << dendl;
     ceph_tool_messenger_shutdown();
-    ceph_tool_common_shutdown();
-    return 1;
+    ceph_tool_common_shutdown(ctx.get());
+    return NULL;
   }
-  if (g.mc.get_monmap() < 0) {
+  if (ctx->mc.get_monmap() < 0) {
     derr << "unable to get monmap" << dendl;
     ceph_tool_messenger_shutdown();
-    ceph_tool_common_shutdown();
-    return 1;
+    ceph_tool_common_shutdown(ctx.get());
+    return NULL;
   }
-  return 0;
+  return ctx.release();
 }
 
 int ceph_tool_messenger_shutdown()
@@ -553,16 +571,16 @@ int ceph_tool_messenger_shutdown()
   return messenger->shutdown();
 }
 
-int ceph_tool_common_shutdown()
+int ceph_tool_common_shutdown(CephToolCtx *ctx)
 {
   // wait for messenger to finish
   messenger->wait();
   messenger->destroy();
   tok_end(tok);
   
-  g.lock.Lock();
-  g.mc.shutdown();
-  g.timer.shutdown();
-  g.lock.Unlock();
+  ctx->lock.Lock();
+  ctx->mc.shutdown();
+  ctx->timer.shutdown();
+  ctx->lock.Unlock();
   return 0;
 }
index 46e310fdfb3c5267b2b38bde8dbc664141c9052c..5a00bde23f8b4d48d20eddf7590a7a1a787db14a 100644 (file)
@@ -18,6 +18,8 @@
 #define MON_MON_UPDATE     (1<<3)
 #define EVERYTHING_UPDATE   0xffffffff
 
+class CephContext;
+
 enum ceph_tool_mode_t {
   CEPH_TOOL_MODE_CLI_INPUT = 0,
   CEPH_TOOL_MODE_OBSERVER = 1,
@@ -26,8 +28,10 @@ enum ceph_tool_mode_t {
 };
 
 // tool/ceph.cc
-struct ceph_tool_data
+class CephToolCtx
 {
+public:
+  CephContext *cct;
   PGMap pgmap;
   MDSMap mdsmap;
   OSDMap osdmap;
@@ -52,26 +56,33 @@ struct ceph_tool_data
 
   bool concise;
 
-  ceph_tool_data() :
+  Dispatcher *dispatcher;
+
+  CephToolCtx(CephContext *cct_, bool concise_) :
+    cct(cct_),
     mc(&g_ceph_context),
     updates(EVERYTHING_UPDATE),
     log(&std::cout),
     slog(NULL),
     lock("ceph.cc lock"), timer(lock),
-    concise(false)
+    concise(concise_),
+    dispatcher(NULL)
   {
   }
+
+  ~CephToolCtx() {
+    delete dispatcher;
+  }
 };
 
 // tool/ceph.cc
-extern struct ceph_tool_data g;
-int ceph_tool_do_cli();
-int run_command(const char *line);
-void send_observe_requests();
-int ceph_tool_common_init(ceph_tool_mode_t mode);
-int ceph_tool_cli_input(std::vector<std::string> &cmd, const char *outfile,
-                       bufferlist &indata);
+int ceph_tool_do_cli(CephToolCtx *data);
+int run_command(CephToolCtx *data, const char *line);
+void send_observe_requests(CephToolCtx *ctx);
+CephToolCtx* ceph_tool_common_init(ceph_tool_mode_t mode, bool concise);
+int ceph_tool_cli_input(CephToolCtx *ctx, std::vector<std::string> &cmd, 
+                       const char *outfile, bufferlist &indata);
 int ceph_tool_messenger_shutdown();
-int ceph_tool_common_shutdown();
+int ceph_tool_common_shutdown(CephToolCtx *ctx);
 
 #endif
index ec7397d57f1c4b0dbbac6f74a06aa4a7f765d8da..c9832d92934da38b33a61fc14e7ff0daac1b51f4 100644 (file)
@@ -23,7 +23,7 @@
 #include <vector>
 
 // tool/gui.cc
-int run_gui(int argc, char **argv);
+int run_gui(CephToolCtx *ctx, int argc, char **argv);
 
 using std::vector;
 
@@ -47,23 +47,26 @@ static void parse_gceph_args(const vector<const char*> &args)
   }
 }
 
-static int cephtool_run_gui(int argc, const char **argv)
+static int cephtool_run_gui(CephToolCtx *ctx, int argc,
+                           const char **argv)
 {
-  g.log = &gss;
-  g.slog = &gss;
+  ctx->log = &gss;
+  ctx->slog = &gss;
 
   // TODO: make sure that we capture the log this generates in the GUI
-  g.lock.Lock();
-  send_observe_requests();
-  g.lock.Unlock();
+  ctx->lock.Lock();
+  send_observe_requests(ctx);
+  ctx->lock.Unlock();
 
-  return run_gui(argc, (char **)argv);
+  return run_gui(ctx, argc, (char **)argv);
 }
 
+static CephToolCtx *ctx = NULL;
+
 void ceph_tool_common_shutdown_wrapper()
 {
   ceph_tool_messenger_shutdown();
-  ceph_tool_common_shutdown();
+  ceph_tool_common_shutdown(ctx);
 }
 
 int main(int argc, const char **argv)
@@ -82,14 +85,15 @@ int main(int argc, const char **argv)
 
   parse_gceph_args(args);
 
-  if (ceph_tool_common_init(CEPH_TOOL_MODE_GUI)) {
+  ctx = ceph_tool_common_init(CEPH_TOOL_MODE_GUI, false);
+  if (!ctx) {
     derr << "cephtool_common_init failed." << dendl;
     return 1;
   }
 
   atexit(ceph_tool_common_shutdown_wrapper);
 
-  if (cephtool_run_gui(argc, argv))
+  if (cephtool_run_gui(ctx, argc, argv))
     ret = 1;
 
   if (ceph_tool_messenger_shutdown())
index dd0b11722aa12dcd5ed9c0cbe942be75cd81e44a..54eb4560b8c94b8197575a71875d7c16fd998521 100644 (file)
@@ -130,13 +130,14 @@ public:
 
   void *entry()
   {
-    g.lock.Lock();
+    CephToolCtx *ctx = gui->ctx;
+    ctx->lock.Lock();
     while (true) {
       utime_t t(g_clock.now());
       t += 3.0;
-      g.gui_cond.WaitUntil(g.lock, t);
+      ctx->gui_cond.WaitUntil(ctx->lock, t);
       if (shutting_down) {
-       g.lock.Unlock();
+       ctx->lock.Unlock();
        return NULL;
       }
       gui->check_status();
@@ -146,7 +147,7 @@ public:
   void shutdown()
   {
     shutting_down = true;
-    g.gui_cond.Signal();
+    gui->ctx->gui_cond.Signal();
     this->join();
   }
 
@@ -155,13 +156,14 @@ private:
   bool shutting_down;
 };
 
-GuiMonitor::GuiMonitor(Glib::RefPtr<Gtk::Builder> builder)
+GuiMonitor::GuiMonitor(Glib::RefPtr<Gtk::Builder> builder, CephToolCtx * ctx_)
   : pg_cluster_zoom(0),
     osd_cluster_zoom(0),
     mds_cluster_zoom(0),
     send_command_success(false),
     view_node_success(false),
-    thread(NULL)
+    thread(NULL),
+    ctx(ctx_)
 {
   MY_GET_WIDGET(guiMonitorWindow);
   MY_GET_WIDGET(guiMonitorQuitImageMenuItem);
@@ -506,7 +508,7 @@ void GuiMonitor::link_elements()
  */
 void GuiMonitor::update_osd_cluster_view()
 {
-  int num_osds = g.osdmap.get_max_osd();
+  int num_osds = ctx->osdmap.get_max_osd();
 
   // Do not update the OSD cluster view if the zoom level is not zero.
   if (!osd_cluster_zoom)
@@ -514,8 +516,8 @@ void GuiMonitor::update_osd_cluster_view()
 
   ostringstream oss;
   oss << num_osds << " OSD" << ((num_osds == 1) ? "" : "s")
-      << " Total: " << g.osdmap.get_num_up_osds() << " Up, "
-      << g.osdmap.get_num_in_osds() << " In";
+      << " Total: " << ctx->osdmap.get_num_up_osds() << " Up, "
+      << ctx->osdmap.get_num_in_osds() << " In";
   guiMonitorOSDClusterStatsLabel->set_label(oss.str());
 }
 
@@ -526,10 +528,10 @@ void GuiMonitor::update_mds_cluster_view()
     view_mds_nodes();
 
   ostringstream oss;
-  oss << g.mdsmap.get_num_mds() << " In, "
-      << g.mdsmap.get_num_failed() << " Failed, "
-      << g.mdsmap.get_num_mds(MDSMap::STATE_STOPPED) << " Stopped, "
-      << g.mdsmap.get_max_mds() << " Max";
+  oss << ctx->mdsmap.get_num_mds() << " In, "
+      << ctx->mdsmap.get_num_failed() << " Failed, "
+      << ctx->mdsmap.get_num_mds(MDSMap::STATE_STOPPED) << " Stopped, "
+      << ctx->mdsmap.get_max_mds() << " Max";
   guiMonitorMDSClusterStatsLabel->set_label(oss.str());
 }
 
@@ -540,11 +542,11 @@ void GuiMonitor::update_pg_cluster_view()
     view_pg_nodes(0, 0, true);
 
   ostringstream oss;
-  oss << g.pgmap.pg_stat.size() << " Placement Groups\n"
-      << kb_t(g.pgmap.pg_sum.num_kb) << " Data, "
-      << kb_t(g.pgmap.osd_sum.kb_used) << " Used, "
-      << kb_t(g.pgmap.osd_sum.kb_avail) << " / "
-      << kb_t(g.pgmap.osd_sum.kb) << " Available";
+  oss << ctx->pgmap.pg_stat.size() << " Placement Groups\n"
+      << kb_t(ctx->pgmap.pg_sum.num_kb) << " Data, "
+      << kb_t(ctx->pgmap.osd_sum.kb_used) << " Used, "
+      << kb_t(ctx->pgmap.osd_sum.kb_avail) << " / "
+      << kb_t(ctx->pgmap.osd_sum.kb) << " Available";
   guiMonitorPGClusterStatsLabel->set_label(oss.str());
 }
 
@@ -554,7 +556,7 @@ void GuiMonitor::update_mon_cluster_view()
   Gtk::TreeModel::Row current_row;
   entity_inst_t currentMonitor;
   string currentAddress;
-  unsigned int monitors = g.mc.monmap.size();
+  unsigned int monitors = ctx->mc.monmap.size();
 
   // Clear current contents of the monitor cluster area of the GUI.
   guiMonitorMonitorClusterEntries->clear();
@@ -565,7 +567,7 @@ void GuiMonitor::update_mon_cluster_view()
 
   // For each monitor in the monitor map, output its ID and its address.
   for (unsigned int i = 0; i < monitors; i++) {
-    currentMonitor = g.mc.monmap.get_inst(i);
+    currentMonitor = ctx->mc.monmap.get_inst(i);
 
     input << currentMonitor.addr;
     currentAddress = input.str();
@@ -627,11 +629,11 @@ void GuiMonitor::view_osd_nodes(unsigned int begin, unsigned int end, bool
          caption = gen_osd_icon_caption(begin_range, end_range);
 
          for (j = begin_range; j <= end_range; j++) {
-            if (g.osdmap.is_out(j)) {
+            if (ctx->osdmap.is_out(j)) {
                icon_status = CEPH_OSD_OUT;
                break;
             }
-            else if (g.osdmap.is_down(j))
+            else if (ctx->osdmap.is_down(j))
                icon_status = ~CEPH_OSD_UP;
          }
 
@@ -691,13 +693,13 @@ void GuiMonitor::view_mds_nodes(unsigned int begin, unsigned int end, bool
   if (viewAll) {
       // Gather all of the names of the up MDSes
       set<int> up_mds;
-      g.mdsmap.get_up_mds_set(up_mds);
+      ctx->mdsmap.get_up_mds_set(up_mds);
       current_up_mds.clear();
       for (set<int>::const_iterator mds_id = up_mds.begin();
           mds_id != up_mds.end();
           ++mds_id)
       {
-         const MDSMap::mds_info_t& info = g.mdsmap.get_mds_info(*mds_id);
+         const MDSMap::mds_info_t& info = ctx->mdsmap.get_mds_info(*mds_id);
 
          current_up_mds.push_back(info.name);
       }
@@ -768,12 +770,12 @@ view_pg_nodes(unsigned int begin, unsigned int end, bool view_all)
     while (!old_pg_cluster_zoom_states.empty())
       old_pg_cluster_zoom_states.pop();
 
-    size = g.pgmap.pg_stat.size();
+    size = ctx->pgmap.pg_stat.size();
     pg_cluster_zoom = 0;
 
     current_pgs.clear();
-    for (hash_map<pg_t,pg_stat_t>::const_iterator p = g.pgmap.pg_stat.begin();
-        p != g.pgmap.pg_stat.end(); ++p) {
+    for (hash_map<pg_t,pg_stat_t>::const_iterator p = ctx->pgmap.pg_stat.begin();
+        p != ctx->pgmap.pg_stat.end(); ++p) {
       current_pgs.push_back(p->first);
     }
   }
@@ -822,20 +824,20 @@ void GuiMonitor::check_status()
   // servers, and the monitors have been updated.  If any of these have been
   // updated, then update the GUI accordingly.
 
-  assert(g.lock.is_locked());
-  if (g.updates & OSD_MON_UPDATE)
+  assert(ctx->lock.is_locked());
+  if (ctx->updates & OSD_MON_UPDATE)
     update_osd_cluster_view();
-  if (g.updates & MDS_MON_UPDATE)
+  if (ctx->updates & MDS_MON_UPDATE)
     update_mds_cluster_view();
-  if (g.updates & PG_MON_UPDATE)
+  if (ctx->updates & PG_MON_UPDATE)
     update_pg_cluster_view();
-  if (g.updates & MON_MON_UPDATE)
+  if (ctx->updates & MON_MON_UPDATE)
     update_mon_cluster_view();
-  g.updates = 0;
+  ctx->updates = 0;
 
   // See if the log has been updated. If it has, then
   // update the log text box in the GUI and then clear the log stream.
-  string log(g.slog->str());
+  string log(ctx->slog->str());
   if (!log.empty()) {
     if (!guiMonitorCopyImageMenuItem->is_sensitive())
       guiMonitorCopyImageMenuItem->set_sensitive(true);
@@ -844,8 +846,8 @@ void GuiMonitor::check_status()
     end.backward_line();
     guiMonitorLogTextView->scroll_to(
                    guiMonitorLogTextBuffer->create_mark(end));
-    g.slog->str("");
-    g.slog->flush();
+    ctx->slog->str("");
+    ctx->slog->flush();
   }
 }
 
@@ -918,7 +920,7 @@ void GuiMonitor::handle_send_command_response(int response)
     return;
   }
 
-  run_command(command_str.c_str());
+  run_command(ctx, command_str.c_str());
 
   send_command_success = true;
 }
@@ -1053,11 +1055,11 @@ void GuiMonitor::osdc_cluster_zoom_in(const Gtk::TreeModel::Path& path)
   int end_range = row[icon_columns.end_range];
 
   if (end_range == begin_range) {
-    if (begin_range >= g.osdmap.get_max_osd()) {
+    if (begin_range >= ctx->osdmap.get_max_osd()) {
       dialog_error("OSD map has changed and the node no longer exists.",
                    Gtk::MESSAGE_ERROR);
 
-      view_osd_nodes(0, g.osdmap.get_max_osd());
+      view_osd_nodes(0, ctx->osdmap.get_max_osd());
     }
     else {
       open_stats(OSD_NODE, false, begin_range);
@@ -1104,7 +1106,7 @@ void GuiMonitor::osdc_cluster_back()
 // Displays all OSDs, with recent updates.
 void GuiMonitor::osdc_cluster_view_all()
 {
-  view_osd_nodes(0, g.osdmap.get_max_osd());
+  view_osd_nodes(0, ctx->osdmap.get_max_osd());
 }
 
 // Allows user to obtain the statistics of the OSD cluster.  Called by
@@ -1143,7 +1145,7 @@ void GuiMonitor::mds_cluster_zoom_in(const Gtk::TreeModel::Path& path)
   }
   else {
     set<int> up_mds;
-    g.mdsmap.get_up_mds_set(up_mds);
+    ctx->mdsmap.get_up_mds_set(up_mds);
 
     if (begin_range >= up_mds.size()) {
       dialog_error("Metadata server map has changed and the node no "
@@ -1216,7 +1218,7 @@ void GuiMonitor::pg_cluster_zoom_in(const Gtk::TreeModel::Path& path)
     view_pg_nodes(begin_range, end_range + 1, false);
   }
   else {
-    if (begin_range >= g.pgmap.pg_stat.size()) {
+    if (begin_range >= ctx->pgmap.pg_stat.size()) {
       dialog_error("Placement group map has changed and the node no "
                    "longer exists.", Gtk::MESSAGE_ERROR);
 
@@ -1279,13 +1281,13 @@ void GuiMonitor::dialog_error(const std::string &msg, Gtk::MessageType type)
 int GuiMonitor::find_mds_id(const std::string &name)
 {
   set<int> upMDSes;
-  g.mdsmap.get_up_mds_set(upMDSes);
+  ctx->mdsmap.get_up_mds_set(upMDSes);
 
   for (set<int>::const_iterator MDSId = upMDSes.begin();
        MDSId != upMDSes.end(); MDSId++)
   {
       const MDSMap::mds_info_t& current_mds_info =
-         g.mdsmap.get_mds_info(*MDSId);
+         ctx->mdsmap.get_mds_info(*MDSId);
       if (current_mds_info.name == name)
        return *MDSId;
   }
@@ -1330,7 +1332,7 @@ void GuiMonitor::handle_view_node_response(int response)
        dialog_error("Node ID must be a number.", Gtk::MESSAGE_ERROR);
        return;
       }
-      unsigned int max_osd = g.osdmap.get_max_osd();
+      unsigned int max_osd = ctx->osdmap.get_max_osd();
       if (id >= max_osd) {
        dialog_error("OSD does not exist.", Gtk::MESSAGE_ERROR);
        return;
@@ -1361,7 +1363,7 @@ void GuiMonitor::handle_view_node_response(int response)
        dialog_error("Node ID must be a number.", Gtk::MESSAGE_ERROR);
        return;
       }
-      if (id >= g.pgmap.pg_stat.size()) {
+      if (id >= ctx->pgmap.pg_stat.size()) {
        dialog_error("PG does not exist.", Gtk::MESSAGE_ERROR);
        return;
       }
@@ -1411,7 +1413,8 @@ GuiMonitor::StatsWindowInfo::StatsWindowInfo(GuiMonitor *gui_,
   : gui(gui_),
     is_cluster(is_cluster_),
     type(type_),
-    id(id_)
+    id(id_),
+    ctx(gui_->ctx)
 {
   builder->get_widget("statsWindow", stats_window);
   builder->get_widget("statsInfoLabel", stats_info_label);
@@ -1475,22 +1478,22 @@ void GuiMonitor::StatsWindowInfo::gen_osd_cluster_columns()
 
   {
     ostringstream oss;
-    oss << g.osdmap.get_epoch();
+    oss << ctx->osdmap.get_epoch();
     insert_stats("Epoch", oss.str());
   }
   {
     ostringstream oss;
-    oss << g.osdmap.get_max_osd();
+    oss << ctx->osdmap.get_max_osd();
     insert_stats("Maximum Amount of OSDs", oss.str());
   }
   {
     ostringstream oss;
-    oss << g.osdmap.get_num_up_osds();
+    oss << ctx->osdmap.get_num_up_osds();
     insert_stats("Amount of Up OSDs", oss.str());
   }
   {
     ostringstream oss;
-    oss << g.osdmap.get_num_in_osds();
+    oss << ctx->osdmap.get_num_in_osds();
     insert_stats("Amount of In OSDs", oss.str());
   }
 }
@@ -1505,17 +1508,17 @@ void GuiMonitor::StatsWindowInfo::gen_mds_cluster_columns()
   set<int> up_mds;
   set<int> stopped_mds;
 
-  g.mdsmap.get_up_mds_set(up_mds);
-  g.mdsmap.get_stopped_mds_set(stopped_mds);
+  ctx->mdsmap.get_up_mds_set(up_mds);
+  ctx->mdsmap.get_stopped_mds_set(stopped_mds);
 
-  insert_stats("Epoch", str(boost::format("%llu") % g.mdsmap.get_epoch()));
+  insert_stats("Epoch", str(boost::format("%llu") % ctx->mdsmap.get_epoch()));
   insert_stats("Maximum Amount of MDSes", str(boost::format("%u") %
-    g.mdsmap.get_max_mds()));
+    ctx->mdsmap.get_max_mds()));
   insert_stats("Amount of Up MDSes", str(boost::format("%d") % up_mds.size()));
   insert_stats("Amount of In MDSes", str(boost::format("%u") %
-    g.mdsmap.get_num_mds()));
+    ctx->mdsmap.get_num_mds()));
   insert_stats("Amount of Failed MDSes", str(boost::format("%d") %
-    g.mdsmap.get_num_failed()));
+    ctx->mdsmap.get_num_failed()));
   insert_stats("Amount of Stopped MDSes", str(boost::format("%d") %
     stopped_mds.size()));
 }
@@ -1529,37 +1532,37 @@ void GuiMonitor::StatsWindowInfo::gen_pg_cluster_columns()
 
   {
     ostringstream oss;
-    oss << g.pgmap.version;
+    oss << ctx->pgmap.version;
     insert_stats("Version", oss.str());
   }
 
   {
     ostringstream oss;
-    oss << g.pgmap.pg_stat.size();
+    oss << ctx->pgmap.pg_stat.size();
     insert_stats("Amount of PGs", oss.str());
   }
 
   {
     ostringstream oss;
-    oss << kb_t(g.pgmap.pg_sum.num_kb);
+    oss << kb_t(ctx->pgmap.pg_sum.num_kb);
     insert_stats("Data ", oss.str());
   }
 
   {
     ostringstream oss;
-    oss << kb_t(g.pgmap.osd_sum.kb_used);
+    oss << kb_t(ctx->pgmap.osd_sum.kb_used);
     insert_stats("Amount of Storage Used", oss.str());
   }
 
   {
     ostringstream oss;
-    oss << kb_t(g.pgmap.osd_sum.kb_avail);
+    oss << kb_t(ctx->pgmap.osd_sum.kb_avail);
     insert_stats("Amount of Storage Available", oss.str());
   }
 
   {
     ostringstream oss;
-    oss << kb_t(g.pgmap.osd_sum.kb);
+    oss << kb_t(ctx->pgmap.osd_sum.kb);
     insert_stats("Total Storage", oss.str());
   }
 }
@@ -1571,36 +1574,36 @@ void GuiMonitor::StatsWindowInfo::gen_monitor_cluster_columns()
   stats_info_label->set_label(label);
   {
     ostringstream oss;
-    oss << g.mc.monmap.size();
+    oss << ctx->mc.monmap.size();
     insert_stats("Amount of Monitors", oss.str());
   }
   {
     ostringstream oss;
-    oss << g.mc.monmap.epoch;
+    oss << ctx->mc.monmap.epoch;
     insert_stats("Epoch", oss.str());
   }
   {
     ostringstream oss;
-    oss << g.mc.monmap.fsid;
+    oss << ctx->mc.monmap.fsid;
     insert_stats("File System ID", oss.str());
   }
   {
     ostringstream oss;
-    oss << g.mc.monmap.last_changed;
+    oss << ctx->mc.monmap.last_changed;
     insert_stats("Last Changed", oss.str());
   }
   {
     ostringstream oss;
-    oss << g.mc.monmap.created;
+    oss << ctx->mc.monmap.created;
     insert_stats("Created", oss.str());
   }
 }
 
 void GuiMonitor::StatsWindowInfo::gen_osd_node_columns()
 {
-  bool isIn = g.osdmap.is_in(id);
-  const entity_addr_t& addr = g.osdmap.get_addr(id);
-  const osd_info_t& osdInfo = g.osdmap.get_info(id);
+  bool isIn = ctx->osdmap.is_in(id);
+  const entity_addr_t& addr = ctx->osdmap.get_addr(id);
+  const osd_info_t& osdInfo = ctx->osdmap.get_info(id);
 
   string label(str(boost::format("OSD %lu Statistics:") % id));
 
@@ -1609,10 +1612,10 @@ void GuiMonitor::StatsWindowInfo::gen_osd_node_columns()
 
   insert_stats("OSD ID", str(boost::format("%lu") % id));
   insert_stats("Address", isIn ? addr_to_str(addr) : "None");
-  insert_stats("Up?", g.osdmap.is_up(id) ? "Up" : "Down");
+  insert_stats("Up?", ctx->osdmap.is_up(id) ? "Up" : "Down");
   insert_stats("In?", isIn ? "In" : "Out");
   insert_stats("Weight", isIn ? str(boost::format("%f") %
-      g.osdmap.get_weightf(id)) : "None");
+      ctx->osdmap.get_weightf(id)) : "None");
   insert_stats("Up From", str(boost::format("%lu") % osdInfo.up_from));
   insert_stats("Up Through", str(boost::format("%lu") % osdInfo.up_thru));
   insert_stats("Down At", str(boost::format("%lu") % osdInfo.down_at));
@@ -1622,7 +1625,7 @@ void GuiMonitor::StatsWindowInfo::gen_osd_node_columns()
 
 void GuiMonitor::StatsWindowInfo::gen_mds_node_columns()
 {
-  const MDSMap::mds_info_t &mdsInfo = g.mdsmap.get_mds_info(id);
+  const MDSMap::mds_info_t &mdsInfo = ctx->mdsmap.get_mds_info(id);
 
   string label(str(boost::format("MDS %s Statistics:") % mdsInfo.name));
 
@@ -1640,8 +1643,8 @@ void GuiMonitor::StatsWindowInfo::gen_pg_node_columns()
 {
   pg_t pg(gui->current_pgs.at(id));
 
-  hash_map<pg_t,pg_stat_t>::const_iterator s = g.pgmap.pg_stat.find(pg);
-  assert(s != g.pgmap.pg_stat.end());
+  hash_map<pg_t,pg_stat_t>::const_iterator s = ctx->pgmap.pg_stat.find(pg);
+  assert(s != ctx->pgmap.pg_stat.end());
 
   const pg_stat_t &stat(s->second);
   std::stringstream converter;
@@ -1744,7 +1747,7 @@ void GuiMonitor::StatsWindowInfo::insert_stats(const std::string &key,
   currentRow[columns.value] = value;
 }
 
-int run_gui(int argc, char **argv)
+int run_gui(CephToolCtx *ctx, int argc, char **argv)
 {
   int ret = EXIT_SUCCESS;
   GuiMonitor *gui = NULL;
@@ -1768,7 +1771,7 @@ int run_gui(int argc, char **argv)
          Gtk::Builder::create_from_file(resource_path(GUI_MONITOR_BUILDER_FILE));
 
     // stores pointers to all GUI elements
-    gui = new GuiMonitor(builder);
+    gui = new GuiMonitor(builder, ctx);
 
     if (!gui->init()) {
       derr << "There was a problem with initializing the GUI." << dendl;
index 1c5aefdfd71d82b3be0a755af47dace1c2435d50..82e8cc018799c240cdbbb304734124ff7097f018 100644 (file)
@@ -143,6 +143,8 @@ private:
       enum NodeType type;
       int id;
 
+      CephToolCtx *ctx;
+
       /*
        * Private Functions
        */
@@ -179,7 +181,7 @@ private:
    friend class StatsWindowInfo;
 
 public:
-  GuiMonitor(Glib::RefPtr<Gtk::Builder> builder);
+  GuiMonitor(Glib::RefPtr<Gtk::Builder> builder, CephToolCtx *ctx_);
   ~GuiMonitor();
 
   bool init();
@@ -404,6 +406,8 @@ private:
   //Glib::RefPtr<Gdk::Pixbuf> stoppedMDSIcon;
 
   NodeIconColumns icon_columns;
+public:
+  CephToolCtx *ctx;
 };
 
 #endif