Subversion Repositories shark

Rev

Rev 908 | Rev 930 | 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
 *
909 trimarchi 37
 * CVS :        $Id: server-task.c,v 1.14 2004-12-17 13:19:42 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
 
53
TASK service_task(void) {
679 trimarchi 54
 
868 trimarchi 55
  struct mess msg;
56
  fsf_renegotiation_status_t status;
57
  int wait_renegotiation=0;
909 trimarchi 58
 
679 trimarchi 59
  PORT rec,tra;
868 trimarchi 60
 
679 trimarchi 61
  rec = port_connect("CHANW",sizeof(struct mess),STREAM,READ);
668 trimarchi 62
 
679 trimarchi 63
  tra = port_connect("CHANR",sizeof(struct mess),STREAM,WRITE);
668 trimarchi 64
 
65
  while(1) {
868 trimarchi 66
    // wait for operation
67
    if (wait_renegotiation) {
68
      if (!fsf_get_renegotiation_status(msg.server,&status)) {
69
        if (status==FSF_ADMITTED) {
70
          // send response server is -1 if the operation fail
71
#ifdef FSF_DEBUG
72
          kern_printf("ADM");
73
#endif
74
          wait_renegotiation=0;
908 trimarchi 75
          //kern_printf("(REN %d)", msg.server);
868 trimarchi 76
          port_send(tra,&msg,BLOCK);
909 trimarchi 77
 
868 trimarchi 78
          continue;
79
        } else
80
#ifdef FSF_DEBUG
81
          kern_printf("WAIT")
82
#endif
83
;
84
      }
85
    } else {
86
      port_receive(rec,&msg,BLOCK);
877 trimarchi 87
#ifdef FSF_DEBUG
868 trimarchi 88
      kern_printf("Message ");
877 trimarchi 89
#endif
868 trimarchi 90
      switch (msg.type) {
91
 
92
      case NEGOTIATE_CONTRACT:
93
        if (negotiate_contract(&msg.contract, &msg.server)==FSF_ERR_CONTRACT_REJECTED)
94
          msg.server=-1;       
880 trimarchi 95
        else {
96
          wait_renegotiation=1;
97
          //task_endcycle();
98
          continue;
99
        }
100
        break;
868 trimarchi 101
 
102
      case REQUEST_RENEGOTIATE_CONTRACT:
103
        if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
104
          msg.server=-1;
105
        break;
106
 
107
      case RENEGOTIATE_CONTRACT:
108
        if (renegotiate_contract(&msg.contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
109
          msg.server=-1;
110
        else {
111
          wait_renegotiation=1;
112
          //task_endcycle();
113
          continue;
114
        }
115
        break;                   
908 trimarchi 116
      case CHANGE_PARAMETER:
117
        {
118
          fsf_contract_parameters_t new_contract;
119
          struct timespec period;
120
          struct timespec budget;
121
          int i=0;
122
          // this CASE change the quality and importance
123
          //fsf_initialize_contract(&new_contract);
124
           // find contract
125
          while(i<current_server) {
126
            if (server_list[i].server==msg.server) break;
127
            i++;
128
          }
129
          //kern_printf("(RCQI)");
130
          NULL_TIMESPEC(&period);
131
          ADDUSEC2TIMESPEC(server_list[i].Tmax, &period);
132
          NULL_TIMESPEC(&budget);
133
          ADDUSEC2TIMESPEC(server_list[i].Cmin, &budget);
134
 
135
          fsf_set_contract_basic_parameters(&new_contract,&budget,&period,FSF_DEFAULT_WORKLOAD);
136
          NULL_TIMESPEC(&period);
137
          ADDUSEC2TIMESPEC(server_list[i].Tmin, &period);
138
          NULL_TIMESPEC(&budget);
139
          ADDUSEC2TIMESPEC(server_list[i].Cmax, &budget);
140
 
141
          fsf_set_contract_reclamation_parameters(&new_contract, &budget, &period, FSF_DEFAULT_GRANULARITY,NULL,
142
                                                  msg.qi.quality,msg.qi.importance);
143
 
144
          if (server_list[i].d_equals_t == true) {
145
            NULL_TIMESPEC(&new_contract.deadline);
146
            new_contract.d_equals_t = true;
147
          } else {
148
            NULL_TIMESPEC(&period);
149
            ADDUSEC2TIMESPEC(server_list[i].deadline, &period);
150
            TIMESPEC_ASSIGN(&new_contract.deadline, &period);
151
            new_contract.d_equals_t = false;
152
          }
153
          if (renegotiate_contract(&new_contract, msg.server)==FSF_ERR_CONTRACT_REJECTED)
154
            msg.server=-1;     
155
        }
893 trimarchi 156
        break;
157
 
868 trimarchi 158
      default :
159
        break;
160
 
161
      }
909 trimarchi 162
 
868 trimarchi 163
      port_send(tra,&msg,BLOCK);
909 trimarchi 164
 
668 trimarchi 165
    }
166
  }
167
}
168
 
688 trimarchi 169
 
170
static __inline void set_contract_parameter(void) {
171
 
172
  int i=0;
679 trimarchi 173
  TIME T,Q;
868 trimarchi 174
 
175
  for (i=0; i<current_server; i++) {
679 trimarchi 176
      mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
177
      if (T > server_list[i].Tmin ) {
178
         server_list[i].actual_budget = server_list[i].Cmin;
179
         server_list[i].actual_period = T;
180
      #ifdef FSF_DEBUG
181
         kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
182
      #endif
908 trimarchi 183
         if (server_list[i].d_equals_t == true)
679 trimarchi 184
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
185
         else
186
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline);
187
 
188
      } else {
189
        mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
190
        server_list[i].actual_budget = Q;
191
        server_list[i].actual_period = server_list[i].Tmin;
192
      #ifdef FSF_DEBUG
193
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
194
      #endif
195
 
908 trimarchi 196
         if (server_list[i].d_equals_t == true)
679 trimarchi 197
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].Tmin);
198
         else
199
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].deadline);                                                                                                        
200
     }
