RE: poll with timeout 0 in main loop

2010-10-22 Thread Juan Pablo L .

this is almost the complete code. you can see all the flow here.
basically, the main.c starts the server thread (gm_tcp_server), inside the 
server thread the application sits waiting for new connection, just sitting 
waiting consumes 100% of the CPU (without accepting any connection yet), the 
code of the server is in file gm_tcp_server.c. the rest of the code is just in 
case you need it for reference.

thanks!

 Date: Fri, 22 Oct 2010 10:17:30 +0100
 From: ch...@cvine.freeserve.co.uk
 To: jpablolorenze...@hotmail.com
 CC: maginot.jun...@gmail.com; gtk-app-devel-list@gnome.org
 Subject: Re: poll with timeout 0 in main loop
 
 On Fri, 22 Oct 2010 02:57:59 +
 Juan Pablo L. jpablolorenze...@hotmail.com wrote:
  hi, this is the code that makes the server socket
 [snip]
 
 There doesn't seem anything especially wrong with this but you have a
 lot of code missing.  In particular, what does your
 tcp_server_handle_connection_events() callback do, such as when it
 encounters errors (you specify that the callback is to be entered in a
 case of G_IO_NVAL and G_IO_ERR), and how do you disconnect sources from
 the main loop when they are finished with (I notice also you don't
 unref the sources after attaching them to the main loop).  Quite
 possibly you are looping in a case of errors or defunct sources.
 
 Probably the best thing you can do is come up with the smallest test
 case which demonstrates the error (with say just one socket worker
 thread) and post it here.
 
 This is off topic, but glib's gio makes it trivial to construct new
 server threads and it does all the hard stuff for you, but you need
 glib version 2.22 for gio's socket API.  Glib's gio has no relationship
 with GIOChannels, despite the similar names.
 
 Chris
  ___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

RE: poll with timeout 0 in main loop

2010-10-22 Thread Juan Pablo L .
;
}

gboolean tcp_server_handle_connection_events(GIOChannel *p_source,GIOCondition 
p_condition,GMTCPServer *p_server)
{
TCPClientConnection *client = NULL;
struct sockaddr_in sockaddr;
socklen_t client_fd_len;
int client_fd;
//GError *error = NULL;
int set_option = 1;

if(p_condition  (G_IO_IN|G_IO_PRI))
{
// accept the connection now
client_fd_len = sizeof(sockaddr);
if((client_fd = 
accept(g_io_channel_unix_get_fd(p_server-socket),(struct 
sockaddr*)sockaddr,client_fd_len))  0)
{
g_error(Could not accept new connection: %s,strerror(errno));
return TRUE;
}

fcntl(client_fd,F_SETFL,O_NONBLOCK);// set the socket non-blocking

setsockopt(client_fd,SOL_SOCKET,SO_KEEPALIVE,set_option,sizeof(set_option));// 
send keep alive messages

g_message(New connection attempt);

// create a client connection
client = gm_connection_new(client_fd,p_server);

// get the address for this client
if 
(inet_ntop(AF_INET,sockaddr.sin_addr.s_addr,client-ip,sizeof(client-ip)) != 
NULL)
{
client-port = ntohs(sockaddr.sin_port);
g_message(Successfully accepted new client connection from 
%s:%d,client-ip,client-port);
}
else
{
client-ip[0] = 0;
client-port = -1;
g_message(Successfully accepted new client connection from an 
unknown location);
}

// run the connection thread
client-connection_thread = 
g_thread_create((GThreadFunc)gm_connection_run,client,TRUE,/*error*/NULL);
if(client-connection_thread == NULL)
{
g_error(Could not start new connection for client: %s:%d, will end 
this connection,client-ip,client-port);
gm_connection_destroy(client);
}
else
{
// push it into the client array
g_ptr_array_add(p_server-clients_list,client);
g_message(Successfully started new connection);
}

return TRUE;
}

// an error happened
// show an error message and try to get a description from errno
g_warning(An error occurred on the server socket: %s,strerror(errno));
return TRUE;
}

gboolean tcp_server_remove_connection(TCPClientConnection *p_client)
{
// remove a client
g_ptr_array_remove(p_client-my_server-clients_list,p_client);

return TRUE;
}



