@@ -488,6 +488,7 @@ static int process_thread_update(struct flb_pe *ctx, uint64_t ts, flb_sds_t pid,
488488 flb_sds_t tmp = NULL ;
489489 flb_sds_t thread_name = NULL ;
490490 flb_sds_t tid_str = NULL ;
491+ flb_sds_t active_key = NULL ;
491492 uint64_t val ;
492493 const char * pattern = "/[0-9]*" ;
493494 struct mk_list * head ;
@@ -522,9 +523,14 @@ static int process_thread_update(struct flb_pe *ctx, uint64_t ts, flb_sds_t pid,
522523 continue ;
523524 }
524525
525- if (flb_hash_table_add (active_tids , tid_str , strlen (tid_str ), NULL , 0 ) < 0 ) {
526+ active_key = flb_sds_create (name );
527+ flb_sds_cat_safe (& active_key , ":" , 1 );
528+ flb_sds_cat_safe (& active_key , tid_str , strlen (tid_str ));
529+ if (flb_hash_table_add (active_tids , active_key , (int ) flb_sds_len (active_key ), NULL , 0 ) < 0 ) {
526530 * active_index_complete = FLB_FALSE ;
527531 }
532+ flb_sds_destroy (active_key );
533+ active_key = NULL ;
528534
529535 if (check_path_for_proc (ctx , thread -> str , "stat" ) != 0 ) {
530536 continue ;
@@ -946,26 +952,35 @@ static char *get_metric_label_value(struct cmt_metric *metric, int index)
946952 return NULL ;
947953}
948954
949- static void purge_stale_metrics (struct cmt_map * map , int label_index ,
955+ static void purge_stale_metrics (struct cmt_map * map ,
956+ int name_label_index ,
957+ int id_label_index ,
950958 struct flb_hash_table * active_ids )
951959{
960+ char * name_val ;
952961 char * id_val ;
953962 void * out_buf ;
954963 size_t out_size ;
964+ flb_sds_t key ;
955965 struct cfl_list * tmp ;
956966 struct cfl_list * head ;
957967 struct cmt_metric * metric ;
958968
959969 cfl_list_foreach_safe (head , tmp , & map -> metrics ) {
960970 metric = cfl_list_entry (head , struct cmt_metric , _head );
961- id_val = get_metric_label_value (metric , label_index );
962- if (!id_val ) {
971+ name_val = get_metric_label_value (metric , name_label_index );
972+ id_val = get_metric_label_value (metric , id_label_index );
973+ if (!name_val || !id_val ) {
963974 continue ;
964975 }
965- if (flb_hash_table_get (active_ids , id_val , strlen (id_val ),
976+ key = flb_sds_create (name_val );
977+ flb_sds_cat_safe (& key , ":" , 1 );
978+ flb_sds_cat_safe (& key , id_val , strlen (id_val ));
979+ if (flb_hash_table_get (active_ids , key , (int ) flb_sds_len (key ),
966980 & out_buf , & out_size ) < 0 ) {
967981 cmt_map_metric_destroy (metric );
968982 }
983+ flb_sds_destroy (key );
969984 }
970985}
971986
@@ -994,6 +1009,7 @@ static int process_update(struct flb_pe *ctx)
9941009 struct flb_hash_table * active_pids = NULL ;
9951010 struct flb_hash_table * active_tids = NULL ;
9961011 int active_index_complete = FLB_TRUE ;
1012+ flb_sds_t active_key = NULL ;
9971013
9981014 mk_list_init (& procfs_list );
9991015
@@ -1080,9 +1096,14 @@ static int process_update(struct flb_pe *ctx)
10801096 continue ;
10811097 }
10821098
1083- if (flb_hash_table_add (active_pids , pid_str , strlen (pid_str ), NULL , 0 ) < 0 ) {
1099+ active_key = flb_sds_create (name );
1100+ flb_sds_cat_safe (& active_key , ":" , 1 );
1101+ flb_sds_cat_safe (& active_key , pid_str , strlen (pid_str ));
1102+ if (flb_hash_table_add (active_pids , active_key , (int ) flb_sds_len (active_key ), NULL , 0 ) < 0 ) {
10841103 active_index_complete = FLB_FALSE ;
10851104 }
1105+ flb_sds_destroy (active_key );
1106+ active_key = NULL ;
10861107
10871108 mk_list_init (& split_list );
10881109
@@ -1243,26 +1264,26 @@ static int process_update(struct flb_pe *ctx)
12431264
12441265 if (active_index_complete == FLB_TRUE ) {
12451266 /* Remove metrics for processes that are no longer running */
1246- purge_stale_metrics (ctx -> cpu_seconds -> map , 1 , active_pids );
1247- purge_stale_metrics (ctx -> read_bytes -> map , 1 , active_pids );
1248- purge_stale_metrics (ctx -> write_bytes -> map , 1 , active_pids );
1249- purge_stale_metrics (ctx -> major_page_faults -> map , 1 , active_pids );
1250- purge_stale_metrics (ctx -> minor_page_faults -> map , 1 , active_pids );
1251- purge_stale_metrics (ctx -> context_switches -> map , 1 , active_pids );
1252- purge_stale_metrics (ctx -> memory_bytes -> map , 1 , active_pids );
1253- purge_stale_metrics (ctx -> open_fds -> map , 1 , active_pids );
1254- purge_stale_metrics (ctx -> fd_ratio -> map , 1 , active_pids );
1255- purge_stale_metrics (ctx -> start_time -> map , 1 , active_pids );
1256- purge_stale_metrics (ctx -> num_threads -> map , 1 , active_pids );
1257- purge_stale_metrics (ctx -> states -> map , 1 , active_pids );
1258- purge_stale_metrics (ctx -> thread_wchan -> map , 1 , active_pids );
1267+ purge_stale_metrics (ctx -> cpu_seconds -> map , 0 , 1 , active_pids );
1268+ purge_stale_metrics (ctx -> read_bytes -> map , 0 , 1 , active_pids );
1269+ purge_stale_metrics (ctx -> write_bytes -> map , 0 , 1 , active_pids );
1270+ purge_stale_metrics (ctx -> major_page_faults -> map , 0 , 1 , active_pids );
1271+ purge_stale_metrics (ctx -> minor_page_faults -> map , 0 , 1 , active_pids );
1272+ purge_stale_metrics (ctx -> context_switches -> map , 0 , 1 , active_pids );
1273+ purge_stale_metrics (ctx -> memory_bytes -> map , 0 , 1 , active_pids );
1274+ purge_stale_metrics (ctx -> open_fds -> map , 0 , 1 , active_pids );
1275+ purge_stale_metrics (ctx -> fd_ratio -> map , 0 , 1 , active_pids );
1276+ purge_stale_metrics (ctx -> start_time -> map , 0 , 1 , active_pids );
1277+ purge_stale_metrics (ctx -> num_threads -> map , 0 , 1 , active_pids );
1278+ purge_stale_metrics (ctx -> states -> map , 0 , 1 , active_pids );
1279+ purge_stale_metrics (ctx -> thread_wchan -> map , 0 , 1 , active_pids );
12591280
12601281 /* Remove metrics for threads that are no longer running */
1261- purge_stale_metrics (ctx -> thread_cpu_seconds -> map , 2 , active_tids );
1262- purge_stale_metrics (ctx -> thread_io_bytes -> map , 2 , active_tids );
1263- purge_stale_metrics (ctx -> thread_major_page_faults -> map , 2 , active_tids );
1264- purge_stale_metrics (ctx -> thread_minor_page_faults -> map , 2 , active_tids );
1265- purge_stale_metrics (ctx -> thread_context_switches -> map , 2 , active_tids );
1282+ purge_stale_metrics (ctx -> thread_cpu_seconds -> map , 0 , 2 , active_tids );
1283+ purge_stale_metrics (ctx -> thread_io_bytes -> map , 0 , 2 , active_tids );
1284+ purge_stale_metrics (ctx -> thread_major_page_faults -> map , 0 , 2 , active_tids );
1285+ purge_stale_metrics (ctx -> thread_minor_page_faults -> map , 0 , 2 , active_tids );
1286+ purge_stale_metrics (ctx -> thread_context_switches -> map , 0 , 2 , active_tids );
12661287 } else {
12671288 flb_plg_warn (ctx -> ins ,
12681289 "skipping stale metric purge because active ID tracking is incomplete" );
0 commit comments