xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdXrootdFile.hh
Go to the documentation of this file.
1 #ifndef _XROOTD_FILE_H_
2 #define _XROOTD_FILE_H_
3 /******************************************************************************/
4 /* */
5 /* X r d X r o o t d F i l e . h h */
6 /* */
7 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <cstring>
33 #include <set>
34 #include <vector>
35 
36 #include "XProtocol/XPtypes.hh"
37 #include "XrdSys/XrdSysPthread.hh"
39 
40 /******************************************************************************/
41 /* X r d X r o o t d F i l e H P */
42 /******************************************************************************/
43 
45 {
46 public:
47 
48 void Avail(int fHandle) {fhMutex.Lock();
49  bool done = (1 == refs--);
50  if (noMore)
51  {fhMutex.UnLock();
52  if (done) delete this;
53  } else {
54  fhAvail.push_back(fHandle);
55  fhMutex.UnLock();
56  }
57  }
58 
59 void Delete() {fhMutex.Lock();
60  if (!refs) {fhMutex.UnLock(); delete this;}
61  else {noMore = true; fhMutex.UnLock();}
62  }
63 
64 int Get() {int fh;
65  fhMutex.Lock();
66  if (fhAvail.empty()) fh = -1;
67  else {fh = fhAvail.back();
68  fhAvail.pop_back();
69  }
70  fhMutex.UnLock();
71  return fh;
72  }
73 
74 void Ref() {fhMutex.Lock(); refs++; fhMutex.UnLock();}
75 
76  XrdXrootdFileHP(int rsv=2) : refs(1), noMore(false)
77  {fhAvail.reserve(rsv);}
78 
79 private:
80 
82 
84 std::vector<int> fhAvail;
85 int refs;
86 bool noMore;
87 };
88 
89 
90 /******************************************************************************/
91 /* X r d X r o o t d F i l e */
92 /******************************************************************************/
93 
94 class XrdSfsFile;
95 class XrdXrootdFileLock;
96 class XrdXrootdAioFob;
97 class XrdXrootdMonitor;
98 class XrdXrootdPgwFob;
99 
101 {
102 public:
103 
104 XrdSfsFile *XrdSfsp; // -> Actual file object
105 union {char *mmAddr; // Memory mapped location, if any
106  unsigned
107  long long cbArg; // Callback argument upon close()
108  };
109 char *FileKey; // -> File hash name (actual file name now)
110 char FileMode; // 'r' or 'w'
111 bool AsyncMode; // 1 -> if file in async r/w mode
112 bool isMMapped; // 1 -> file is memory mapped
113 bool sfEnabled; // 1 -> file is sendfile enabled
114 union {int fdNum; // File descriptor number if regular file
115  int fHandle; // The file handle upon close()
116  };
117 XrdXrootdAioFob *aioFob; // Aio freight pointer for reads
118 XrdXrootdPgwFob *pgwFob; // Pgw freight pointer for writes
119 XrdXrootdFileHP *fhProc; // File handle processor (set at close time)
120 const char *ID; // File user
121 
122 XrdXrootdFileStats Stats; // File access statistics
123 
124 static void Init(XrdXrootdFileLock *lp, XrdSysError *erP, bool sfok);
125 
126  void Ref(int num);
127 
128  void Serialize();
129 
130  XrdXrootdFile(const char *id, const char *path, XrdSfsFile *fp,
131  char mode='r', bool async=false, struct stat *sP=0);
132  ~XrdXrootdFile();
133 
134 private:
135 int bin2hex(char *outbuff, char *inbuff, int inlen);
137 static int sfOK;
138 static const char *TraceID;
139 
140 int refCount; // Reference counter
144 };
145 
146 /******************************************************************************/
147 /* X r d X r o o t d F i l e T a b l e */
148 /******************************************************************************/
149 
150 // The before define the structure of the file table. We will have FTABSIZE
151 // internal table entries. We will then provide an external linear table
152 // that increases by FTABSIZE entries. There is one file table per link and
153 // it is owned by the base protocol object.
154 //
155 #define XRD_FTABSIZE 16
156 
157 // WARNING! Manipulation (i.e., Add/Del/delete) of this object must be
158 // externally serialized at the link level. Only one thread
159 // may be active w.r.t this object during manipulation!
160 //
162 {
163 public:
164 
165  int Add(XrdXrootdFile *fp);
166 
167  XrdXrootdFile *Del(XrdXrootdMonitor *monP, int fnum, bool dodel=true);
168 
169 inline XrdXrootdFile *Get(int fnum)
170  {if (fnum >= 0)
171  {if (fnum < XRD_FTABSIZE) return FTab[fnum];
172  if (XTab && (fnum-XRD_FTABSIZE)<XTnum)
173  return XTab[fnum-XRD_FTABSIZE];
174  }
175  return (XrdXrootdFile *)0;
176  }
177 
178  void Recycle(XrdXrootdMonitor *monP);
179 
180  XrdXrootdFileTable(unsigned int mid=0) : fhProc(0), FTfree(0), monID(mid),
181  XTab(0), XTnum(0), XTfree(0)
182  {memset((void *)FTab, 0, sizeof(FTab));}
183 
184 private:
185 
186  ~XrdXrootdFileTable() {} // Always use Recycle() to delete this object!
187 
188 static const char *TraceID;
189 static const char *ID;
191 
193 int FTfree;
194 unsigned int monID;
195 
197 int XTnum;
198 int XTfree;
199 };
200 #endif
void Ref()
Definition: XrdXrootdFile.hh:74
~XrdXrootdFileTable()
Definition: XrdXrootdFile.hh:186
XrdSysSemaphore * syncWait
Definition: XrdXrootdFile.hh:142
XrdXrootdFile * FTab[XRD_FTABSIZE]
Definition: XrdXrootdFile.hh:192
XrdXrootdPgwFob * pgwFob
Definition: XrdXrootdFile.hh:118
void Delete()
Definition: XrdXrootdFile.hh:59
std::vector< int > fhAvail
Definition: XrdXrootdFile.hh:84
void Recycle(XrdXrootdMonitor *monP)
XrdSysMutex fhMutex
Definition: XrdXrootdFile.hh:83
int fdNum
Definition: XrdXrootdFile.hh:114
int bin2hex(char *outbuff, char *inbuff, int inlen)
bool isMMapped
Definition: XrdXrootdFile.hh:112
Definition: XrdXrootdPgwFob.hh:41
int FTfree
Definition: XrdXrootdFile.hh:193
~XrdXrootdFileHP()
Definition: XrdXrootdFile.hh:81
static int sfOK
Definition: XrdXrootdFile.hh:137
static const char * TraceID
Definition: XrdXrootdFile.hh:188
XrdXrootdFile * Get(int fnum)
Definition: XrdXrootdFile.hh:169
static const char * ID
Definition: XrdXrootdFile.hh:189
Definition: XrdXrootdMonitor.hh:75
XrdSysMutex fileMutex
Definition: XrdXrootdFile.hh:143
Definition: XrdSysError.hh:89
XrdXrootdFile(const char *id, const char *path, XrdSfsFile *fp, char mode='r', bool async=false, struct stat *sP=0)
XrdSfsFile * XrdSfsp
Definition: XrdXrootdFile.hh:104
Definition: XrdXrootdFile.hh:100
Definition: XrdSysPthread.hh:164
int reserved
Definition: XrdXrootdFile.hh:141
Definition: XrdXrootdFile.hh:161
XrdXrootdFileHP * fhProc
Definition: XrdXrootdFile.hh:190
static void Init(XrdXrootdFileLock *lp, XrdSysError *erP, bool sfok)
int XTfree
Definition: XrdXrootdFile.hh:198
bool sfEnabled
Definition: XrdXrootdFile.hh:113
void Serialize()
char * FileKey
Definition: XrdXrootdFile.hh:109
Definition: XrdSysPthread.hh:493
XrdXrootdFileTable(unsigned int mid=0)
Definition: XrdXrootdFile.hh:180
bool AsyncMode
Definition: XrdXrootdFile.hh:111
#define XRD_FTABSIZE
Definition: XrdXrootdFile.hh:155
int Get()
Definition: XrdXrootdFile.hh:64
XrdXrootdFile * Del(XrdXrootdMonitor *monP, int fnum, bool dodel=true)
Definition: XrdXrootdFileStats.hh:35
int XTnum
Definition: XrdXrootdFile.hh:197
#define stat(a, b)
Definition: XrdPosix.hh:96
void Lock()
Definition: XrdSysPthread.hh:222
XrdXrootdFile ** XTab
Definition: XrdXrootdFile.hh:196
bool noMore
Definition: XrdXrootdFile.hh:86
static XrdXrootdFileLock * Locker
Definition: XrdXrootdFile.hh:136
XrdXrootdFileHP * fhProc
Definition: XrdXrootdFile.hh:119
int fHandle
Definition: XrdXrootdFile.hh:115
unsigned int monID
Definition: XrdXrootdFile.hh:194
int Add(XrdXrootdFile *fp)
Definition: XrdXrootdAioFob.hh:37
void Avail(int fHandle)
Definition: XrdXrootdFile.hh:48
char * mmAddr
Definition: XrdXrootdFile.hh:105
int refs
Definition: XrdXrootdFile.hh:85
XrdXrootdAioFob * aioFob
Definition: XrdXrootdFile.hh:117
Definition: XrdSfsInterface.hh:364
XrdXrootdFileStats Stats
Definition: XrdXrootdFile.hh:122
void UnLock()
Definition: XrdSysPthread.hh:224
char FileMode
Definition: XrdXrootdFile.hh:110
XrdXrootdFileHP(int rsv=2)
Definition: XrdXrootdFile.hh:76
const char * ID
Definition: XrdXrootdFile.hh:120
void Ref(int num)
Definition: XrdXrootdFileLock.hh:32
Definition: XrdXrootdFile.hh:44
unsigned long long cbArg
Definition: XrdXrootdFile.hh:107
static const char * TraceID
Definition: XrdXrootdFile.hh:138
int refCount
Definition: XrdXrootdFile.hh:140