DiskReadThreadPool.cpp 9.88 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
/******************************************************************************
 *
 * 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
 *****************************************************************************/

#include "castor/tape/tapeserver/daemon/DiskReadThreadPool.hpp"
25
#include "castor/tape/tapeserver/daemon/MigrationTaskInjector.hpp"
26
#include "castor/tape/tapeserver/daemon/MigrationReportPacker.hpp"
27
#include "castor/common/CastorConfiguration.hpp"
28
29
#include <memory>
#include <sstream>
30

31
32
33
34
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
35
36
37
38

//------------------------------------------------------------------------------
// DiskReadThreadPool constructor
//------------------------------------------------------------------------------
39
40
DiskReadThreadPool::DiskReadThreadPool(int nbThread, uint64_t maxFilesReq,uint64_t maxBytesReq,
    castor::tape::tapeserver::daemon::MigrationWatchDog & migrationWatchDog,
Victor Kotlyar's avatar
Victor Kotlyar committed
41
    cta::log::LogContext lc, const std::string & remoteFileProtocol,
42
    const std::string & xrootPrivateKeyPath, uint16_t xrootTimeout) : 
43
44
45
    m_remoteFileProtocol(remoteFileProtocol),
    m_xrootPrivateKeyPath(xrootPrivateKeyPath),
    m_xrootTimeout(xrootTimeout),
46
    m_watchdog(migrationWatchDog),
47
    m_lc(lc),m_maxFilesReq(maxFilesReq),
48
    m_maxBytesReq(maxBytesReq), m_nbActiveThread(0) {
49
50
51
  for(int i=0; i<nbThread; i++) {
    DiskReadWorkerThread * thr = new DiskReadWorkerThread(*this);
    m_threads.push_back(thr);
Victor Kotlyar's avatar
Victor Kotlyar committed
52
53
    m_lc.pushOrReplace(cta::log::Param("threadID",i));
    m_lc.log(cta::log::DEBUG, "DiskReadWorkerThread created");
54
  }
55
56
57
58
59
60
61
62
63
}

//------------------------------------------------------------------------------
// DiskReadThreadPool destructor
//------------------------------------------------------------------------------
DiskReadThreadPool::~DiskReadThreadPool() { 
  while (m_threads.size()) {
    delete m_threads.back();
    m_threads.pop_back();
64
  }
Victor Kotlyar's avatar
Victor Kotlyar committed
65
  m_lc.log(cta::log::DEBUG, "Deleted threads in DiskReadThreadPool::~DiskReadThreadPool");
66
67
68
69
70
71
72
73
74
}

//------------------------------------------------------------------------------
// DiskReadThreadPool::startThreads
//------------------------------------------------------------------------------
void DiskReadThreadPool::startThreads() {
  for (std::vector<DiskReadWorkerThread *>::iterator i=m_threads.begin();
          i != m_threads.end(); i++) {
    (*i)->start();
75
  }
Victor Kotlyar's avatar
Victor Kotlyar committed
76
  m_lc.log(cta::log::INFO, "All the DiskReadWorkerThreads are started");
77
78
79
80
81
82
83
84
85
}

//------------------------------------------------------------------------------
// DiskReadThreadPool::waitThreads
//------------------------------------------------------------------------------
void DiskReadThreadPool::waitThreads() {
  for (std::vector<DiskReadWorkerThread *>::iterator i=m_threads.begin();
          i != m_threads.end(); i++) {
    (*i)->wait();
86
  }
87
88
89
90
91
92
93
}

//------------------------------------------------------------------------------
// DiskReadThreadPool::push
//------------------------------------------------------------------------------
void DiskReadThreadPool::push(DiskReadTask *t) { 
  m_tasks.push(t); 
Victor Kotlyar's avatar
Victor Kotlyar committed
94
  m_lc.log(cta::log::INFO, "Push a task into the DiskReadThreadPool");
95
96
97
98
99
100
101
102
103
}

