Fix: live_test regression on large number of cpus
[lttng-tools.git] / tests / regression / tools / live / live_test.c
1 /*
2 * Copyright (c) - 2013 Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <sys/types.h>
27 #include <inttypes.h>
28 #include <stdlib.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <fcntl.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35
36 #include <tap/tap.h>
37 #include <lttng/lttng.h>
38
39 #include <urcu/list.h>
40 #include <bin/lttng-sessiond/session.h>
41 #include <common/common.h>
42
43 #include <bin/lttng-relayd/lttng-viewer-abi.h>
44 #include <common/index/ctf-index.h>
45
46 #include <common/compat/endian.h>
47
48 #define SESSION1 "test1"
49 #define RELAYD_URL "net://localhost"
50 #define LIVE_TIMER 2000000
51
52 /* Number of TAP tests in this file */
53 #define NUM_TESTS 8
54 #define mmap_size 524288
55
56 int ust_consumerd32_fd;
57 int ust_consumerd64_fd;
58
59 static int control_sock;
60 struct live_session *session;
61
62 static int first_packet_offset;
63 static int first_packet_len;
64 static int first_packet_stream_id;
65
66 struct viewer_stream {
67 uint64_t id;
68 uint64_t ctf_trace_id;
69 void *mmap_base;
70 int fd;
71 int metadata_flag;
72 int first_read;
73 char path[PATH_MAX];
74 };
75
76 struct live_session {
77 struct viewer_stream *streams;
78 uint64_t live_timer_interval;
79 uint64_t stream_count;
80 };
81
82 static
83 ssize_t lttng_live_recv(int fd, void *buf, size_t len)
84 {
85 ssize_t ret;
86 size_t copied = 0, to_copy = len;
87
88 do {
89 ret = recv(fd, buf + copied, to_copy, 0);
90 if (ret > 0) {
91 assert(ret <= to_copy);
92 copied += ret;
93 to_copy -= ret;
94 }
95 } while ((ret > 0 && to_copy > 0)
96 || (ret < 0 && errno == EINTR));
97 if (ret > 0)
98 ret = copied;
99 /* ret = 0 means orderly shutdown, ret < 0 is error. */
100 return ret;
101 }
102
103 static
104 ssize_t lttng_live_send(int fd, const void *buf, size_t len)
105 {
106 ssize_t ret;
107
108 do {
109 ret = send(fd, buf, len, MSG_NOSIGNAL);
110 } while (ret < 0 && errno == EINTR);
111 return ret;
112 }
113
114 static
115 int connect_viewer(char *hostname)
116 {
117 struct hostent *host;
118 struct sockaddr_in server_addr;
119 int ret;
120
121 host = gethostbyname(hostname);
122 if (!host) {
123 ret = -1;
124 goto end;
125 }
126
127 if ((control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
128 PERROR("Socket");
129 ret = -1;
130 goto end;
131 }
132
133 server_addr.sin_family = AF_INET;
134 server_addr.sin_port = htons(5344);
135 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
136 bzero(&(server_addr.sin_zero), 8);
137
138 if (connect(control_sock, (struct sockaddr *) &server_addr,
139 sizeof(struct sockaddr)) == -1) {
140 PERROR("Connect");
141 ret = -1;
142 goto end;
143 }
144
145 server_addr.sin_family = AF_INET;
146 server_addr.sin_port = htons(5345);
147 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
148 bzero(&(server_addr.sin_zero), 8);
149
150 ret = 0;
151
152 end:
153 return ret;
154 }
155
156 int establish_connection(void)
157 {
158 struct lttng_viewer_cmd cmd;
159 struct lttng_viewer_connect connect;
160 ssize_t ret_len;
161
162 cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
163 cmd.data_size = sizeof(connect);
164 cmd.cmd_version = 0;
165
166 memset(&connect, 0, sizeof(connect));
167 connect.major = htobe32(VERSION_MAJOR);
168 connect.minor = htobe32(VERSION_MINOR);
169 connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
170
171 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
172 if (ret_len < 0) {
173 fprintf(stderr, "Error sending cmd\n");
174 goto error;
175 }
176 ret_len = lttng_live_send(control_sock, &connect, sizeof(connect));
177 if (ret_len < 0) {
178 fprintf(stderr, "Error sending version\n");
179 goto error;
180 }
181
182 ret_len = lttng_live_recv(control_sock, &connect, sizeof(connect));
183 if (ret_len == 0) {
184 fprintf(stderr, "[error] Remote side has closed connection\n");
185 goto error;
186 }
187 if (ret_len < 0) {
188 fprintf(stderr, "Error receiving version\n");
189 goto error;
190 }
191 return 0;
192
193 error:
194 return -1;
195 }
196
197 /*
198 * Returns the number of sessions, should be 1 during the unit test.
199 */
200 int list_sessions(int *session_id)
201 {
202 struct lttng_viewer_cmd cmd;
203 struct lttng_viewer_list_sessions list;
204 struct lttng_viewer_session lsession;
205 int i;
206 ssize_t ret_len;
207 int first_session = 0;
208
209 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
210 cmd.data_size = 0;
211 cmd.cmd_version = 0;
212
213 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
214 if (ret_len < 0) {
215 fprintf(stderr, "Error sending cmd\n");
216 goto error;
217 }
218
219 ret_len = lttng_live_recv(control_sock, &list, sizeof(list));
220 if (ret_len == 0) {
221 fprintf(stderr, "[error] Remote side has closed connection\n");
222 goto error;
223 }
224 if (ret_len < 0) {
225 fprintf(stderr, "Error receiving session list\n");
226 goto error;
227 }
228
229 for (i = 0; i < be32toh(list.sessions_count); i++) {
230 ret_len = lttng_live_recv(control_sock, &lsession, sizeof(lsession));
231 if (ret_len < 0) {
232 fprintf(stderr, "Error receiving session\n");
233 goto error;
234 }
235 if (lsession.streams > 0 && first_session <= 0) {
236 first_session = be64toh(lsession.id);
237 *session_id = first_session;
238 }
239 }
240
241 return be32toh(list.sessions_count);
242
243 error:
244 return -1;
245 }
246
247 int create_viewer_session(void)
248 {
249 struct lttng_viewer_cmd cmd;
250 struct lttng_viewer_create_session_response resp;
251 ssize_t ret_len;
252
253 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
254 cmd.data_size = 0;
255 cmd.cmd_version = 0;
256
257 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
258 if (ret_len < 0) {
259 fprintf(stderr, "[error] Error sending cmd\n");
260 goto error;
261 }
262 assert(ret_len == sizeof(cmd));
263
264 ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp));
265 if (ret_len == 0) {
266 fprintf(stderr, "[error] Remote side has closed connection\n");
267 goto error;
268 }
269 if (ret_len < 0) {
270 fprintf(stderr, "[error] Error receiving create session reply\n");
271 goto error;
272 }
273 assert(ret_len == sizeof(resp));
274
275 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
276 fprintf(stderr, "[error] Error creating viewer session\n");
277 goto error;
278 }
279 return 0;
280
281 error:
282 return -1;
283 }
284
285 int attach_session(int id)
286 {
287 struct lttng_viewer_cmd cmd;
288 struct lttng_viewer_attach_session_request rq;
289 struct lttng_viewer_attach_session_response rp;
290 struct lttng_viewer_stream stream;
291 int i;
292 ssize_t ret_len;
293
294 session = zmalloc(sizeof(struct live_session));
295 if (!session) {
296 goto error;
297 }
298
299 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
300 cmd.data_size = sizeof(rq);
301 cmd.cmd_version = 0;
302
303 memset(&rq, 0, sizeof(rq));
304 rq.session_id = htobe64(id);
305 rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
306
307 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
308 if (ret_len < 0) {
309 fprintf(stderr, "Error sending cmd\n");
310 goto error;
311 }
312 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
313 if (ret_len < 0) {
314 fprintf(stderr, "Error sending attach request\n");
315 goto error;
316 }
317
318 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
319 if (ret_len == 0) {
320 fprintf(stderr, "[error] Remote side has closed connection\n");
321 goto error;
322 }
323 if (ret_len < 0) {
324 fprintf(stderr, "Error receiving attach response\n");
325 goto error;
326 }
327 if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {
328 goto error;
329 }
330
331 session->stream_count = be32toh(rp.streams_count);
332 session->streams = zmalloc(session->stream_count *
333 sizeof(struct viewer_stream));
334 if (!session->streams) {
335 goto error;
336 }
337
338 for (i = 0; i < be32toh(rp.streams_count); i++) {
339 ret_len = lttng_live_recv(control_sock, &stream, sizeof(stream));
340 if (ret_len == 0) {
341 fprintf(stderr, "[error] Remote side has closed connection\n");
342 goto error;
343 }
344 if (ret_len < 0) {
345 fprintf(stderr, "Error receiving stream\n");
346 goto error;
347 }
348 session->streams[i].id = be64toh(stream.id);
349
350 session->streams[i].ctf_trace_id = be64toh(stream.ctf_trace_id);
351 session->streams[i].first_read = 1;
352 session->streams[i].mmap_base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
353 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
354 if (session->streams[i].mmap_base == MAP_FAILED) {
355 fprintf(stderr, "mmap error\n");
356 goto error;
357 }
358
359 if (be32toh(stream.metadata_flag)) {
360 session->streams[i].metadata_flag = 1;
361 }
362 }
363 return session->stream_count;
364
365 error:
366 return -1;
367 }
368
369 int get_metadata(void)
370 {
371 struct lttng_viewer_cmd cmd;
372 struct lttng_viewer_get_metadata rq;
373 struct lttng_viewer_metadata_packet rp;
374 ssize_t ret_len;
375 int ret;
376 uint64_t i;
377 char *data = NULL;
378 uint64_t len = 0;
379 int metadata_stream_id = -1;
380
381 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
382 cmd.data_size = sizeof(rq);
383 cmd.cmd_version = 0;
384
385 for (i = 0; i < session->stream_count; i++) {
386 if (session->streams[i].metadata_flag) {
387 metadata_stream_id = i;
388 break;
389 }
390 }
391
392 if (metadata_stream_id < 0) {
393 fprintf(stderr, "No metadata stream found\n");
394 goto error;
395 }
396
397 rq.stream_id = htobe64(session->streams[metadata_stream_id].id);
398
399 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
400 if (ret_len < 0) {
401 fprintf(stderr, "Error sending cmd\n");
402 goto error;
403 }
404 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
405 if (ret_len < 0) {
406 fprintf(stderr, "Error sending get_metadata request\n");
407 goto error;
408 }
409 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
410 if (ret_len == 0) {
411 fprintf(stderr, "[error] Remote side has closed connection\n");
412 goto error;
413 }
414 if (ret_len < 0) {
415 fprintf(stderr, "Error receiving metadata response\n");
416 goto error;
417 }
418 switch (be32toh(rp.status)) {
419 case LTTNG_VIEWER_METADATA_OK:
420 break;
421 case LTTNG_VIEWER_NO_NEW_METADATA:
422 fprintf(stderr, "NO NEW\n");
423 ret = 0;
424 goto end;
425 case LTTNG_VIEWER_METADATA_ERR:
426 fprintf(stderr, "ERR\n");
427 goto error;
428 default:
429 fprintf(stderr, "UNKNOWN\n");
430 goto error;
431 }
432
433 len = be64toh(rp.len);
434 if (len <= 0) {
435 goto error;
436 }
437
438 data = zmalloc(len);
439 if (!data) {
440 PERROR("relay data zmalloc");
441 goto error;
442 }
443 ret_len = lttng_live_recv(control_sock, data, len);
444 if (ret_len == 0) {
445 fprintf(stderr, "[error] Remote side has closed connection\n");
446 goto error_free_data;
447 }
448 if (ret_len < 0) {
449 fprintf(stderr, "Error receiving trace packet\n");
450 goto error_free_data;
451 }
452 free(data);
453 ret = len;
454 end:
455 return ret;
456
457 error_free_data:
458 free(data);
459 error:
460 return -1;
461 }
462
463 int get_next_index(void)
464 {
465 struct lttng_viewer_cmd cmd;
466 struct lttng_viewer_get_next_index rq;
467 struct lttng_viewer_index rp;
468 ssize_t ret_len;
469 int id;
470
471 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
472 cmd.data_size = sizeof(rq);
473 cmd.cmd_version = 0;
474
475 for (id = 0; id < session->stream_count; id++) {
476 if (session->streams[id].metadata_flag) {
477 continue;
478 }
479 memset(&rq, 0, sizeof(rq));
480 rq.stream_id = htobe64(session->streams[id].id);
481
482 retry:
483 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
484 if (ret_len < 0) {
485 fprintf(stderr, "Error sending cmd\n");
486 goto error;
487 }
488 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
489 if (ret_len < 0) {
490 fprintf(stderr, "Error sending get_next_index request\n");
491 goto error;
492 }
493 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
494 if (ret_len == 0) {
495 fprintf(stderr, "[error] Remote side has closed connection\n");
496 goto error;
497 }
498 if (ret_len < 0) {
499 fprintf(stderr, "Error receiving index response\n");
500 goto error;
501 }
502
503 rp.flags = be32toh(rp.flags);
504
505 switch (be32toh(rp.status)) {
506 case LTTNG_VIEWER_INDEX_INACTIVE:
507 fprintf(stderr, "(INACTIVE)\n");
508 break;
509 case LTTNG_VIEWER_INDEX_OK:
510 break;
511 case LTTNG_VIEWER_INDEX_RETRY:
512 sleep(1);
513 goto retry;
514 case LTTNG_VIEWER_INDEX_HUP:
515 fprintf(stderr, "(HUP)\n");
516 session->streams[id].id = -1ULL;
517 session->streams[id].fd = -1;
518 break;
519 case LTTNG_VIEWER_INDEX_ERR:
520 fprintf(stderr, "(ERR)\n");
521 goto error;
522 default:
523 fprintf(stderr, "SHOULD NOT HAPPEN\n");
524 goto error;
525 }
526 if (!first_packet_stream_id) {
527 first_packet_offset = be64toh(rp.offset);
528 first_packet_len = be64toh(rp.packet_size) / CHAR_BIT;
529 first_packet_stream_id = id;
530 }
531 }
532 return 0;
533
534 error:
535 return -1;
536 }
537
538 static
539 int get_data_packet(int id, uint64_t offset,
540 uint64_t len)
541 {
542 struct lttng_viewer_cmd cmd;
543 struct lttng_viewer_get_packet rq;
544 struct lttng_viewer_trace_packet rp;
545 ssize_t ret_len;
546
547 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
548 cmd.data_size = sizeof(rq);
549 cmd.cmd_version = 0;
550
551 memset(&rq, 0, sizeof(rq));
552 rq.stream_id = htobe64(session->streams[id].id);
553 /* Already in big endian. */
554 rq.offset = offset;
555 rq.len = htobe32(len);
556
557 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
558 if (ret_len < 0) {
559 fprintf(stderr, "Error sending cmd\n");
560 goto error;
561 }
562 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
563 if (ret_len < 0) {
564 fprintf(stderr, "Error sending get_data_packet request\n");
565 goto error;
566 }
567 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
568 if (ret_len == 0) {
569 fprintf(stderr, "[error] Remote side has closed connection\n");
570 goto error;
571 }
572 if (ret_len < 0) {
573 fprintf(stderr, "Error receiving data response\n");
574 goto error;
575 }
576 rp.flags = be32toh(rp.flags);
577
578 switch (be32toh(rp.status)) {
579 case LTTNG_VIEWER_GET_PACKET_OK:
580 len = be32toh(rp.len);
581 break;
582 case LTTNG_VIEWER_GET_PACKET_RETRY:
583 fprintf(stderr, "RETRY\n");
584 goto error;
585 case LTTNG_VIEWER_GET_PACKET_ERR:
586 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
587 fprintf(stderr, "NEW_METADATA\n");
588 goto end;
589 }
590 fprintf(stderr, "ERR\n");
591 goto error;
592 default:
593 fprintf(stderr, "UNKNOWN\n");
594 goto error;
595 }
596
597 if (len == 0) {
598 goto error;
599 }
600
601 if (len > mmap_size) {
602 fprintf(stderr, "mmap_size not big enough\n");
603 goto error;
604 }
605
606 ret_len = lttng_live_recv(control_sock, session->streams[id].mmap_base, len);
607 if (ret_len == 0) {
608 fprintf(stderr, "[error] Remote side has closed connection\n");
609 goto error;
610 }
611 if (ret_len < 0) {
612 fprintf(stderr, "Error receiving trace packet\n");
613 goto error;
614 }
615 end:
616 return 0;
617 error:
618 return -1;
619 }
620
621 int main(int argc, char **argv)
622 {
623 int ret;
624 int session_id;
625
626 plan_tests(NUM_TESTS);
627
628 diag("Live unit tests");
629
630 ret = connect_viewer("localhost");
631 ok(ret == 0, "Connect viewer to relayd");
632
633 ret = establish_connection();
634 ok(ret == 0, "Established connection and version check with %d.%d",
635 VERSION_MAJOR, VERSION_MINOR);
636
637 ret = list_sessions(&session_id);
638 ok(ret > 0, "List sessions : %d session(s)", ret);
639
640 ret = create_viewer_session();
641 ok(ret == 0, "Create viewer session");
642
643 ret = attach_session(session_id);
644 ok(ret > 0, "Attach to session, %d streams received", ret);
645
646 ret = get_metadata();
647 ok(ret > 0, "Get metadata, received %d bytes", ret);
648
649 ret = get_next_index();
650 ok(ret == 0, "Get one index per stream");
651
652 ret = get_data_packet(first_packet_stream_id, first_packet_offset,
653 first_packet_len);
654 ok(ret == 0,
655 "Get one data packet for stream %d, offset %d, len %d",
656 first_packet_stream_id, first_packet_offset,
657 first_packet_len);
658
659 return exit_status();
660 }
This page took 0.042184 seconds and 4 git commands to generate.