File: CJobObject.cpp

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-13.2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 57,848 kB
  • sloc: ansic: 432,690; java: 87,068; cpp: 31,958; sh: 29,445; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,705; csh: 3,934; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (542 lines) | stat: -rw-r--r-- 16,705 bytes parent folder | download | duplicates (7)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/
#include "stdafx.h"
#include <windows.h>
#include <winbase.h>
#include <userenv.h>
#include <shlwapi.h>

#include <iostream>

#include "CBasicTypes.h"
#include "CJobObject.h"
#include "CError.h"
#include "CException.h"
#include "CIoCompletionPort.h"
#include "CMutex.h"
#include "CSehTranslator.h"
#include "CThread.h"

using namespace std;

namespace GridEngine {

CJobObject::CJobIoCompletionPort::CJobIoCompletionPort(PVOID lpParameter, DWORD dwCompletionKey, DWORD dwCuncurrentThreads, DWORD dwTimeout) :
   CIoCompletionPort(lpParameter, dwCompletionKey, dwCuncurrentThreads, dwTimeout)
{
}

DWORD CJobObject::CJobIoCompletionPort::IoCompletionPortMain(PVOID lpParameter, CStatus TheStatus) {
   CJobObject *TheJob = (CJobObject*)(lpParameter);

   switch (TheStatus.dwNumberOfBytes) {
      case JOB_OBJECT_MSG_END_OF_JOB_TIME:
         TheJob->ActionEndOfJobTime();
         break;
      case JOB_OBJECT_MSG_END_OF_PROCESS_TIME:
         TheJob->ActionEndOfProcessTime();
         break;
      case JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT:
         TheJob->ActionActiveProcessLimit();
         break;
      case JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS:
         TheJob->ActionAbnormalExitProcess();
         break;
      case JOB_OBJECT_MSG_JOB_MEMORY_LIMIT:
         TheJob->ActionJobMemoryLimit();
         break;
      case JOB_OBJECT_MSG_PROCESS_MEMORY_LIMIT:
         TheJob->ActionProcessMemoryLimit();
         break;
      case JOB_OBJECT_MSG_NEW_PROCESS:
         TheJob->ActionNewProcess();
         break;
      case JOB_OBJECT_MSG_EXIT_PROCESS:
         TheJob->ActionExitProcess();
         break;
      case JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO:
         TheJob->m_JobTerminated = true;
         TheJob->ActionActiveProcessZero();
         break;
   }
   return 0;
}

CJobObject::CJobObject() :
   m_dwJobId(0),
   m_JobTerminated(FALSE),
   m_Processes(),
   m_dwBuffer(NULL),
   m_pCompletionPort(NULL)
{
   CMutex::CEnter MutexEnter(m_Mutex);

   CreateNewJob();
   AssignCompletionPort();
}

CJobObject::CJobObject(DWORD dwJobId) :
   m_dwJobId(dwJobId),
   m_JobTerminated(FALSE),
   m_Processes(),
   m_dwBuffer(NULL),
   m_pCompletionPort(NULL)
{
   CMutex::CEnter MutexEnter(m_Mutex);

   OpenExistingJob();

   // It is not possible to assign a second,third... completion port
   // I don't know why

//   AssignCompletionPort();
}

CJobObject::~CJobObject() {
   CMutex::CEnter MutexEnter(m_Mutex);
   if (m_pCompletionPort) {
      delete m_pCompletionPort;
   }
}

void CJobObject::AssignProcess(CProcess& Process) {
   BOOL bRet;
   CMutex::CEnter MutexEnter(m_Mutex);

   bRet = AssignProcessToJobObject(GetHandle(), Process.GetHandle());   
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   }
}

void CJobObject::AssignCompletionPort() {
   CMutex::CEnter MutexEnter(m_Mutex);
   BOOL bRet;
   JOBOBJECT_ASSOCIATE_COMPLETION_PORT ObjectInfo;

   m_pCompletionPort = new CJobIoCompletionPort(this, 0, 0, INFINITE);

   ObjectInfo.CompletionKey = 0;
   ObjectInfo.CompletionPort = m_pCompletionPort->GetHandle();
   bRet = SetInformationJobObject(GetHandle(), JobObjectAssociateCompletionPortInformation, 
      &ObjectInfo, sizeof(JOBOBJECT_ASSOCIATE_COMPLETION_PORT));
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   }

}