201
 
202
     server_list[i].U=server_list[i].Umin;
203
   }
204
 
688 trimarchi 205
}
206
 
207
 
208
int renegotiate_contract
209
  (const fsf_contract_parameters_t *new_contract,
210
   fsf_server_id_t                 server)
211
{
212
 
213
  #ifdef FSF_DEBUG
214
    kern_printf("(Renegotiate for server %d)",server);
215
  #endif
216
 
217
  if (!new_contract)
868 trimarchi 218
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 219
 
220
  if (server < 0)
875 trimarchi 221
    return  FSF_ERR_BAD_ARGUMENT;
688 trimarchi 222
 
223
   // change the parameter
224
   relink_contract_to_server(new_contract, server);
225
   if (recalculate_contract(fsf_max_bw)==-1)  {
875 trimarchi 226
       return  FSF_ERR_CONTRACT_REJECTED;
688 trimarchi 227
   }
228
 
229
   set_contract_parameter();
230
 
679 trimarchi 231
   return 0;
232
}
233
 
668 trimarchi 234
int negotiate_contract
679 trimarchi 235
  (const fsf_contract_parameters_t *contract,
236
   fsf_server_id_t                 *server)
668 trimarchi 237
{
238
  /* Check if contract is initialized */
868 trimarchi 239
  if (!contract) return  FSF_ERR_BAD_ARGUMENT;
668 trimarchi 240
 
241
  /* Admission Test */
242
  if (FSF_ADMISSION_TEST_IS_ENABLED)
243
    if (add_contract(contract))
244
      return FSF_ERR_CONTRACT_REJECTED;
245
 
679 trimarchi 246
  /* SERVER => BUDGET */    
247
  set_SERVER_budget_from_contract(contract,server);
668 trimarchi 248
 
688 trimarchi 249
#ifdef FSF_DEBUG
250
  kern_printf("(New Server %d)",*server);
251
#endif
679 trimarchi 252
 
253
  if (*server >= 0) {
254
    link_contract_to_server(contract,*server);
255
    if (recalculate_contract(fsf_max_bw)==-1)  {
868 trimarchi 256
      remove_contract(server);
875 trimarchi 257
      return  FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 258
    }
688 trimarchi 259
    set_contract_parameter();
679 trimarchi 260
#ifdef  FSF_DEBUG
261
    kern_printf("(Adjust budget)");
262
#endif    
688 trimarchi 263
 
679 trimarchi 264
  }
265
  else  {
875 trimarchi 266
    return  FSF_ERR_CONTRACT_REJECTED;
679 trimarchi 267
  }
880 trimarchi 268
  //kern_printf("(ENCR)"); 
679 trimarchi 269
  return 0;
270
 
668 trimarchi 271
}
272