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