DWORD CJobObject::ActionEndOfJobTime() {
   return 0;
}

DWORD CJobObject::ActionEndOfProcessTime() {
   return 0;
}

DWORD CJobObject::ActionActiveProcessLimit() {
   return 0;
}

DWORD CJobObject::ActionAbnormalExitProcess() {
   return 0;
}

DWORD CJobObject::ActionJobMemoryLimit() {
   return 0;
}

DWORD CJobObject::ActionProcessMemoryLimit() {
   return 0;
}

DWORD CJobObject::ActionNewProcess() {
   return 0;
}

DWORD CJobObject::ActionExitProcess() {
   return 0;
}

DWORD CJobObject::ActionActiveProcessZero() {
   return 0;
}

void CJobObject::CreateNewJob() {
   CMutex::CEnter MutexEnter(m_Mutex);
   static const DWORD dwMaxBufferSize = 1024;
   WCHAR pszBuffer[dwMaxBufferSize];
   CString JobName;
   BOOL bError;
   HANDLE hJob;

   m_dwJobId = 0;
   bError = true;
   while (bError) {
      m_dwJobId++;
      if (m_dwJobId == 0) {
         m_dwJobId++;
      }
      wsprintf(pszBuffer, L"%ld", m_dwJobId);
      JobName = pszBuffer;

      hJob = CreateJobObject(0, const_cast<PWSTR>(JobName.c_str()));
      if(hJob == NULL) {
         bError = true;
      } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
         bError = true;
      } else {
         bError = false;
         SetHandle(hJob);
      }
   }
}

void CJobObject::SetExtendedLimit(const JOBOBJECT_EXTENDED_LIMIT_INFORMATION& pLimit) {
   static BOOL bAlreadySet = FALSE;
   BOOL bRet;

   if (bAlreadySet == FALSE) {
      JOBOBJECT_END_OF_JOB_TIME_INFORMATION EndOfJobTimeAction;

      EndOfJobTimeAction.EndOfJobTimeAction = JOB_OBJECT_POST_AT_END_OF_JOB;
      bRet = SetInformationJobObject(GetHandle(), JobObjectEndOfJobTimeInformation,
         (PVOID) &EndOfJobTimeAction, sizeof(JOBOBJECT_END_OF_JOB_TIME_INFORMATION));
   }
   bRet = SetInformationJobObject(GetHandle(), JobObjectExtendedLimitInformation,
      (PVOID) &pLimit, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   }
}

const JOBOBJECT_EXTENDED_LIMIT_INFORMATION& CJobObject::GetExtendedLimit() const {
   BOOL bRet;

   bRet = QueryInformationJobObject(GetHandle(), JobObjectExtendedLimitInformation,
      (PVOID) &m_Limit, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION), NULL);
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   }
   return m_Limit;
}

DWORD CJobObject::GetId() {
   return m_dwJobId;
}

const JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION& CJobObject::GetExtendedUsageInfo() const{
   BOOL bRet;

   bRet = QueryInformationJobObject(GetHandle(), JobObjectBasicAndIoAccountingInformation,
      (PVOID) &m_UsageInfo, sizeof(JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION), NULL);
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   }
   return m_UsageInfo;
}


void CJobObject::OpenExistingJob() {
   CMutex::CEnter MutexEnter(m_Mutex);
   static const DWORD dwMaxBufferSize = 1024;
   WCHAR pszBuffer[dwMaxBufferSize];
   CString JobName;
   HANDLE hJob;

   if (m_dwJobId != 0) {
      wsprintf(pszBuffer, L"%ld", m_dwJobId);
      JobName = pszBuffer;
      hJob = CreateJobObject(0, const_cast<PWSTR>(JobName.c_str()));

      if(hJob == NULL) {
         throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
      } else {
         SetHandle(hJob);
      }
   } else {
      throw CCodineException(L"Can not create a job object with id 0", __FILE__, __LINE__);
   }
}

