4 * Linux Trace Toolkit Health Control Library
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
15 #include <sys/types.h>
20 #include <lttng/health-internal.h>
22 #include <bin/lttng-sessiond/health-sessiond.h>
23 #include <bin/lttng-consumerd/health-consumerd.h>
24 #include <bin/lttng-relayd/health-relayd.h>
25 #include <common/defaults.h>
26 #include <common/utils.h>
28 #include "lttng-ctl-helper.h"
30 enum health_component
{
31 HEALTH_COMPONENT_SESSIOND
,
32 HEALTH_COMPONENT_CONSUMERD
,
33 HEALTH_COMPONENT_RELAYD
,
38 struct lttng_health_thread
{
39 struct lttng_health
*p
;
44 enum health_component component
;
46 unsigned int nr_threads
;
47 char health_sock_path
[PATH_MAX
];
48 /* For consumer health only */
49 enum lttng_health_consumerd consumerd_type
;
50 struct lttng_health_thread thread
[];
54 const char *sessiond_thread_name
[NR_HEALTH_SESSIOND_TYPES
] = {
55 [ HEALTH_SESSIOND_TYPE_CMD
] = "Session daemon command",
56 [ HEALTH_SESSIOND_TYPE_APP_MANAGE
] = "Session daemon application manager",
57 [ HEALTH_SESSIOND_TYPE_APP_REG
] = "Session daemon application registration",
58 [ HEALTH_SESSIOND_TYPE_KERNEL
] = "Session daemon kernel",
59 [ HEALTH_SESSIOND_TYPE_CONSUMER
] = "Session daemon consumer manager",
60 [ HEALTH_SESSIOND_TYPE_HT_CLEANUP
] = "Session daemon hash table cleanup",
61 [ HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
] = "Session daemon application notification manager",
62 [ HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
] = "Session daemon application registration dispatcher",
63 [ HEALTH_SESSIOND_TYPE_ROTATION
] = "Session daemon rotation manager",
64 [ HEALTH_SESSIOND_TYPE_TIMER
] = "Session daemon timer manager",
65 [ HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR
] = "Session daemon trigger action executor",
69 const char *consumerd_thread_name
[NR_HEALTH_CONSUMERD_TYPES
] = {
70 [ HEALTH_CONSUMERD_TYPE_CHANNEL
] = "Consumer daemon channel",
71 [ HEALTH_CONSUMERD_TYPE_METADATA
] = "Consumer daemon metadata",
72 [ HEALTH_CONSUMERD_TYPE_DATA
] = "Consumer daemon data",
73 [ HEALTH_CONSUMERD_TYPE_SESSIOND
] = "Consumer daemon session daemon command manager",
74 [ HEALTH_CONSUMERD_TYPE_METADATA_TIMER
] = "Consumer daemon metadata timer",
78 const char *relayd_thread_name
[NR_HEALTH_RELAYD_TYPES
] = {
79 [ HEALTH_RELAYD_TYPE_DISPATCHER
] = "Relay daemon dispatcher",
80 [ HEALTH_RELAYD_TYPE_WORKER
] = "Relay daemon worker",
81 [ HEALTH_RELAYD_TYPE_LISTENER
] = "Relay daemon listener",
82 [ HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
] = "Relay daemon live dispatcher",
83 [ HEALTH_RELAYD_TYPE_LIVE_WORKER
] = "Relay daemon live worker",
84 [ HEALTH_RELAYD_TYPE_LIVE_LISTENER
] = "Relay daemon live listener",
88 const char **thread_name
[NR_HEALTH_COMPONENT
] = {
89 [ HEALTH_COMPONENT_SESSIOND
] = sessiond_thread_name
,
90 [ HEALTH_COMPONENT_CONSUMERD
] = consumerd_thread_name
,
91 [ HEALTH_COMPONENT_RELAYD
] = relayd_thread_name
,
95 * Set health socket path.
97 * Returns 0 on success or -ENOMEM.
100 int set_health_socket_path(struct lttng_health
*lh
,
106 /* Global and home format strings */
107 const char *global_str
, *home_str
;
109 switch (lh
->component
) {
110 case HEALTH_COMPONENT_SESSIOND
:
111 global_str
= DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
;
112 home_str
= DEFAULT_HOME_HEALTH_UNIX_SOCK
;
114 case HEALTH_COMPONENT_CONSUMERD
:
115 switch (lh
->consumerd_type
) {
116 case LTTNG_HEALTH_CONSUMERD_UST_32
:
117 global_str
= DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK
;
118 home_str
= DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
;
120 case LTTNG_HEALTH_CONSUMERD_UST_64
:
121 global_str
= DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
;
122 home_str
= DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
;
124 case LTTNG_HEALTH_CONSUMERD_KERNEL
:
125 global_str
= DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
;
126 home_str
= DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
;
132 case HEALTH_COMPONENT_RELAYD
:
133 if (lh
->health_sock_path
[0] == '\0') {
138 break; /* Unreached */
145 if (uid
== 0 || tracing_group
) {
146 lttng_ctl_copy_string(lh
->health_sock_path
,
148 sizeof(lh
->health_sock_path
));
153 * With GNU C < 2.1, snprintf returns -1 if the target buffer
154 * is too small; With GNU C >= 2.1, snprintf returns the
155 * required size (excluding closing null).
157 home
= utils_get_home_dir();
159 /* Fallback in /tmp */
163 ret
= snprintf(lh
->health_sock_path
, sizeof(lh
->health_sock_path
),
165 if ((ret
< 0) || (ret
>= sizeof(lh
->health_sock_path
))) {
173 struct lttng_health
*lttng_health_create(enum health_component hc
,
174 unsigned int nr_threads
)
176 struct lttng_health
*lh
;
179 lh
= zmalloc(sizeof(*lh
) + sizeof(lh
->thread
[0]) * nr_threads
);
185 lh
->state
= UINT64_MAX
; /* All bits in error initially */
186 lh
->nr_threads
= nr_threads
;
187 for (i
= 0; i
< nr_threads
; i
++) {
188 lh
->thread
[i
].p
= lh
;
193 struct lttng_health
*lttng_health_create_sessiond(void)
195 struct lttng_health
*lh
;
197 lh
= lttng_health_create(HEALTH_COMPONENT_SESSIOND
,
198 NR_HEALTH_SESSIOND_TYPES
);
205 struct lttng_health
*
206 lttng_health_create_consumerd(enum lttng_health_consumerd consumerd
)
208 struct lttng_health
*lh
;
210 lh
= lttng_health_create(HEALTH_COMPONENT_CONSUMERD
,
211 NR_HEALTH_CONSUMERD_TYPES
);
215 lh
->consumerd_type
= consumerd
;
219 struct lttng_health
*lttng_health_create_relayd(const char *path
)
221 struct lttng_health
*lh
;
227 lh
= lttng_health_create(HEALTH_COMPONENT_RELAYD
,
228 NR_HEALTH_RELAYD_TYPES
);
232 lttng_ctl_copy_string(lh
->health_sock_path
, path
,
233 sizeof(lh
->health_sock_path
));
237 void lttng_health_destroy(struct lttng_health
*lh
)
242 int lttng_health_query(struct lttng_health
*health
)
244 int sock
, ret
, i
, tracing_group
;
245 struct health_comm_msg msg
;
246 struct health_comm_reply reply
;
252 tracing_group
= lttng_check_tracing_group();
254 ret
= set_health_socket_path(health
, tracing_group
);
258 /* Connect to component */
259 sock
= lttcomm_connect_unix_sock(health
->health_sock_path
);
262 /* For tracing group, fallback to per-user */
270 memset(&msg
, 0, sizeof(msg
));
271 msg
.cmd
= HEALTH_CMD_CHECK
;
273 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
279 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
285 health
->state
= reply
.ret_code
;
286 for (i
= 0; i
< health
->nr_threads
; i
++) {
287 if (health
->state
& (1ULL << i
)) {
288 health
->thread
[i
].state
= -1;
290 health
->thread
[i
].state
= 0;
298 closeret
= close(sock
);
308 int lttng_health_state(const struct lttng_health
*health
)
314 if (health
->state
== 0) {
321 int lttng_health_get_nr_threads(const struct lttng_health
*health
)
326 return health
->nr_threads
;
329 const struct lttng_health_thread
*
330 lttng_health_get_thread(const struct lttng_health
*health
,
331 unsigned int nth_thread
)
333 if (!health
|| nth_thread
>= health
->nr_threads
) {
336 return &health
->thread
[nth_thread
];
339 int lttng_health_thread_state(const struct lttng_health_thread
*thread
)
344 return thread
->state
;
347 const char *lttng_health_thread_name(const struct lttng_health_thread
*thread
)
354 nr
= thread
- &thread
->p
->thread
[0];
355 return thread_name
[thread
->p
->component
][nr
];