Subversion Repositories shark

Rev

Rev 679 | Rev 688 | 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
 *
681 trimarchi 37
 * CVS :        $Id: server-task.c,v 1.3 2004-05-25 12:41:47 trimarchi Exp $
668 trimarchi 38
 */
39
 
679 trimarchi 40
#include "ll/i386/64bit.h"
668 trimarchi 41
#include <kernel/kern.h>
679 trimarchi 42
#include <modules/sem.h>
43
#include <modules/hartport.h>
668 trimarchi 44
#include <modules/cabs.h>
45
#include <string.h>
679 trimarchi 46
#include "fsf_contract.h"
47
#include "fsf_server.h"
48
#include "fsf_service_task.h"
668 trimarchi 49
#include "message.h"
50
 
679 trimarchi 51
TASK    service_task() {
52
 
53
  struct mess message;
54
  PORT rec,tra;
668 trimarchi 55
 
679 trimarchi 56
  rec = port_connect("CHANW",sizeof(struct mess),STREAM,READ);
668 trimarchi 57
 
679 trimarchi 58
  tra = port_connect("CHANR",sizeof(struct mess),STREAM,WRITE);
668 trimarchi 59
 
60
  while(1) {
679 trimarchi 61
    // wait for renegotiation
681 trimarchi 62
    port_receive(rec,&message,BLOCK);
668 trimarchi 63
    switch (message.type) {
64
 
65
    case NEGOTIATE_CONTRACT:
679 trimarchi 66
      if (negotiate_contract(&message.contract, &message.server)==FSF_ERR_CONTRACT_REJECTED)
67
        message.server=-1;
668 trimarchi 68
 
69
    break;
70
    case RENEGOTIATE_CONTRACT:
679 trimarchi 71
      if (renegotiate_contract(&message.contract, message.server)==FSF_ERR_CONTRACT_REJECTED)
72
        message.server=-1;
668 trimarchi 73
      break;                     
74
 
75
    default :
76
      break;
77
 
78
    }
679 trimarchi 79
    // send response server is -1 if the operation fail
681 trimarchi 80
    port_send(tra,&message,BLOCK);
668 trimarchi 81
 
82
    task_endcycle();
83
 
84
  }
85
}
86
 
679 trimarchi 87
int renegotiate_contract
88
  (const fsf_contract_parameters_t *new_contract,
89
   fsf_server_id_t                 server)
90
{
91
  TIME T,Q;
92
  int i;
93
 
94
  #ifdef FSF_DEBUG
95
    kern_printf("(Renegotiate for server %d)",server);
96
  #endif
97
 
98
  if (!new_contract)
99
    return FSF_ERR_NOT_INITIALIZED;
100
 
101
  if (server < 0)
102
    return FSF_ERR_INVALID_SERVER;
103
 
104
   // change the parameter
105
   relink_contract_to_server(new_contract, server);
106
   if (recalculate_contract(fsf_max_bw)==-1)  {
107
       return FSF_ERR_CREATE_SERVER;
108
   }
109
 
110
   for (i=0; i<current_server; i++) {
111
      mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
112
      if (T > server_list[i].Tmin ) {
113
         server_list[i].actual_budget = server_list[i].Cmin;
114
         server_list[i].actual_period = T;
115
      #ifdef FSF_DEBUG
116
         kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
117
      #endif
118
         if (server_list[i].d_equals_t == TRUE)
119
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, T);
120
         else
121
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T, server_list[i].deadline);
122
 
123
      } else {
124
        mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
125
        server_list[i].actual_budget = Q;
126
        server_list[i].actual_period = server_list[i].Tmin;
127
      #ifdef FSF_DEBUG
128
         kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
129
      #endif
130
 
131
         if (server_list[i].d_equals_t == TRUE)
132
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].Tmin);
133
         else
134
           adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, server_list[i].Tmin, server_list[i].deadline);                                                                                                        
135
     }
136
 
137
     server_list[i].U=server_list[i].Umin;
138
   }
139
 
140
   return 0;
141
}
142
 
668 trimarchi 143
int negotiate_contract
679 trimarchi 144
  (const fsf_contract_parameters_t *contract,
145
   fsf_server_id_t                 *server)