void CJobObject::Resume() {
   CMutex::CEnter MutexEnter(m_Mutex);
   PJOBOBJECT_BASIC_PROCESS_ID_LIST pPidList;

   pPidList = CreatePidList();

   if (pPidList == NULL) {
      throw CCodineException(L"Pid List is NULL", __FILE__, __LINE__);
   } else {
      for (int i = 0; i < pPidList->NumberOfProcessIdsInList; i++) {
         try {
            CProcess NewProcess(pPidList->ProcessIdList[i]);

            NewProcess.Resume();
         } catch (CCodineException&) {
            ;
         }
      }
   }
   DestroyPidList();
}

void CJobObject::Suspend() {
   CMutex::CEnter MutexEnter(m_Mutex);

   map<DWORD, int> ErrorProcesses;             
   map<DWORD, int>::iterator ErrorProcess;
   map<DWORD, BOOL> Processes;             
   map<DWORD, BOOL>::iterator CurrentProcess;
   map<DWORD, CProcess*>::iterator OldProcess;
   BOOL bNewProcesses;

   // Clear all Threads contained in the m_Threads member
   for (OldProcess = m_Processes.begin();
        OldProcess != m_Processes.end();
        OldProcess++) {
      delete OldProcess->second;
   }
   m_Processes.clear();

   
   // A)

   // Seach all threads which belong to this process and suspend them. To do this we create
   // a snapshot and suspend all threads contained in the snapshot.
   // Between A1 and A2 it is possible that new threads will be created. This makes it necessary to 
   // create a new snapshot and to suspend all new threads which we not suspended in a previous 
   // iteration.

   bNewProcesses = TRUE;
   while (bNewProcesses) {
      PJOBOBJECT_BASIC_PROCESS_ID_LIST pPidList;

      bNewProcesses = FALSE;
      pPidList = CreatePidList();
      // A1

      if (pPidList == NULL) {
         throw CCodineException(L"Pid List is NULL", __FILE__, __LINE__);
      } else {
         for (int i = 0; i < pPidList->NumberOfProcessIdsInList; i++) {
            if (m_Processes.find(pPidList->ProcessIdList[i]) == m_Processes.end()) {
               CProcess *NewProcess = NULL;

               try {
                  NewProcess = new CProcess(pPidList->ProcessIdList[i]);
                  NewProcess->Suspend();
                  m_Processes[pPidList->ProcessIdList[i]] = NewProcess;
                  Processes[pPidList->ProcessIdList[i]] = true;
                  bNewProcesses = TRUE;
               } catch (CCodineException& ex) {
                  ErrorProcess = ErrorProcesses.find(pPidList->ProcessIdList[i]);
                  if (ErrorProcess == ErrorProcesses.end() 
                      || (ErrorProcess != ErrorProcesses.end() && ErrorProcess->second < 3)) {

                     // Possible reasons for the exception
                     //    * We didn't get a handle
                     //       - Thread exited
                     //    * Thread belongs to a different process 
                     //      (no access)
                     //       - Thread exited and another process created 
                     //         a thread with the same id between A1 and A2
                     //
                     // => we try it once more -> the next snapshot won't 
                     //    contain the current thread

                     CTrace::Print(CTrace::Layer::TEST, L"Catched Exception during suspended"
                        L" for thread %ld", pPidList->ProcessIdList[i]);

                     ErrorProcesses[pPidList->ProcessIdList[i]]++;
                     bNewProcesses = TRUE;
                  } else {

                     // If we get an error three times for the same thread
                     // then we will assume that this thread belongs really 
                     // to this process
                     //
                     // => we could not solve the problem => ERROR
                     throw ex;
                  }
               }
            }
         }
         // A2
      }
      DestroyPidList();
   }      

   // B

   // Between A and B we created  a map of suspended threads (Threads) 
   // but we can not be sure that all these threads belong to this process 
   // (=> recycling of thread id's). It is possible, that we suspended 
   // threads of foreign processes!. Now we will set pair.second to false 
   // for all threads which belong to the current process
   {
      PJOBOBJECT_BASIC_PROCESS_ID_LIST pPidList;
      int i;


      pPidList = CreatePidList();
      if (pPidList == NULL) {
         throw CCodineException(L"Pid List is NULL", __FILE__, __LINE__);
      } else {
         try {
            for (i = 0; i < pPidList->NumberOfProcessIdsInList; i++) {
               CurrentProcess = Processes.find(pPidList->ProcessIdList[i]);
               if (CurrentProcess == Processes.end()) {
                  // If we get a new thread id here, then someone else
                  // created a new thread for this process (CreateRemoteThread())
                  CTrace::Print(CTrace::Layer::TEST, L"Unexpected new thread %ld", pPidList->ProcessIdList[i]);
                  throw CCodineException(L"", __FILE__, __LINE__);
               } else {
                  CurrentProcess->second = FALSE;
               }
            }
         } catch (CCodineException& ex) {
            map<DWORD, CProcess*>::iterator TheProcess;

            // If we come here then we will rollback previous activities
            // Therefore we have to resume all threads contained in Threads            
            for (TheProcess = m_Processes.begin();
                 TheProcess != m_Processes.end();
                 TheProcess++) {
               try {
                  (*TheProcess).second->Resume();
               } catch (CCodineException&) {
                  ;
               }
            }
            throw ex;
         }
      }
      DestroyPidList();
   }

   // C

   // Between B and C we set pair.second to false for all threads 
   // which belong to the current process
   // Now we have to resume the remaining threads
   // We will also add the suspended threads of this process to
   // the member m_Threads. 
   try {
      // Resume and copy
      for (CurrentProcess = Processes.begin();
           CurrentProcess != Processes.end();
           CurrentProcess++) {
         if (CurrentProcess->second == TRUE) {
            CProcess *ForeignProcess = m_Processes[CurrentProcess->first];

            ForeignProcess->Resume();
            m_Processes.erase(CurrentProcess->first);
            CTrace::Print(CTrace::Layer::TEST, L"Resumed foreign thread %ld", CurrentProcess->first);
            break;
         } 
      }
   } catch (CCodineException& ex) {
   
      // What can we do here?

      throw ex;
   }
}

