File: virtualidtable.h

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (292 lines) | stat: -rw-r--r-- 9,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/****************************************************************************
 *   Copyright (C) 2006-2008 by Jason Ansel, Kapil Arya, and Gene Cooperman *
 *   jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu              *
 *                                                                          *
 *   This file is part of the dmtcp/src module of DMTCP (DMTCP:dmtcp/src).  *
 *                                                                          *
 *  DMTCP is free software: you can redistribute it and/or        *
 *  modify it under the terms of the GNU Lesser General Public License as   *
 *  published by the Free Software Foundation, either version 3 of the      *
 *  License, or (at your option) any later version.                         *
 *                                                                          *
 *  DMTCP is distributed in the hope that it will be useful,      *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU Lesser General Public License for more details.                     *
 *                                                                          *
 *  You should have received a copy of the GNU Lesser General Public        *
 *  License along with DMTCP:dmtcp/src.  If not, see                        *
 *  <http://www.gnu.org/licenses/>.                                         *
 ****************************************************************************/

#ifndef VIRTUAL_ID_TABLE_H
#define VIRTUAL_ID_TABLE_H

#include <sys/types.h>
#include "../jalib/jserialize.h"
#include "../jalib/jfilesystem.h"
#include "../jalib/jalloc.h"
#include "../jalib/jconvert.h"
#include "../jalib/jassert.h"
#include "dmtcpalloc.h"
#include "util.h"

#define MAX_VIRTUAL_ID 999

namespace dmtcp
{
  template <typename IdType>
    class VirtualIdTable
    {
      protected:
        void _do_lock_tbl() {
          JASSERT(pthread_mutex_lock(&tblLock) == 0) (JASSERT_ERRNO);
        }

        void _do_unlock_tbl() {
          JASSERT(pthread_mutex_unlock(&tblLock) == 0) (JASSERT_ERRNO);
        }

      public:
#ifdef JALIB_ALLOCATOR
        static void* operator new(size_t nbytes, void* p) { return p; }
        static void* operator new(size_t nbytes) { JALLOC_HELPER_NEW(nbytes); }
        static void  operator delete(void* p) { JALLOC_HELPER_DELETE(p); }
#endif
        VirtualIdTable(string typeStr, IdType base,
                       size_t max = MAX_VIRTUAL_ID) {
          pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
          tblLock = lock;
          _do_lock_tbl();
          _idMapTable.clear();
          _do_unlock_tbl();
          _typeStr = typeStr;
          _base = base;
          _max = max;
          resetNextVirtualId();
        }

        void resetNextVirtualId() {
          _nextVirtualId = (IdType) ((unsigned long)_base + 1);
        }

        IdType addOneToNextVirtualId() {
          IdType ret = _nextVirtualId;
          _nextVirtualId = (IdType) ((unsigned long)_nextVirtualId + 1);
          if ((unsigned long)_nextVirtualId >= (unsigned long)_base + _max) {
            resetNextVirtualId();
          }
          return ret;
        }

        size_t size() {
          _do_lock_tbl();
          size_t size = _idMapTable.size();
          _do_unlock_tbl();
          return size;
        }

        void clear() {
          _do_lock_tbl();
          _idMapTable.clear();
          resetNextVirtualId();
          _do_unlock_tbl();
        }

        void postRestart() {
          _do_lock_tbl();
          _idMapTable.clear();
          resetNextVirtualId();
          _do_unlock_tbl();
        }

        void resetOnFork(IdType newBase) {
          _base = newBase;
          pthread_mutex_t newlock = PTHREAD_MUTEX_INITIALIZER;
          tblLock = newlock;
          resetNextVirtualId();
        }

        bool getNewVirtualId(IdType *id) {
          bool res = false;
          _do_lock_tbl();
          if (_idMapTable.size() < _max) {

            size_t count = 0;
            while (1) {
              IdType newId = addOneToNextVirtualId();
              id_iterator i = _idMapTable.find(newId);
              if (i == _idMapTable.end()) {
                *id = newId;
                res = true;
                break;
              }
              if (++count == _max) {
                break;
              }
            }
          }
          _do_unlock_tbl();
          return res;
        }

        bool isIdCreatedByCurrentProcess(IdType id) {
          return (size_t)id > (size_t)getpid() && (size_t)id <= (size_t)getpid() + _max;
        }

        bool virtualIdExists(IdType id) {
          bool retVal = false;
          _do_lock_tbl();
          id_iterator j = _idMapTable.find ( id );
          if ( j != _idMapTable.end() )
            retVal = true;

          _do_unlock_tbl();
          return retVal;
        }

