Logo ROOT   6.12/07
Reference Guide
TSelHandleDataSet.cxx
Go to the documentation of this file.
1 // @(#)root/proof:$Id$
2 // Author: Sangsu Ryu 22/06/2010
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 TSelHandleDataSet
13 \ingroup proofbench
14 
15 PROOF selector for file cache release.
16 List of files to be cleaned for each node is provided by client.
17 
18 */
19 
20 #define TSelHandleDataSet_cxx
21 
22 #include "TSelHandleDataSet.h"
23 
24 #include "TDSet.h"
25 #include "TEnv.h"
26 #include "TFile.h"
27 #include "TMap.h"
28 #include "TParameter.h"
29 #include "TProofBenchTypes.h"
30 #include "TSystem.h"
31 #include "TUrl.h"
32 #include "errno.h"
33 #include <unistd.h>
34 #include <fcntl.h>
35 
36 
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Init the type from the input parameters
41 
43 {
44 
45  TObject *o = fInput->FindObject("PROOF_Benchmark_HandleDSType");
46  if (o) fType = dynamic_cast<TPBHandleDSType *>(o);
47 
48  TNamed *n = dynamic_cast<TNamed *>(fInput->FindObject("PROOF_Benchmark_DestDir"));
49  if (n) {
50  fDestDir = n->GetTitle();
52  // Create the directory
53  if (gSystem->mkdir(fDestDir, kTRUE) != 0) {
54  fDestDir = "";
55  Error("SlaveBegin", "could not create dir '%s'", fDestDir.Data());
56  } else {
58  fDestDir = "";
59  Error("SlaveBegin", "dir '%s' is not writable by this process", fDestDir.Data());
60  } else {
61  Info("SlaveBegin", "dir '%s' successfully created", fDestDir.Data());
62  }
63  }
64  }
65  }
66 
67  // Use default if nothing found in the input list
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Release the memory cache associated with file 'fn'.
73 
74 void TSelHandleDataSet::ReleaseCache(const char *fn)
75 {
76 #if defined(R__LINUX)
77  TString filename(fn);
78  Int_t fd;
79  fd = open(filename.Data(), O_RDONLY);
80  if (fd > -1) {
81  fdatasync(fd);
82  posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
83  close(fd);
84  Info("ReleaseCache", "file cache for file '%s' cleaned ...", filename.Data());
85  } else {
86  Error("ReleaseCache", "cannot open file '%s' for cache clean up; errno=%d",
87  filename.Data(), errno);
88  }
89 #else
90  Info("ReleaseCache", "dummy function: file '%s' untouched ...", fn);
91 #endif
92  // Done
93  return;
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Check the memory cache associated with file 'fn'.
98 
99 void TSelHandleDataSet::CheckCache(const char * /*fn*/)
100 {
101  Warning("CheckCache", "cache checker not implemented yet");
102  // Done
103  return;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Physically remove the file 'fn'.
108 
109 void TSelHandleDataSet::RemoveFile(const char *fn)
110 {
112  if (gSystem->Unlink(fn) != 0) {
113  Error("RemoveFile", "problems removing file '%s' ...", fn);
114  } else {
115  if (!gSystem->AccessPathName(fn))
116  Warning("RemoveFile", "'unlink' returned success but the file still exists ('%s')", fn);
117  }
118  } else {
119  if (!gSystem->AccessPathName(fn)) {
120  Error("RemoveFile", "file '%s' cannot removed by this process", fn);
121  } else {
122  Error("RemoveFile", "file '%s' does not exists", fn);
123  }
124  }
125  // Done
126  return;
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// Copy file 'fn' to fDestDir
131 
132 void TSelHandleDataSet::CopyFile(const char *fn)
133 {
134  // Check if we have a destination dir
135  if (fDestDir.IsNull()) {
136  Error("CopyFile", "destination dir undefined: file '%s' not copied", fn);
137  return;
138  }
139 
140  TString basefn = gSystem->BaseName(TUrl(fn, kTRUE).GetFile());
141  TString dst = TString::Format("%s/%s", fDestDir.Data(), basefn.Data());
142  if (!TFile::Cp(fn, dst.Data())) {
143  Error("CopyFile", "problems copying file '%s' to '%s'", fn, dst.Data());
144  return;
145  }
146  Info("CopyFile", "file '%s' created ...", dst.Data());
147 
148  // Done
149  return;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// The Process() function is called for each entry in the tree (or possibly
154 /// keyed object in the case of PROOF) to be processed. The entry argument
155 /// specifies which entry in the currently loaded tree is to be processed.
156 /// It can be passed to either TTree::GetEntry() or TBranch::GetEntry()
157 /// to read either all or the required parts of the data. When processing
158 /// keyed objects with PROOF, the object is already loaded and is available
159 /// via the fObject pointer.
160 ///
161 /// This function should contain the "body" of the analysis. It can contain
162 /// simple or elaborate selection criteria, run algorithms on the data
163 /// of the event and typically fill histograms.
164 
166 {
167  // WARNING when a selector is used with a TChain, you must use
168  // the pointer to the current TTree to call GetEntry(entry).
169  // The entry is always the local entry number in the current tree.
170  // Assuming that fChain is the pointer to the TChain being processed,
171  // use fChain->GetTree()->GetEntry(entry).
172 
173  TDSetElement *fCurrent = 0;
174  TPair *elemPair = 0;
175  if (fInput && (elemPair = dynamic_cast<TPair *>
176  (fInput->FindObject("PROOF_CurrentElement")))) {
177  if ((fCurrent = dynamic_cast<TDSetElement *>(elemPair->Value()))) {
178  Info("Process", "entry %lld: file: '%s'", entry, fCurrent->GetName());
179  } else {
180  Error("Process", "entry %lld: no file specified!", entry);
181  return kFALSE;
182  }
183  }
184 
185  // Resolve the file type; this also adjusts names for Xrd based systems
186  TUrl url(fCurrent->GetName());
187  url.SetAnchor(0);
188  TString lfname = gEnv->GetValue("Path.Localroot", "");
189  TFile::EFileType type = TFile::GetType(url.GetUrl(), "", &lfname);
190  if (type == TFile::kLocal &&
191  strcmp(url.GetProtocol(),"root") && strcmp(url.GetProtocol(),"xrd"))
192  lfname = url.GetFileAndOptions();
193 
195  // Release the file cache
196  if (type == TFile::kLocal) {
197  ReleaseCache(lfname);
198  } else if (type == TFile::kFile) {
199  ReleaseCache(url.GetFile());
200  } else {
201  Error("Process",
202  "attempt to call ReleaseCache for a non-local file: '%s'", url.GetUrl());
203  }
204  } else if (fType->GetType() == TPBHandleDSType::kCheckCache) {
205  // Check the file cache
206  if (type == TFile::kLocal) {
207  CheckCache(lfname);
208  } else if (type == TFile::kFile) {
209  CheckCache(url.GetFile());
210  } else {
211  Error("Process",
212  "attempt to call CheckCache for a non-local file: '%s'", url.GetUrl());
213  }
214  } else if (fType->GetType() == TPBHandleDSType::kRemoveFiles) {
215  // Remove the file
216  RemoveFile(url.GetFileAndOptions());
217  } else if (fType->GetType() == TPBHandleDSType::kCopyFiles) {
218  // Copy file
219  CopyFile(url.GetFileAndOptions());
220  } else {
221  // Type unknown
222  Warning("Process", "type: %d is unknown", fType->GetType());
223  }
224 
225  return kTRUE;
226 }
227 
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
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
This class represents a WWW compatible URL.
Definition: TUrl.h:35
void CheckCache(const char *fn)
Check the memory cache associated with file &#39;fn&#39;.
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition: TSystem.cxx:904
virtual TObject * FindObject(const char *name) const
Delete a TObjLink object.
Definition: TList.cxx:574
virtual Bool_t Cp(const char *dst, Bool_t progressbar=kTRUE, UInt_t buffersize=1000000)
Allows to copy this file to the dst URL.
Definition: TFile.cxx:4848
void ReleaseCache(const char *fn)
Release the memory cache associated with file &#39;fn&#39;.
Manages an element of a TDSet.
Definition: TDSet.h:66
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1357
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2365
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
static EFileType GetType(const char *name, Option_t *option="", TString *prefix=0)
Resolve the file type as a function of the protocol field in &#39;name&#39;.
Definition: TFile.cxx:4665
TObject * Value() const
Definition: TMap.h:121
void RemoveFile(const char *fn)
Physically remove the file &#39;fn&#39;.
EFileType
File type.
Definition: TFile.h:174
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
virtual Bool_t Process(Long64_t entry)
The Process() function is called for each entry in the tree (or possibly keyed object in the case of ...
const Bool_t kFALSE
Definition: RtypesCore.h:88
PROOF selector for file cache release.
virtual void SlaveBegin(TTree *)
Init the type from the input parameters.
Class used by TMap to store (key,value) pairs.
Definition: TMap.h:102
void SetAnchor(const char *anchor)
Definition: TUrl.h:89
#define ClassImp(name)
Definition: Rtypes.h:359
int type
Definition: TGX11.cxx:120
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
EHandleDSType GetType() const
Bool_t IsNull() const
Definition: TString.h:383
Mother of all ROOT objects.
Definition: TObject.h:37
TList * fInput
List of objects available during processing.
Definition: TSelector.h:43
A TTree object has a header with a name and a title.
Definition: TTree.h:70
TPBHandleDSType * fType
void CopyFile(const char *fn)
Copy file &#39;fn&#39; to fDestDir.
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
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
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:345