//------------------------------------------------------------------------------
// DiskReadThreadPool::finish
//------------------------------------------------------------------------------
void DiskReadThreadPool::finish() {
  /* Insert one endOfSession per thread */
  for (size_t i=0; i<m_threads.size(); i++) {
    m_tasks.push(NULL);
104
  }
105
106
107
108
109
}

//------------------------------------------------------------------------------
// DiskReadThreadPool::popAndRequestMore
//------------------------------------------------------------------------------
Victor Kotlyar's avatar
Victor Kotlyar committed
110
DiskReadTask* DiskReadThreadPool::popAndRequestMore(cta::log::LogContext &lc){
111
  cta::threading::BlockingQueue<DiskReadTask*>::valueRemainingPair 
112
  vrp = m_tasks.popGetSize();
Victor Kotlyar's avatar
Victor Kotlyar committed
113
114
  cta::log::LogContext::ScopedParam sp(lc, cta::log::Param("m_maxFilesReq", m_maxFilesReq));
  cta::log::LogContext::ScopedParam sp0(lc, cta::log::Param("m_maxBytesReq", m_maxBytesReq));
115
116
117

  if(0==vrp.remaining){
    m_injector->requestInjection(true);
Victor Kotlyar's avatar
Victor Kotlyar committed
118
    lc.log(cta::log::DEBUG, "Requested injection from MigrationTaskInjector (with last call)");
119
120
  }else if(vrp.remaining + 1 ==  m_maxFilesReq/2){
    m_injector->requestInjection(false);
Victor Kotlyar's avatar
Victor Kotlyar committed
121
    lc.log(cta::log::DEBUG, "Requested injection from MigrationTaskInjector (without last call)");
122
  }
123
124
  return vrp.value;
}
125
126
127
//------------------------------------------------------------------------------
//addThreadStats
//------------------------------------------------------------------------------
128
void DiskReadThreadPool::addThreadStats(const DiskStats& other){
129
  cta::threading::MutexLocker lock(m_statAddingProtection);
130
131
  m_pooldStat+=other;
}
132
//------------------------------------------------------------------------------
133
134
135
//logWithStat
//------------------------------------------------------------------------------
void DiskReadThreadPool::logWithStat(int level, const std::string& message){
136
  m_pooldStat.totalTime = m_totalTime.secs();
Victor Kotlyar's avatar
Victor Kotlyar committed
137
  cta::log::ScopedParamContainer params(m_lc);
138
139
140
141
142
143
  params.add("poolReadWriteTime", m_pooldStat.readWriteTime)
        .add("poolWaitFreeMemoryTime",m_pooldStat.waitFreeMemoryTime)
        .add("poolCheckingErrorTime",m_pooldStat.checkingErrorTime)
        .add("poolOpeningTime",m_pooldStat.openingTime)
        .add("poolTransferTime", m_pooldStat.transferTime)
        .add("poolRealTime",m_pooldStat.totalTime)
144
        .add("poolFileCount",m_pooldStat.filesCount)
Eric Cano's avatar
Eric Cano committed
145
        .add("poolDataVolume", m_pooldStat.dataVolume)
146
        .add("poolGlobalPayloadTransferSpeedMBps",
147
           m_pooldStat.totalTime?1.0*m_pooldStat.dataVolume/1000/1000/m_pooldStat.totalTime:0)
148
        .add("poolAverageDiskPerformanceMBps",
149
           m_pooldStat.transferTime?1.0*m_pooldStat.dataVolume/1000/1000/m_pooldStat.transferTime:0.0)
150
        .add("poolOpenRWCloseToTransferTimeRatio",
151
           m_pooldStat.transferTime?(m_pooldStat.openingTime+m_pooldStat.readWriteTime+m_pooldStat.closingTime)/m_pooldStat.transferTime:0.0);
152
  m_lc.log(level,message);
153
154
}
//------------------------------------------------------------------------------
155
// DiskReadWorkerThread::run
156
157
//------------------------------------------------------------------------------
void DiskReadThreadPool::DiskReadWorkerThread::run() {
Victor Kotlyar's avatar
Victor Kotlyar committed
158
  cta::log::ScopedParamContainer logParams(m_lc);
159
160
  logParams.add("thread", "DiskRead")
           .add("threadID", m_threadID);
Victor Kotlyar's avatar
Victor Kotlyar committed
161
  m_lc.log(cta::log::DEBUG, "Starting DiskReadWorkerThread");
162
  
163
  std::unique_ptr<DiskReadTask> task;
Victor Kotlyar's avatar
Victor Kotlyar committed
164
165
  cta::utils::Timer localTime;
  cta::utils::Timer totalTime;
166
  
167
168
  while(1) {
    task.reset( m_parent.popAndRequestMore(m_lc));
Victor Kotlyar's avatar
Victor Kotlyar committed
169
    m_threadStat.waitInstructionsTime += localTime.secs(cta::utils::Timer::resetCounter);
170
    if (NULL!=task.get()) {
171
      task->execute(m_lc, m_diskFileFactory,m_parent.m_watchdog, m_threadID);
172
      m_threadStat += task->getTaskStats();
173
    }
174
175
    else {
      break;
176
    }
177
  } //end of while(1)
178
179
  m_threadStat.totalTime = totalTime.secs();
  m_parent.addThreadStats(m_threadStat);
Victor Kotlyar's avatar
Victor Kotlyar committed
180
  logWithStat(cta::log::INFO, "Finishing of DiskReadWorkerThread");
181
182
  // We now acknowledge to the task injector that read reached the end. There
  // will hence be no more requests for more. (last thread turns off the light)
183
184
  int remainingThreads = --m_parent.m_nbActiveThread;
  if (!remainingThreads) {
185
    m_parent.m_injector->finish();
Victor Kotlyar's avatar
Victor Kotlyar committed
186
187
    m_lc.log(cta::log::INFO, "Signalled to task injector the end of disk read threads");
    m_parent.logWithStat(cta::log::INFO, "All the DiskReadWorkerThreads have completed");
188
  } else {
Victor Kotlyar's avatar
Victor Kotlyar committed
189
    cta::log::ScopedParamContainer params(m_lc);
190
    params.add("remainingThreads", remainingThreads);
Victor Kotlyar's avatar
Victor Kotlyar committed
191
    m_lc.log(cta::log::DEBUG, "Will not signal the end to task injector yet");
192
  }
193
194
195
196
197
198
199
}

