cmCTestMultiProcessHandler.cxx 9.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmCTestMultiProcessHandler.h"
#include "cmProcess.h"
#include "cmStandardIncludes.h"
#include "cmCTest.h"
21
22
#include "cmSystemTools.h"
#include <stdlib.h>
23
24
25
26

cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
{
  this->ParallelLevel = 1;
27
  this->Completed = 0;
28
29
30
}
  // Set the tests
void 
31
cmCTestMultiProcessHandler::SetTests(TestMap& tests,
32
                                     PropertiesMap& properties)
33
34
{
  // set test run map to false for all
35
  for(TestMap::iterator i = this->Tests.begin();
36
37
38
39
40
41
      i != this->Tests.end(); ++i)
    {
    this->TestRunningMap[i->first] = false;
    this->TestFinishMap[i->first] = false;
    }
  this->Tests = tests;
42
  this->Total = this->Tests.size();
43
  this->Properties = properties;
44
}
45

46
  // Set the max number of tests that can be run at the same time.
47
void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
48
{
49
  this->ParallelLevel = level < 1 ? 1 : level;
50
51
52
53
}

void cmCTestMultiProcessHandler::RunTests()
{
54
55
56
57
58
  if(this->CTest->GetBatchJobs())
    {
    this->SubmitBatchTests();
    return;
    }
59
  this->CheckResume();
60
  this->TestHandler->SetMaxIndex(this->FindMaxIndex());
61
62
63
64
65
66
67
68
69
70
  this->StartNextTests();
  while(this->Tests.size() != 0)
    {
    this->CheckOutput();
    this->StartNextTests();
    }
  // let all running tests finish
  while(this->CheckOutput())
    {
    }
71
  this->MarkFinished();
72
73
}

74
75
void cmCTestMultiProcessHandler::SubmitBatchTests()
{
Zach's avatar
Zach committed
76
  for(cmCTest::CTestConfigurationMap::iterator i =
77
78
      this->CTest->CTestConfiguration.begin();
      i != this->CTest->CTestConfiguration.end(); ++i)
79
    {
80
    cmCTestLog(this->CTest, HANDLER_OUTPUT, i->first 
Zach's avatar
Zach committed
81
               << " = " << i->second << std::endl);
82
83
84
    }
}

85
86
void cmCTestMultiProcessHandler::StartTestProcess(int test)
{
87
88
  cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, test << ": "
            << " test " << test << "\n");
89
90
91
  this->TestRunningMap[test] = true; // mark the test as running
  // now remove the test itself
  this->Tests.erase(test);
92
93
94
95
96
97
  cmCTestRunTest* testRun = new cmCTestRunTest;
  testRun->SetCTest(this->CTest);
  testRun->SetTestHandler(this->TestHandler);
  testRun->SetIndex(test);
  testRun->SetTestProperties(this->Properties[test]);
  if(testRun->StartTest())
98
    {
99
    this->RunningTests.insert(testRun);
100
101
102
    }
  else
    {
103
    testRun->EndTest(this->Completed, this->Total);
104
105
106
107
108
109
110
111
    }
}

bool cmCTestMultiProcessHandler::StartTest(int test)
{
  // copy the depend tests locally because when 
  // a test is finished it will be removed from the depend list
  // and we don't want to be iterating a list while removing from it
112
  TestSet depends = this->Tests[test];
113
114
115
  size_t totalDepends = depends.size();
  if(totalDepends)
    {
116
    for(TestSet::const_iterator i = depends.begin();
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
        i != depends.end(); ++i)
      {
      // if the test is not already running then start it
      if(!this->TestRunningMap[*i])
        {
        // this test might be finished, but since
        // this is a copy of the depend map we might
        // still have it
        if(!this->TestFinishMap[*i])
          {
          // only start one test in this function
          return this->StartTest(*i);
          }
        else
          {
          // the depend has been and finished
          totalDepends--;
          }
        }
      }
    }
  // if there are no depends left then run this test
  if(totalDepends == 0)
    {
    // Start this test it has no depends 
    this->StartTestProcess(test);
    return true;
    }
  // This test was not able to start because it is waiting 
  // on depends to run
147
  return false;
148
149
150
151
}

void cmCTestMultiProcessHandler::StartNextTests()
{
152
153
154
  //cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  //           << "Number of running tests : " << this->RunningTests.size()
  //           << "\n");
155
156
157
158
159
  size_t numToStart = this->ParallelLevel - this->RunningTests.size();
  if(numToStart == 0)
    {
    return;
    }
160
161
  TestMap tests = this->Tests;
  for(TestMap::iterator i = tests.begin();
162
163
      i !=  tests.end(); ++i)
    {
Zach's avatar
Zach committed
164
    //int processors = this->Properties[i->first]->Processors;
165
166
    
//    if(processors > )
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    // start test should start only one test
    if(this->StartTest(i->first))
      {
      numToStart--;
      }
    else
      {
      cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
             << "Test did not start waiting on depends to finish: " 
                 << i->first << "\n");
      }
    if(numToStart == 0 )
      {
      return;
      }
    }
}

