4545#include "modservice.h"
4646
4747struct broker_module {
48+ flux_t * h ; /* ref to broker's internal flux_t handle */
49+
4850 flux_watcher_t * broker_w ;
4951
5052 double lastseen ;
@@ -78,6 +80,7 @@ struct broker_module {
7880
7981 struct flux_msglist * rmmod_requests ;
8082 struct flux_msglist * insmod_requests ;
83+ struct flux_msglist * trace_requests ;
8184 struct flux_msglist * deferred_messages ;
8285
8386 flux_t * h_module_end ; /* module end of interthread_channel */
@@ -332,6 +335,7 @@ module_t *module_create (flux_t *h,
332335 p -> main = mod_main ;
333336 p -> dso = dso ;
334337 p -> rank = rank ;
338+ p -> h = h ;
335339 if (!(p -> conf = flux_conf_copy (flux_get_conf (h ))))
336340 goto cleanup ;
337341 if (!(p -> parent_uuid_str = strdup (parent_uuid )))
@@ -349,7 +353,8 @@ module_t *module_create (flux_t *h,
349353 }
350354 if (!(p -> path = strdup (path ))
351355 || !(p -> rmmod_requests = flux_msglist_create ())
352- || !(p -> insmod_requests = flux_msglist_create ()))
356+ || !(p -> insmod_requests = flux_msglist_create ())
357+ || !(p -> trace_requests = flux_msglist_create ()))
353358 goto nomem ;
354359 if (name ) {
355360 if (!(p -> name = strdup (name )))
@@ -437,9 +442,70 @@ int module_get_status (module_t *p)
437442 return p ? p -> status : 0 ;
438443}
439444
445+ static void message_trace (module_t * p ,
446+ const char * prefix ,
447+ const flux_msg_t * msg )
448+ {
449+ const flux_msg_t * req ;
450+ double now = flux_reactor_now (flux_get_reactor (p -> h ));
451+ int type = 0 ;
452+ char buf [64 ];
453+ const char * topic = NULL ;
454+ int payload_size = 0 ;
455+
456+ (void )flux_msg_get_type (msg , & type );
457+ if (type == FLUX_MSGTYPE_CONTROL ) {
458+ int ctype ;
459+ int cstatus ;
460+ if (flux_control_decode (msg , & ctype , & cstatus ) == 0 )
461+ snprintf (buf ,
462+ sizeof (buf ),
463+ "%s %d" ,
464+ ctype == FLUX_MODSTATE_INIT ? "init" :
465+ ctype == FLUX_MODSTATE_RUNNING ? "running" :
466+ ctype == FLUX_MODSTATE_FINALIZING ? "finalizing" :
467+ ctype == FLUX_MODSTATE_EXITED ? "exited" : "unknown" ,
468+ cstatus );
469+ }
470+ else {
471+ (void )flux_msg_get_topic (msg , & topic );
472+ (void )flux_msg_get_payload (msg , NULL , & payload_size );
473+ if (topic && streq (topic , "module.trace" ))
474+ return ;
475+ }
476+
477+ req = flux_msglist_first (p -> trace_requests );
478+ while (req ) {
479+ struct flux_match match = FLUX_MATCH_ANY ;
480+ if (flux_request_unpack (req ,
481+ NULL ,
482+ "{s:i s:s}" ,
483+ "typemask" , & match .typemask ,
484+ "topic_glob" , & match .topic_glob ) < 0
485+ || !flux_msg_cmp (msg , match ))
486+ goto next ;
487+ if (flux_respond_pack (p -> h ,
488+ req ,
489+ "{s:f s:s s:i s:s s:s s:i}" ,
490+ "timestamp" , now ,
491+ "prefix" , prefix ,
492+ "type" , type ,
493+ "name" , p -> name ,
494+ "topic" , topic ? topic : "NO-TOPIC" ,
495+ "payload_size" , payload_size ) < 0 )
496+ flux_log_error (p -> h , "error responding to module.trace" );
497+ next :
498+ req = flux_msglist_next (p -> trace_requests );
499+ }
500+ }
501+
440502flux_msg_t * module_recvmsg (module_t * p )
441503{
442- return flux_recv (p -> h_broker_end , FLUX_MATCH_ANY , FLUX_O_NONBLOCK );
504+ flux_msg_t * msg ;
505+ msg = flux_recv (p -> h_broker_end , FLUX_MATCH_ANY , FLUX_O_NONBLOCK );
506+ if (msg && flux_msglist_count (p -> trace_requests ) > 0 )
507+ message_trace (p , "tx" , msg );
508+ return msg ;
443509}
444510
445511int module_sendmsg_new (module_t * p , flux_msg_t * * msg )
@@ -468,6 +534,8 @@ int module_sendmsg_new (module_t *p, flux_msg_t **msg)
468534 * msg = NULL ;
469535 return 0 ;
470536 }
537+ if (flux_msglist_count (p -> trace_requests ) > 0 )
538+ message_trace (p , "rx" , * msg );
471539 return flux_send_new (p -> h_broker_end , msg , 0 );
472540}
473541
@@ -528,6 +596,7 @@ void module_destroy (module_t *p)
528596 flux_msglist_destroy (p -> rmmod_requests );
529597 flux_msglist_destroy (p -> insmod_requests );
530598 flux_msglist_destroy (p -> deferred_messages );
599+ flux_msglist_destroy (p -> trace_requests );
531600 subhash_destroy (p -> sub );
532601 free (p );
533602 errno = saved_errno ;
@@ -715,6 +784,18 @@ ssize_t module_get_recv_queue_count (module_t *p)
715784 return count ;
716785}
717786
787+ int module_trace (module_t * p , const flux_msg_t * msg )
788+ {
789+ if (flux_msglist_append (p -> trace_requests , msg ) < 0 )
790+ return -1 ;
791+ return 0 ;
792+ }
793+
794+ void module_trace_disconnect (module_t * p , const flux_msg_t * msg )
795+ {
796+ (void )flux_msglist_disconnect (p -> trace_requests , msg );
797+ }
798+
718799/*
719800 * vi:tabstop=4 shiftwidth=4 expandtab
720801 */
0 commit comments