TProofMgrLite.cxx

Go to the documentation of this file.
00001 // @(#)root/proofx:$Id: TProofMgrLite.cxx 38178 2011-02-21 14:06:44Z ganis $
00002 // Author: Gerardo Ganis Apr 2008
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TProofMgrLite                                                        //
00015 //                                                                      //
00016 // Basic functionality implementtaion in the case of Lite sessions      //
00017 //                                                                      //
00018 //////////////////////////////////////////////////////////////////////////
00019 
00020 #include <errno.h>
00021 #ifdef WIN32
00022 #include <io.h>
00023 #endif
00024 
00025 #include "TProofMgrLite.h"
00026 
00027 #include "Riostream.h"
00028 #include "TEnv.h"
00029 #include "TError.h"
00030 #include "TObjString.h"
00031 #include "TProofLite.h"
00032 #include "TProofLog.h"
00033 #include "TROOT.h"
00034 #include "TRegexp.h"
00035 #include "TSortedList.h"
00036 
00037 ClassImp(TProofMgrLite)
00038 
00039 //______________________________________________________________________________
00040 TProofMgrLite::TProofMgrLite(const char *url, Int_t dbg, const char *alias)
00041           : TProofMgr(url, dbg, alias)
00042 {
00043    // Create a PROOF manager for the Lite environment.
00044 
00045    // Set the correct servert type
00046    fServType = kProofLite;
00047 }
00048 
00049 //______________________________________________________________________________
00050 TProof *TProofMgrLite::CreateSession(const char *cfg,
00051                                      const char *, Int_t loglevel)
00052 {
00053    // Create a new session
00054 
00055    Int_t nwrk = TProofLite::GetNumberOfWorkers(fUrl.GetOptions());
00056    if (nwrk == 0) return (TProof *)0;
00057 
00058    // Check if we have already a running session
00059    if (gProof && gProof->IsLite()) {
00060       if (gProof->IsValid()) {
00061          if (nwrk > 0 && gProof->GetParallel() != nwrk) {
00062             delete gProof;
00063             gProof = 0;
00064          } else {
00065             // We have already a running session
00066             return gProof;
00067          }
00068       } else {
00069          // Remove existing instance
00070          delete gProof;
00071          gProof = 0;
00072       }
00073    }
00074 
00075    // Create the instance
00076    TString u = (strlen(fUrl.GetOptions()) > 0) ? Form("lite/?%s", fUrl.GetOptions())
00077                                                : "lite";
00078    TProof *p = new TProofLite(u, cfg, 0, loglevel, 0, this);
00079 
00080    if (p && p->IsValid()) {
00081 
00082       // Save record about this session
00083       Int_t ns = 1;
00084       if (fSessions) {
00085          // To avoid ambiguities in case of removal of some elements
00086          if (fSessions->Last())
00087             ns = ((TProofDesc *)(fSessions->Last()))->GetLocalId() + 1;
00088       } else {
00089          // Create the list
00090          fSessions = new TList;
00091       }
00092 
00093       // Create the description class
00094       Int_t st = (p->IsIdle()) ? TProofDesc::kIdle : TProofDesc::kRunning ;
00095       TProofDesc *d =
00096          new TProofDesc(p->GetName(), p->GetTitle(), p->GetUrl(),
00097                                ns, p->GetSessionID(), st, p);
00098       fSessions->Add(d);
00099 
00100    } else {
00101       // Session creation failed
00102       Error("CreateSession", "creating PROOF session");
00103       SafeDelete(p);
00104    }
00105 
00106    // We are done
00107    return p;
00108 }
00109 
00110 //_____________________________________________________________________________
00111 TProofLog *TProofMgrLite::GetSessionLogs(Int_t isess,
00112                                          const char *stag, const char *pattern)
00113 {
00114    // Get logs or log tails from last session associated with this manager
00115    // instance.
00116    // The arguments allow to specify a session different from the last one:
00117    //      isess   specifies a position relative to the last one, i.e. 1
00118    //              for the next to last session; the absolute value is taken
00119    //              so -1 and 1 are equivalent.
00120    //      stag    specifies the unique tag of the wanted session
00121    // The special value stag = "NR" allows to just initialize the TProofLog
00122    // object w/o retrieving the files; this may be useful when the number
00123    // of workers is large and only a subset of logs is required.
00124    // If 'stag' is specified 'isess' is ignored (unless stag = "NR").
00125    // If 'pattern' is specified only the lines containing it are retrieved
00126    // (remote grep functionality); to filter out a pattern 'pat' use
00127    // pattern = "-v pat".
00128    // Returns a TProofLog object (to be deleted by the caller) on success,
00129    // 0 if something wrong happened.
00130 
00131    TProofLog *pl = 0;
00132 
00133    // The absolute value of isess counts
00134    isess = (isess < 0) ? -isess : isess;
00135 
00136    // Special option in stag
00137    bool retrieve = 1;
00138    TString tag(stag);
00139    if (tag == "NR") {
00140       retrieve = 0;
00141       tag = "";
00142    }
00143 
00144    // The working dir
00145    TString sandbox(gSystem->WorkingDirectory());
00146    sandbox.ReplaceAll(gSystem->HomeDirectory(),"");
00147    sandbox.ReplaceAll("/","-");
00148    sandbox.Replace(0,1,"/",1);
00149    if (strlen(gEnv->GetValue("ProofLite.Sandbox", "")) > 0) {
00150       sandbox.Insert(0, gEnv->GetValue("ProofLite.Sandbox", ""));
00151    } else if (strlen(gEnv->GetValue("Proof.Sandbox", "")) > 0) {
00152       sandbox.Insert(0, gEnv->GetValue("Proof.Sandbox", ""));
00153    } else {
00154       TString sb;
00155       sb.Form("~/%s", kPROOF_WorkDir);
00156       sandbox.Insert(0, sb.Data());
00157    }
00158    gSystem->ExpandPathName(sandbox);
00159 
00160    TString sessiondir;
00161    if (tag.Length() > 0) {
00162       sessiondir.Form("%s/session-%s", sandbox.Data(), tag.Data());
00163       if (gSystem->AccessPathName(sessiondir, kReadPermission)) {
00164          Error("GetSessionLogs", "information for session '%s' not available", tag.Data());
00165          return (TProofLog *)0;
00166       }
00167    } else {
00168       // Get the list of available dirs
00169       TSortedList *olddirs = new TSortedList(kFALSE);
00170       void *dirp = gSystem->OpenDirectory(sandbox);
00171       if (dirp) {
00172          const char *e = 0;
00173          while ((e = gSystem->GetDirEntry(dirp))) {
00174             if (!strncmp(e, "session-", 8)) {
00175                TString d(e);
00176                Int_t i = d.Last('-');
00177                if (i != kNPOS) d.Remove(i);
00178                i = d.Last('-');
00179                if (i != kNPOS) d.Remove(0,i+1);
00180                TString path = Form("%s/%s", sandbox.Data(), e);
00181                olddirs->Add(new TNamed(d, path));
00182             }
00183          }
00184          gSystem->FreeDirectory(dirp);
00185       }
00186 
00187       // Check isess
00188       if (isess > olddirs->GetSize() - 1) {
00189          Warning("GetSessionLogs",
00190                  "session index out of range (%d): take oldest available session", isess);
00191          isess = olddirs->GetSize() - 1;
00192       }
00193 
00194       // Locate the session dir
00195       Int_t isx = isess;
00196       TNamed *n = (TNamed *) olddirs->First();
00197       while (isx-- > 0) {
00198          olddirs->Remove(n);
00199          delete n;
00200          n = (TNamed *) olddirs->First();
00201       }
00202       if (!n) {
00203          Error("GetSessionLogs", "cannot locate session dir for index '%d' under '%s':"
00204                                  " cannot continue!", isess, sandbox.Data());
00205          return (TProofLog *)0;
00206       }
00207       sessiondir = n->GetTitle();
00208       tag = gSystem->BaseName(sessiondir);
00209       tag.ReplaceAll("session-", "");
00210 
00211       // Cleanup
00212       olddirs->SetOwner();
00213       delete olddirs;
00214    }
00215    Info("GetSessionLogs", "analysing session dir %s", sessiondir.Data());
00216 
00217    // Create the instance now
00218    pl = new TProofLog(tag, "", this);
00219 
00220    void *dirp = gSystem->OpenDirectory(sessiondir);
00221    if (dirp) {
00222       TSortedList *logs = new TSortedList;
00223       const char *e = 0;
00224       while ((e = gSystem->GetDirEntry(dirp))) {
00225          TString fn(e);
00226          if (fn.EndsWith(".log") && fn.CountChar('-') > 0) {
00227             TString ord, url;
00228             if (fn.BeginsWith("session-")) {
00229                ord = "-1";
00230             } else if (fn.BeginsWith("worker-")) {
00231                ord = fn;
00232                ord.ReplaceAll("worker-", "");
00233                Int_t id = ord.First('-');
00234                if (id != kNPOS) {
00235                   ord.Remove(id);
00236                } else if (ord.Contains(".valgrind")) {
00237                   // Add to the list (special tag for valgrind outputs)
00238                   ord.ReplaceAll(".valgrind.log","-valgrind");
00239                } else {
00240                   // Not a good path
00241                   ord = "";
00242                }
00243                if (!ord.IsNull()) ord.ReplaceAll("0.", "");
00244             }
00245             if (!ord.IsNull()) {
00246                url = Form("%s/%s", sessiondir.Data(), e);
00247                // Add to the list
00248                logs->Add(new TNamed(ord, url));
00249                // Notify
00250                if (gDebug > 1)
00251                   Info("GetSessionLogs", "ord: %s, url: %s", ord.Data(), url.Data());
00252             }
00253          }
00254       }
00255       gSystem->FreeDirectory(dirp);
00256 
00257       TIter nxl(logs);
00258       TNamed *n = 0;
00259       while ((n = (TNamed *) nxl())) {
00260          TString ord = Form("0.%s", n->GetName());
00261          if (ord == "0.-1") ord = "0";
00262          // Add to the list
00263          pl->Add(ord, n->GetTitle());
00264       }
00265 
00266       // Cleanup
00267       logs->SetOwner();
00268       delete logs;
00269    }
00270 
00271    // Retrieve the default part
00272    if (pl && retrieve) {
00273       if (pattern && strlen(pattern) > 0)
00274          pl->Retrieve("*", TProofLog::kGrep, 0, pattern);
00275       else
00276          pl->Retrieve();
00277    }
00278 
00279    // Done
00280    return pl;
00281 }
00282 
00283 //______________________________________________________________________________
00284 TObjString *TProofMgrLite::ReadBuffer(const char *fin, Long64_t ofs, Int_t len)
00285 {
00286    // Read 'len' bytes from offset 'ofs' of the local file 'fin'.
00287    // Returns a TObjString with the content or 0, in case of failure
00288 
00289    if (!fin || strlen(fin) <= 0) {
00290       Error("ReadBuffer", "undefined path!");
00291       return (TObjString *)0;
00292    }
00293 
00294    // Open the file
00295    TString fn = TUrl(fin).GetFile();
00296    Int_t fd = open(fn.Data(), O_RDONLY);
00297    if (fd < 0) {
00298       Error("ReadBuffer", "problems opening file %s", fn.Data());
00299       return (TObjString *)0;
00300    }
00301 
00302    // Total size
00303    off_t start = 0, end = lseek(fd, (off_t) 0, SEEK_END);
00304 
00305    // Set the offset
00306    if (ofs > 0 && ofs < end) {
00307       start = lseek(fd, (off_t) ofs, SEEK_SET);
00308    } else {
00309       start = lseek(fd, (off_t) 0, SEEK_SET);
00310    }
00311    if (len > (end - start + 1) || len <= 0)
00312       len = end - start + 1;
00313 
00314    TString outbuf;
00315    const Int_t kMAXBUF = 32768;
00316    char buf[kMAXBUF];
00317    Int_t left = len;
00318    Int_t wanted = (left > kMAXBUF - 1) ? kMAXBUF - 1 : left;
00319    do {
00320       while ((len = read(fd, buf, wanted)) < 0 && TSystem::GetErrno() == EINTR)
00321          TSystem::ResetErrno();
00322 
00323       if (len < 0) {
00324          Error("ReadBuffer", "error reading file %s", fn.Data());
00325          close(fd);
00326          return (TObjString *)0;
00327       } else if (len > 0) {
00328          if (len == wanted)
00329             buf[len-1] = '\n';
00330          buf[len] = '\0';
00331          outbuf += buf;
00332       }
00333 
00334       // Update counters
00335       left -= len;
00336       wanted = (left > kMAXBUF - 1) ? kMAXBUF - 1 : left;
00337 
00338    } while (len > 0 && left > 0);
00339 
00340    // Done
00341    return new TObjString(outbuf.Data());
00342 }
00343 
00344 //______________________________________________________________________________
00345 TObjString *TProofMgrLite::ReadBuffer(const char *fin, const char *pattern)
00346 {
00347    // Read lines containing 'pattern' in 'file'.
00348    // Returns a TObjString with the content or 0, in case of failure
00349 
00350    // If no pattern, read everything
00351    if (!pattern || strlen(pattern) <= 0)
00352       return (TObjString *)0;
00353 
00354    if (!fin || strlen(fin) <= 0) {
00355       Error("ReadBuffer", "undefined path!");
00356       return (TObjString *)0;
00357    }
00358    TString fn = TUrl(fin).GetFile();
00359 
00360    TString pat(pattern);
00361    // Check if "-v"
00362    Bool_t excl = kFALSE;
00363    if (pat.Contains("-v ")) {
00364       pat.ReplaceAll("-v ", "");
00365       excl = kTRUE;
00366    }
00367    pat = pat.Strip(TString::kLeading, ' ');
00368    pat = pat.Strip(TString::kTrailing, ' ');
00369    pat = pat.Strip(TString::kLeading, '\"');
00370    pat = pat.Strip(TString::kTrailing, '\"');
00371 
00372    // Use a regular expression
00373    TRegexp re(pat);
00374 
00375    // Open file with file info
00376    ifstream in;
00377    in.open(fn.Data());
00378 
00379    TString outbuf;
00380 
00381    // Read the input list of files and add them to the chain
00382    TString line;
00383    while(in.good()) {
00384 
00385       // Read next line
00386       line.ReadLine(in);
00387 
00388       // Keep only lines with pattern
00389       if ((excl && line.Index(re) != kNPOS) ||
00390           (!excl && line.Index(re) == kNPOS)) continue;
00391 
00392       // Remove trailing '\n', if any
00393       if (!line.EndsWith("\n")) line.Append('\n');
00394 
00395       // Add to output
00396       outbuf += line;
00397    }
00398    in.close();
00399 
00400    // Done
00401    return new TObjString(outbuf.Data());
00402 }

Generated on Tue Jul 5 14:51:39 2011 for ROOT_528-00b_version by  doxygen 1.5.1