//------------------------------------------------------------------------------
// DiskReadWorkerThread::logWithStat
//------------------------------------------------------------------------------
void DiskReadThreadPool::DiskReadWorkerThread::
logWithStat(int level, const std::string& message){
Victor Kotlyar's avatar
Victor Kotlyar committed
200
  cta::log::ScopedParamContainer params(m_lc);
201
202
203
204
205
206
     params.add("threadReadWriteTime", m_threadStat.readWriteTime)
           .add("threadWaitFreeMemoryTime",m_threadStat.waitFreeMemoryTime)
           .add("threadCheckingErrorTime",m_threadStat.checkingErrorTime)
           .add("threadOpeningTime",m_threadStat.openingTime)
           .add("threadTransferTime",m_threadStat.transferTime)
           .add("threadTotalTime",m_threadStat.totalTime)
207
208
           .add("threadDataVolume",m_threadStat.dataVolume)
           .add("threadFileCount",m_threadStat.filesCount)
209
           .add("threadGlobalPayloadTransferSpeedMBps",
210
              m_threadStat.totalTime?1.0*m_threadStat.dataVolume/1000/1000/m_threadStat.totalTime:0)
211
           .add("threadAverageDiskPerformanceMBps",
212
              m_threadStat.transferTime?1.0*m_threadStat.dataVolume/1000/1000/m_threadStat.transferTime:0.0)
213
           .add("threadOpenRWCloseToTransferTimeRatio",
214
              m_threadStat.transferTime?(m_threadStat.openingTime+m_threadStat.readWriteTime+m_threadStat.closingTime)/m_threadStat.transferTime:0.0);
215
    m_lc.log(level,message);
216
}
217
}}}}
218