Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add3f885cf78aab9dd8a0fe8bc580f0b95b728d8
[simgrid.git] / src / gras / Transport / rl_transport.c
1 /* $Id$ */
2
3 /* rl_transport - RL specific functions for transport                       */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/ex.h"
11 #include "portable.h"
12 #include "gras/Transport/transport_private.h"
13 XBT_LOG_EXTERNAL_CATEGORY(transport);
14 XBT_LOG_DEFAULT_CATEGORY(transport);
15
16 /**
17  * gras_trp_select:
18  *
19  * Returns the next socket to service because it receives a message.
20  *
21  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
22  *
23  * if timeout=0, do not wait for new message, only handle the ones already there.
24  *
25  * if timeout>0 and no message there, wait at most that amount of time before giving up.
26  */
27 gras_socket_t gras_trp_select(double timeout) {
28   xbt_dynar_t sockets= ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
29   int done = -1;
30   double wakeup = gras_os_time() + timeout;
31   double now = 0;
32   /* nextToService used to make sure socket with high number do not starve */
33   /*  static int nextToService = 0; */
34   struct timeval tout, *p_tout;
35
36   int max_fds=0; /* first arg of select: number of existing sockets */
37   /* but accept() of winsock returns sockets bigger than the limit, so don't bother 
38      with this tiny optimisation on BillWare */
39   fd_set FDS;
40   int ready; /* return of select: number of socket ready to be serviced */
41   int fd_setsize; /* FD_SETSIZE not always defined. Get this portably */
42
43   gras_socket_t sock_iter; /* iterating over all sockets */
44   int cursor;              /* iterating over all sockets */
45
46    
47   /* Compute FD_SETSIZE */
48 #ifdef HAVE_SYSCONF
49    fd_setsize = sysconf( _SC_OPEN_MAX );
50 #else
51 #  ifdef HAVE_GETDTABLESIZE 
52    fd_setsize = getdtablesize();
53 #  else
54    fd_setsize = FD_SETSIZE;
55 #  endif /* !USE_SYSCONF */
56 #endif
57
58   while (done == -1) {
59     if (timeout > 0) { /* did we timeout already? */
60       now = gras_os_time();
61       DEBUG2("wakeup=%f now=%f",wakeup, now);
62       if (now == -1 || now >= wakeup) {
63         /* didn't find anything */
64         THROW1(timeout_error,0,
65                "Timeout (%f) elapsed with selecting for incomming connexions",
66                timeout);
67       }
68     }
69
70     /* construct the set of socket to ear from */
71     FD_ZERO(&FDS);
72     max_fds = -1;
73     xbt_dynar_foreach(sockets,cursor,sock_iter) {
74       if (sock_iter->incoming) {
75         DEBUG1("Considering socket %d for select",sock_iter->sd);
76 #ifndef HAVE_WINSOCK_H
77         if (max_fds < sock_iter->sd)
78           max_fds = sock_iter->sd;
79 #endif
80         FD_SET(sock_iter->sd, &FDS);
81       } else {
82         DEBUG1("Not considering socket %d for select",sock_iter->sd);
83       }
84     }
85
86     if (max_fds == -1) {
87        if (timeout > 0) {
88           DEBUG1("No socket to select onto. Sleep %f sec instead.",timeout);
89           gras_os_sleep(timeout);
90           THROW1(timeout_error,0,"No socket to select onto. Sleep %f sec instead",timeout);
91        } else {
92           DEBUG0("No socket to select onto. Return directly.");
93           THROW0(timeout_error,0, "No socket to select onto. Return directly.");
94        }
95     }
96
97 #ifndef HAVE_WINSOCK_H
98     /* we cannot have more than FD_SETSIZE sockets 
99        ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
100     if (++max_fds > fd_setsize && fd_setsize > 0) {
101       WARN1("too many open sockets (%d).",max_fds);
102       done = 0;
103       break;
104     }
105 #else
106     max_fds = fd_setsize;
107 #endif
108
109     if (timeout > 0) { 
110       /* set the timeout */
111       tout.tv_sec = (unsigned long)(wakeup - now);
112       tout.tv_usec = ((wakeup -now) - ((unsigned long)(wakeup - now))) * 1000000;
113       p_tout = &tout;
114     } else if (timeout == 0) {
115       /* polling only */
116       tout.tv_sec = 0;
117       tout.tv_usec = 0;
118       p_tout = &tout;
119       /* we just do one loop around */
120       done = 0;
121     } else { 
122       /* no timeout: good luck! */
123       p_tout = NULL;
124     }
125      
126     DEBUG2("Selecting over %d socket(s); timeout=%f", max_fds-1,timeout);
127     ready = select(max_fds, &FDS, NULL, NULL, p_tout);
128     DEBUG1("select returned %d", ready);
129     if (ready == -1) {
130       switch (errno) {
131       case  EINTR: /* a signal we don't care about occured. we don't care */
132         /* if we cared, we would have set an handler */
133         continue;
134       case EINVAL: /* invalid value */
135         THROW3(system_error,EINVAL,"invalid select: nb fds: %d, timeout: %d.%d",
136                max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
137       case ENOMEM: 
138         xbt_assert0(0,"Malloc error during the select");
139       default:
140         THROW2(system_error,errno,"Error during select: %s (%d)",
141                strerror(errno),errno);
142       }
143       THROW_IMPOSSIBLE;
144     } else if (ready == 0) {
145       continue;  /* this was a timeout */
146     }
147
148     xbt_dynar_foreach(sockets,cursor,sock_iter) { 
149        if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
150         continue;
151        }
152        
153        /* Got a socket to serve */
154        ready--;
155
156        if (   sock_iter->accepting
157            && sock_iter->plugin->socket_accept) { 
158          /* not a socket but an ear. accept on it and serve next socket */
159          gras_socket_t accepted=NULL;
160          
161          accepted = (sock_iter->plugin->socket_accept)(sock_iter);
162          DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
163          accepted->meas = sock_iter->meas;
164        } else {
165 #if 0 
166        FIXME: this fails of files. quite logical
167          /* Make sure the socket is still alive by reading the first byte */
168          char lookahead;
169          int recvd;
170
171          recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
172          if (recvd < 0) {
173            WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
174            /* done with this socket */
175            gras_socket_close(&sock_iter);
176            cursor--;
177          } else if (recvd == 0) {
178            /* Connection reset (=closed) by peer. */
179            DEBUG1("Connection %d reset by peer", sock_iter->sd);
180            gras_socket_close(&sock_iter); 
181            cursor--; 
182          } else { 
183 #endif
184            /* Got a suited socket ! */
185            XBT_OUT;
186            return sock_iter;
187 #if 0
188          }
189 #endif
190        }
191
192        
193        /* if we're here, the socket we found wasn't really ready to be served */
194        if (ready == 0) /* exausted all sockets given by select. Request new ones */
195          break; 
196     }
197
198   }
199
200   XBT_OUT;
201   return NULL;
202 }
203
204 void gras_trp_sg_setup(gras_trp_plugin_t plug) {
205   THROW0(mismatch_error,0,"No SG transport on live platforms");
206 }
207