Subversion Repositories shark

Rev

Rev 949 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
949 trimarchi 1
//=====================================================================
2
//       FFFFFFIII   RRRRR      SSTTTTTTT
3
//      FF         IIR   RR    SS
4
//     FF           IR        SS
5
//    FFFFFF         RRRR    SSSSST      
6
//   FF       FI       RRR  SS
7
//  FF         II     RRR  SS
8
// FF           IIIIIR    RS 
9
//       
10
// FIRST Scheduling Framework
11
// 
12
//=====================================================================
13
 
14
 
15
 
16
#include <stdio.h>
17
#include <unistd.h>
18
#include <time.h>
19
#include <errno.h>
20
#include <pthread.h>
21
#include <sched.h>
22
#include <fsf.h>
23
 
24
#include "timespec_operations.h"
25
#include "fsf_os_compatibility.h"
26
 
27
//#include <debug_marte.h>
28
 
29
/*
30
 
31
 * This is part of the common tests:
32
 
33
 ** Test to get the time it takes the negotiation of contracts and
34
    the maximum reacheable utilization
35
 
36
  -  Prepare the configuration files to accept up to 100 servers,
37
     enable the acceptance test, set the service thread with 100s period
38
     and 1s budget, and the scheduler priority to be 102
39
 
40
  -  Fill a contract with the values: Cmin 1s, Cmax 50s, Tmax 100s,
41
     Tmin 50s, INDETERMINATE workload, and the preemptibility level equal
42
     to 101 minus the number of already accepted servers. Create a server
43
     with that contract, and bound it to the main thread using the
44
     fsf_negotiate_contract_for_myself primitive.
45
 
46
  -  then as a servered task, negotiates as much of the prepared contracts
47
     as possible, taking the utilization given to each of the contracts
48
     after each new contract is accepted and the time it took the
49
     negotiation.
50
 
51
 ** Test to get the time it takes re-negotiation of contracts and
52
    the change of quality and importance
53
 
54
  -  change the last parragraph of the test above, so that contracts are
55
     negotiated and bound at the same time to the ever running threads,
56
     and also after each succesful negotiation, the contracts are
57
     re-negotiated, and its quality and importance changed to the same
58
     values. They are taken the utilization given to each server and the
59
     time it took each operation.
60
 
61
*/
62
 
63
 
64
#define FSF_MAX_N_TIME_VALUES     1000
65
 
66
#define N_SERVERS          10
67
 
68
struct timespec            instant_zero = {0,0};
69
int                        main_priority = N_SERVERS+1;
70
struct timespec            last_time = {0,0};
71
fsf_server_id_t            server[N_SERVERS];
72
struct server_res_t {
73
  struct timespec        negotiation;
74
  struct timespec        renegotiation;
75
  struct timespec        change_q_and_i;
76
  struct timespec        cancellation;
77
  double                 min_u, max_u, tot_u;
78
}                          server_res[N_SERVERS];
79
 
80
int                        n_servers = 0;
81
pthread_t                  task[N_SERVERS];
82
 
83
 
84
 
85
struct pseudo_printed_results {
86
  char            *s;
87
  struct timespec  t;
88
}                                  res[FSF_MAX_N_TIME_VALUES];
89
int                                res_index = 0;
90
 
91
 
92
extern int cal_cycles;
93
extern int calibrate_cycle(void);
94
extern void eat(TIME wait);
95
 
96
//put_time has been reduced in this experiment
97
//so that it just reports the real-time it is
98
struct timespec
99
put_time(const struct timespec *time, char *s)
100
{
101
  struct timespec now = {0,0};
102
  int terror;
103
 
104
  if ((terror=clock_gettime(CLOCK_REALTIME,&now)))
105
    ERROR(terror,"clock_gettime failed");
106
 
107
  return now;
108
} /* End of put_time */
109
 
