Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The buffered transport is now RL only (will allow a zero-copy version)
[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_socketset_get();
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       if (now == -1 || now >= wakeup) {
62         done = 0;       /* didn't find anything */
63         break;
64       }
65     }
66
67     /* construct the set of socket to ear from */
68     FD_ZERO(&FDS);
69     max_fds = -1;
70     xbt_dynar_foreach(sockets,cursor,sock_iter) {
71       if (sock_iter->incoming) {
72         DEBUG1("Considering socket %d for select",sock_iter->sd);
73 #ifndef HAVE_WINSOCK_H
74         if (max_fds < sock_iter->sd)
75           max_fds = sock_iter->sd;
76 #endif
77         FD_SET(sock_iter->sd, &FDS);
78       } else {
79         DEBUG1("Not considering socket %d for select",sock_iter->sd);
80       }
81     }
82
83     if (max_fds == -1) {
84        if (timeout > 0) {
85           DEBUG1("No socket to select onto. Sleep %f sec instead.",timeout);
86           gras_os_sleep(timeout);
87           THROW1(timeout_error,0,"No socket to select onto. Sleep %f sec instead",timeout);
88        } else {
89           DEBUG0("No socket to select onto. Return directly.");
90           THROW0(timeout_error,0, "No socket to select onto. Return directly.");
91        }
92     }
93
94 #ifndef HAVE_WINSOCK_H
95     /* we cannot have more than FD_SETSIZE sockets 
96        ... but with WINSOCK which returns sockets higher than the limit (killing this optim) */
97     if (++max_fds > fd_setsize && fd_setsize > 0) {
98       WARN1("too many open sockets (%d).",max_fds);
99       done = 0;
100       break;
101     }
102 #else
103     max_fds = fd_setsize;
104 #endif
105
106     if (timeout > 0) { 
107       /* set the timeout */
108       tout.tv_sec = (unsigned long)((wakeup - now)/1000000);
109       tout.tv_usec = (unsigned long)(wakeup - now) % 1000000;
110       p_tout = &tout;
111     } else if (timeout == 0) {
112       /* polling only */
113       tout.tv_sec = 0;
114       tout.tv_usec = 0;
115       p_tout = &tout;
116       /* we just do one loop around */
117       done = 0;
118     } else { 
119       /* no timeout: good luck! */
120       p_tout = NULL;
121     }
122      
123     DEBUG1("Selecting over %d socket(s)", max_fds-1);
124     ready = select(max_fds, &FDS, NULL, NULL, p_tout);
125     if (ready == -1) {
126       switch (errno) {
127       case  EINTR: /* a signal we don't care about occured. we don't care */
128         /* if we cared, we would have set an handler */
129         continue;
130       case EINVAL: /* invalid value */
131         THROW3(system_error,EINVAL,"invalid select: nb fds: %d, timeout: %d.%d",
132                max_fds, (int)tout.tv_sec,(int) tout.tv_usec);
133       case ENOMEM: 
134         xbt_assert0(0,"Malloc error during the select");
135       default:
136         THROW2(system_error,errno,"Error during select: %s (%d)",
137                strerror(errno),errno);
138       }
139       THROW_IMPOSSIBLE;
140     } else if (ready == 0) {
141       continue;  /* this was a timeout */
142     }
143
144     xbt_dynar_foreach(sockets,cursor,sock_iter) { 
145        if(!FD_ISSET(sock_iter->sd, &FDS)) { /* this socket is not ready */
146         continue;
147        }
148        
149        /* Got a socket to serve */
150        ready--;
151
152        if (   sock_iter->accepting
153            && sock_iter->plugin->socket_accept) { 
154          /* not a socket but an ear. accept on it and serve next socket */
155          gras_socket_t accepted=NULL;
156          
157          accepted = (sock_iter->plugin->socket_accept)(sock_iter);
158          DEBUG2("accepted=%p,&accepted=%p",accepted,&accepted);
159          accepted->meas = sock_iter->meas;
160        } else {
161 #if 0 
162        FIXME: this fails of files. quite logical
163          /* Make sure the socket is still alive by reading the first byte */
164          char lookahead;
165          int recvd;
166
167          recvd = recv(sock_iter->sd, &lookahead, 1, MSG_PEEK);
168          if (recvd < 0) {
169            WARN2("socket %d failed: %s", sock_iter->sd, strerror(errno));
170            /* done with this socket */
171            gras_socket_close(&sock_iter);
172            cursor--;
173          } else if (recvd == 0) {
174            /* Connection reset (=closed) by peer. */
175            DEBUG1("Connection %d reset by peer", sock_iter->sd);
176            gras_socket_close(&sock_iter); 
177            cursor--; 
178          } else { 
179 #endif
180            /* Got a suited socket ! */
181            XBT_OUT;
182            return sock_iter;
183 #if 0
184          }
185 #endif
186        }
187
188        
189        /* if we're here, the socket we found wasn't really ready to be served */
190        if (ready == 0) /* exausted all sockets given by select. Request new ones */
191          break; 
192     }
193
194   }
195
196   XBT_OUT;
197   return NULL;
198 }
199
200 void gras_trp_sg_setup(gras_trp_plugin_t plug) {
201   THROW0(mismatch_error,0,"No SG transport on live platforms");
202 }
203