MigrationMemoryManager.hpp 3.64 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        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 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 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.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
18
19

#pragma once

20
#include "common/threading/BlockingQueue.hpp"
21
#include "common/threading/Thread.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
22
#include "common/log/LogContext.hpp"
23

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

  /**
   * Destructor
   */
96
  ~MigrationMemoryManager() throw();
97
98
99
private:
  
  
100
101
  const size_t m_blockCapacity;
  
102
103
104
105
106
  /**
   * Total number of allocated memory blocks
   */
  size_t m_totalNumberOfBlocks;
  
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  /**
   * 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
   */
125
  cta::threading::Mutex m_countersMutex;
126
  
127
128
129
  /**
   * Container for the free blocks
   */
130
  cta::threading::BlockingQueue<MemBlock *> m_freeBlocks;
131
132
133
134
135
  
  /**
   * The client queue: we will feed them as soon as blocks
   * become free. This is done in a dedicated thread.
   */
136
  cta::threading::BlockingQueue<DataPipeline *> m_clientQueue;
137

138
139
140
141
  /**
   * Logging purpose. Given the fact the class is threaded, the LogContext
   * has to be copied.
   */
Victor Kotlyar's avatar
Victor Kotlyar committed
142
  cta::log::LogContext m_lc;
143
    
144
145
146
  /**
   * Thread routine: pops a client and provides him blocks until he is happy!
   */
147
  void run() ;
148
149
  
};
150
151
152
153
154
155

}
}
}
}