2000-09-11 11:03:10 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-09-13 11:07:17 +04:00
Copyright ( C ) Andrew Tridgell 2000
2010-07-19 23:00:31 +04:00
2000-09-11 11:03:10 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2000-09-11 11:03:10 +04:00
( at your option ) any later version .
2010-07-19 23:00:31 +04:00
2000-09-11 11:03:10 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2010-07-19 23:00:31 +04:00
2000-09-11 11:03:10 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2000-09-11 11:03:10 +04:00
*/
/*
2000-09-13 11:07:17 +04:00
test code for internal messaging
2000-09-11 11:03:10 +04:00
*/
# include "includes.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2000-09-11 11:03:10 +04:00
2000-09-12 10:57:25 +04:00
static int pong_count ;
2006-05-20 06:09:35 +04:00
2000-09-12 10:57:25 +04:00
/****************************************************************************
a useful function for testing the message system
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-15 18:39:18 +04:00
static void pong_message ( struct messaging_context * msg_ctx ,
void * private_data ,
uint32_t msg_type ,
struct server_id pid ,
DATA_BLOB * data )
2000-09-12 10:57:25 +04:00
{
pong_count + + ;
}
2000-09-11 11:03:10 +04:00
int main ( int argc , char * argv [ ] )
{
2009-01-21 09:31:33 +03:00
struct tevent_context * evt_ctx ;
2007-05-15 18:39:18 +04:00
struct messaging_context * msg_ctx ;
2000-09-11 11:03:10 +04:00
pid_t pid ;
2000-09-12 10:57:25 +04:00
int i , n ;
2000-11-17 00:38:24 +03:00
char buf [ 12 ] ;
2009-01-21 09:31:33 +03:00
int ret ;
2014-04-23 02:55:53 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2000-09-11 11:03:10 +04:00
2015-03-21 22:00:06 +03:00
smb_init_locale ( ) ;
2006-05-20 06:04:19 +04:00
2010-10-29 07:19:32 +04:00
setup_logging ( argv [ 0 ] , DEBUG_STDOUT ) ;
2010-07-19 23:00:31 +04:00
2011-07-27 19:03:51 +04:00
lp_load_global ( get_dyn_CONFIGFILE ( ) ) ;
2000-09-11 11:03:10 +04:00
2013-02-18 12:11:19 +04:00
if ( ! ( evt_ctx = samba_tevent_context_init ( NULL ) ) | |
2011-12-12 17:55:54 +04:00
! ( msg_ctx = messaging_init ( NULL , evt_ctx ) ) ) {
2007-05-15 18:39:18 +04:00
fprintf ( stderr , " could not init messaging context \n " ) ;
2014-04-23 02:55:53 +04:00
TALLOC_FREE ( frame ) ;
2007-05-15 18:39:18 +04:00
exit ( 1 ) ;
}
2010-07-19 23:00:31 +04:00
2000-11-17 00:38:24 +03:00
if ( argc ! = 3 ) {
2006-08-01 01:11:03 +04:00
fprintf ( stderr , " %s: Usage - %s pid count \n " , argv [ 0 ] ,
argv [ 0 ] ) ;
2014-04-23 02:55:53 +04:00
TALLOC_FREE ( frame ) ;
2000-11-17 00:38:24 +03:00
exit ( 1 ) ;
}
2000-09-11 11:03:10 +04:00
pid = atoi ( argv [ 1 ] ) ;
2000-09-12 10:57:25 +04:00
n = atoi ( argv [ 2 ] ) ;
2007-05-15 18:39:18 +04:00
messaging_register ( msg_ctx , NULL , MSG_PONG , pong_message ) ;
2000-09-12 10:57:25 +04:00
for ( i = 0 ; i < n ; i + + ) {
2007-05-15 18:39:18 +04:00
messaging_send ( msg_ctx , pid_to_procid ( pid ) , MSG_PING ,
& data_blob_null ) ;
2000-09-12 10:57:25 +04:00
}
2000-09-11 11:03:10 +04:00
2000-09-13 11:07:17 +04:00
while ( pong_count < i ) {
2009-01-21 09:31:33 +03:00
ret = tevent_loop_once ( evt_ctx ) ;
if ( ret ! = 0 ) {
break ;
}
2000-09-12 10:57:25 +04:00
}
2000-09-11 11:03:10 +04:00
2014-04-23 03:07:18 +04:00
/* Ensure all messages get through to ourselves. */
2000-11-17 00:38:24 +03:00
pong_count = 0 ;
2011-05-04 00:10:01 +04:00
strlcpy ( buf , " 1234567890 " , sizeof ( buf ) ) ;
2000-11-17 00:38:24 +03:00
for ( i = 0 ; i < n ; i + + ) {
2010-07-19 23:00:41 +04:00
messaging_send ( msg_ctx , messaging_server_id ( msg_ctx ) , MSG_PING ,
2007-05-15 18:39:18 +04:00
& data_blob_null ) ;
2010-07-19 23:00:41 +04:00
messaging_send_buf ( msg_ctx , messaging_server_id ( msg_ctx ) ,
2015-05-09 06:10:12 +03:00
MSG_PING , ( uint8_t * ) buf , 11 ) ;
2000-11-17 00:38:24 +03:00
}
2014-04-23 03:07:18 +04:00
/*
* We have to loop at least 2 times for
* each message as local ping messages are
* handled by an immediate callback , that
* has to be dispatched , which sends a pong
* message , which also has to be dispatched .
* Above we sent 2 * n messages , which means
* we have to dispatch 4 * n times .
*/
while ( pong_count < n * 2 ) {
2009-01-21 09:31:33 +03:00
ret = tevent_loop_once ( evt_ctx ) ;
if ( ret ! = 0 ) {
break ;
}
2000-11-17 00:38:24 +03:00
}
2014-04-23 03:07:18 +04:00
if ( pong_count ! = 2 * n ) {
fprintf ( stderr , " Message count failed (%d). \n " , pong_count ) ;
2006-05-20 06:09:35 +04:00
}
/* Speed testing */
pong_count = 0 ;
{
struct timeval tv = timeval_current ( ) ;
size_t timelimit = n ;
size_t ping_count = 0 ;
2006-07-12 01:23:44 +04:00
printf ( " Sending pings for %d seconds \n " , ( int ) timelimit ) ;
2006-08-01 01:11:03 +04:00
while ( timeval_elapsed ( & tv ) < timelimit ) {
2007-05-15 18:39:18 +04:00
if ( NT_STATUS_IS_OK ( messaging_send_buf (
msg_ctx , pid_to_procid ( pid ) ,
MSG_PING ,
2015-05-09 06:10:12 +03:00
( uint8_t * ) buf , 11 ) ) )
2007-02-02 01:06:59 +03:00
ping_count + + ;
2007-05-15 18:39:18 +04:00
if ( NT_STATUS_IS_OK ( messaging_send (
msg_ctx , pid_to_procid ( pid ) ,
MSG_PING , & data_blob_null ) ) )
2007-02-02 01:06:59 +03:00
ping_count + + ;
2006-05-20 06:09:35 +04:00
while ( ping_count > pong_count + 20 ) {
2009-01-21 09:31:33 +03:00
ret = tevent_loop_once ( evt_ctx ) ;
if ( ret ! = 0 ) {
break ;
}
2006-05-20 06:09:35 +04:00
}
}
2010-07-19 23:00:31 +04:00
2006-05-20 06:09:35 +04:00
printf ( " waiting for %d remaining replies (done %d) \n " ,
2006-07-12 01:23:44 +04:00
( int ) ( ping_count - pong_count ) , pong_count ) ;
2006-05-20 06:09:35 +04:00
while ( timeval_elapsed ( & tv ) < 30 & & pong_count < ping_count ) {
2009-01-21 09:31:33 +03:00
ret = tevent_loop_once ( evt_ctx ) ;
if ( ret ! = 0 ) {
break ;
}
2006-05-20 06:09:35 +04:00
}
2010-07-19 23:00:31 +04:00
2006-05-20 06:09:35 +04:00
if ( ping_count ! = pong_count ) {
2006-07-12 01:23:44 +04:00
fprintf ( stderr , " ping test failed! received %d, sent "
" %d \n " , pong_count , ( int ) ping_count ) ;
2006-05-20 06:09:35 +04:00
}
2010-07-19 23:00:31 +04:00
2006-05-20 06:09:35 +04:00
printf ( " ping rate of %.0f messages/sec \n " ,
2006-08-01 01:11:03 +04:00
( ping_count + pong_count ) / timeval_elapsed ( & tv ) ) ;
2000-11-17 00:38:24 +03:00
}
2014-04-23 02:55:53 +04:00
TALLOC_FREE ( frame ) ;
2000-09-11 11:03:10 +04:00
return ( 0 ) ;
}