MigrationMemoryManager.hpp 4.14 KB
Newer Older
1
/******************************************************************************
2
 *                      MigrationMemoryManager.hpp
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * 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

27
28
#include "castor/tape/tapeserver/threading/BlockingQueue.hpp"
#include "castor/tape/tapeserver/threading/Threading.hpp"
29
#include "castor/log/LogContext.hpp"
30

31
namespace castor {
32
33
34
35
namespace exception {
// Forward declaration  
class Exception;
}
36
37
38
namespace tape {
namespace tapeserver {
namespace daemon {
39
40
41
// Forward declaration
class TapeWriteTask;
class MemBlock;
David COME's avatar
David COME committed
42
class DataPipeline;
43
44
/**
 * The memory manager is responsible for allocating memory blocks and distributing
45
46
 * the free ones around to any class in need. The distribution is actively run in
 * a thread.
47
 */
48
class MigrationMemoryManager: private castor::tape::threading::Thread {
49
50
51
52
53
54
55
public:
  
  /**
   * Constructor
   * @param numberOfBlocks: number of blocks to allocate
   * @param blockSize: size of each block
   */
56
57
58
  MigrationMemoryManager(const size_t numberOfBlocks, const size_t blockSize, 
          castor::log::LogContext lc) 
          throw(castor::exception::Exception);
59
  
60
61
62
63
  /**
   * 
   * @return the nominal capacity of one block 
   */
64
  size_t blockCapacity();
65
  
66
67
68
69
  /**
   * Are all sheep back to the farm?
   * @return 
   */
70
  bool areBlocksAllBack() throw();
71
72
73
74
  
  /**
   * Start serving clients (in the dedicated thread)
   */
75
  void startThreads() throw(castor::exception::Exception);
76
77
78
79
  
  /**
   * Waiting for clients to finish (in the dedicated thread)
   */
80
  void waitThreads() throw(castor::exception::Exception);
81
82
83
84
85
  
  /**
   * Adds a new client in need for free memory blocks
   * @param c: the new client
   */
David COME's avatar
David COME committed
86
  void addClient(DataPipeline* c) throw(castor::exception::Exception);
87
88
89
90
91
  
  /**
   * Takes back a block which has been released by one of the clients
   * @param mb: the pointer to the block
   */
92
  void releaseBlock(MemBlock *mb) throw(castor::exception::Exception);
93
94
95
96
97
  
  /**
   * Function used to specify that there are no more clients for this memory manager.
   * See the definition of endOfClients below.
   */
98
  void finish() throw(castor::exception::Exception);
99
100
101
102

  /**
   * Destructor
   */
103
  ~MigrationMemoryManager() throw();
104
105
106
private:
  
  
107
108
  const size_t m_blockCapacity;
  
109
110
111
112
113
  /**
   * Total number of allocated memory blocks
   */
  size_t m_totalNumberOfBlocks;
  
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  /**
   * 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;
  
134
135
136
  /**
   * Container for the free blocks
   */
137
  castor::tape::threading::BlockingQueue<MemBlock *> m_freeBlocks;
138
139
140
141
142
  
  /**
   * The client queue: we will feed them as soon as blocks
   * become free. This is done in a dedicated thread.
   */
David COME's avatar
David COME committed
143
   castor::tape::threading::BlockingQueue<DataPipeline *> m_clientQueue;
144

145
146
147
148
149
150
   /**
    * Logging purpose. Given the fact the class is threaded, the LogContext
    * has to be copied.
    */
   castor::log::LogContext m_lc;
    
151
152
153
  /**
   * Thread routine: pops a client and provides him blocks until he is happy!
   */
154
  void run() throw(castor::exception::Exception);
155
156
  
};
157
158
159
160
161
162

}
}
}
}