Logo ROOT   6.12/07
Reference Guide
TProofMgrLite.cxx
Go to the documentation of this file.
1 // @(#)root/proofx:$Id$
2 // Author: Gerardo Ganis Apr 2008
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TProofMgrLite
13 \ingroup proofkernel
14 
15 Basic TProofMgr functionality implementation in the case of Lite session.
16 
17 */
18 
19 #include <errno.h>
20 #ifdef WIN32
21 #include <io.h>
22 #endif
23 
24 #include "TProofMgrLite.h"
25 
26 #include "Riostream.h"
27 #include "TEnv.h"
28 #include "TError.h"
29 #include "TObjString.h"
30 #include "TProofLite.h"
31 #include "TProofLog.h"
32 #include "TROOT.h"
33 #include "TRegexp.h"
34 #include "TSortedList.h"
35 
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Create a PROOF manager for the Lite environment.
40 
41 TProofMgrLite::TProofMgrLite(const char *url, Int_t dbg, const char *alias)
42  : TProofMgr(url, dbg, alias)
43 {
44  // Set the correct servert type
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Create a new session
50 
52  const char *, Int_t loglevel)
53 {
54  TString c(fUrl.GetOptions());
55  if (!c.Contains("workers=") && cfg && strstr(cfg, "workers=")) c = cfg;
57  if (nwrk == 0) return (TProof *)0;
58 
59  // Check if we have already a running session
60  if (gProof && gProof->IsLite()) {
61  if (gProof->IsValid()) {
62  if (nwrk > 0 && gProof->GetParallel() != nwrk) {
63  delete gProof;
64  gProof = 0;
65  } else {
66  // We have already a running session
67  return gProof;
68  }
69  } else {
70  // Remove existing instance
71  delete gProof;
72  gProof = 0;
73  }
74  }
75 
76  // Create the instance
77  TString u("lite");
78  if (strlen(fUrl.GetOptions()) > 0) u.Form("lite/?%s", fUrl.GetOptions());
79  TProof *p = new TProofLite(u, cfg, 0, loglevel, 0, this);
80 
81  if (p && p->IsValid()) {
82 
83  // Save record about this session
84  Int_t ns = 1;
85  if (fSessions) {
86  // To avoid ambiguities in case of removal of some elements
87  if (fSessions->Last())
88  ns = ((TProofDesc *)(fSessions->Last()))->GetLocalId() + 1;
89  } else {
90  // Create the list
91  fSessions = new TList;
92  }
93 
94  // Create the description class
96  TProofDesc *d =
97  new TProofDesc(p->GetName(), p->GetTitle(), p->GetUrl(),
98  ns, p->GetSessionID(), st, p);
99  fSessions->Add(d);
100 
101  } else {
102  // Session creation failed
103  Error("CreateSession", "creating PROOF session");
104  SafeDelete(p);
105  }
106 
107  // We are done
108  return p;
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// Get logs or log tails from last session associated with this manager
113 /// instance.
114 /// The arguments allow to specify a session different from the last one:
115 /// isess specifies a position relative to the last one, i.e. 1
116 /// for the next to last session; the absolute value is taken
117 /// so -1 and 1 are equivalent.
118 /// stag specifies the unique tag of the wanted session
119 /// The special value stag = "NR" allows to just initialize the TProofLog
120 /// object w/o retrieving the files; this may be useful when the number
121 /// of workers is large and only a subset of logs is required.
122 /// If 'stag' is specified 'isess' is ignored (unless stag = "NR").
123 /// If 'pattern' is specified only the lines containing it are retrieved
124 /// (remote grep functionality); to filter out a pattern 'pat' use
125 /// pattern = "-v pat".
126 /// Returns a TProofLog object (to be deleted by the caller) on success,
127 /// 0 if something wrong happened.
128 
130  const char *pattern, Bool_t)
131 {
132  TProofLog *pl = 0;
133 
134  // The absolute value of isess counts
135  isess = (isess < 0) ? -isess : isess;
136 
137  // Special option in stag
138  bool retrieve = 1;
139  TString tag(stag);
140  if (tag == "NR") {
141  retrieve = 0;
142  tag = "";
143  }
144 
145  // The working dir
146  TString sandbox(gSystem->WorkingDirectory());
147  sandbox.ReplaceAll(gSystem->HomeDirectory(),"");
148  sandbox.ReplaceAll("/","-");
149  sandbox.Replace(0,1,"/",1);
150  if (strlen(gEnv->GetValue("ProofLite.Sandbox", "")) > 0) {
151  sandbox.Insert(0, gEnv->GetValue("ProofLite.Sandbox", ""));
152  } else if (strlen(gEnv->GetValue("Proof.Sandbox", "")) > 0) {
153  sandbox.Insert(0, gEnv->GetValue("Proof.Sandbox", ""));
154  } else {
155  TString sb;
156  sb.Form("~/%s", kPROOF_WorkDir);
157  sandbox.Insert(0, sb.Data());
158  }
159  gSystem->ExpandPathName(sandbox);
160 
161  TString sessiondir;
162  if (tag.Length() > 0) {
163  sessiondir.Form("%s/session-%s", sandbox.Data(), tag.Data());
164  if (gSystem->AccessPathName(sessiondir, kReadPermission)) {
165  Error("GetSessionLogs", "information for session '%s' not available", tag.Data());
166  return (TProofLog *)0;
167  }
168  } else {
169  // Get the list of available dirs
170  TSortedList *olddirs = new TSortedList(kFALSE);
171  void *dirp = gSystem->OpenDirectory(sandbox);
172  if (dirp) {
173  const char *e = 0;
174  while ((e = gSystem->GetDirEntry(dirp))) {
175  if (!strncmp(e, "session-", 8)) {
176  TString d(e);
177  Int_t i = d.Last('-');
178  if (i != kNPOS) d.Remove(i);
179  i = d.Last('-');
180  if (i != kNPOS) d.Remove(0,i+1);
181  TString path = Form("%s/%s", sandbox.Data(), e);
182  olddirs->Add(new TNamed(d, path));
183  }
184  }
185  gSystem->FreeDirectory(dirp);
186  }
187 
188  // Check isess
189  if (isess > olddirs->GetSize() - 1) {
190  Warning("GetSessionLogs",
191  "session index out of range (%d): take oldest available session", isess);
192  isess = olddirs->GetSize() - 1;
193  }
194 
195  // Locate the session dir
196  Int_t isx = isess;
197  TNamed *n = (TNamed *) olddirs->First();
198  while (isx-- > 0) {
199  olddirs->Remove(n);
200  delete n;
201  n = (TNamed *) olddirs->First();
202  }
203  if (!n) {
204  Error("GetSessionLogs", "cannot locate session dir for index '%d' under '%s':"
205  " cannot continue!", isess, sandbox.Data());
206  return (TProofLog *)0;
207  }
208  sessiondir = n->GetTitle();
209  tag = gSystem->BaseName(sessiondir);
210  tag.ReplaceAll("session-", "");
211 
212  // Cleanup
213  olddirs->SetOwner();
214  delete olddirs;
215  }
216  Info("GetSessionLogs", "analysing session dir %s", sessiondir.Data());
217 
218  // Create the instance now
219  pl = new TProofLog(tag, "", this);
220 
221  void *dirp = gSystem->OpenDirectory(sessiondir);
222  if (dirp) {
223  TSortedList *logs = new TSortedList;
224  const char *e = 0;
225  while ((e = gSystem->GetDirEntry(dirp))) {
226  TString fn(e);
227  if (fn.EndsWith(".log") && fn.CountChar('-') > 0) {
228  TString ord, url;
229  if (fn.BeginsWith("session-")) {
230  ord = "-1";
231  } else if (fn.BeginsWith("worker-")) {
232  ord = fn;
233  ord.ReplaceAll("worker-", "");
234  Int_t id = ord.First('-');
235  if (id != kNPOS) {
236  ord.Remove(id);
237  } else if (ord.Contains(".valgrind")) {
238  // Add to the list (special tag for valgrind outputs)
239  ord.ReplaceAll(".valgrind.log","-valgrind");
240  } else {
241  // Not a good path
242  ord = "";
243  }
244  if (!ord.IsNull()) ord.ReplaceAll("0.", "");
245  }
246  if (!ord.IsNull()) {
247  url = Form("%s/%s", sessiondir.Data(), e);
248  // Add to the list
249  logs->Add(new TNamed(ord, url));
250  // Notify
251  if (gDebug > 1)
252  Info("GetSessionLogs", "ord: %s, url: %s", ord.Data(), url.Data());
253  }
254  }
255  }
256  gSystem->FreeDirectory(dirp);
257 
258  TIter nxl(logs);
259  TNamed *n = 0;
260  while ((n = (TNamed *) nxl())) {
261  TString ord = Form("0.%s", n->GetName());
262  if (ord == "0.-1") ord = "0";
263  // Add to the list
264  pl->Add(ord, n->GetTitle());
265  }
266 
267  // Cleanup
268  logs->SetOwner();
269  delete logs;
270  }
271 
272  // Retrieve the default part
273  if (pl && retrieve) {
274  const char *pat = pattern ? pattern : "-v \"| SvcMsg\"";
275  if (pat && strlen(pat) > 0)
276  pl->Retrieve("*", TProofLog::kGrep, 0, pat);
277  else
278  pl->Retrieve();
279  }
280 
281  // Done
282  return pl;
283 }
284 
285 ////////////////////////////////////////////////////////////////////////////////
286 /// Read 'len' bytes from offset 'ofs' of the local file 'fin'.
287 /// Returns a TObjString with the content or 0, in case of failure
288 
290 {
291  if (!fin || strlen(fin) <= 0) {
292  Error("ReadBuffer", "undefined path!");
293  return (TObjString *)0;
294  }
295 
296  // Open the file
297  TString fn = TUrl(fin).GetFile();
298  Int_t fd = open(fn.Data(), O_RDONLY);
299  if (fd < 0) {
300  Error("ReadBuffer", "problems opening file %s", fn.Data());
301  return (TObjString *)0;
302  }
303 
304  // Total size
305  off_t start = 0, end = lseek(fd, (off_t) 0, SEEK_END);
306 
307  // Set the offset
308  if (ofs > 0 && ofs < end) {
309  start = lseek(fd, (off_t) ofs, SEEK_SET);
310  } else {
311  start = lseek(fd, (off_t) 0, SEEK_SET);
312  }
313  if (len > (end - start + 1) || len <= 0)
314  len = end - start + 1;
315 
316  TString outbuf;
317  const Int_t kMAXBUF = 32768;
318  char buf[kMAXBUF];
319  Int_t left = len;
320  Int_t wanted = (left > kMAXBUF - 1) ? kMAXBUF - 1 : left;
321  do {
322  while ((len = read(fd, buf, wanted)) < 0 && TSystem::GetErrno() == EINTR)
324 
325  if (len < 0) {
326  Error("ReadBuffer", "error reading file %s", fn.Data());
327  close(fd);
328  return (TObjString *)0;
329  } else if (len > 0) {
330  if (len == wanted)
331  buf[len-1] = '\n';
332  buf[len] = '\0';
333  outbuf += buf;
334  }
335 
336  // Update counters
337  left -= len;
338  wanted = (left > kMAXBUF - 1) ? kMAXBUF - 1 : left;
339 
340  } while (len > 0 && left > 0);
341 
342  // Close file
343  close(fd);
344 
345  // Done
346  return new TObjString(outbuf.Data());
347 }
348 
349 ////////////////////////////////////////////////////////////////////////////////
350 /// Read lines containing 'pattern' in 'file'.
351 /// Returns a TObjString with the content or 0, in case of failure
352 
353 TObjString *TProofMgrLite::ReadBuffer(const char *fin, const char *pattern)
354 {
355  // If no pattern, read everything
356  if (!pattern || strlen(pattern) <= 0)
357  return (TObjString *)0;
358 
359  if (!fin || strlen(fin) <= 0) {
360  Error("ReadBuffer", "undefined path!");
361  return (TObjString *)0;
362  }
363  TString fn = TUrl(fin).GetFile();
364 
365  TString pat(pattern);
366  // Check if "-v"
367  Bool_t excl = kFALSE;
368  if (pat.Contains("-v ")) {
369  pat.ReplaceAll("-v ", "");
370  excl = kTRUE;
371  }
372  pat = pat.Strip(TString::kLeading, ' ');
373  pat = pat.Strip(TString::kTrailing, ' ');
374  pat = pat.Strip(TString::kLeading, '\"');
375  pat = pat.Strip(TString::kTrailing, '\"');
376 
377  // Use a regular expression
378  TRegexp re(pat);
379 
380  // Open file with file info
381  std::ifstream in;
382  in.open(fn.Data());
383 
384  TString outbuf;
385 
386  // Read the input list of files and add them to the chain
387  TString line;
388  while(in.good()) {
389 
390  // Read next line
391  line.ReadLine(in);
392 
393  // Keep only lines with pattern
394  if ((excl && line.Index(re) != kNPOS) ||
395  (!excl && line.Index(re) == kNPOS)) continue;
396 
397  // Remove trailing '\n', if any
398  if (!line.EndsWith("\n")) line.Append('\n');
399 
400  // Add to output
401  outbuf += line;
402  }
403  in.close();
404 
405  // Done
406  return new TObjString(outbuf.Data());
407 }
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:932
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
This class starts a PROOF session on the local machine: no daemons, client and master merged...
Definition: TProofLite.h:40
TList * fSessions
Definition: TProofMgr.h:61
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:854
long long Long64_t
Definition: RtypesCore.h:69
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:869
TLine * line
Collectable string class.
Definition: TObjString.h:28
const Ssiz_t kNPOS
Definition: RtypesCore.h:111
This class represents a WWW compatible URL.
Definition: TUrl.h:35
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:638
std::istream & ReadLine(std::istream &str, Bool_t skipWhite=kTRUE)
Read a line from stream upto newline skipping any whitespace.
Definition: Stringio.cxx:65
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
The PROOF manager interacts with the PROOF server coordinator to create or destroy a PROOF session...
Definition: TProofMgr.h:43
const char *const kPROOF_WorkDir
Definition: TProof.h:124
virtual TObject * Last() const
Return the last object in the list. Returns 0 when list is empty.
Definition: TList.cxx:689
virtual const char * HomeDirectory(const char *userName=0)
Return the user&#39;s home directory.
Definition: TSystem.cxx:885
Regular expression class.
Definition: TRegexp.h:31
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:585
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const char * GetOptions() const
Definition: TUrl.h:74
static void retrieve(const gsl_integration_workspace *workspace, double *a, double *b, double *r, double *e)
TString & Insert(Ssiz_t pos, const char *s)
Definition: TString.h:595
Bool_t IsLite() const
Definition: TProof.h:933
Int_t GetSessionID() const
Definition: TProof.h:919
TString & Replace(Ssiz_t pos, Ssiz_t n, const char *s)
Definition: TString.h:628
static Int_t GetErrno()
Static function returning system error number.
Definition: TSystem.cxx:268
TProofMgrLite(const char *url, Int_t loglevel=-1, const char *alias="")
Create a PROOF manager for the Lite environment.
const char * GetFile() const
Definition: TUrl.h:72
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:851
#define SafeDelete(p)
Definition: RConfig.h:509
Basic TProofMgr functionality implementation in the case of Lite session.
Definition: TProofMgrLite.h:27
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
A sorted doubly linked list.
Definition: TSortedList.h:28
TString & Append(const char *cs)
Definition: TString.h:495
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2231
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition: TString.cxx:477
EServType fServType
Definition: TProofMgr.h:60
A doubly linked list.
Definition: TList.h:44
TObjString * ReadBuffer(const char *file, Long64_t ofs, Int_t len)
Read &#39;len&#39; bytes from offset &#39;ofs&#39; of the local file &#39;fin&#39;.
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:655
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:818
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:561
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2343
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:386
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1080
const char * GetUrl()
Definition: TProof.h:911
static Int_t GetNumberOfWorkers(const char *url=0)
Static method to determine the number of workers giving priority to users request.
Definition: TProofLite.cxx:406
Bool_t IsIdle() const
Definition: TProof.h:940
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:843
TProofLog * GetSessionLogs(Int_t ridx=0, const char *stag=0, const char *pattern="-v | SvcMsg", Bool_t rescan=kFALSE)
Get logs or log tails from last session associated with this manager instance.
const Bool_t kFALSE
Definition: RtypesCore.h:88
TString & Remove(Ssiz_t pos)
Definition: TString.h:619
R__EXTERN TProof * gProof
Definition: TProof.h:1077
TUrl fUrl
Definition: TProofMgr.h:62
#define ClassImp(name)
Definition: Rtypes.h:359
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:875
Int_t GetParallel() const
Returns number of slaves active in parallel mode.
Definition: TProof.cxx:2282
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
TProof * CreateSession(const char *=0, const char *=0, Int_t=-1)
Create a new session.
TNamed()
Definition: TNamed.h:36
This class controls a Parallel ROOT Facility, PROOF, cluster.
Definition: TProof.h:316
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:570
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
Bool_t IsNull() const
Definition: TString.h:383
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:834
Int_t CountChar(Int_t c) const
Return number of times character c occurs in the string.
Definition: TString.cxx:454
R__EXTERN Int_t gDebug
Definition: Rtypes.h:86
static void ResetErrno()
Static function resetting system error number.
Definition: TSystem.cxx:284
Bool_t IsValid() const
Definition: TProof.h:937
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition: TSystem.cxx:1254
void Add(TObject *obj)
Add object in sorted list.
Definition: TSortedList.cxx:27
virtual Int_t GetSize() const
Definition: TCollection.h:180
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition: TEnv.cxx:491
const Bool_t kTRUE
Definition: RtypesCore.h:87
static constexpr double ns
const Int_t n
Definition: legend1.C:16
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:866
Implementation of the PROOF session log handler.
Definition: TProofLog.h:32
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:345