MemManager.hpp 6.2 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
#include "castor/exception/Exception.hpp"
32
#include "castor/log/LogContext.hpp"
33
34
#include <iostream>

35
36
37
38
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
39
40
/**
 * The memory manager is responsible for allocating memory blocks and distributing
41
42
 * the free ones around to any class in need. The distribution is actively run in
 * a thread.
43
 */
44
class MigrationMemoryManager: private castor::tape::threading::Thread {
45
46
47
48
49
50
51
public:
  
  /**
   * Constructor
   * @param numberOfBlocks: number of blocks to allocate
   * @param blockSize: size of each block
   */
52
  MigrationMemoryManager(const size_t numberOfBlocks, const size_t blockSize, castor::log::LogContext lc) 
53
          throw(castor::exception::Exception) : 
Eric Cano's avatar
Eric Cano committed
54
  m_blockCapacity(blockSize), m_totalNumberOfBlocks(0),
55
  m_totalMemoryAllocated(0), m_blocksProvided(0), m_blocksReturned(0),m_lc(lc) {
56
57
    for (size_t i = 0; i < numberOfBlocks; i++) {
      m_freeBlocks.push(new MemBlock(i, blockSize));
58
59
      m_totalNumberOfBlocks++;
      m_totalMemoryAllocated+=blockSize;
60
61
      
      m_lc.pushOrReplace(log::Param("blockId",i));
62
      m_lc.log(LOG_DEBUG,"MigrationMemoryManager Created a block");
63
    }
64
    m_lc.log(LOG_INFO,"MigrationMemoryManager: all blocks have been created");
65
66
  }
  
67
68
69
70
71
72
73
74
  /**
   * 
   * @return the nominal capacity of one block 
   */
  size_t blockCapacity(){
    return m_blockCapacity;
  }
  
75
76
77
78
79
80
81
82
83
84
85
86
  /**
   * 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) {
87
    castor::tape::threading::Thread::start();
88
    m_lc.log(LOG_INFO,"MigrationMemoryManager starting thread");
89
90
91
92
93
94
  }
  
  /**
   * Waiting for clients to finish (in the dedicated thread)
   */
  void waitThreads() throw(castor::exception::Exception) {
95
    castor::tape::threading::Thread::wait();
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  }
  
  /**
   * 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);
113
114
115
116
    {
      castor::tape::threading::MutexLocker ml(&m_countersMutex);
      m_blocksReturned++;
    }
117
118
119
120
121
122
123
  }
  
  /**
   * 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) {
124
    addClient(NULL);
125
126
127
128
129
  }

  /**
   * Destructor
   */
130
  ~MigrationMemoryManager() throw() {
131
132
    // Make sure the thread is finished: this should be done by the caller,
    // who should have called waitThreads.
133
    // castor::tape::threading::Thread::wait();
134
135
    // we expect to be called after all users are finished. Just "free"
    // the memory blocks we still have.
136
137
138
139
140
141
    castor::tape::threading::BlockingQueue<MemBlock*>::valueRemainingPair ret;
    do{
      ret=m_freeBlocks.popGetSize();
      delete ret.value;
    }while(ret.remaining>0);
    
142
    m_lc.log(LOG_INFO,"MigrationMemoryManager destruction : all memory blocks have been deleted");
143
144
145
146
  }
private:
  
  
147
148
  const size_t m_blockCapacity;
  
149
150
151
152
153
  /**
   * Total number of allocated memory blocks
   */
  size_t m_totalNumberOfBlocks;
  
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  /**
   * 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;
  
174
175
176
  /**
   * Container for the free blocks
   */
177
  castor::tape::threading::BlockingQueue<MemBlock *> m_freeBlocks;
178
179
180
181
182
  
  /**
   * The client queue: we will feed them as soon as blocks
   * become free. This is done in a dedicated thread.
   */
183
   castor::tape::threading::BlockingQueue<MemoryManagerClient *> m_clientQueue;
184

185
186
187
188
189
190
   /**
    * Logging purpose. Given the fact the class is threaded, the LogContext
    * has to be copied.
    */
   castor::log::LogContext m_lc;
    
191
192
193
194
195
196
197
  /**
   * 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();
    
198
199
      // If the c is a NULL pointer, that means end of client 
      if (!c) {
200
201
202
203
204
        return;
      };
      
      /* Spin on the the client. We rely on the fact that he will want
       at least one block (which is the case currently) */
205
206
207
208
209
210
211
212
      while (c->provideBlock(m_freeBlocks.pop())) {
        {
          castor::tape::threading::MutexLocker ml (&m_countersMutex);
          m_blocksProvided++;
        }
        
      }
        
213
214
215
216
    }
  }
  
};
217
218
219
220
221
222

}
}
}
}