Subversion Repositories shark

Rev

Rev 964 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
668 trimarchi 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Trimarchi Michael   <trimarchi@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 2000 Giorgio Buttazzo, Paolo Gai
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 *
975 trimarchi 37
 * CVS :        $Id: server-task.c,v 1.17 2005-02-26 14:12:28 trimarchi Exp $
668 trimarchi 38
 */
39
 
679 trimarchi 40
#include "ll/i386/64bit.h"
868 trimarchi 41
#include "fsf_configuration_parameters.h"
880 trimarchi 42
#include "fsf.h"
868 trimarchi 43
#include "fsf_service_task.h"
44
#include "message.h"
668 trimarchi 45
#include <kernel/kern.h>
975 trimarchi 46
#include <sem/sem/sem.h>
47
#include <hartport/hartport/hartport.h>
48
#include <cabs/cabs/cabs.h>
668 trimarchi 49
 
868 trimarchi 50
//#define FSF_DEBUG
880 trimarchi 51
extern fsf_server_id_t service_server;
868 trimarchi 52
 
930 trimarchi 53
typedef enum {WAIT_SIGNAL, WAIT_RENEGOTIATION, ACTIVE} service_task_status;
54
 
868 trimarchi 55
TASK service_task(void) {
679 trimarchi 56
 
868 trimarchi 57
  struct mess msg;
58
  fsf_renegotiation_status_t status;
930 trimarchi 59
  service_task_status stask_status=ACTIVE;
909 trimarchi 60
 
679 trimarchi 61
  PORT rec,tra;
868 trimarchi 62
 
679 trimarchi 63
  rec = port_connect("CHANW",sizeof(struct mess),STREAM,READ);
668 trimarchi 64
 
679 trimarchi 65
  tra = port_connect("CHANR",sizeof(struct mess),STREAM,WRITE);
668 trimarchi 66
 
67
  while(1) {
964 trimarchi 68
    //kern_printf("(Status %d)", stask_status); 
930 trimarchi 69
    switch(stask_status) {
70
    // wait for operation    
71
      case WAIT_RENEGOTIATION:
72
        if (!fsf_get_renegotiation_status(msg.server,&status)) {
73
          if (status==FSF_ADMITTED) {
74
            // send response server is -1 if the operation fail
868 trimarchi 75
#ifdef FSF_DEBUG
930 trimarchi 76
            kern_printf("ADM");
868 trimarchi 77
#endif
930 trimarchi 78
            stask_status=ACTIVE;
79
            //kern_printf("(REN %d)", msg.server);
80
            port_send(tra,&msg,BLOCK);
964 trimarchi 81
            //kern_printf("(--END-- 1)");
909 trimarchi 82
 
930 trimarchi 83
            continue;
84
          } else
868 trimarchi 85
#ifdef FSF_DEBUG
930 trimarchi 86
            kern_printf("WAIT")
868 trimarchi 87
#endif
88
;
930 trimarchi 89
        }
90
        break;
91
 
92
    case WAIT_SIGNAL:
93
      if (!fsf_get_renegotiation_status(msg.server,&status)) {
94
        if (status==FSF_ADMITTED) {            
964 trimarchi 95
          //sigqueue(msg.process, msg.sig_notify, msg.sig_value);        
930 trimarchi 96
          stask_status=ACTIVE;
964 trimarchi 97
          port_send(tra,&msg,BLOCK);
98
          //kern_printf("(--END-- 2)");
99
          continue;
930 trimarchi 100
        }
868 trimarchi 101
      }
930 trimarchi 102
      break;
103
 
104
    case ACTIVE:
105
      {
106
        port_receive(rec,&msg,BLOCK);
877 trimarchi 107
#ifdef FSF_DEBUG
930 trimarchi 108
        kern_printf("Message ");
877 trimarchi 109
#endif
930 trimarchi 110
        switch (msg.type) {
868 trimarchi 111
 
964 trimarchi 112
        case NEGOTIATE_CONTRACT:         
930 trimarchi 113
          if (negotiate_contract(&msg.contract, &msg.server)==FSF_ERR_CONTRACT_REJECTED)
114
            msg.server=-1;     
115
          else {
116
            stask_status=WAIT_RENEGOTIATION;
117
            //task_endcycle();
118
            continue;
908 trimarchi 119
          }
930 trimarchi 120
          break;
908 trimarchi 121
 
930 trimarchi 122
        case REQUEST_RENEGOTIATE_CONTRACT:
123
          if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
124
            msg.server=-1;
964 trimarchi 125
          else {
126
            stask_status=WAIT_SIGNAL;
127
            //task_endcycle();
128
            continue;
129
          }
908 trimarchi 130
 
930 trimarchi 131
          break;
132
 
133
        case RENEGOTIATE_CONTRACT:
134
          if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
135
            msg.server=-1;
136
          else {
137
            stask_status=WAIT_RENEGOTIATION;
138
            //task_endcycle();
139
            continue;
140
          }
141
          break;                         
142
        case CHANGE_PARAMETER:
143
          {
144
            fsf_contract_parameters_t new_contract;
145
            struct timespec period;
146
            struct timespec budget;
147
            int i=0;
148
            // this CASE change the quality and importance
149
            //fsf_initialize_contract(&new_contract);
150
            // find contract
151
            while(i<current_server) {
152
              if (server_list[i].server==msg.server) break;
153
              i++;
154
            }
155
            //kern_printf("(RCQI)");
908 trimarchi 156
            NULL_TIMESPEC(&period);
930 trimarchi 157
            ADDUSEC2TIMESPEC(server_list[i].Tmax, &period);
158
            NULL_TIMESPEC(&budget);
159
            ADDUSEC2TIMESPEC(server_list[i].Cmin, &budget);
160
 
161
            fsf_set_contract_basic_parameters(&new_contract,&budget,&period,FSF_DEFAULT_WORKLOAD);
162
            NULL_TIMESPEC(&period);
163
            ADDUSEC2TIMESPEC(server_list[i].Tmin, &period);
164
            NULL_TIMESPEC(&budget);
165
            ADDUSEC2TIMESPEC(server_list[i].Cmax, &budget);
166
 
167
            fsf_set_contract_reclamation_parameters(&new_contract, &budget, &period, FSF_DEFAULT_GRANULARITY,NULL,
168
                                                    msg.qi.quality,msg.qi.importance);
169
 
170
            if (server_list[i].d_equals_t == true) {
171
              NULL_TIMESPEC(&new_contract.deadline);
172
              new_contract.d_equals_t = true;
173
            } else {
174
              NULL_TIMESPEC(&period);
175
              ADDUSEC2TIMESPEC(server_list[i].deadline, &period);
908 trimarchi 176
            TIMESPEC_ASSIGN(&new_contract.deadline, &period);
177
            new_contract.d_equals_t = false;
930 trimarchi 178
            }
179
            if (renegotiate_contract(&new_contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
180
              msg.server=-1;   
908 trimarchi 181
        }
930 trimarchi 182
          break;
183
 
184
        default :
185
          break;
186
 
187
        }
893 trimarchi 188
        break;
868 trimarchi 189
      }
930 trimarchi 190
    }
964 trimarchi 191
 
930 trimarchi 192
    port_send(tra,&msg,BLOCK);
964 trimarchi 193
    //kern_printf("(--END--)");
909 trimarchi 194
 
668 trimarchi 195
  }
196
}
197
 
688 trimarchi 198
 
199
static __inline void set_contract_parameter(void) {
200
 
201
  int i=0;
679 trimarchi 202
  TIME T,Q;
868 trimarchi 203
 
204
  for (i=0; i<current_server; i++) {
964 trimarchi 205
 
679 trimarchi 206
      mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
964 trimarchi 207
 
679 trimarchi 208
      if (T > server_list[i].Tmin ) {
209
         server_list[i].actual_budget = server_list[i].Cmin;
964 trimarchi 210
 
211
         if (T > server_list[i].Tmax)
212
           T=server_list[i].Tmax;
213
 
679 trimarchi 214
         server_list[i].actual_period = T;
964 trimarchi 215
         server_list[i].actual_budget = server_list[i].Cmin;
216
 
679 trimarchi 217
      #ifdef FSF_DEBUG
218
         kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
219
      #endif
908 trimarchi 220
         if (server_list[i].d_equals_t == true)
679 trimarchi 221
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
222
         else
964 trimarchi 223
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T); // server_list[i].deadline);
679 trimarchi 224
 
225
      } else {
964 trimarchi 226
 
679 trimarchi 227
        mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
964 trimarchi 228
 
679 trimarchi 229
        server_list[i].actual_budget = Q;
230
        server_list[i].actual_period = server_list[i].Tmin;
964 trimarchi 231
 
232
        if (Q>server_list[i].Cmax)
233
          Q=server_list[i].Cmax;
234
 
679 trimarchi 235
      #ifdef FSF_DEBUG
236
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
237
      #endif
238
 
908 trimarchi 239
         if (server_list[i].d_equals_t == true)
964 trimarchi 240
           adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin);
679 trimarchi 241
         else
964 trimarchi 242
           adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin, server_list[i].Tmin); //server_list[i].deadline);                                                                                                        
679 trimarchi 243
     }
244
 
245
     server_list[i].U=server_list[i].Umin;
246
   }
