Subversion Repositories shark

Rev

Rev 909 | Rev 964 | Go to most recent revision | 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
 *
930 trimarchi 37
 * CVS :        $Id: server-task.c,v 1.15 2005-01-18 15:04:52 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>
679 trimarchi 46
#include <modules/sem.h>
47
#include <modules/hartport.h>
668 trimarchi 48
#include <modules/cabs.h>
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) {
930 trimarchi 68
 
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);
909 trimarchi 81
 
930 trimarchi 82
            continue;
83
          } else
868 trimarchi 84
#ifdef FSF_DEBUG
930 trimarchi 85
            kern_printf("WAIT")
868 trimarchi 86
#endif
87
;
930 trimarchi 88
        }
89
        break;
90
 
91
    case WAIT_SIGNAL:
92
      if (!fsf_get_renegotiation_status(msg.server,&status)) {
93
        if (status==FSF_ADMITTED) {            
94
          sigqueue(msg.process, msg.sig_notify, msg.sig_value);  
95
          stask_status=ACTIVE;
96
        }
868 trimarchi 97
      }
930 trimarchi 98
      break;
99
 
100
    case ACTIVE:
101
      {
102
        port_receive(rec,&msg,BLOCK);
877 trimarchi 103
#ifdef FSF_DEBUG
930 trimarchi 104
        kern_printf("Message ");
877 trimarchi 105
#endif
930 trimarchi 106
        switch (msg.type) {
868 trimarchi 107
 
930 trimarchi 108
        case NEGOTIATE_CONTRACT:
109
          if (negotiate_contract(&msg.contract, &msg.server)==FSF_ERR_CONTRACT_REJECTED)
110
            msg.server=-1;     
111
          else {
112
            stask_status=WAIT_RENEGOTIATION;
113
            //task_endcycle();
114
            continue;
908 trimarchi 115
          }
930 trimarchi 116
          break;
908 trimarchi 117
 
930 trimarchi 118
        case REQUEST_RENEGOTIATE_CONTRACT:
119
          if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
120
            msg.server=-1;
121
          else stask_status=WAIT_SIGNAL;
908 trimarchi 122
 
930 trimarchi 123
          break;
124
 
125
        case RENEGOTIATE_CONTRACT:
126
          if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
127
            msg.server=-1;
128
          else {
129
            stask_status=WAIT_RENEGOTIATION;
130
            //task_endcycle();
131
            continue;
132
          }
133
          break;                         
134
        case CHANGE_PARAMETER:
135
          {
136
            fsf_contract_parameters_t new_contract;
137
            struct timespec period;
138
            struct timespec budget;
139
            int i=0;
140
            // this CASE change the quality and importance
141
            //fsf_initialize_contract(&new_contract);
142
            // find contract
143
            while(i<current_server) {
144
              if (server_list[i].server==msg.server) break;
145
              i++;
146
            }
147
            //kern_printf("(RCQI)");
908 trimarchi 148
            NULL_TIMESPEC(&period);
930 trimarchi 149
            ADDUSEC2TIMESPEC(server_list[i].Tmax, &period);
150
            NULL_TIMESPEC(&budget);
151
            ADDUSEC2TIMESPEC(server_list[i].Cmin, &budget);
152
 
153
            fsf_set_contract_basic_parameters(&new_contract,&budget,&period,FSF_DEFAULT_WORKLOAD);
154
            NULL_TIMESPEC(&period);
155
            ADDUSEC2TIMESPEC(server_list[i].Tmin, &period);
156
            NULL_TIMESPEC(&budget);
157
            ADDUSEC2TIMESPEC(server_list[i].Cmax, &budget);
158
 
159
            fsf_set_contract_reclamation_parameters(&new_contract, &budget, &period, FSF_DEFAULT_GRANULARITY,NULL,
160
                                                    msg.qi.quality,msg.qi.importance);
161
 
162
            if (server_list[i].d_equals_t == true) {
163
              NULL_TIMESPEC(&new_contract.deadline);
164
              new_contract.d_equals_t = true;
165
            } else {
166
              NULL_TIMESPEC(&period);
167
              ADDUSEC2TIMESPEC(server_list[i].deadline, &period);
908 trimarchi 168
            TIMESPEC_ASSIGN(&new_contract.deadline, &period);
169
            new_contract.d_equals_t = false;
930 trimarchi 170
            }
171
            if (renegotiate_contract(&new_contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
172
              msg.server=-1;   
908 trimarchi 173
        }
930 trimarchi 174
          break;
175
 
176
        default :
177
          break;
178
 
179
        }
893 trimarchi 180
        break;
868 trimarchi 181
      }
930 trimarchi 182
    }
183
    port_send(tra,&msg,BLOCK);
909 trimarchi 184
 
668 trimarchi 185
  }