        bool realIdExists(IdType id) {
          bool retval = false;
          _do_lock_tbl();
          for (id_iterator i = _idMapTable.begin(); i != _idMapTable.end(); ++i) {
            if (i->second == id) {
              retval = true;
              break;
            }
          }
          _do_unlock_tbl();
          return retval;
        }

        void updateMapping(IdType virtualId, IdType realId) {
          _do_lock_tbl();
          _idMapTable[virtualId] = realId;
          _do_unlock_tbl();
        }

        void erase(IdType virtualId) {
          _do_lock_tbl();
          _idMapTable.erase( virtualId );
          _do_unlock_tbl();
        }

        void printMaps() {
          ostringstream out;
          out << _typeStr << " Maps\n";
          out << "      Virtual" << "  ->  " << "Real" << "\n";
          for ( id_iterator i = _idMapTable.begin(); i != _idMapTable.end(); ++i ) {
            IdType virtualId = i->first;
            IdType realId  = i->second;
            out << "\t" << virtualId << "\t->   " << realId << "\n";
          }
          JTRACE("Virtual To Real Mappings:") (_idMapTable.size()) (out.str());
        }

        vector< IdType > getIdVector() {
          vector< IdType > idVec;
          _do_lock_tbl();
          for ( id_iterator i = _idMapTable.begin(); i != _idMapTable.end(); ++i )
            idVec.push_back ( i->first );
          _do_unlock_tbl();
          return idVec;
        }


        virtual IdType virtualToReal(IdType virtualId) {
          IdType retVal = 0;

          /* This code is called from MTCP while the checkpoint thread is holding
             the JASSERT log lock. Therefore, don't call JTRACE/JASSERT/JINFO/etc. in
             this function. */
          _do_lock_tbl();
          id_iterator i = _idMapTable.find(virtualId);
          if (i == _idMapTable.end()) {
            retVal = virtualId;
          } else {
            retVal = i->second;
          }
          _do_unlock_tbl();
          return retVal;
        }

        virtual IdType realToVirtual(IdType realId) {

          /* This code is called from MTCP while the checkpoint thread is holding
             the JASSERT log lock. Therefore, don't call JTRACE/JASSERT/JINFO/etc. in
             this function. */
          _do_lock_tbl();
          for (id_iterator i = _idMapTable.begin(); i != _idMapTable.end(); ++i) {
            if ( realId == i->second ) {
              _do_unlock_tbl();
              return i->first;
            }
          }

          _do_unlock_tbl();
          return realId;
        }


        void serialize(jalib::JBinarySerializer& o) {
          JSERIALIZE_ASSERT_POINT("VirtualIdTable:");
          o.serializeMap(_idMapTable);
          JSERIALIZE_ASSERT_POINT("EOF");
          printMaps();
        }

        void writeMapsToFile(int fd) {
          string file = "/proc/self/fd/" + jalib::XToString(fd);
          string mapFile = jalib::Filesystem::ResolveSymlink(file);

          JASSERT (mapFile.length() > 0) (mapFile);
          JTRACE ("Write Maps to file") (mapFile);

          // Lock fileset before any operations
          Util::lockFile(fd);
          _do_lock_tbl();
          JASSERT(lseek(fd, 0, SEEK_END) != -1);

          jalib::JBinarySerializeWriterRaw mapwr(mapFile, fd);
          mapwr.serializeMap(_idMapTable);

          _do_unlock_tbl();
          Util::unlockFile(fd);
        }

        void readMapsFromFile(int fd) {
          string file = "/proc/self/fd/" + jalib::XToString(fd);
          string mapFile = jalib::Filesystem::ResolveSymlink(file);

          JASSERT(mapFile.length() > 0) (mapFile);
          JTRACE("Read Maps from file") (mapFile);

          // No need to lock the file as we are the only process using this fd.
          //Util::lockFile(fd);
          _do_lock_tbl();

          jalib::JBinarySerializeReaderRaw maprd(mapFile, fd);
          maprd.rewind();

          while (!maprd.isEOF()) {
            maprd.serializeMap(_idMapTable);
          }

          _do_unlock_tbl();
          //Util::unlockFile(fd);

          printMaps();
        }

      private:
        string _typeStr;
        pthread_mutex_t tblLock;
      protected:
        typedef typename map<IdType, IdType>::iterator id_iterator;
        map<IdType, IdType> _idMapTable;
        IdType _base;
        size_t _max;
        IdType _nextVirtualId;
    };
}

#endif