and the server is astarted only once from the main thread.
regarding GIO, i like to have like control of things so i m just sticking with 
glib, what i need from glib are the special structucures like 
array pointers, sometimes the strings as containers and of course, the mail 
loop :)  

 Date: Fri, 22 Oct 2010 10:17:30 +0100
 From: ch...@cvine.freeserve.co.uk
 To: jpablolorenze...@hotmail.com
 CC: maginot.jun...@gmail.com; gtk-app-devel-list@gnome.org
 Subject: Re: poll with timeout 0 in main loop
 
 On Fri, 22 Oct 2010 02:57:59 +
 Juan Pablo L. jpablolorenze...@hotmail.com wrote:
  hi, this is the code that makes the server socket
 [snip]
 
 There doesn't seem anything especially wrong with this but you have a
 lot of code missing.  In particular, what does your
 tcp_server_handle_connection_events() callback do, such as when it
 encounters errors (you specify that the callback is to be entered in a
 case of G_IO_NVAL and G_IO_ERR), and how do you disconnect sources from
 the main loop when they are finished with (I notice also you don't
 unref the sources after attaching them to the main loop).  Quite
 possibly you are looping in a case of errors or defunct sources.
 
 Probably the best thing you can do is come up with the smallest test
 case which demonstrates the error (with say just one socket worker
 thread) and post it here.
 
 This is off topic, but glib's gio makes it trivial to construct new
 server threads and it does all the hard stuff for you, but you need
 glib version 2.22 for gio's socket API.  Glib's gio has no relationship
 with GIOChannels, despite the similar names.
 
 Chris
  
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: poll with timeout 0 in main loop

2010-10-22 Thread Chris Vine
On Fri, 22 Oct 2010 10:37:01 +
Juan Pablo L. jpablolorenze...@hotmail.com wrote:
 i attached the code but did not make thru, here is the server code:

Something odd is going on.

I see you are using GIOChannel not to do any reading, but in order to
notify you that a connection is available.  poll()/select() of course
support this in unix-like systems, but I don't know if GIOChannel
does (I have never tried). Possibly it only works if you set the channel
as non-blocking with g_io_channel_set_flags() rather than by
manipulating the file descriptor directly, which you have done.  Again,
I don't know.

Sending snippets of code really isn't going to help.  It could of
course be other things, such as you not having initialized glib for
threads correctly. You need to send the simplest test case which
demonstrates the problem, in compilable form: given the nature of
the problem you should be able to reproduce it with very simple code.

Chris
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: poll with timeout 0 in main loop

2010-10-22 Thread Chris Vine
On Fri, 22 Oct 2010 13:53:55 +0100
Chris Vine ch...@cvine.freeserve.co.uk wrote:

 On Fri, 22 Oct 2010 10:37:01 +
 Juan Pablo L. jpablolorenze...@hotmail.com wrote:
  i attached the code but did not make thru, here is the server code:
 
 Something odd is going on.
 
 I see you are using GIOChannel not to do any reading, but in order to
 notify you that a connection is available.  poll()/select() of course
 support this in unix-like systems, but I don't know if GIOChannel
 does (I have never tried). Possibly it only works if you set the
 channel as non-blocking with g_io_channel_set_flags() rather than by
 manipulating the file descriptor directly, which you have done.
 Again, I don't know.

Actually, in this usage (attaching the file descriptor of a listening
socket to a main loop) there is no need to make the file descriptor
non-blocking since you will not make any blocking calls on it until you
actually get to call accept() to establish the connection, and you may
get undefined behaviour if you do - I don't know what POSIX says about
that when you attempt to use poll() with it.  Once poll() indicates
that a connection is available, accept() can't block anyway.

Does it work if you omit your call to fctl()?

Chris
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


RE: poll with timeout 0 in main loop

2010-10-22 Thread Juan Pablo L .

Hi Chris, yes it works without setting it as non-blocking so i decided to leave 
none blocking just in case .. :)  anyways
i was preparing the test code to send to you guys but i discovered that in the 
test code the cpu usage is 0% (as i would expect in the real application)  ... 
the code i was going to send was a stripped version of the original code so 
that ... but there is something that might be very important but have not 
mention yet . i also created a GSource to run functions in other main loops 
(for example i use this to fire the connection cleaning code in the server when 
a connection dies) asynchronously, anyways, this is makes me very suspicious 
now:

static gboolean async_callback_prepare(GSource *p_source,int *p_timeout)
{
AsyncCallbackSource *async_source = (AsyncCallbackSource *)p_source;
gboolean ret;

*p_timeout = 0;
g_mutex_lock(async_source-mutex1);
ret = g_queue_is_empty(async_source-callback_list);
g_mutex_unlock(async_source-mutex1);

return !ret;
}
   


