Logo ROOT   6.18/05
Reference Guide
TMPWorker.h
Go to the documentation of this file.
1/* @(#)root/multiproc:$Id$ */
2// Author: Enrico Guiraud July 2015
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TMPWorker
13#define ROOT_TMPWorker
14
15#include "MPCode.h"
16#include "MPSendRecv.h" //MPCodeBufPair
17#include "PoolUtils.h"
18#include "TSysEvtHandler.h" //TFileHandler
19
20#include <memory> //unique_ptr
21#include <string>
22#include <sstream>
23#include <type_traits> //std::result_of
24#include <unistd.h> //pid_t
25
26class TMPWorker {
27public:
29 fProcessedEntries(0), fS(), fPid(0), fNWorker(0) { }
30 TMPWorker(unsigned nWorkers, ULong64_t maxEntries)
31 : fNWorkers(nWorkers), fMaxNEntries(maxEntries),
32 fProcessedEntries(0), fS(), fPid(0), fNWorker(0) { }
33 virtual ~TMPWorker() { }
34 //it doesn't make sense to copy a TMPWorker (each one has a uniq_ptr to its socket)
35 TMPWorker(const TMPWorker &) = delete;
36 TMPWorker &operator=(const TMPWorker &) = delete;
37
38 virtual void Init(int fd, unsigned workerN);
39 void Run();
40 TSocket *GetSocket() { return fS.get(); }
41 pid_t GetPid() { return fPid; }
42 unsigned GetNWorker() const { return fNWorker; }
43
44protected:
45 std::string fId; ///< identifier string in the form W<nwrk>|P<proc id>
46 unsigned fNWorkers; ///< the number of workers spawned
47 ULong64_t fMaxNEntries; ///< the maximum number of entries to be processed by this worker
48 ULong64_t fProcessedEntries; ///< the number of entries processed by this worker so far
49
50 void SendError(const std::string& errmsg, unsigned int code = MPCode::kError);
51
52private:
53 virtual void HandleInput(MPCodeBufPair &msg);
54
55 std::unique_ptr<TSocket> fS; ///< This worker's socket. The unique_ptr makes sure resources are released.
56 pid_t fPid; ///< the PID of the process in which this worker is running
57 unsigned fNWorker; ///< the ordinal number of this worker (0 to nWorkers-1)
58};
59
60#endif
std::pair< unsigned, std::unique_ptr< TBufferFile > > MPCodeBufPair
An std::pair that wraps the code and optional object contained in a message.
Definition: MPSendRecv.h:31
unsigned long long ULong64_t
Definition: RtypesCore.h:70
This class works in conjuction with TMPClient, reacting to messages received from it as specified by ...
Definition: TMPWorker.h:26
void SendError(const std::string &errmsg, unsigned int code=MPCode::kError)
Error sender.
Definition: TMPWorker.cxx:115
unsigned GetNWorker() const
Definition: TMPWorker.h:42
std::string fId
identifier string in the form W<nwrk>|P<proc id>
Definition: TMPWorker.h:45
TMPWorker()
Definition: TMPWorker.h:28
pid_t GetPid()
Definition: TMPWorker.h:41
ULong64_t fMaxNEntries
the maximum number of entries to be processed by this worker
Definition: TMPWorker.h:47
ULong64_t fProcessedEntries
the number of entries processed by this worker so far
Definition: TMPWorker.h:48
TSocket * GetSocket()
Definition: TMPWorker.h:40
unsigned fNWorker
the ordinal number of this worker (0 to nWorkers-1)
Definition: TMPWorker.h:57
virtual void HandleInput(MPCodeBufPair &msg)
Handle a message with an EMPCode.
Definition: TMPWorker.cxx:89
void Run()
Definition: TMPWorker.cxx:64
pid_t fPid
the PID of the process in which this worker is running
Definition: TMPWorker.h:56
virtual ~TMPWorker()
Definition: TMPWorker.h:33
std::unique_ptr< TSocket > fS
This worker's socket. The unique_ptr makes sure resources are released.
Definition: TMPWorker.h:55
TMPWorker(unsigned nWorkers, ULong64_t maxEntries)
Definition: TMPWorker.h:30
virtual void Init(int fd, unsigned workerN)
This method is called by children processes right after forking.
Definition: TMPWorker.cxx:55
TMPWorker(const TMPWorker &)=delete
unsigned fNWorkers
the number of workers spawned
Definition: TMPWorker.h:46
TMPWorker & operator=(const TMPWorker &)=delete
@ kError
Error message.
Definition: MPCode.h:47