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 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License, version 2.1 only,
11 * as published by the Free Software Foundation.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
30 #include <lttng/health-internal.h>
32 #include <bin/lttng-sessiond/health-sessiond.h>
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <bin/lttng-relayd/health-relayd.h>
35 #include <common/defaults.h>
36 #include <common/utils.h>
38 #include "lttng-ctl-helper.h"
40 enum health_component
{
41 HEALTH_COMPONENT_SESSIOND
,
42 HEALTH_COMPONENT_CONSUMERD
,
43 HEALTH_COMPONENT_RELAYD
,
48 struct lttng_health_thread
{
49 struct lttng_health
*p
;
54 enum health_component component
;
56 unsigned int nr_threads
;
57 char health_sock_path
[PATH_MAX
];
58 /* For consumer health only */
59 enum lttng_health_consumerd consumerd_type
;
60 struct lttng_health_thread thread
[];
64 const char *sessiond_thread_name
[NR_HEALTH_SESSIOND_TYPES
] = {
65 [ HEALTH_SESSIOND_TYPE_CMD
] = "Session daemon command",
66 [ HEALTH_SESSIOND_TYPE_APP_MANAGE
] = "Session daemon application manager",
67 [ HEALTH_SESSIOND_TYPE_APP_REG
] = "Session daemon application registration",
68 [ HEALTH_SESSIOND_TYPE_KERNEL
] = "Session daemon kernel",
69 [ HEALTH_SESSIOND_TYPE_CONSUMER
] = "Session daemon consumer manager",
70 [ HEALTH_SESSIOND_TYPE_HT_CLEANUP
] = "Session daemon hash table cleanup",
71 [ HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
] = "Session daemon application notification manager",
72 [ HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
] = "Session daemon application registration dispatcher",
76 const char *consumerd_thread_name
[NR_HEALTH_CONSUMERD_TYPES
] = {
77 [ HEALTH_CONSUMERD_TYPE_CHANNEL
] = "Consumer daemon channel",
78 [ HEALTH_CONSUMERD_TYPE_METADATA
] = "Consumer daemon metadata",
79 [ HEALTH_CONSUMERD_TYPE_DATA
] = "Consumer daemon data",
80 [ HEALTH_CONSUMERD_TYPE_SESSIOND
] = "Consumer daemon session daemon command manager",
81 [ HEALTH_CONSUMERD_TYPE_METADATA_TIMER
] = "Consumer daemon metadata timer",
85 const char *relayd_thread_name
[NR_HEALTH_RELAYD_TYPES
] = {
86 [ HEALTH_RELAYD_TYPE_DISPATCHER
] = "Relay daemon dispatcher",
87 [ HEALTH_RELAYD_TYPE_WORKER
] = "Relay daemon worker",
88 [ HEALTH_RELAYD_TYPE_LISTENER
] = "Relay daemon listener",
89 [ HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
] = "Relay daemon live dispatcher",
90 [ HEALTH_RELAYD_TYPE_LIVE_WORKER
] = "Relay daemon live worker",
91 [ HEALTH_RELAYD_TYPE_LIVE_LISTENER
] = "Relay daemon live listener",
95 const char **thread_name
[NR_HEALTH_COMPONENT
] = {
96 [ HEALTH_COMPONENT_SESSIOND
] = sessiond_thread_name
,
97 [ HEALTH_COMPONENT_CONSUMERD
] = consumerd_thread_name
,
98 [ HEALTH_COMPONENT_RELAYD
] = relayd_thread_name
,
102 * Set health socket path.
104 * Returns 0 on success or -ENOMEM.
107 int set_health_socket_path(struct lttng_health
*lh
,
113 /* Global and home format strings */
114 const char *global_str
, *home_str
;
116 switch (lh
->component
) {
117 case HEALTH_COMPONENT_SESSIOND
:
118 global_str
= DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
;
119 home_str
= DEFAULT_HOME_HEALTH_UNIX_SOCK
;
121 case HEALTH_COMPONENT_CONSUMERD
:
122 switch (lh
->consumerd_type
) {
123 case LTTNG_HEALTH_CONSUMERD_UST_32
:
124 global_str
= DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK
;
125 home_str
= DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
;
127 case LTTNG_HEALTH_CONSUMERD_UST_64
:
128 global_str
= DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
;
129 home_str
= DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
;
131 case LTTNG_HEALTH_CONSUMERD_KERNEL
:
132 global_str
= DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
;
133 home_str
= DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
;
139 case HEALTH_COMPONENT_RELAYD
:
140 if (lh
->health_sock_path
[0] == '\0') {
145 break; /* Unreached */
152 if (uid
== 0 || tracing_group
) {
153 lttng_ctl_copy_string(lh
->health_sock_path
,
155 sizeof(lh
->health_sock_path
));
160 * With GNU C < 2.1, snprintf returns -1 if the target buffer
161 * is too small; With GNU C >= 2.1, snprintf returns the
162 * required size (excluding closing null).
164 home
= utils_get_home_dir();
166 /* Fallback in /tmp */
170 ret
= snprintf(lh
->health_sock_path
, sizeof(lh
->health_sock_path
),
172 if ((ret
< 0) || (ret
>= sizeof(lh
->health_sock_path
))) {
180 struct lttng_health
*lttng_health_create(enum health_component hc
,
181 unsigned int nr_threads
)
183 struct lttng_health
*lh
;
186 lh
= zmalloc(sizeof(*lh
) + sizeof(lh
->thread
[0]) * nr_threads
);
192 lh
->state
= UINT64_MAX
; /* All bits in error initially */
193 lh
->nr_threads
= nr_threads
;
194 for (i
= 0; i
< nr_threads
; i
++) {
195 lh
->thread
[i
].p
= lh
;
200 struct lttng_health
*lttng_health_create_sessiond(void)
202 struct lttng_health
*lh
;
204 lh
= lttng_health_create(HEALTH_COMPONENT_SESSIOND
,
205 NR_HEALTH_SESSIOND_TYPES
);
212 struct lttng_health
*
213 lttng_health_create_consumerd(enum lttng_health_consumerd consumerd
)
215 struct lttng_health
*lh
;
217 lh
= lttng_health_create(HEALTH_COMPONENT_CONSUMERD
,
218 NR_HEALTH_CONSUMERD_TYPES
);
222 lh
->consumerd_type
= consumerd
;
226 struct lttng_health
*lttng_health_create_relayd(const char *path
)
228 struct lttng_health
*lh
;
234 lh
= lttng_health_create(HEALTH_COMPONENT_RELAYD
,
235 NR_HEALTH_RELAYD_TYPES
);
239 lttng_ctl_copy_string(lh
->health_sock_path
, path
,
240 sizeof(lh
->health_sock_path
));
244 void lttng_health_destroy(struct lttng_health
*lh
)
249 int lttng_health_query(struct lttng_health
*health
)
251 int sock
, ret
, i
, tracing_group
;
252 struct health_comm_msg msg
;
253 struct health_comm_reply reply
;
259 tracing_group
= lttng_check_tracing_group();
261 ret
= set_health_socket_path(health
, tracing_group
);
265 /* Connect to component */
266 sock
= lttcomm_connect_unix_sock(health
->health_sock_path
);
269 /* For tracing group, fallback to per-user */
277 memset(&msg
, 0, sizeof(msg
));
278 msg
.cmd
= HEALTH_CMD_CHECK
;
280 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
286 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
292 health
->state
= reply
.ret_code
;
293 for (i
= 0; i
< health
->nr_threads
; i
++) {
294 if (health
->state
& (1ULL << i
)) {
295 health
->thread
[i
].state
= -1;
297 health
->thread
[i
].state
= 0;
305 closeret
= close(sock
);
315 int lttng_health_state(const struct lttng_health
*health
)
321 if (health
->state
== 0) {
328 int lttng_health_get_nr_threads(const struct lttng_health
*health
)
333 return health
->nr_threads
;
336 const struct lttng_health_thread
*
337 lttng_health_get_thread(const struct lttng_health
*health
,
338 unsigned int nth_thread
)
340 if (!health
|| nth_thread
>= health
->nr_threads
) {
343 return &health
->thread
[nth_thread
];
346 int lttng_health_thread_state(const struct lttng_health_thread
*thread
)
351 return thread
->state
;
354 const char *lttng_health_thread_name(const struct lttng_health_thread
*thread
)
361 nr
= thread
- &thread
->p
->thread
[0];
362 return thread_name
[thread
->p
->component
][nr
];