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
24 #include <sys/types.h>
28 #include <lttng/health-internal.h>
30 #include <bin/lttng-sessiond/health-sessiond.h>
31 #include <bin/lttng-consumerd/health-consumerd.h>
32 #include <bin/lttng-relayd/health-relayd.h>
33 #include <common/defaults.h>
34 #include <common/utils.h>
36 #include "lttng-ctl-helper.h"
38 enum health_component
{
39 HEALTH_COMPONENT_SESSIOND
,
40 HEALTH_COMPONENT_CONSUMERD
,
41 HEALTH_COMPONENT_RELAYD
,
46 struct lttng_health_thread
{
47 struct lttng_health
*p
;
52 enum health_component component
;
54 unsigned int nr_threads
;
55 char health_sock_path
[PATH_MAX
];
56 /* For consumer health only */
57 enum lttng_health_consumerd consumerd_type
;
58 struct lttng_health_thread thread
[];
62 const char *sessiond_thread_name
[NR_HEALTH_SESSIOND_TYPES
] = {
63 [ HEALTH_SESSIOND_TYPE_CMD
] = "Session daemon command",
64 [ HEALTH_SESSIOND_TYPE_APP_MANAGE
] = "Session daemon application manager",
65 [ HEALTH_SESSIOND_TYPE_APP_REG
] = "Session daemon application registration",
66 [ HEALTH_SESSIOND_TYPE_KERNEL
] = "Session daemon kernel",
67 [ HEALTH_SESSIOND_TYPE_CONSUMER
] = "Session daemon consumer manager",
68 [ HEALTH_SESSIOND_TYPE_HT_CLEANUP
] = "Session daemon hash table cleanup",
69 [ HEALTH_SESSIOND_TYPE_APP_MANAGE_NOTIFY
] = "Session daemon application notification manager",
70 [ HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
] = "Session daemon application registration dispatcher",
74 const char *consumerd_thread_name
[NR_HEALTH_CONSUMERD_TYPES
] = {
75 [ HEALTH_CONSUMERD_TYPE_CHANNEL
] = "Consumer daemon channel",
76 [ HEALTH_CONSUMERD_TYPE_METADATA
] = "Consumer daemon metadata",
77 [ HEALTH_CONSUMERD_TYPE_DATA
] = "Consumer daemon data",
78 [ HEALTH_CONSUMERD_TYPE_SESSIOND
] = "Consumer daemon session daemon command manager",
79 [ HEALTH_CONSUMERD_TYPE_METADATA_TIMER
] = "Consumer daemon metadata timer",
83 const char *relayd_thread_name
[NR_HEALTH_RELAYD_TYPES
] = {
84 [ HEALTH_RELAYD_TYPE_DISPATCHER
] = "Relay daemon dispatcher",
85 [ HEALTH_RELAYD_TYPE_WORKER
] = "Relay daemon worker",
86 [ HEALTH_RELAYD_TYPE_LISTENER
] = "Relay daemon listener",
87 [ HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
] = "Relay daemon live dispatcher",
88 [ HEALTH_RELAYD_TYPE_LIVE_WORKER
] = "Relay daemon live worker",
89 [ HEALTH_RELAYD_TYPE_LIVE_LISTENER
] = "Relay daemon live listener",
93 const char **thread_name
[NR_HEALTH_COMPONENT
] = {
94 [ HEALTH_COMPONENT_SESSIOND
] = sessiond_thread_name
,
95 [ HEALTH_COMPONENT_CONSUMERD
] = consumerd_thread_name
,
96 [ HEALTH_COMPONENT_RELAYD
] = relayd_thread_name
,
100 * Set health socket path.
102 * Returns 0 on success or -ENOMEM.
105 int set_health_socket_path(struct lttng_health
*lh
,
111 /* Global and home format strings */
112 const char *global_str
, *home_str
;
114 switch (lh
->component
) {
115 case HEALTH_COMPONENT_SESSIOND
:
116 global_str
= DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
;
117 home_str
= DEFAULT_HOME_HEALTH_UNIX_SOCK
;
119 case HEALTH_COMPONENT_CONSUMERD
:
120 switch (lh
->consumerd_type
) {
121 case LTTNG_HEALTH_CONSUMERD_UST_32
:
122 global_str
= DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK
;
123 home_str
= DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
;
125 case LTTNG_HEALTH_CONSUMERD_UST_64
:
126 global_str
= DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
;
127 home_str
= DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
;
129 case LTTNG_HEALTH_CONSUMERD_KERNEL
:
130 global_str
= DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
;
131 home_str
= DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
;
137 case HEALTH_COMPONENT_RELAYD
:
138 if (lh
->health_sock_path
[0] == '\0') {
143 break; /* Unreached */
150 if (uid
== 0 || tracing_group
) {
151 lttng_ctl_copy_string(lh
->health_sock_path
,
153 sizeof(lh
->health_sock_path
));
158 * With GNU C < 2.1, snprintf returns -1 if the target buffer
159 * is too small; With GNU C >= 2.1, snprintf returns the
160 * required size (excluding closing null).
162 home
= utils_get_home_dir();
164 /* Fallback in /tmp */
168 ret
= snprintf(lh
->health_sock_path
, sizeof(lh
->health_sock_path
),
170 if ((ret
< 0) || (ret
>= sizeof(lh
->health_sock_path
))) {
178 struct lttng_health
*lttng_health_create(enum health_component hc
,
179 unsigned int nr_threads
)
181 struct lttng_health
*lh
;
184 lh
= zmalloc(sizeof(*lh
) + sizeof(lh
->thread
[0]) * nr_threads
);
190 lh
->state
= UINT64_MAX
; /* All bits in error initially */
191 lh
->nr_threads
= nr_threads
;
192 for (i
= 0; i
< nr_threads
; i
++) {
193 lh
->thread
[i
].p
= lh
;
198 struct lttng_health
*lttng_health_create_sessiond(void)
200 struct lttng_health
*lh
;
202 lh
= lttng_health_create(HEALTH_COMPONENT_SESSIOND
,
203 NR_HEALTH_SESSIOND_TYPES
);
210 struct lttng_health
*
211 lttng_health_create_consumerd(enum lttng_health_consumerd consumerd
)
213 struct lttng_health
*lh
;
215 lh
= lttng_health_create(HEALTH_COMPONENT_CONSUMERD
,
216 NR_HEALTH_CONSUMERD_TYPES
);
220 lh
->consumerd_type
= consumerd
;
224 struct lttng_health
*lttng_health_create_relayd(const char *path
)
226 struct lttng_health
*lh
;
232 lh
= lttng_health_create(HEALTH_COMPONENT_RELAYD
,
233 NR_HEALTH_RELAYD_TYPES
);
237 lttng_ctl_copy_string(lh
->health_sock_path
, path
,
238 sizeof(lh
->health_sock_path
));
242 void lttng_health_destroy(struct lttng_health
*lh
)
247 int lttng_health_query(struct lttng_health
*health
)
249 int sock
, ret
, i
, tracing_group
;
250 struct health_comm_msg msg
;
251 struct health_comm_reply reply
;
257 tracing_group
= lttng_check_tracing_group();
259 ret
= set_health_socket_path(health
, tracing_group
);
263 /* Connect to the sesssion daemon */
264 sock
= lttcomm_connect_unix_sock(health
->health_sock_path
);
267 /* For tracing group, fallback to per-user */
275 msg
.cmd
= HEALTH_CMD_CHECK
;
277 ret
= lttcomm_send_unix_sock(sock
, (void *)&msg
, sizeof(msg
));
283 ret
= lttcomm_recv_unix_sock(sock
, (void *)&reply
, sizeof(reply
));
289 health
->state
= reply
.ret_code
;
290 for (i
= 0; i
< health
->nr_threads
; i
++) {
291 if (health
->state
& (1ULL << i
)) {
292 health
->thread
[i
].state
= -1;
294 health
->thread
[i
].state
= 0;
302 closeret
= close(sock
);
312 int lttng_health_state(const struct lttng_health
*health
)
318 if (health
->state
== 0) {
325 int lttng_health_get_nr_threads(const struct lttng_health
*health
)
330 return health
->nr_threads
;
333 const struct lttng_health_thread
*
334 lttng_health_get_thread(const struct lttng_health
*health
,
335 unsigned int nth_thread
)
337 if (!health
|| nth_thread
>= health
->nr_threads
) {
340 return &health
->thread
[nth_thread
];
343 int lttng_health_thread_state(const struct lttng_health_thread
*thread
)
348 return thread
->state
;
351 const char *lttng_health_thread_name(const struct lttng_health_thread
*thread
)
358 nr
= thread
- &thread
->p
->thread
[0];
359 return thread_name
[thread
->p
->component
][nr
];