Directory: | ./ |
---|---|
File: | src/phoenix_program_call.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 74 | 74 | 100.0% |
Branches: | 80 | 80 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <iostream> | ||
8 | |||
9 | #include "phoenix_color.h" | ||
10 | #include "phoenix_system.h" | ||
11 | #include "convertToString.h" | ||
12 | #include "phoenix_vector_split.h" | ||
13 | |||
14 | #include "lauchParallelThread.h" | ||
15 | #include "PMultiThreadProgress.h" | ||
16 | |||
17 | #include "phoenix_program_call.h" | ||
18 | |||
19 | |||
20 | ///Call a program | ||
21 | /** @param[out] call : program to be executed | ||
22 | */ | ||
23 | 16 | void phoenix_program_call(ProgramCall & call){ | |
24 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
|
16 | PString command(call.getCommand()); |
25 |
4/4✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
✓ Branch 6 taken 15 times.
✓ Branch 7 taken 1 times.
|
16 | if(call.getWorkingDirectory() != ""){ |
26 |
6/6✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 15 times.
✓ Branch 10 taken 15 times.
✓ Branch 13 taken 15 times.
✓ Branch 16 taken 15 times.
|
15 | command = "cd " + PString(call.getWorkingDirectory()) + ";" + command; |
27 | } | ||
28 |
4/4✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
✓ Branch 6 taken 15 times.
✓ Branch 7 taken 1 times.
|
16 | if(call.getLogFile() != ""){ |
29 |
1/1✓ Branch 1 taken 15 times.
|
15 | PString logContent(""); |
30 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
|
15 | call.setExitStatus(phoenix_popen(logContent, command)); |
31 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
|
15 | call.getLogFile().saveFileContent(logContent); |
32 | 15 | }else{ | |
33 |
8/8✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 22 taken 1 times.
|
1 | std::cout << termCyan() << "Call command '"<<command<<"'" << termDefault() << std::endl; |
34 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | call.setExitStatus(system(command.c_str())); |
35 | } | ||
36 | 16 | } | |
37 | |||
38 | ///Call a vector of program | ||
39 | /** @param[out] vecCall : vector of program to be executed | ||
40 | */ | ||
41 | 4 | void phoenix_program_callVec(VecProgramCall & vecCall){ | |
42 |
2/2✓ Branch 4 taken 12 times.
✓ Branch 5 taken 4 times.
|
16 | for(VecProgramCall::iterator it(vecCall.begin()); it != vecCall.end(); ++it){ |
43 |
1/1✓ Branch 2 taken 12 times.
|
12 | phoenix_program_call(*it); |
44 | } | ||
45 | 4 | } | |
46 | |||
47 | ///Exectute programs on several threads | ||
48 | /** @param[out] vecCall : vector of commands to be exectuted | ||
49 | * @param nbThread : number of thread to be used | ||
50 | */ | ||
51 | 2 | void phoenix_program_callParallel(VecProgramCall & vecCall, size_t nbThread){ | |
52 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if(vecCall.size() == 0lu){return;} |
53 | 1 | lauchParallelThread(vecCall, nbThread, phoenix_program_callVec); | |
54 | } | ||
55 | |||
56 | ///Call a vector of program | ||
57 | /** @param[out] progress : multithread progress bar to be used | ||
58 | * @param[out] vecCall : vector of program to be executed | ||
59 | * @param threadIndex : index of the current thread | ||
60 | */ | ||
61 | 2 | void phoenix_program_callVecProgress(PMultiThreadProgress & progress, VecProgramCall & vecCall, size_t threadIndex){ | |
62 |
3/3✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
|
2 | size_t indexProgressBar = progress.addProgressBar("Progress of thread " + valueToString(threadIndex), vecCall.size()); |
63 |
2/2✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2 times.
|
6 | for(VecProgramCall::iterator it(vecCall.begin()); it != vecCall.end(); ++it){ |
64 |
1/1✓ Branch 2 taken 4 times.
|
4 | phoenix_program_call(*it); |
65 |
1/1✓ Branch 1 taken 4 times.
|
4 | progress.incrementProgress(indexProgressBar); //Update the progress bar |
66 | } | ||
67 | 2 | } | |
68 | |||
69 | ///Exectute programs on several threads (with a multithread progress bar) | ||
70 | /** @param[out] vecVecProgramCall : vector of commands to be exectuted (already split over threads) | ||
71 | */ | ||
72 | 1 | void phoenix_program_callParallelProgressBar(VecVecProgramCall & vecVecProgramCall){ | |
73 | 1 | size_t nbThread = vecVecProgramCall.size(); //In case we need less thread than required | |
74 |
1/1✓ Branch 1 taken 1 times.
|
1 | PMultiThreadProgress progress(nbThread); |
75 | 1 | std::vector<std::thread> vecThread; | |
76 |
1/1✓ Branch 1 taken 1 times.
|
1 | vecThread.resize(nbThread); |
77 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::thread threadProgress(phoenix_print_parallel_progress, std::ref(progress), 1); |
78 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for(size_t i(0lu); i < nbThread; ++i){ |
79 |
1/1✓ Branch 4 taken 2 times.
|
2 | vecThread[i] = std::thread(phoenix_program_callVecProgress, std::ref(progress), std::ref(vecVecProgramCall[i]), i); |
80 | } | ||
81 |
1/1✓ Branch 1 taken 1 times.
|
1 | threadProgress.join(); |
82 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for(size_t i(0lu); i < nbThread; ++i){ |
83 |
1/1✓ Branch 2 taken 2 times.
|
2 | vecThread[i].join(); |
84 | } | ||
85 |
1/1✓ Branch 1 taken 1 times.
|
1 | size_t nbSuccess(phoenix_program_getNbSuccessCall(vecVecProgramCall)); |
86 |
1/1✓ Branch 1 taken 1 times.
|
1 | size_t nbFail(phoenix_program_getNbCall(vecVecProgramCall) - nbSuccess); |
87 |
6/6✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
|
1 | std::cout << "VecProgramCall : summary : " << nbSuccess << " success, " << nbFail << " fail" << std::endl; |
88 | 1 | } | |
89 | |||
90 | ///Exectute programs on several threads (with a multithread progress bar) | ||
91 | /** @param[out] vecCall : vector of commands to be exectuted | ||
92 | * @param nbThread : number of thread to be used | ||
93 | */ | ||
94 | 2 | void phoenix_program_callParallelProgressBar(VecProgramCall & vecCall, size_t nbThread){ | |
95 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if(vecCall.size() == 0lu){return;} |
96 | 1 | VecVecProgramCall vecVecProgramCall; | |
97 |
1/1✓ Branch 1 taken 1 times.
|
1 | phoenix_vector_split(vecVecProgramCall, vecCall, nbThread); |
98 |
1/1✓ Branch 1 taken 1 times.
|
1 | phoenix_program_callParallelProgressBar(vecVecProgramCall); |
99 | 1 | } | |
100 | |||
101 | ///Add a program call | ||
102 | /** @param[out] vecCall : vector of program call | ||
103 | * @param name : name of the call | ||
104 | * @param command : command of the call | ||
105 | * @param workingDirectory : directory in which to call the program | ||
106 | * @param logFile : log file of the call | ||
107 | */ | ||
108 | 16 | void phoenix_program_addcall(VecProgramCall & vecCall, const PString & name, const PString & command, | |
109 | const PPath & workingDirectory, const PPath & logFile) | ||
110 | { | ||
111 |
1/1✓ Branch 1 taken 16 times.
|
16 | ProgramCall call; |
112 |
1/1✓ Branch 1 taken 16 times.
|
16 | call.setName(name); |
113 |
1/1✓ Branch 1 taken 16 times.
|
16 | call.setCommand(command); |
114 |
1/1✓ Branch 1 taken 16 times.
|
16 | call.setWorkingDirectory(workingDirectory); |
115 |
1/1✓ Branch 1 taken 16 times.
|
16 | call.setLogFile(logFile); |
116 |
1/1✓ Branch 1 taken 16 times.
|
16 | call.setExitStatus(0); |
117 |
1/1✓ Branch 1 taken 16 times.
|
16 | vecCall.push_back(call); |
118 | 16 | } | |
119 | |||
120 | ///Get number of call result of a given type | ||
121 | /** @param vecCall : vector of call | ||
122 | * @param result : expected result | ||
123 | * @return number of call with given result | ||
124 | */ | ||
125 | 6 | size_t phoenix_program_getNbCallExitStatusType(const VecProgramCall & vecCall, int result){ | |
126 | 6 | size_t nbSuccessCall(0lu); | |
127 |
2/2✓ Branch 3 taken 22 times.
✓ Branch 4 taken 6 times.
|
28 | for(VecProgramCall::const_iterator it(vecCall.begin()); it != vecCall.end(); ++it){ |
128 |
1/1✓ Branch 2 taken 22 times.
|
22 | nbSuccessCall += it->getExitStatus() == result; |
129 | } | ||
130 | 6 | return nbSuccessCall; | |
131 | } | ||
132 | |||
133 | ///Get number of call result of a given type | ||
134 | /** @param vecCall : vector of vector of call | ||
135 | * @param result : expected result | ||
136 | * @return number of call with given result | ||
137 | */ | ||
138 | 2 | size_t phoenix_program_getNbCallExitStatusType(const VecVecProgramCall & vecCall, int result){ | |
139 | 2 | size_t nbSuccessCall(0lu); | |
140 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
|
5 | for(VecVecProgramCall::const_iterator it(vecCall.begin()); it != vecCall.end(); ++it){ |
141 |
1/1✓ Branch 2 taken 3 times.
|
3 | nbSuccessCall += phoenix_program_getNbCallExitStatusType(*it, result); |
142 | } | ||
143 | 2 | return nbSuccessCall; | |
144 | } | ||
145 | |||
146 | ///Get the number of success call | ||
147 | /** @param vecCall : vector of call | ||
148 | * @return number of success call | ||
149 | */ | ||
150 | 3 | size_t phoenix_program_getNbSuccessCall(const VecProgramCall & vecCall){ | |
151 | 3 | return phoenix_program_getNbCallExitStatusType(vecCall, 0); | |
152 | } | ||
153 | |||
154 | ///Get the number of success call | ||
155 | /** @param vecCall : vector of vector of call | ||
156 | * @return number of success call | ||
157 | */ | ||
158 | 2 | size_t phoenix_program_getNbSuccessCall(const VecVecProgramCall & vecCall){ | |
159 | 2 | return phoenix_program_getNbCallExitStatusType(vecCall, 0); | |
160 | } | ||
161 | |||
162 | ///Get the number of call | ||
163 | /** @param vecCall : vector of vector of call | ||
164 | * @return number of call | ||
165 | */ | ||
166 | 2 | size_t phoenix_program_getNbCall(const VecVecProgramCall & vecCall){ | |
167 | 2 | size_t nbCall(0lu); | |
168 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
|
5 | for(VecVecProgramCall::const_iterator it(vecCall.begin()); it != vecCall.end(); ++it){ |
169 | 3 | nbCall += it->size(); | |
170 | } | ||
171 | 2 | return nbCall; | |
172 | } | ||
173 | |||
174 |