PJOBOBJECT_BASIC_PROCESS_ID_LIST CJobObject::CreatePidList() const{
   DWORD dwMaxEntries = 8;
   JOBOBJECT_BASIC_PROCESS_ID_LIST *Structure;


   DestroyPidList();
   do {
      BOOL bRet;

      if (m_dwBuffer) {
         dwMaxEntries *= 2;
         delete [] m_dwBuffer;
         m_dwBuffer = NULL;
      }
      m_dwBuffer = new DWORD[dwMaxEntries + 2];
      Structure = (JOBOBJECT_BASIC_PROCESS_ID_LIST*)m_dwBuffer;

      memset(m_dwBuffer, 0, sizeof(m_dwBuffer));
      bRet = QueryInformationJobObject(GetHandle(), JobObjectBasicProcessIdList, 
         m_dwBuffer, sizeof(DWORD) * (dwMaxEntries + 2), NULL);
      if (bRet == 0 && GetLastError() != ERROR_MORE_DATA) {
         delete [] m_dwBuffer;
         throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
      }
   } while (Structure->NumberOfAssignedProcesses > Structure->NumberOfProcessIdsInList);

   return Structure;
}

void CJobObject::DestroyPidList() const {
   if (m_dwBuffer) {
      delete [] m_dwBuffer;
      m_dwBuffer = NULL;
   }
}

void CJobObject::Terminate(DWORD dwExitCode) {
   CMutex::CEnter MutexEnter(m_Mutex);
   BOOL bRet;

   bRet = TerminateJobObject(GetHandle(), dwExitCode);
   if (bRet == 0) {
      throw CCodineException(CError::GetErrorMessage(GetLastError()), __FILE__, __LINE__);
   }
}


void CJobObject::WaitForObject(DWORD dwMilliseconds) const {
   PJOBOBJECT_BASIC_PROCESS_ID_LIST pPidList;

   while ((pPidList = CreatePidList())) {
      DWORD dwProcesses = pPidList->NumberOfProcessIdsInList;

      DestroyPidList();
      if (dwProcesses == 0) {
         break;
      } else if (dwProcesses > 1) {
         Sleep(1000);
      } else if (dwProcesses > 10) {
         Sleep(2000);
      } else if (dwProcesses > 100) {
         Sleep(3000);
      } 
   }
}

} // namespace