668 trimarchi 146
{
679 trimarchi 147
  int i=0;
668 trimarchi 148
  TIME T;
149
  TIME Q;
150
  /* Check if contract is initialized */
151
  if (!contract) return FSF_ERR_NOT_INITIALIZED;
152
 
153
  /* Admission Test */
154
  if (FSF_ADMISSION_TEST_IS_ENABLED)
155
    if (add_contract(contract))
156
      return FSF_ERR_CONTRACT_REJECTED;
157
 
679 trimarchi 158
  /* SERVER => BUDGET */    
159
  set_SERVER_budget_from_contract(contract,server);
668 trimarchi 160
 
679 trimarchi 161
  #ifdef FSF_DEBUG
162
    kern_printf("(New Server %d)",*server);
163
  #endif
164
 
165
  if (*server >= 0) {
166
    link_contract_to_server(contract,*server);
167
    if (recalculate_contract(fsf_max_bw)==-1)  {
168
       return FSF_ERR_CREATE_SERVER;
169
    }
170
#ifdef  FSF_DEBUG
171
    kern_printf("(Adjust budget)");
172
#endif    
173
    for (i=0; i<current_server; i++) {
174
       mul32div32to32(MAX_BANDWIDTH,server_list[i].Cmin,server_list[i].U,T);
175
       if (T > server_list[i].Tmin ) {
176
          server_list[i].actual_budget = server_list[i].Cmin;
177
          server_list[i].actual_period = T;
178
       #ifdef FSF_DEBUG
179
          kern_printf("(1 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
180
       #endif
181
 
182
          if (server_list[i].d_equals_t == TRUE)
183
            adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T , T);
184
          else
185
            adjust_SERVER_budget(server_list[i].server,server_list[i].Cmin, T , server_list[i].deadline);
186
 
187
       } else {
188
         mul32div32to32(server_list[i].Tmin,server_list[i].U,MAX_BANDWIDTH,Q);
189
         server_list[i].actual_budget = Q;
190
         server_list[i].actual_period = server_list[i].Tmin;
191
       #ifdef FSF_DEBUG
192
          kern_printf("(2 - Q %ld T %ld)", server_list[i].actual_budget, server_list[i].actual_period);
193
       #endif
194
 
195
          if (server_list[i].d_equals_t == TRUE)
196
            adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin , server_list[i].Tmin);
197
          else
198
            adjust_SERVER_budget(server_list[i].server,Q, server_list[i].Tmin , server_list[i].deadline);
199
 
200
        }
201
 
202
 
203
       server_list[i].U=server_list[i].Umin;
204
 
205
    }
206
  }
207
  else  {
208
    return FSF_ERR_CREATE_SERVER;
209
  }
210
 
211
  return 0;
212
 
668 trimarchi 213
}
214
 
215
 
679 trimarchi 216
#if defined MICHAEL
668 trimarchi 217
int recalculate_contract(bandwidth_t U) {
218
  bandwidth_t current_bandwidth;
219
  unsigned int temp_U;
220
  int        Qt;
221
  int isok=0;
222
  int i=0;
223
 
224
  #ifdef FSF_DEBUG
225
    int temp;
226
 
227
    kern_printf("(Recalculate contract)");
228
  #endif
229
 
230
  /* The current bandwidth is the min bandwidth */
231
  //current_bandwidth=SERVER_return_bandwidth(fsf_server_level);
232
  #ifdef FSF_DEBUG
233
     kern_printf("(nserver %d)", current_server);
234
  #endif  
235
 
236
  do  {
237
    current_bandwidth=0;
238
    Qt=0;
239
    for (i=0; i<current_server; i++) {
240
      if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax)
241
         Qt+=server_list[i].Qs;
242
       current_bandwidth+=server_list[i].U;
243
    }
244
 
245
    #ifdef FSF_DEBUG
246
    kern_printf("(Total Quality %d)", Qt);
247
    #endif
248
    isok=1;
249
    for (i=0; i<current_server; i++) {
250
      if (server_list[i].Qs!=0 && server_list[i].U<server_list[i].Umax) {
251
        temp_U=server_list[i].U;
252
        server_list[i].U=U-current_bandwidth;
253
        mul32div32to32(server_list[i].U, server_list[i].Qs, Qt, server_list[i].U);
254
        temp_U+=server_list[i].U;
255
 
256
        if (temp_U<=server_list[i].Umin) {
257
           server_list[i].U=server_list[i].Umin;
258
        } else if (temp_U>server_list[i].Umax)  {
259
           server_list[i].U=server_list[i].Umax;
260
           isok=0;
261
        } else server_list[i].U=temp_U;
262
 
263
#ifdef FSF_DEBUG
264
        mul32div32to32(server_list[i].U,100, MAX_BANDWIDTH, temp);
265
        kern_printf("(Server %d bw %d)", server_list[i].server, temp);
266
#endif 
267
      }
268
    }  
269
 
270
  } while (!isok);
271
 
272
 return 0;
273
}
679 trimarchi 274
#endif