247
 
688 trimarchi 248
}
249
 
250
 
251
int renegotiate_contract
252
  (const fsf_contract_parameters_t *new_contract,
253
   fsf_server_id_t                 server)
254
{
255
 
256
  #ifdef FSF_DEBUG
257
    kern_printf("(Renegotiate for server %d)",server);
258
  #endif
259
 
260
  if (!new_contract)
868 trimarchi 261
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 262
 
263
  if (server < 0)
875 trimarchi 264
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 265
 
266
   // change the parameter
267
   relink_contract_to_server(new_contract, server);
268
   if (recalculate_contract(fsf_max_bw)==-1)  {
875 trimarchi 269
       return  FSF_ERR_CONTRACT_REJECTED;
688 trimarchi 270
   }
271
 
272
   set_contract_parameter();
964 trimarchi 273
   //kern_printf("(ERNC %d)", server);
679 trimarchi 274
   return 0;
275
}
276
 
668 trimarchi 277
int negotiate_contract
679 trimarchi 278
  (const fsf_contract_parameters_t *contract,
279
   fsf_server_id_t                 *server)
668 trimarchi 280
{
281
  /* Check if contract is initialized */
868 trimarchi 282
  if (!contract) return  FSF_ERR_BAD_ARGUMENT;
668 trimarchi 283
 
284
  /* Admission Test */
285
  if (FSF_ADMISSION_TEST_IS_ENABLED)
286
    if (add_contract(contract))
287
      return FSF_ERR_CONTRACT_REJECTED;
288
 
679 trimarchi 289
  /* SERVER => BUDGET */    
290
  set_SERVER_budget_from_contract(contract,server);
668 trimarchi 291
 
688 trimarchi 292
#ifdef FSF_DEBUG
293
  kern_printf("(New Server %d)",*server);
294
#endif
679 trimarchi 295
 
296
  if (*server >= 0) {
297
    link_contract_to_server(contract,*server);
298
    if (recalculate_contract(fsf_max_bw)==-1)  {
868 trimarchi 299
      remove_contract(server);
875 trimarchi 300
      return  FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 301
    }
688 trimarchi 302
    set_contract_parameter();
679 trimarchi 303
#ifdef  FSF_DEBUG
304
    kern_printf("(Adjust budget)");
305
#endif    
688 trimarchi 306
 
679 trimarchi 307
  }
308
  else  {
875 trimarchi 309
    return  FSF_ERR_CONTRACT_REJECTED;
964 trimarchi 310
  }
679 trimarchi 311
  return 0;
312
 
668 trimarchi 313
}
314