i think i misunderstood the documentation here and maybe this is the 0 being 
passed to poll ???  i think i will test this in the real code and let you know 
asap 



 Date: Fri, 22 Oct 2010 14:27:54 +0100
 From: ch...@cvine.freeserve.co.uk
 To: ch...@cvine.freeserve.co.uk
 CC: jpablolorenze...@hotmail.com; gtk-app-devel-list@gnome.org
 Subject: Re: poll with timeout 0 in main loop
 
 On Fri, 22 Oct 2010 13:53:55 +0100
 Chris Vine ch...@cvine.freeserve.co.uk wrote:
 
  On Fri, 22 Oct 2010 10:37:01 +
  Juan Pablo L. jpablolorenze...@hotmail.com wrote:
   i attached the code but did not make thru, here is the server code:
  
  Something odd is going on.
  
  I see you are using GIOChannel not to do any reading, but in order to
  notify you that a connection is available.  poll()/select() of course
  support this in unix-like systems, but I don't know if GIOChannel
  does (I have never tried). Possibly it only works if you set the
  channel as non-blocking with g_io_channel_set_flags() rather than by
  manipulating the file descriptor directly, which you have done.
  Again, I don't know.
 
 Actually, in this usage (attaching the file descriptor of a listening
 socket to a main loop) there is no need to make the file descriptor
 non-blocking since you will not make any blocking calls on it until you
 actually get to call accept() to establish the connection, and you may
 get undefined behaviour if you do - I don't know what POSIX says about
 that when you attempt to use poll() with it.  Once poll() indicates
 that a connection is available, accept() can't block anyway.
 
 Does it work if you omit your call to fctl()?
 
 Chris
  
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


poll with timeout 0 in main loop

2010-10-21 Thread Juan Pablo L .

Hi, i have a problem with an application i m building with glib-2.24.2 on 
linux, i create a server socket which i use to create a GIOChannel and add it 
to a main loop, everything was fine until i found that the application consumes 
99.8% of the CPU while waiting for connections (doing nothing else just sitting 
there waiting for connections) and it gets even worse as new connections come 
in because i create a new loop in each connection (i create a thread for each 
connection) to deal with the incoming packages and each connection has N 
handlers to process the requests, each handler has its main loop for 
asynchronous communication between them and the connection thread that owns the 
handlers, so i did a strace and found out that the time out being passed to 
poll is 0 so for each iteration the poll returns inmediatly therefore i m stuck 
with a busy wait in all main loops, so i have read the documentation trying to 
find out how to modify this bahaviour but i could find nothing ab
 out it, can you please tell me why this is happenning ?? thanks!
  
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: poll with timeout 0 in main loop

2010-10-21 Thread Maginot Junior
On Thu, Oct 21, 2010 at 11:27 PM, Juan Pablo L. 
jpablolorenze...@hotmail.com wrote:


 Hi, i have a problem with an application i m building with glib-2.24.2 on
 linux, i create a server socket which i use to create a GIOChannel and add
 it to a main loop, everything was fine until i found that the application
 consumes 99.8% of the CPU while waiting for connections (doing nothing else
 just sitting there waiting for connections) and it gets even worse as new
 connections come in because i create a new loop in each connection (i create
 a thread for each connection) to deal with the incoming packages and each
 connection has N handlers to process the requests, each handler has its main
 loop for asynchronous communication between them and the connection thread
 that owns the handlers, so i did a strace and found out that the time out
 being passed to poll is 0 so for each iteration the poll returns inmediatly
 therefore i m stuck with a busy wait in all main loops, so i have read the
 documentation trying to find out how to modify this bahaviour but i could
 find nothing ab
  out it, can you please tell me why this is happenning ?? thanks!

 ___
 gtk-app-devel-list mailing list
 gtk-app-devel-list@gnome.org
 http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



Can you post an snipt of your code?

And if you started a Thread and a loop inside it, it will stay interacting
if you don't stop it, this will consume CPU for sure. I think you must work
with signals or interrupts, I'm not sure about GIOChannel, maybe a snipt
would clear a bit.

[ ]'s


--
Maginot JĂșnior
LPIC 1 - LPIC 2 - LPIC 3 -  CCNA - CLA - Forensics Analyst
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: poll with timeout 0 in main loop

2010-10-21 Thread Maginot Junior
2010/10/22 Juan Pablo L. jpablolorenze...@hotmail.com

  hi, this is the code that makes the server socket

 // create the server socket and return the file descriptor
 fd = tcp_server_listerner_new(new_server-configurations,error);
 if(fd  0)
 {
 g_error(Could not create listener, server creation aborted:
 %s,error-message);
 g_error_free(error);
 return NULL;
 }

 // create the GIOChannel for the server socket
 new_server-socket = g_io_channel_unix_new(fd);
 // set the encoding safe to read binary data on the server GIOChannel
 g_io_channel_set_encoding(new_server-socket,NULL,NULL);

