2 * Copyright (c) - 2013 Julien Desfossez <jdesfossez@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
37 #include <lttng/lttng.h>
39 #include <urcu/list.h>
40 #include <bin/lttng-sessiond/session.h>
41 #include <common/common.h>
43 #include <bin/lttng-relayd/lttng-viewer-abi.h>
44 #include <common/index/ctf-index.h>
46 #include <common/compat/endian.h>
48 #define SESSION1 "test1"
49 #define RELAYD_URL "net://localhost"
50 #define LIVE_TIMER 2000000
52 /* Number of TAP tests in this file */
54 #define mmap_size 524288
56 int ust_consumerd32_fd
;
57 int ust_consumerd64_fd
;
59 static int control_sock
;
60 struct live_session
*session
;
62 static int first_packet_offset
;
63 static int first_packet_len
;
64 static int first_packet_stream_id
;
66 struct viewer_stream
{
68 uint64_t ctf_trace_id
;
77 struct viewer_stream
*streams
;
78 uint64_t live_timer_interval
;
79 uint64_t stream_count
;
83 int connect_viewer(char *hostname
)
86 struct sockaddr_in server_addr
;
89 host
= gethostbyname(hostname
);
95 if ((control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
101 server_addr
.sin_family
= AF_INET
;
102 server_addr
.sin_port
= htons(5344);
103 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
104 bzero(&(server_addr
.sin_zero
), 8);
106 if (connect(control_sock
, (struct sockaddr
*) &server_addr
,
107 sizeof(struct sockaddr
)) == -1) {
113 server_addr
.sin_family
= AF_INET
;
114 server_addr
.sin_port
= htons(5345);
115 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
116 bzero(&(server_addr
.sin_zero
), 8);
124 int establish_connection(void)
126 struct lttng_viewer_cmd cmd
;
127 struct lttng_viewer_connect connect
;
130 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
131 cmd
.data_size
= sizeof(connect
);
134 memset(&connect
, 0, sizeof(connect
));
135 connect
.major
= htobe32(VERSION_MAJOR
);
136 connect
.minor
= htobe32(VERSION_MINOR
);
137 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
140 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
141 } while (ret
< 0 && errno
== EINTR
);
143 fprintf(stderr
, "Error sending cmd\n");
147 ret
= send(control_sock
, &connect
, sizeof(connect
), 0);
148 } while (ret
< 0 && errno
== EINTR
);
150 fprintf(stderr
, "Error sending version\n");
155 ret
= recv(control_sock
, &connect
, sizeof(connect
), 0);
156 } while (ret
< 0 && errno
== EINTR
);
158 fprintf(stderr
, "Error receiving version\n");
168 * Returns the number of sessions, should be 1 during the unit test.
170 int list_sessions(int *session_id
)
172 struct lttng_viewer_cmd cmd
;
173 struct lttng_viewer_list_sessions list
;
174 struct lttng_viewer_session lsession
;
176 int first_session
= 0;
178 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
183 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
184 } while (ret
< 0 && errno
== EINTR
);
186 fprintf(stderr
, "Error sending cmd\n");
191 ret
= recv(control_sock
, &list
, sizeof(list
), 0);
192 } while (ret
< 0 && errno
== EINTR
);
194 fprintf(stderr
, "Error receiving session list\n");
198 for (i
= 0; i
< be32toh(list
.sessions_count
); i
++) {
200 ret
= recv(control_sock
, &lsession
, sizeof(lsession
), 0);
201 } while (ret
< 0 && errno
== EINTR
);
203 fprintf(stderr
, "Error receiving session\n");
206 if (lsession
.streams
> 0 && first_session
<= 0) {
207 first_session
= be64toh(lsession
.id
);
208 *session_id
= first_session
;
212 ret
= be32toh(list
.sessions_count
);
218 int create_viewer_session()
220 struct lttng_viewer_cmd cmd
;
221 struct lttng_viewer_create_session_response resp
;
225 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
230 ret_len
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
231 } while (ret_len
< 0 && errno
== EINTR
);
233 fprintf(stderr
, "[error] Error sending cmd\n");
237 assert(ret_len
== sizeof(cmd
));
240 ret_len
= recv(control_sock
, &resp
, sizeof(resp
), 0);
241 } while (ret_len
< 0 && errno
== EINTR
);
243 fprintf(stderr
, "[error] Error receiving create session reply\n");
247 assert(ret_len
== sizeof(resp
));
249 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
250 fprintf(stderr
, "[error] Error creating viewer session\n");
260 int attach_session(int id
)
262 struct lttng_viewer_cmd cmd
;
263 struct lttng_viewer_attach_session_request rq
;
264 struct lttng_viewer_attach_session_response rp
;
265 struct lttng_viewer_stream stream
;
268 session
= zmalloc(sizeof(struct live_session
));
274 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
275 cmd
.data_size
= sizeof(rq
);
278 memset(&rq
, 0, sizeof(rq
));
279 rq
.session_id
= htobe64(id
);
280 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_BEGINNING
);
283 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
284 } while (ret
< 0 && errno
== EINTR
);
286 fprintf(stderr
, "Error sending cmd\n");
290 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
291 } while (ret
< 0 && errno
== EINTR
);
293 fprintf(stderr
, "Error sending attach request\n");
298 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
299 } while (ret
< 0 && errno
== EINTR
);
301 fprintf(stderr
, "Error receiving attach response\n");
304 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
309 session
->stream_count
= be32toh(rp
.streams_count
);
310 session
->streams
= zmalloc(session
->stream_count
*
311 sizeof(struct viewer_stream
));
312 if (!session
->streams
) {
317 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
319 ret
= recv(control_sock
, &stream
, sizeof(stream
), 0);
320 } while (ret
< 0 && errno
== EINTR
);
322 fprintf(stderr
, "Error receiving stream\n");
325 session
->streams
[i
].id
= be64toh(stream
.id
);
327 session
->streams
[i
].ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
328 session
->streams
[i
].first_read
= 1;
329 session
->streams
[i
].mmap_base
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
330 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
331 if (session
->streams
[i
].mmap_base
== MAP_FAILED
) {
332 fprintf(stderr
, "mmap error\n");
337 if (be32toh(stream
.metadata_flag
)) {
338 session
->streams
[i
].metadata_flag
= 1;
341 ret
= session
->stream_count
;
348 int get_metadata(void)
350 struct lttng_viewer_cmd cmd
;
351 struct lttng_viewer_get_metadata rq
;
352 struct lttng_viewer_metadata_packet rp
;
357 int metadata_stream_id
= -1;
359 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
360 cmd
.data_size
= sizeof(rq
);
363 for (i
= 0; i
< session
->stream_count
; i
++) {
364 if (session
->streams
[i
].metadata_flag
) {
365 metadata_stream_id
= i
;
370 if (metadata_stream_id
< 0) {
371 fprintf(stderr
, "No metadata stream found\n");
376 rq
.stream_id
= htobe64(session
->streams
[metadata_stream_id
].id
);
379 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
380 } while (ret
< 0 && errno
== EINTR
);
382 fprintf(stderr
, "Error sending cmd\n");
386 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
387 } while (ret
< 0 && errno
== EINTR
);
389 fprintf(stderr
, "Error sending get_metadata request\n");
393 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
394 } while (ret
< 0 && errno
== EINTR
);
396 fprintf(stderr
, "Error receiving metadata response\n");
399 switch (be32toh(rp
.status
)) {
400 case LTTNG_VIEWER_METADATA_OK
:
402 case LTTNG_VIEWER_NO_NEW_METADATA
:
403 fprintf(stderr
, "NO NEW\n");
406 case LTTNG_VIEWER_METADATA_ERR
:
407 fprintf(stderr
, "ERR\n");
411 fprintf(stderr
, "UNKNOWN\n");
416 len
= be64toh(rp
.len
);
423 perror("relay data zmalloc");
427 ret
= recv(control_sock
, data
, len
, MSG_WAITALL
);
428 } while (ret
< 0 && errno
== EINTR
);
430 fprintf(stderr
, "Error receiving trace packet\n");
442 int get_next_index(void)
444 struct lttng_viewer_cmd cmd
;
445 struct lttng_viewer_get_next_index rq
;
446 struct lttng_viewer_index rp
;
450 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
451 cmd
.data_size
= sizeof(rq
);
454 for (id
= 0; id
< session
->stream_count
; id
++) {
455 if (session
->streams
[id
].metadata_flag
) {
458 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
462 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
463 } while (ret
< 0 && errno
== EINTR
);
465 fprintf(stderr
, "Error sending cmd\n");
469 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
470 } while (ret
< 0 && errno
== EINTR
);
472 fprintf(stderr
, "Error sending get_next_index request\n");
476 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
477 } while (ret
< 0 && errno
== EINTR
);
479 fprintf(stderr
, "Error receiving index response\n");
483 rp
.flags
= be32toh(rp
.flags
);
485 switch (be32toh(rp
.status
)) {
486 case LTTNG_VIEWER_INDEX_INACTIVE
:
487 fprintf(stderr
, "(INACTIVE)\n");
489 case LTTNG_VIEWER_INDEX_OK
:
491 case LTTNG_VIEWER_INDEX_RETRY
:
494 case LTTNG_VIEWER_INDEX_HUP
:
495 fprintf(stderr
, "(HUP)\n");
496 session
->streams
[id
].id
= -1ULL;
497 session
->streams
[id
].fd
= -1;
499 case LTTNG_VIEWER_INDEX_ERR
:
500 fprintf(stderr
, "(ERR)\n");
504 fprintf(stderr
, "SHOULD NOT HAPPEN\n");
508 if (!first_packet_stream_id
) {
509 first_packet_offset
= be64toh(rp
.offset
);
510 first_packet_len
= be64toh(rp
.packet_size
) / CHAR_BIT
;
511 first_packet_stream_id
= id
;
521 int get_data_packet(int id
, uint64_t offset
,
524 struct lttng_viewer_cmd cmd
;
525 struct lttng_viewer_get_packet rq
;
526 struct lttng_viewer_trace_packet rp
;
529 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
530 cmd
.data_size
= sizeof(rq
);
533 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
534 /* Already in big endian. */
536 rq
.len
= htobe32(len
);
539 ret
= send(control_sock
, &cmd
, sizeof(cmd
), 0);
540 } while (ret
< 0 && errno
== EINTR
);
542 fprintf(stderr
, "Error sending cmd\n");
546 ret
= send(control_sock
, &rq
, sizeof(rq
), 0);
547 } while (ret
< 0 && errno
== EINTR
);
549 fprintf(stderr
, "Error sending get_data_packet request\n");
553 ret
= recv(control_sock
, &rp
, sizeof(rp
), 0);
554 } while (ret
< 0 && errno
== EINTR
);
556 fprintf(stderr
, "Error receiving data response\n");
559 rp
.flags
= be32toh(rp
.flags
);
561 switch (be32toh(rp
.status
)) {
562 case LTTNG_VIEWER_GET_PACKET_OK
:
564 case LTTNG_VIEWER_GET_PACKET_RETRY
:
565 fprintf(stderr
, "RETRY\n");
568 case LTTNG_VIEWER_GET_PACKET_ERR
:
569 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
570 fprintf(stderr
, "NEW_METADATA\n");
574 fprintf(stderr
, "ERR\n");
578 fprintf(stderr
, "UNKNOWN\n");
583 len
= be32toh(rp
.len
);
588 if (len
> mmap_size
) {
589 fprintf(stderr
, "mmap_size not big enough\n");
595 ret
= recv(control_sock
, session
->streams
[id
].mmap_base
, len
, MSG_WAITALL
);
596 } while (ret
< 0 && errno
== EINTR
);
598 fprintf(stderr
, "Error receiving trace packet\n");
608 int main(int argc
, char **argv
)
613 plan_tests(NUM_TESTS
);
615 diag("Live unit tests");
617 ret
= connect_viewer("localhost");
618 ok(ret
== 0, "Connect viewer to relayd");
620 ret
= establish_connection();
621 ok(ret
== 0, "Established connection and version check with %d.%d",
622 VERSION_MAJOR
, VERSION_MINOR
);
624 ret
= list_sessions(&session_id
);
625 ok(ret
> 0, "List sessions : %d session(s)", ret
);
627 ret
= create_viewer_session();
628 ok(ret
== 0, "Create viewer session");
630 ret
= attach_session(session_id
);
631 ok(ret
> 0, "Attach to session, %d streams received", ret
);
633 ret
= get_metadata();
634 ok(ret
> 0, "Get metadata, received %d bytes", ret
);
636 ret
= get_next_index();
637 ok(ret
== 0, "Get one index per stream");
639 ret
= get_data_packet(first_packet_stream_id
, first_packet_offset
,
641 ok(ret
== first_packet_len
,
642 "Get one data packet for stream %d, offset %d, len %d",
643 first_packet_stream_id
, first_packet_offset
,
646 return exit_status();