1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
2 | // Eric Aubourg
|
---|
3 | // Christophe Magneville
|
---|
4 | // Reza Ansari
|
---|
5 | // $Id: toimanager.cc,v 1.17 2002-09-09 15:33:15 aubourg Exp $
|
---|
6 |
|
---|
7 | #include "toimanager.h"
|
---|
8 | #include <limits.h>
|
---|
9 | #include <pthread.h>
|
---|
10 | #include <iostream.h>
|
---|
11 | #include <unistd.h>
|
---|
12 | #include <map>
|
---|
13 |
|
---|
14 | #ifndef MAXINT
|
---|
15 | #define MAXINT 2147483647
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | TOIManager::TOIManager() {
|
---|
19 | reqBegin = 0;
|
---|
20 | reqEnd = MAXINT;
|
---|
21 |
|
---|
22 | // -----------ajout cgt vf 19/08/2002
|
---|
23 | // par defaut TOISegmented
|
---|
24 | selectTOISegmented(1024, 20);
|
---|
25 | // ----------- fin ajout cgt
|
---|
26 |
|
---|
27 | }
|
---|
28 |
|
---|
29 | TOIManager* TOIManager::instance = NULL;
|
---|
30 |
|
---|
31 | TOIManager* TOIManager::getManager() {
|
---|
32 | if (instance == NULL) instance = new TOIManager();
|
---|
33 | return instance;
|
---|
34 | }
|
---|
35 |
|
---|
36 | // ajout vf 26/07/2002
|
---|
37 |
|
---|
38 | // enregistrement d'un processeur dans la liste des processeurs pour une execution en groupe
|
---|
39 |
|
---|
40 | void TOIManager::registerProcessor(TOIProcessor* proc) {
|
---|
41 |
|
---|
42 | cout << "Adding processor to TOIManager for group execution" << endl;
|
---|
43 | processors.push_back(proc);
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | // demarrage de tous les processeurs et verification auto des samplenum pour chaque processeur parametre
|
---|
49 |
|
---|
50 | void TOIManager::startAll() {
|
---|
51 | // verification des samplenum
|
---|
52 | bool samples_ok=checkSamplesLimits(1);
|
---|
53 | if (samples_ok) {
|
---|
54 | cout << "All limits ok" << endl << "Starting processors" << endl;
|
---|
55 | } else {
|
---|
56 | cout << "One or more limits ajusted for execution" << endl << "Starting processors" << endl;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // mise a jour des limites apres verification
|
---|
60 | checkSamplesLimits(2);
|
---|
61 | checkSamplesLimits(3);
|
---|
62 |
|
---|
63 | // debogage affichage des limites apres calcul
|
---|
64 | for (vector<TOIProcessor*>::iterator i = processors.begin();
|
---|
65 | i != processors.end(); i++) {
|
---|
66 | TOIProcessor* proc = *i;
|
---|
67 | proc->printLimits();
|
---|
68 | }
|
---|
69 |
|
---|
70 | // demarrage
|
---|
71 | for (vector<TOIProcessor*>::iterator i = processors.begin();
|
---|
72 | i != processors.end(); i++) {
|
---|
73 | TOIProcessor* proc = *i;
|
---|
74 | cout << "**********************" << endl;
|
---|
75 | cout << "starting processor " << endl;
|
---|
76 | proc->start();
|
---|
77 | cout << "processor started " << endl;
|
---|
78 | }
|
---|
79 | cout << "**********************" << endl;
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool TOIManager::checkSamplesLimits(int pass)
|
---|
83 | {
|
---|
84 | bool processor_ok=true;
|
---|
85 | bool samples_ok=true;
|
---|
86 | for (vector<TOIProcessor*>::iterator i = processors.begin();
|
---|
87 | i != processors.end(); i++) {
|
---|
88 | TOIProcessor* proc = *i;
|
---|
89 | cout << "testing processor limits " << endl;
|
---|
90 | // test du processeur
|
---|
91 |
|
---|
92 | // test seulement pour les processor cle
|
---|
93 | //if (proc->getRequested()) {
|
---|
94 | processor_ok = proc->checkSampleLimits(pass);
|
---|
95 | //}
|
---|
96 |
|
---|
97 | if (processor_ok) {
|
---|
98 | cout << "processor limits ok " << endl;
|
---|
99 | } else {
|
---|
100 | cout << "processor limits ajusted" << endl;
|
---|
101 | samples_ok = false;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return samples_ok;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // fin ajout vf
|
---|
108 |
|
---|
109 | void TOIManager::setRequestedSample(int begin, int end) {
|
---|
110 | reqBegin = begin;
|
---|
111 | reqEnd = end;
|
---|
112 | }
|
---|
113 |
|
---|
114 | int TOIManager::getRequestedBegin() {
|
---|
115 | return reqBegin;
|
---|
116 | }
|
---|
117 |
|
---|
118 | int TOIManager::getRequestedEnd() {
|
---|
119 | return reqEnd;
|
---|
120 | }
|
---|
121 |
|
---|
122 | void TOIManager::addThread(pthread_t* t) {
|
---|
123 | // cout << "adding thread " << t << endl;
|
---|
124 | threads.push_back(t);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void TOIManager::joinAll() {
|
---|
128 | waitForAll();
|
---|
129 | }
|
---|
130 |
|
---|
131 | void TOIManager::waitForAll() {
|
---|
132 | for (vector<pthread_t*>::iterator i = threads.begin();
|
---|
133 | i != threads.end(); i++) {
|
---|
134 | pthread_t* pth = *i;
|
---|
135 | cout << "joining thread " << pth << endl;
|
---|
136 | pthread_join(*pth, NULL);
|
---|
137 | cout << "thread joined " << pth << endl;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | // -----------ajout cgt vf 19/08/2002
|
---|
143 |
|
---|
144 |
|
---|
145 | void TOIManager::selectTOISegmented(int bufsz, int maxseg)
|
---|
146 | {
|
---|
147 | fgSegmented = true;
|
---|
148 | segBuffsz = bufsz;
|
---|
149 | segMaxseg = maxseg;
|
---|
150 | }
|
---|
151 |
|
---|
152 | void TOIManager::selectTOISeqBuffered(int wsz)
|
---|
153 | {
|
---|
154 | fgSegmented = false;
|
---|
155 | segBuffsz = wsz;
|
---|
156 | }
|
---|
157 |
|
---|
158 | // methode connect de cgt simplifiee et corrigee
|
---|
159 | TOI& TOIManager::connect(TOIProcessor& prout, string& out,
|
---|
160 | TOIProcessor& prin, string& in, string nom, int wbsz, bool withFlag)
|
---|
161 | {
|
---|
162 | TOI* toi;
|
---|
163 | if (nom.length() < 1) {
|
---|
164 | char buff[128];
|
---|
165 | sprintf(buff, "TOI%s_[%s-%s]", nom, in, out);
|
---|
166 | nom = buff;
|
---|
167 | }
|
---|
168 | if (wbsz < 16) wbsz = segBuffsz;
|
---|
169 |
|
---|
170 | // ajout test pour eviter de creer 2 tois en sortie
|
---|
171 | if ((toi=prout.getOutToi(out)) == NULL) {
|
---|
172 | //cout << "toi cree" << endl;
|
---|
173 | if (fgSegmented) toi = new TOISegmented(nom, wbsz, segMaxseg);
|
---|
174 | else toi = new TOISeqBuffered(nom, wbsz);
|
---|
175 | // on ajoute le toi de sortie
|
---|
176 | prout.addOutput(out, toi);
|
---|
177 | } else {
|
---|
178 | //cout << "toi deja cree stop" << endl;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (withFlag) { // Si c'est un FITSTOIWriter
|
---|
182 | FITSTOIWriter* ftw = dynamic_cast< FITSTOIWriter* >(&prin);
|
---|
183 | if (ftw) ftw->addInput(in, toi, withFlag);
|
---|
184 | else prin.addInput(in, toi);
|
---|
185 | }
|
---|
186 | else prin.addInput(in, toi);
|
---|
187 | return(*toi);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | TOI& TOIManager::connect(TOIProcessor& prout, const char* out,
|
---|
192 | TOIProcessor& prin, const char* in, string nom, int wbsz, bool withFlag)
|
---|
193 | {
|
---|
194 | string outs = out;
|
---|
195 | string ins = in;
|
---|
196 | return connect(prout, outs, prin, ins, nom, wbsz, withFlag);
|
---|
197 | }
|
---|
198 |
|
---|
199 | // ----------- fin ajout cgt
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 | // -----------------------------------------------------------------
|
---|
204 | // Classe pour affichage de l'avancement des TOIProcessors
|
---|
205 | // Reza 08/2001
|
---|
206 | // -----------------------------------------------------------------
|
---|
207 |
|
---|
208 | RzProcSampleCounter::RzProcSampleCounter()
|
---|
209 | {
|
---|
210 | _msg = "SampleCounter/Info";
|
---|
211 | _rate = 50;
|
---|
212 | }
|
---|
213 |
|
---|
214 | RzProcSampleCounter::~RzProcSampleCounter()
|
---|
215 | {
|
---|
216 | }
|
---|
217 |
|
---|
218 | long RzProcSampleCounter::PrintStats()
|
---|
219 | {
|
---|
220 | int istart = 0;
|
---|
221 | int iend = 0;
|
---|
222 | long dns_print = 1000;
|
---|
223 | int dns_print_fac = _rate;
|
---|
224 | int nbmax_dns_print = 2;
|
---|
225 |
|
---|
226 | TOIManager* mgr = TOIManager::getManager();
|
---|
227 |
|
---|
228 | // istart = mgr->getRequestedBegin();
|
---|
229 | // iend = mgr->getRequestedEnd();
|
---|
230 | istart = SampleBegin();
|
---|
231 | iend = SampleEnd();
|
---|
232 |
|
---|
233 | dns_print = (iend-istart)/dns_print_fac;
|
---|
234 | if (dns_print < 1000) dns_print = ((iend-istart) < 1000) ? (iend-istart) : 1000;
|
---|
235 | if (dns_print < 1) dns_print = 1;
|
---|
236 | nbmax_dns_print = (iend-istart)/dns_print;
|
---|
237 |
|
---|
238 | cout << "RzProcSampleCounter::PrintStats() InfoMessage=" << _msg
|
---|
239 | << "\n ... " << _msg << " istart="
|
---|
240 | << istart << " iend= " << iend << " dns_print= " << dns_print
|
---|
241 | << " nbmax_dns_print= " << nbmax_dns_print << endl;
|
---|
242 | // ------------------- Impression continu de stat ------------------------
|
---|
243 | long nb_dns_print = 0;
|
---|
244 | int nb_sleep = 0;
|
---|
245 | long last_sample_count = 0;
|
---|
246 | long processed_samples = 0;
|
---|
247 | long total_sample_count = dns_print*nbmax_dns_print;
|
---|
248 | bool alldone = false;
|
---|
249 | double fracperc = 0.;
|
---|
250 | int fperc = 0;
|
---|
251 | while (!alldone) {
|
---|
252 | processed_samples = ProcessedSampleCount();
|
---|
253 | if ( (processed_samples-last_sample_count > dns_print) ||
|
---|
254 | (processed_samples > total_sample_count-10) ) {
|
---|
255 | last_sample_count = processed_samples;
|
---|
256 | if (nb_dns_print == 0) cout << "\n";
|
---|
257 | nb_dns_print++;
|
---|
258 | fracperc = (double)processed_samples*100./(double)total_sample_count;
|
---|
259 | fperc = fracperc*100;
|
---|
260 | cout << ">>> " << _msg << ": ProcessedSampleCount()= " << last_sample_count
|
---|
261 | << " Frac done = " << (double)fperc/100. << " %" << endl;
|
---|
262 | if (last_sample_count > total_sample_count-10) alldone = true;
|
---|
263 | nb_sleep = 0;
|
---|
264 | }
|
---|
265 | else if ((nb_sleep+1)%5 == 0) {
|
---|
266 | fracperc = (double)processed_samples*100./(double)total_sample_count;
|
---|
267 | fperc = fracperc*100;
|
---|
268 | cout << "> " << _msg << ": ProcSamples()= " << processed_samples
|
---|
269 | << " Done = " << " %" << (double)fperc/100.
|
---|
270 | << " NbSleep(1) = " << nb_sleep << endl;
|
---|
271 | }
|
---|
272 | sleep(1); nb_sleep++;
|
---|
273 | }
|
---|
274 |
|
---|
275 | // -----------------------------------------------------------------------
|
---|
276 |
|
---|
277 | return last_sample_count;
|
---|
278 |
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 |
|
---|
283 |
|
---|
284 |
|
---|
285 |
|
---|
286 |
|
---|
287 |
|
---|
288 |
|
---|
289 |
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 |
|
---|