186
}
187
 
688 trimarchi 188
 
189
static __inline void set_contract_parameter(void) {
190
 
191
  int i=0;
679 trimarchi 192
  TIME T,Q;
868 trimarchi 193
 
194
  for (i=0; i<current_server; i++) {
679 trimarchi 195
      mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
196
      if (T > server_list[i].Tmin ) {
197
         server_list[i].actual_budget = server_list[i].Cmin;
198
         server_list[i].actual_period = T;
199
      #ifdef FSF_DEBUG
200
         kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
201
      #endif
908 trimarchi 202
         if (server_list[i].d_equals_t == true)
679 trimarchi 203
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
204
         else
205
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline);
206
 
207
      } else {
208
        mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
209
        server_list[i].actual_budget = Q;
210
        server_list[i].actual_period = server_list[i].Tmin;
211
      #ifdef FSF_DEBUG
212
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
213
      #endif
214
 
908 trimarchi 215
         if (server_list[i].d_equals_t == true)
679 trimarchi 216
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].Tmin);
217
         else
218
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].deadline);                                                                                                        
219
     }
220
 
221
     server_list[i].U=server_list[i].Umin;
222
   }
223
 
688 trimarchi 224
}
225
 
226
 
227
int renegotiate_contract
228
  (const fsf_contract_parameters_t *new_contract,
229
   fsf_server_id_t                 server)
230
{
231
 
232
  #ifdef FSF_DEBUG
233
    kern_printf("(Renegotiate for server %d)",server);
234
  #endif
235
 
236
  if (!new_contract)
868 trimarchi 237
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 238
 
239
  if (server < 0)
875 trimarchi 240
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 241
 
242
   // change the parameter
243
   relink_contract_to_server(new_contract, server);
244
   if (recalculate_contract(fsf_max_bw)==-1)  {
875 trimarchi 245
       return  FSF_ERR_CONTRACT_REJECTED;
688 trimarchi 246
   }
247
 
248
   set_contract_parameter();
249
 
679 trimarchi 250
   return 0;
251
}
252
 
668 trimarchi 253
int negotiate_contract
679 trimarchi 254
  (const fsf_contract_parameters_t *contract,
255
   fsf_server_id_t                 *server)
668 trimarchi 256
{
257
  /* Check if contract is initialized */
868 trimarchi 258
  if (!contract) return  FSF_ERR_BAD_ARGUMENT;
668 trimarchi 259
 
260
  /* Admission Test */
261
  if (FSF_ADMISSION_TEST_IS_ENABLED)
262
    if (add_contract(contract))
263
      return FSF_ERR_CONTRACT_REJECTED;
264
 
679 trimarchi 265
  /* SERVER => BUDGET */    
266
  set_SERVER_budget_from_contract(contract,server);
668 trimarchi 267
 
688 trimarchi 268
#ifdef FSF_DEBUG
269
  kern_printf("(New Server %d)",*server);
270
#endif
679 trimarchi 271
 
272
  if (*server >= 0) {
273
    link_contract_to_server(contract,*server);
274
    if (recalculate_contract(fsf_max_bw)==-1)  {
868 trimarchi 275
      remove_contract(server);
875 trimarchi 276
      return  FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 277
    }
688 trimarchi 278
    set_contract_parameter();
679 trimarchi 279
#ifdef  FSF_DEBUG
280
    kern_printf("(Adjust budget)");
281
#endif    
688 trimarchi 282
 
679 trimarchi 283
  }
284
  else  {
875 trimarchi 285
    return  FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 286
  }
880 trimarchi 287
  //kern_printf("(ENCR)"); 
679 trimarchi 288
  return 0;
289
 
668 trimarchi 290
}
291