MemManager.hpp 5.81 KB
Newer Older
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
/******************************************************************************
 *                      MemManager.hpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program 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 General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

#pragma once

#include "castor/tape/tapeserver/daemon/MemBlock.hpp"
#include "castor/tape/tapeserver/daemon/MemManagerClient.hpp"
29
30
#include "castor/tape/tapeserver/threading/BlockingQueue.hpp"
#include "castor/tape/tapeserver/threading/Threading.hpp"
31
32
33
#include "castor/exception/Exception.hpp"
#include <iostream>

34
35
36
37
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
38
39
40
41
/**
 * The memory manager is responsible for allocating memory blocks and distributing
 * the free ones around to any class in need.
 */
42
class MemoryManager: private castor::tape::threading::Thread {
43
44
45
46
47
48
49
public:
  
  /**
   * Constructor
   * @param numberOfBlocks: number of blocks to allocate
   * @param blockSize: size of each block
   */
50
51
  MemoryManager(const size_t numberOfBlocks, const size_t blockSize) 
          throw(castor::exception::Exception) : 
Eric Cano's avatar
Eric Cano committed
52
53
  m_blockCapacity(blockSize), m_totalNumberOfBlocks(0),
  m_totalMemoryAllocated(0), m_blocksProvided(0), m_blocksReturned(0) {
54
55
    for (size_t i = 0; i < numberOfBlocks; i++) {
      m_freeBlocks.push(new MemBlock(i, blockSize));
56
57
      m_totalNumberOfBlocks++;
      m_totalMemoryAllocated+=blockSize;
58
59
60
    }
  }
  
61
62
63
64
65
66
67
68
  /**
   * 
   * @return the nominal capacity of one block 
   */
  size_t blockCapacity(){
    return m_blockCapacity;
  }
  
69
70
71
72
73
74
75
76
77
78
79
80
  /**
   * Are all sheep back to the farm?
   * @return 
   */
  bool areBlocksAllBack() throw() {
    return m_totalNumberOfBlocks==m_freeBlocks.size();
  }
  
  /**
   * Start serving clients (in the dedicated thread)
   */
  void startThreads() throw(castor::exception::Exception) {
81
    castor::tape::threading::Thread::start();
82
83
84
85
86
87
  }
  
  /**
   * Waiting for clients to finish (in the dedicated thread)
   */
  void waitThreads() throw(castor::exception::Exception) {
88
    castor::tape::threading::Thread::wait();
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  }
  
  /**
   * Adds a new client in need for free memory blocks
   * @param c: the new client
   */
  void addClient(MemoryManagerClient* c) throw(castor::exception::Exception) {
    m_clientQueue.push(c);
  }
  
  /**
   * Takes back a block which has been released by one of the clients
   * @param mb: the pointer to the block
   */
  void releaseBlock(MemBlock *mb) throw(castor::exception::Exception) {
    mb->reset();
    m_freeBlocks.push(mb);
106
107
108
109
    {
      castor::tape::threading::MutexLocker ml(&m_countersMutex);
      m_blocksReturned++;
    }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  }
  
  /**
   * Function used to specify that there are no more clients for this memory manager.
   * See the definition of endOfClients below.
   */
  void finish() throw(castor::exception::Exception) {
    addClient(new endOfClients());
  }

  /**
   * Destructor
   */
  ~MemoryManager() throw() {
    // Make sure the thread is finished: this should be done by the caller,
    // who should have called waitThreads.
126
    // castor::tape::threading::Thread::wait();
127
128
129
130
    // we expect to be called after all users are finished. Just "free"
    // the memory blocks we still have.
    try {
      while(true) {
131
        delete m_freeBlocks.tryPop();
132
133
      }
    }
134
    catch (castor::tape::threading::noMore) {
135
136
137
138
139
140
141
142
143
144
145
146
147
148
      //done
    } 
  }
private:
  
  /**
   * Special token used to say that there are no more clients for this memory manager
   */
  class endOfClients: public MemoryManagerClient {
    virtual bool endOfWork() throw() {
      return true;
    }
  };
  
149
150
  const size_t m_blockCapacity;
  
151
152
153
154
155
  /**
   * Total number of allocated memory blocks
   */
  size_t m_totalNumberOfBlocks;
  
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  /**
   * Total amount of memory allocated
   */
  size_t m_totalMemoryAllocated;
  
  /**
   * Count of blocks provided
   */
  size_t m_blocksProvided;
  
  /**
   * Count of blocks returned
   */
  size_t m_blocksReturned;
  
  /**
   * Mutex protecting the counters
   */
  castor::tape::threading::Mutex m_countersMutex;
  
176
177
178
  /**
   * Container for the free blocks
   */
179
  castor::tape::threading::BlockingQueue<MemBlock *> m_freeBlocks;
180
181
182
183
184
  
  /**
   * The client queue: we will feed them as soon as blocks
   * become free. This is done in a dedicated thread.
   */
185
   castor::tape::threading::BlockingQueue<MemoryManagerClient *> m_clientQueue;
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

  /**
   * Thread routine: pops a client and provides him blocks until he is happy!
   */
  void run() throw(castor::exception::Exception) {
    while(true) {
      MemoryManagerClient* c = m_clientQueue.pop();
    
      /* This is an unfortunate special case: WE allocate the end block
       and WE have to delete it. */
      if (c->endOfWork()) {
        delete c;
        return;
      };
      
      /* Spin on the the client. We rely on the fact that he will want
       at least one block (which is the case currently) */
203
204
205
206
207
208
209
210
      while (c->provideBlock(m_freeBlocks.pop())) {
        {
          castor::tape::threading::MutexLocker ml (&m_countersMutex);
          m_blocksProvided++;
        }
        
      }
        
211
212
213
214
    }
  }
  
};
215
216
217
218
219
220

}
}
}
}