This repository was archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMWWorkerID.h
663 lines (518 loc) · 15 KB
/
MWWorkerID.h
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//
// MWWorkerID.h
//
#ifndef __MWWorkerID_h
#define __MWWorkerID_h
#include <list>
#include <cstring>
#include <ctime>
#include <sys/time.h>
#include <sys/types.h>
//#include <errno.h>
//#include <stdio.h>
//#include <stdarg.h>
//#include <memory.h>
#include "MW.h"
#include "MWAbstractDriver.h"
#include "MWGroup.h"
//
// Forward declaration
//
template <class TaskType>
class MWTask;
template <class CommType, class Worker>
class MWStatistics;
/**
* This class is used by MWDriver to keep tabs on a worker application.
* It keeps statistical information that is useful to the MWstats class.
* This information is used at the end of a run.
*/
template <class CommType>
class MWWorkerID : public MWBase {
/// Allow access to worker's data
friend class MWStatistics<MWWorkerID<CommType>,CommType>;
public:
/** This static array is used to hold strings that represent the
MWworker_states. They are handy for printing out a worker - now
you'll see "WORKING" instead of "2" in printself().
@see MWworker_states */
static const char* MWworker_statenames[];
/// Default constructor
MWWorkerID(MWAbstractDriver<CommType>* master);
/// Default destructor
virtual ~MWWorkerID() {}
/// Return primary id of this worker
int get_id1() { return id1; };
/// Return secondary id of this worker
int get_id2() { return id2; };
/** Return the virtual id. This is an int starting at zero and
increasing by one with each worker. When a worker dies, this
number can be reused by a worker that joins us. */
int get_vid() { return virtual_id; };
/// Set primary id
void set_id1( int i ) { id1 = i; };
/// Set secondary id
void set_id2( int i ) { id2 = i; };
/// Set the machine's name
void set_machine_name( char* name)
{strncpy( mach_name, name, 64 );}
/// get the current machine information
virtual void get_machine_info()
{}
/// Returns a pointer to the machine's name
const char *machine_name() const
{return mach_name;}
/// Set this worker's arch class
void set_arch ( int a ) { arch = a; };
/// Get this worker's arch class
int get_arch() const { return arch; };
/// Sets the exec_class
void set_exec_class ( int num ) { exec_class = num; };
/// Gets the exec_class
int get_exec_class ( ) const { return exec_class; };
/// Sets the executable
void set_executable ( char *exec ) { executable_name = exec; };
/// returns the executable name
char* get_executable ( ) const { return executable_name; };
/// Mark this worker so that when the task completes it will exit
void mark_for_removal() { doomed = true; };
/// returns true if mark_for_removal was called previously
bool is_doomed() { return doomed; };
///
void set_bench_result( double bres );
///
double get_bench_result() { return bench_result; };
///
double getNetworkConnectivity ( double &t );
//
void setNetworkConnectivity ( double );
/// The task running on this worker. NULL if there isn't one.
MWTask<CommType> *runningtask;
/// The state of the worker.
enum MWworker_states currentState()
{return state;}
/// Set the state of the worker
void setState ( MWworker_states st )
{ state = st; };
/** Print out a description of this worker.
@param level The debug level to use */
virtual void printself( int level = 40 ) const;
/**@name Statistics Collection Calls
These methods are called when events happen to workers.
*/
//@{
/// Resets the statistics
void reset_stats()
{
total_time = 0.0;
total_working = 0.0;
total_suspended = 0.0;
cpu_while_working = 0.0;
normalized_cpu_working_time = 0.0;
sum_benchmark = 0.0;
num_benchmark = 0;
}
/// Returns true if this worker is idle.
bool isIdle() { return (state==IDLE); }
/// Returns true if this worker is suspended.
bool isSusp() { return (state==SUSPENDED); }
/** This should be called when the master becomes aware of the
existence of this worker. */
void started();
/// Called when the worker is doing the benchmarking task
void benchmark();
/// Called when the worker has done the benchmarking task
void benchmarkOver();
/// Called when this worker gets a task.
void gottask( int tasknum );
/// Called when this worker just finished the task it was working on.
void completedtask( double wall_time = 0.0, double cpu_time = 0.0 );
/// Called when the worker becomes suspended
void suspended();
/// Called when the worker resumes.
void resumed();
/// Called when the worker dies.
void ended();
///
double get_total_time()
{ return total_time;};
///
double get_last_event()
{return last_event;};
///
double get_total_suspended()
{return total_suspended;};
///
double get_total_working()
{return total_working;};
//@}
///
void initGroups ( int num )
{group.init(num);}
///
void addGroup ( int num );
///
void deleteGroup ( int num );
///
bool doesBelong ( int num )
{return group.belong(num);}
///
MWGroup& getGroup ( )
{return group;}
/**@name Checkpointing Call
Each instance of a MWWorkerID needs to be able to checkpoint
itself. It has to store statistics information on itself.
It never has to read them in, though, because stats that we write
out here wind up being read in by the stats class when we restart.
*/
//@{
/// Return the relevant stats info for this worker.
void ckpt_stats( double *up,
double *working,
double *susp,
double *cpu,
double *norm,
double* s_bench,
int* n_bench );
//@}
protected:
/** @name Protected Data */
//@{
/// The machine name of the worker
char mach_name[64];
/// A "primary" identifier of this machine. (Was pvmd_tid)
int id1;
/// A "secondary" identifier of this machine. (Was user_tid)
int id2;
/** A "virtual" number for this worker. Assigned starting at
0 and working upwards by one. Also, when a worker dies,
this number can get taken over by another worker */
int virtual_id;
/// This worker's arch class
int arch; // (should be just a 0, 1, 2, etc...)
/// This is the exec_class
int exec_class;
/// This is the executable_name
char *executable_name;
/// true if marked for removal, false otherwise
bool doomed;
/// The results of the benchmarking process.
double bench_result;
/// The state of this worker.
enum MWworker_states state;
//@}
/** @name Time information
Note that all time information is really stored as a double.
We use gettimeofday() internally, and convert the result to
a double. That way, we don't have to mess around with the
struct timeval... */
//@{
/// The time that this worker started.
double start_time;
/// The total time that this worker ran for.
double total_time;
/// The time of the last 'event'.
double last_event;
/// The time spent in the suspended state
double total_suspended;
/// The time spent working
double total_working;
/// The cpu usage while working
double cpu_while_working;
/// The benchmarked (weighted) cpu working time
double benchmarked_cpu_working_time;
/// The benchmarked (normalized) cpu working time
double normalized_cpu_working_time;
/// The sum of the benchmark values for that vid
double sum_benchmark;
/// The number of the benchmark values for that vid
int num_benchmark;
//@}
///
bool isBenchMark;
///
bool isBenchMarkAvailable;
/** @name Virtual Id Helpers */
//@{
/// Set virtual id
void set_vid( int i )
{ virtual_id = i; };
/// Returns the next available virtual id
int get_next_vid()
{
if (misc_vid_nums.empty())
return max_vid_num++;
int tmp = *(misc_vid_nums.begin());
misc_vid_nums.pop_back();
return tmp;
}
/// Returns a virtual id to the pool
void release_vid( int vid )
{ misc_vid_nums.push_back(vid); }
///
static int max_vid_num;
///
static list<int> misc_vid_nums;
//@}
/// Variables regarding the nw bandwidth.
double networkLatency;
///
double networkLatency_lastMeasuredTime;
/// The tasks that this worker can work on.
MWGroup group;
/// Pointer to an abstract MWDriver
MWAbstractDriver<CommType>* master;
};
template <class CommType>
const char* MWWorkerID<CommType>::MWworker_statenames[] = {
"INITIALIZING",
"IDLE",
"BENCHMARKING/INITIAL DATA HANDLING",
"WORKING",
"SUSPENDED",
"EXITED"
};
template <class CommType>
list<int> MWWorkerID<CommType>::misc_vid_nums;
template <class CommType>
int MWWorkerID<CommType>::max_vid_num = 0;
template <class CommType>
MWWorkerID<CommType>::MWWorkerID(MWAbstractDriver<CommType>* master_)
: runningtask(0),
id1(-1),
id2(-1),
virtual_id(-1),
arch(-1),
exec_class(-1),
executable_name(0),
doomed(false),
bench_result(-1.0),
state(INITIALIZING),
start_time(0.0),
total_time(0.0),
last_event(0.0),
total_suspended(0.0),
total_working(0.0),
cpu_while_working(0.0),
benchmarked_cpu_working_time(-1.0),
normalized_cpu_working_time(0.0),
sum_benchmark(0.0),
num_benchmark(0),
isBenchMark(false),
isBenchMarkAvailable(false),
group(1),
master(master_)
{
assert(master != 0);
memset ( mach_name, 0, 64 );
// We use a negative number to signify that no benchmark has been set
set_vid ( get_next_vid() );
struct timeval timeVal;
gettimeofday ( &timeVal, NULL );
double currentTime = timeval_to_double ( timeVal );
networkLatency = 0.0;
networkLatency_lastMeasuredTime = currentTime;
}
template <class CommType>
void MWWorkerID<CommType>::printself( int level ) const
{
MWprintf ( level, "id1: %d id2: %d, vid: %d, name: \"%s\"\n",
id1, id2, virtual_id, mach_name ? mach_name : "" );
//MWprintf ( level, "Memory: %d, KFlops: %d, Mips: %d\n", Memory, KFlops, Mips );
MWprintf ( level, "arch: \"%d\" state: %s\n",
arch, MWworker_statenames[state] );
if ( runningtask != NULL ) {
MWprintf ( level, " -> Running task: " );
runningtask->printself( level );
}
else
MWprintf ( level, " -> There's no task on this worker.\n" );
}
template <class CommType>
void MWWorkerID<CommType>::started()
{
// this is called when a worker first starts up.
if ( state != INITIALIZING && state != BENCHMARKING ) {
MWprintf( 10, "Error: started() worker whose "
"state != INITIALIZING && state != BENCHMARKING\n" );
printself(10);
}
state = IDLE;
// To get parallel efficiency "right" on
// MWfiles (with worker timeouts), we don't want to count
// the machines that just sit there in INITIALIZING state
// until they time out. Thus, we do not start the counter here, but
// only when we send a benchmark.
//
// struct timeval t;
// gettimeofday ( &t, NULL );
// start_time = timeval_to_double ( t );
}
template <class CommType>
void MWWorkerID<CommType>::benchmark()
{
// this is called when a worker is sent the benchmark task
if ( state != IDLE ) {
MWprintf( 10, "MWWorkerID::benchmark -- ERROR: benchmarking a worker "
"state != IDLE\n" );
printself(10);
}
state = BENCHMARKING;
isBenchMark = true;
}
template <class CommType>
void MWWorkerID<CommType>::benchmarkOver()
{
if ( state != BENCHMARKING ) {
MWprintf( 10, "MWWorkerID::benchmarkOver -- ERROR: benchmarking over of "
" a worker whose state != BENCHMARKING\n" );
printself(10);
}
isBenchMark = false;
state = IDLE;
// To get parallel efficiency "right" on
// MWfiles (with worker timeouts), we don't want to count
// the machines that just sit there in INITIALIZING state
// until they time out. Thus, we start the counter here
struct timeval t;
gettimeofday ( &t, NULL );
start_time = timeval_to_double ( t );
}
template <class CommType>
void MWWorkerID<CommType>::gottask( int tasknum )
{
state = WORKING;
struct timeval t;
gettimeofday ( &t, NULL );
last_event = timeval_to_double ( t );
}
template <class CommType>
void MWWorkerID<CommType>::completedtask( double wall_time, double cpu_time )
{
state = IDLE;
struct timeval t;
gettimeofday ( &t, NULL );
last_event = timeval_to_double( t );
total_working += wall_time;
cpu_while_working += cpu_time;
if (isBenchMarkAvailable){
if ( (cpu_time * bench_result) > 0)
normalized_cpu_working_time += cpu_time * bench_result;
else
MWprintf ( 10, "PROBLEM !!! cpu_time = %lf ; bench = %lf\n", cpu_time,
bench_result);
}
}
template <class CommType>
void MWWorkerID<CommType>::suspended()
{
state = SUSPENDED;
struct timeval t;
gettimeofday ( &t, NULL );
last_event = timeval_to_double ( t );
}
template <class CommType>
void MWWorkerID<CommType>::resumed()
{
struct timeval t;
gettimeofday ( &t, NULL );
double now = timeval_to_double ( t );
if ( state != SUSPENDED )
MWprintf ( 10, "Got resume while not suspended!\n" );
else {
if ( runningtask ) {
total_suspended += now - last_event;
state = WORKING;
}
else if ( isBenchMark == true )
state = BENCHMARKING;
else
state = IDLE;
}
last_event = now;
}
template <class CommType>
void MWWorkerID<CommType>::set_bench_result( double bres )
{
if ( bres < 1.0e-8 ) {
if (bres < 0.0)
MWprintf( 10, "Benchmark result must be a *positive* number\nSetting to 0\n");
bench_result = 0.0;
}
else {
bench_result = bres;
sum_benchmark += bres;
num_benchmark++;
isBenchMarkAvailable = true;
}
}
template <class CommType>
void MWWorkerID<CommType>::ended()
{
struct timeval t;
gettimeofday ( &t, NULL );
double now = timeval_to_double ( t );
//
// If state==WORKING then we're running
// a task, but MWDriver is finished. Thus whatever we were doing wasn't
// important and we shouldn't count it towards our work. (It's not
// part of "GoodPut", in other words.)
//
// However, we should count suspended work, since that was effort that
// was arguably GoodPut, but it simply didn't finish in time...
//
// TODO: evaluate the assumptions here!
//
if ( state == SUSPENDED )
total_suspended += now - last_event;
state = EXITED;
if ( start_time > 1.0 )
total_time = now - start_time;
else
total_time = 0.0;
release_vid ( get_vid() );
for ( size_t i = 0; i < master->workGroupWorkerNum.size(); i++ )
if ( doesBelong ( i ) )
deleteGroup ( i );
}
template <class CommType>
void MWWorkerID<CommType>::ckpt_stats( double *up,
double *working,
double *susp,
double *cpu,
double *norm,
double *s_bench,
int *n_bench )
{
// print duration, total_working, total_suspended
struct timeval t;
gettimeofday ( &t, NULL );
double now = timeval_to_double ( t );
if ( start_time > 1.0 )
*up = now - start_time;
else
*up = 0.0; // possible if not yet started().
*working = total_working;
*susp = total_suspended;
*cpu = cpu_while_working;
*norm = normalized_cpu_working_time;
*s_bench = sum_benchmark;
*n_bench = num_benchmark;
}
template <class CommType>
void MWWorkerID<CommType>::addGroup ( int num )
{
if (doesBelong(num))
return;
master->workGroupWorkerNum[num]++;
group.join(num);
}
template <class CommType>
void MWWorkerID<CommType>::deleteGroup ( int num )
{
if (!doesBelong(num))
return;
master->workGroupWorkerNum[num]--;
group.leave(num);
}
#endif