110
/*
111
int
112
print_time_results()
113
{
114
  struct timespec now = {0,0};
115
  struct timespec diff = {0,0};
116
  char            *s = NULL;
117
  int i;
118
 
119
  printf("\n");
120
  for (i=0;i<res_index;i++)
121
  {
122
    now  = res[i].t;
123
    s    = res[i].s;
124
    if (s==NULL) s = "  - timestamp - ";
125
    diff = now;
126
    decr_timespec(&diff, &last_time);
127
    last_time = now;
128
    printf("%-60s", s);
129
    printf(" %2d %9d", now.tv_sec,now.tv_nsec);
130
    printf(" [diff=%13.9f]\n", (double)diff.tv_sec+((double)diff.tv_nsec/(double)1000000000));
131
  }
132
 
133
  return 0;
134
} / * End of print_time_results * /
135
*/
136
/*
137
int
138
fsf_priority_map (unsigned long plevel)
139
{
140
  return plevel;
141
}
142
*/
143
 
144
void * fsf_indeterminate_server (void * arg)
145
{
146
 
147
  //executes for ever
148
  while (1)  eat(1000000);
149
 
150
} /* End of fsf_indeterminate_server */
151
 
152
 
153
 
154
int main()
155
{
156
   struct sched_param   param;
157
   //struct timespec      half_second={0,500000000};
158
   struct timespec      tmp = {0,0};
159
   int                  terror = 0;
160
 
161
  fsf_contract_parameters_t  contract;
162
  struct timespec    budget_min = {1,0};      //{20,710678118};
163
  //  struct timespec    budget_min = {21,210678118};
164
  struct timespec    period_max = {100,0};
165
  struct timespec    budget_max = {50,0}; //{20,710678118};
166
  struct timespec    period_min = {50,0};
167
  fsf_workload_t     workload = FSF_INDETERMINATE; //FSF_BOUNDED;
168
 
169
  bool               d_equals_t = true;
170
  struct timespec    deadline = {0,0};
171
  int                budget_overrun_sig_notify = FSF_NULL_SIGNAL;   // 0
172
  union sigval       budget_overrun_sig_value = {0};
173
  int                deadline_miss_sig_notify = FSF_NULL_SIGNAL;    // 0
174
  union sigval       deadline_miss_sig_value = {0};
175
 
176
  fsf_granularity_t      granularity = FSF_DEFAULT_GRANULARITY;       // CONTINUOUS
177
  fsf_utilization_set_t *utilization_set = FSF_NULL_UTILIZATION_SET;  // NULL
178
  int                    quality = 1;
179
  int                    importance = FSF_DEFAULT_IMPORTANCE;         // 1
180
 
181
  fsf_preemption_level_t   preemption_level;
182
  fsf_critical_sections_t *critical_sections = NULL;
183
 
184
  struct timespec    budget = {0,0};
185
  struct timespec    period = {0,0};
186
  struct timespec    start, end;
187
  int                i;
188
  double             u;
189
 
190
   INITIALIZATION_CODE;
191
 
192
   param.sched_priority = main_priority;
193
   if ((terror=pthread_setschedparam (pthread_self(), SCHED_FIFO, &param)))
194
     ERROR(terror,"pthread_setschedparam");
195
 
196
   instant_zero.tv_sec = 10000000;
197
   instant_zero.tv_nsec = 10000000;
198
   clock_settime(CLOCK_REALTIME,&instant_zero);
199
   clock_gettime(CLOCK_REALTIME,&instant_zero);
200
   last_time = instant_zero;
201
   put_time(&instant_zero, "instant_zero");
202
   put_time(NULL, "printing point 1");
203
   put_time(NULL, "printing point 2");
204
 
205
 
206
 
207
 
208
  if ((terror=fsf_initialize_contract(&contract)))
209
  {
950 trimarchi 210
     fsf_printf(" Initialize fail for server A\n");
949 trimarchi 211
     ERROR(terror,"fsf_initialize_contract failed");
212
  }
213
 
214
  if ((terror=fsf_set_contract_basic_parameters (&contract,
215
                                    &budget_min,
216
                                    &period_max,                              
217
                                    workload)))
218
  {
950 trimarchi 219
     fsf_printf("Set_Basic_Parameters failed for server A\n");
949 trimarchi 220
     ERROR(terror,"set_contract_basic_parameters failed");
221
  }
222
 
223
  if ((terror=fsf_set_contract_timing_requirements (&contract,
224
                                      d_equals_t,
225
                                      (d_equals_t?NULL:&deadline),
226
                                      budget_overrun_sig_notify,
227
                                      budget_overrun_sig_value,
228
                                      deadline_miss_sig_notify,
229
                                      deadline_miss_sig_value)))
230
  {
950 trimarchi 231
     fsf_printf("Set_Timing_Requirements failed for server A\n");
949 trimarchi 232
     ERROR(terror,"fsf_set_contract_timing_requirements failed");
233
  }
234
 
235
  if ((terror=fsf_set_contract_reclamation_parameters (&contract,
236
                                          &budget_max,
237
                                          &period_min,
238
                                          granularity,
239
                                          utilization_set,
240
                                          quality,
241
                                          importance)))
242
  {
950 trimarchi 243
     fsf_printf("Set_Reclamation_Parameters failed for server A\n");
949 trimarchi 244
     ERROR(terror,"fsf_set_contract_reclamation_parameters failed");
245
  }
246
 
247
  preemption_level = (fsf_preemption_level_t) param.sched_priority;
248
  if ((terror=fsf_set_contract_synchronization_parameters (&contract,
249
                                                           critical_sections)))
250
  {
950 trimarchi 251
     fsf_printf("Set_Synchronization_Parameters failed for server A\n");
949 trimarchi 252
     ERROR(terror,"fsf_set_contract_synchronization_parameters failed");
253
  }
254
 
255
  start = put_time(NULL, "start first server contract negotiation");
256
  terror = fsf_negotiate_contract_for_myself (&contract, &server[0]);
257
  end   = put_time(NULL, "  end first server contract negotiation");
258
  if (terror)
259
  {
950 trimarchi 260
    fsf_printf("Negotiate_Contract failed for server 0\n");
949 trimarchi 261
    ERROR(terror,"fsf_negotiate_contract_for_myself failed");
262
  }
263
 
264
  decr_timespec(&end, &start);
265
  server_res[0].negotiation = end;
266
 
267
  start = put_time(NULL, "start first server contract renegotiation");
268
  terror = fsf_renegotiate_contract (&contract, server[0]);
269
  end   = put_time(NULL, "  end first server contract renegotiation");
270
  if (terror)
271
  {
950 trimarchi 272
    fsf_printf("ReNegotiate_Contract failed for server 0\n");
949 trimarchi 273
    ERROR(terror,"fsf_renegotiate_contract failed");
274
  }
275
 
276
  decr_timespec(&end, &start);
277
  server_res[0].renegotiation = end;
278
 
279
  start = put_time(NULL, "start first server change quality and importance");
280
  terror = fsf_request_change_quality_and_importance(server[0],1,1);
281
  end   = put_time(NULL, "  end first server change quality and importance");
282
  if (terror)
283
  {
950 trimarchi 284
    fsf_printf("Change quality and importancet failed for server 0\n");
949 trimarchi 285
    ERROR(terror,"fsf_renegotiate_contract failed");
286
  }
287
 
288
  decr_timespec(&end, &start);
289
  server_res[0].change_q_and_i = end;
290
 
291
  tmp = server_res[0].renegotiation;
292
  incr_timespec(&tmp, &server_res[0].negotiation);
293
 
294
  //release the CPU for a while to get the quality change done
295
  if ((terror=nanosleep(&tmp,NULL)))
296
    ERROR(terror, "nanosleep failed");
297
 
298
//set_break_point_here;
299
 
300
  //ask for the utilization actually gotten
301
  terror = fsf_get_budget_and_period (server[0], &budget, &period);
302
  if (terror)
303
  {
950 trimarchi 304
    fsf_printf("fsf_get_budget_and_period failed for server 0\n");
949 trimarchi 305
    ERROR(terror,"fsf_get_budget_and_period failed");
306
  }
307
//  put_time(&budget,"the budget gotten is: ");
308
//  put_time(&period,"the period gotten is: ");
309
  server_res[0].min_u = server_res[0].max_u = server_res[0].tot_u = t2d(budget) / t2d(period);
310
/*
311
  budget_min.tv_sec = 1;
312
  budget_min.tv_nsec = 0;
313
//  budget_max = budget_min;
314
  if ((terror=fsf_set_contract_basic_parameters (&contract,
315
                                    &budget_min,
316
                                    &period_max,
317
                                    &budget_max,
318
                                    &period_min,
319
                                    workload)))
320
  {
321
     printf("Set_Basic_Parameters failed for a server \n");
322
  }
323
*/
324
  while(++n_servers < N_SERVERS) {
325
 
326
    //preparation of the rest of the servers
327
////if (n_servers==27) set_break_point_here;
328
    preemption_level = main_priority - n_servers;
329
    if ((terror=fsf_set_contract_synchronization_parameters (&contract,
330
                                                             critical_sections)))
331
    {
950 trimarchi 332
       fsf_printf("Set_Synchronization_Parameters failed for server %d\n", n_servers);
949 trimarchi 333
       ERROR(terror,"fsf_set_contract_synchronization_parameters failed");
334
    }
335
 
336
    start = put_time(NULL, "start server contract negotiation");
337
    //terror=fsf_negotiate_contract_for_new_thread (&contract, &server[n_servers], &task[n_servers], NULL, fsf_indeterminate_server, NULL);
338
    terror=fsf_negotiate_contract(&contract, &server[n_servers]);
339
    end   = put_time(NULL, "  end server contract negotiation");
340
    if (terror)
341
    {
950 trimarchi 342
      fsf_printf("Negotiate_Contract failed for server %d\n", n_servers);
949 trimarchi 343
      ERROR(terror,"fsf_negotiate_contract failed");
344
    }
345
 
346
    if (!server[n_servers]) {
950 trimarchi 347
      fsf_printf("The negotiation for the server number %d was not succesful!! (n_servers=%d)\n",n_servers+1, n_servers);
949 trimarchi 348
      break;
349
    }
350
 
351
////set_break_point_here;
352
 
353
    decr_timespec(&end, &start);
354
    server_res[n_servers].negotiation = end;
355
 
356
    start = put_time(NULL, "start server contract renegotiation");
357
    terror = fsf_renegotiate_contract (&contract, server[n_servers]);
358
    end   = put_time(NULL, "  end server contract renegotiation");
359
    if (terror)
360
    {
361
      printf("ReNegotiate_Contract failed for server %d\n", n_servers);
362
      ERROR(terror,"fsf_renegotiate_contract failed");
363
    }
364
 
365
    decr_timespec(&end, &start);
366
    server_res[n_servers].renegotiation = end;
367
 
368
    start = put_time(NULL, "start first server change quality and importance");
369
    terror = fsf_request_change_quality_and_importance(server[n_servers],1,1);
370
    end   = put_time(NULL, "  end first server change quality and importance");
371
    if (terror)
372
    {
950 trimarchi 373
      fsf_printf("Change quality and importancet failed for server %d\n", n_servers);
949 trimarchi 374
      ERROR(terror,"fsf_request_change_quality_and_importance failed");
375
    }
376
 
377
    decr_timespec(&end, &start);
378
    server_res[n_servers].change_q_and_i = end;
379
 
380
    tmp = server_res[n_servers].renegotiation;
381
    incr_timespec(&tmp, &server_res[n_servers].negotiation);
382
 
383
    //release the CPU for a while long enough to get the change done
384
    if ((terror=nanosleep(&tmp,NULL)))
385
      ERROR(terror, "nanosleep failed");
386
 
387
////set_break_point_here;
388
 
389
    //ask for the utilization actually gotten
390
    terror = fsf_get_budget_and_period (server[0], &budget, &period);
391
    if (terror)
392
    {
950 trimarchi 393
      fsf_printf("fsf_get_budget_and_period failed for server 0\n");
949 trimarchi 394
      ERROR(terror,"fsf_get_budget_and_period failed");
395
    }
396
    if (period.tv_sec==0 && period.tv_nsec==0) {
950 trimarchi 397
      fsf_printf("while processing server %d, the period gotten for server %d is cero!!\n", n_servers, 0);
949 trimarchi 398
      exit(-1);
399
    }
400
    u = t2d(budget) / t2d(period);
401
    server_res[n_servers].min_u = server_res[n_servers].max_u = server_res[n_servers].tot_u = u;
402
 
403
    //get statistics for the utilization of all the accepted contracts up to the current one
404
    for (i=1;i<=n_servers;i++){
405
 
406
      terror = fsf_get_budget_and_period (server[i], &budget, &period);
407
      if (terror)
408
      {
950 trimarchi 409
        fsf_printf("fsf_get_budget_and_period failed for server %d\n", n_servers);
949 trimarchi 410
        ERROR(terror,"fsf_get_budget_and_period failed");
411
      }
412
      if (period.tv_sec==0 && period.tv_nsec==0) {
950 trimarchi 413
        fsf_printf("while processing server %d, the period gotten for server %d is cero!!\n", n_servers, i);
949 trimarchi 414
        exit(-1);
415
      }
416
      u = t2d(budget) / t2d(period);
417
      server_res[n_servers].tot_u += u;
418
      if (u > server_res[n_servers].max_u) server_res[n_servers].max_u = u;
419
      if (u < server_res[n_servers].min_u) server_res[n_servers].min_u = u;
420
    }
421
 
422
    //printf(" %d", n_servers);
423
 
424
  } /* End of the while */
425
 
426
  //cancellation of contracts
427
  for (i=n_servers-1; i>0;i--) {
428
    start = put_time(NULL, "start server contract cancellation");
429
    terror=fsf_cancel_contract(server[i]);
430
    end   = put_time(NULL, "  end server contract cancellation");
431
    if (terror)
432
    {
950 trimarchi 433
      fsf_printf("Cancel_Contract failed for server %d\n", i);
949 trimarchi 434
      ERROR(terror,"fsf_cancel_contract failed");
435
    }
436
 
437
    decr_timespec(&end, &start);
438
    server_res[i].cancellation = end;
439
  }
440
 
950 trimarchi 441
  fsf_printf("\n");
442
  fsf_printf("The number of accepted servers was: %d \n", n_servers);
443
  fsf_printf("==================================\n\n");
949 trimarchi 444
 
950 trimarchi 445
  fsf_printf("server    negotiation   renegotiation  qualityChange  cancellation  max_utilizati  min_utilizati total_utilization\n\n");
949 trimarchi 446
 
447
  for (i=0;i<n_servers;i++){
950 trimarchi 448
    fsf_printf("  %2d   ", i+1);
449
    fsf_printf(" %13.9f ", t2d(server_res[i].negotiation));
450
    fsf_printf(" %13.9f ", t2d(server_res[i].renegotiation));
451
    fsf_printf(" %13.9f ", t2d(server_res[i].change_q_and_i));
452
    fsf_printf(" %13.9f ", t2d(server_res[i].cancellation));
453
    fsf_printf(" %13.9f ", server_res[i].max_u);
454
    fsf_printf(" %13.9f ", server_res[i].min_u);
455
    fsf_printf(" %13.9f ", server_res[i].tot_u);
456
    fsf_printf("\n");
949 trimarchi 457
  }
458
 
459
  STANDARD_CONSOLE_INIT; //marte1.26i+
460
 
950 trimarchi 461
  fsf_printf("\n");
462
  fsf_printf("The number of accepted servers was: %d \n", n_servers);
463
  fsf_printf("==================================\n\n");
949 trimarchi 464
 
950 trimarchi 465
  fsf_printf("server    negotiation   renegotiation  qualityChange  cancellation  max_utilizati  min_utilizati total_utilization\n\n");
949 trimarchi 466
 
467
  for (i=0;i<n_servers;i++){
950 trimarchi 468
    fsf_printf("  %2d   ", i+1);
469
    fsf_printf(" %13.9f ", t2d(server_res[i].negotiation));
470
    fsf_printf(" %13.9f ", t2d(server_res[i].renegotiation));
471
    fsf_printf(" %13.9f ", t2d(server_res[i].change_q_and_i));
472
    fsf_printf(" %13.9f ", t2d(server_res[i].cancellation));
473
    fsf_printf(" %13.9f ", server_res[i].max_u);
474
    fsf_printf(" %13.9f ", server_res[i].min_u);
475
    fsf_printf(" %13.9f ", server_res[i].tot_u);
476
    fsf_printf("\n");
949 trimarchi 477
  }
478
 
950 trimarchi 479
  fsf_printf("\nThe end.\n");
949 trimarchi 480
 
481
//....
482
  exit(-1);
483
  return 0;
484
} /* End of main */