// create a source for connection events on the server socket
 new_server-event_source =
 g_io_create_watch(new_server-socket,G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL);
 // set the callback function to handle connection events

 g_source_set_callback(new_server-event_source,(GSourceFunc)tcp_server_handle_connection_events,new_server,NULL);
 // attach the server socket connection event source to the server
 context
 g_source_attach(new_server-event_source,new_server-context);

new_server-main_loop = g_main_loop_new(new_server-context,FALSE);

 and this is how i run the main loop:

 void tcp_server_run(GMTCPServer *p_server)
 {
 // run the main loop now
 g_main_loop_run(p_server-main_loop);

 g_thread_exit(NULL);
 }

 this method is run from the main thread like this:

 gm_tcp_server-server_thread =
 g_thread_create((GThreadFunc)tcp_server_run,gm_tcp_server,TRUE,/*error*/NULL);

 and this is how i create the server socket:

 int tcp_server_listerner_new(const Configurations *p_configurations,GError
 **p_error)
 {
 int fd;
 struct sockaddr_in servAddr;
 int set_option = 1;

 // create the socket
 fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
 if(fd  0)
 {
 *p_error =
 g_error_new(G_IO_CHANNEL_ERROR,G_IO_CHANNEL_ERROR_FAILED,Could not create
 server socket: %s,strerror(errno));
 return -1;
 }
 // set some options on the socket
 fcntl(fd,F_SETFL,O_NONBLOCK);// set the socket non-blocking

 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,set_option,sizeof(set_option));//
 reuse address

 setsockopt(fd,SOL_SOCKET,SO_KEEPALIVE,set_option,sizeof(set_option));//
 send keep alive messages

 // construct local address structure
 memset(servAddr,0x00, sizeof(servAddr));
 servAddr.sin_family = AF_INET;
 servAddr.sin_addr.s_addr =
 inet_addr(p_configurations-tcp_server.interface);
 servAddr.sin_port = htons(p_configurations-tcp_server.port);
 // Bind to the local address
 if(bind(fd,(struct sockaddr*)servAddr,sizeof(servAddr))  0)
 {
 *p_error =
 g_error_new(G_IO_CHANNEL_ERROR,G_IO_CHANNEL_ERROR_FAILED,Could not bind
 server socket: %s,strerror(errno));
 // close the socket
 close(fd);
 return -1;
 }
 if(listen(fd,10)  0)
 {
 *p_error =
 g_error_new(G_IO_CHANNEL_ERROR,G_IO_CHANNEL_ERROR_FAILED,Could not start
 listening on the server socket: %s,strerror(errno));
 // close the socket
 close(fd);
 return -1;
 }

 return fd;
 }


 thanks !


 --
 Date: Fri, 22 Oct 2010 00:06:08 -0200
 Subject: Re: poll with timeout 0 in main loop
 From: maginot.jun...@gmail.com
 To: jpablolorenze...@hotmail.com
 CC: gtk-app-devel-list@gnome.org



 On Thu, Oct 21, 2010 at 11:27 PM, Juan Pablo L. 
 jpablolorenze...@hotmail.com wrote:


 Hi, i have a problem with an application i m building with glib-2.24.2 on
 linux, i create a server socket which i use to create a GIOChannel and add
 it to a main loop, everything was fine until i found that the application
 consumes 99.8% of the CPU while waiting for connections (doing nothing else
 just sitting there waiting for connections) and it gets even worse as new
 connections come in because i create a new loop in each connection (i create
 a thread for each connection) to deal with the incoming packages and each
 connection has N handlers to process the requests, each handler has its main
 loop for asynchronous communication between them and the connection thread
 that owns the handlers, so i did a strace and found out that the time out
 being passed to poll is 0 so for each iteration the poll returns inmediatly
 therefore i m stuck with a busy wait in all main loops, so i have read the
 documentation trying to find out how to modify this bahaviour but i could
 find nothing ab
  out it, can you please tell me why this is happenning ?? thanks!

 ___
 gtk-app-devel-list mailing list
 gtk-app-devel-list@gnome.org
 http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



 Can you post an snipt of your code?

 And if you started a Thread and a loop inside it, it will stay interacting
 if you don't stop it, this will consume CPU for sure. I think you must work
 with signals or interrupts