bool cmCTestMultiProcessHandler::CheckOutput()
{
  // no more output we are done
  if(this->RunningTests.size() == 0)
    {
    return false;
    }
192
  std::vector<cmCTestRunTest*> finished;
193
  std::string out, err;
194
  for(std::set<cmCTestRunTest*>::const_iterator i = this->RunningTests.begin();
195
196
      i != this->RunningTests.end(); ++i)
    {
197
198
199
    cmCTestRunTest* p = *i;
    p->CheckOutput(); //reads and stores the process output
    
200
201
202
203
204
    if(!p->IsRunning())
      {
      finished.push_back(p);
      }
    }
205
206
  for( std::vector<cmCTestRunTest*>::iterator i = finished.begin();
       i != finished.end(); ++i)
207
    {
208
    this->Completed++;
209
210
    cmCTestRunTest* p = *i;
    int test = p->GetIndex();
211
212

    if(p->EndTest(this->Completed, this->Total))
213
      {
214
      this->Passed->push_back(p->GetTestProperties()->Name);
215
216
217
      }
    else
      {
218
      this->Failed->push_back(p->GetTestProperties()->Name);
219
220
221
      }
    for(TestMap::iterator j = this->Tests.begin();
       j!=  this->Tests.end(); ++j)
222
    {
223
    j->second.erase(test);
224
    }
225
226
227
    this->TestFinishMap[test] = true;
    this->TestRunningMap[test] = false;
    this->RunningTests.erase(p);
228
    this->WriteCheckpoint(test);
229
230

    delete p;
231
    }
232
  return true;
233
234
}

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
{
  std::string fname = this->CTest->GetBinaryDir()
    + "/Testing/Temporary/CTestCheckpoint.txt";
  std::fstream fout;
  fout.open(fname.c_str(), std::ios::app);
  fout << index << "\n";
  fout.close();
}

void cmCTestMultiProcessHandler::MarkFinished()
{
  std::string fname = this->CTest->GetBinaryDir()
    + "/Testing/Temporary/CTestCheckpoint.txt";
  cmSystemTools::RemoveFile(fname.c_str());
}

252
253
254
//---------------------------------------------------------------------
//For ShowOnly mode
void cmCTestMultiProcessHandler::PrintTestList()
255
{
256
257
258
  int count = 0;
  for (PropertiesMap::iterator it = this->Properties.begin();
       it != this->Properties.end(); it ++ )
259
    {
260
261
262
263
264
265
266
267
268
269
    count++;
    cmCTestTestHandler::cmCTestTestProperties& p = *it->second;

    cmCTestRunTest testRun;
    testRun.SetCTest(this->CTest);
    testRun.SetTestHandler(this->TestHandler);
    testRun.SetIndex(p.Index);
    testRun.SetTestProperties(&p);
    testRun.ComputeArguments(); //logs the command in verbose mode

Zach's avatar
Zach committed
270
    cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3)
271
             << count << "/");
Zach's avatar
Zach committed
272
    cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3)
273
274
275
276
277
278
             << this->Total << " ");
    if (this->TestHandler->MemCheck)
      {
      cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory Check");
      }
     else
279
      {
280
      cmCTestLog(this->CTest, HANDLER_OUTPUT, "Testing");
281
      }
282
283
    cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
    cmCTestLog(this->CTest, HANDLER_OUTPUT, p.Name.c_str() << std::endl);
284
285
286
    }
}

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//----------------------------------------------------------------
void cmCTestMultiProcessHandler::CheckResume()
{
  std::string fname = this->CTest->GetBinaryDir()
      + "/Testing/Temporary/CTestCheckpoint.txt";
  if(this->CTest->GetFailover())
    {
    if(cmSystemTools::FileExists(fname.c_str(), true))
      {
      *this->TestHandler->LogFile << "Resuming previously interrupted test set"
        << std::endl
        << "----------------------------------------------------------"
        << std::endl;
        
      std::ifstream fin;
      fin.open(fname.c_str());
      std::string line;
      while(std::getline(fin, line))
        {
        int index = atoi(line.c_str());
        this->RemoveTest(index);
        }
      fin.close();
      }
    }
  else
    {
    if(cmSystemTools::FileExists(fname.c_str(), true))
      {
      cmSystemTools::RemoveFile(fname.c_str());
      }
    }
}

void cmCTestMultiProcessHandler::RemoveTest(int index)
{
  this->Tests.erase(index);
  this->Properties.erase(index);
  this->TestRunningMap[index] = false;
  this->TestFinishMap[index] = true;
327
  this->Completed++;
328
}
329
330
331
332
333
334
335
336
337
338
339
340
341
342

int cmCTestMultiProcessHandler::FindMaxIndex()
{
  int max = 0;
  cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  for(; i != this->Tests.end(); ++i)
    {
    if(i->first > max)
      {
      max = i->first;
      }
    }
  return max;
}