1 /* Copyright (c) 2012-2014, 2016-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peer, "Messages specific for the peer");
11 void peer_init_chain(peer_t peer, message_t msg)
13 peer->prev = msg->prev_hostname;
14 peer->next = msg->next_hostname;
15 peer->total_pieces = msg->num_pieces;
19 static void peer_forward_msg(peer_t peer, message_t msg)
21 msg_task_t task = task_message_data_new(NULL, msg->data_length);
22 XBT_DEBUG("Sending (isend) from %s into mailbox %s", peer->me, peer->next);
23 msg_comm_t comm = MSG_task_isend(task, peer->next);
24 queue_pending_connection(comm, peer->pending_sends);
27 int peer_execute_task(peer_t peer, msg_task_t task)
30 message_t msg = MSG_task_get_data(task);
32 XBT_DEBUG("Peer %s got message of type %u\n", peer->me, msg->type);
33 if (msg->type == MESSAGE_BUILD_CHAIN)
34 peer_init_chain(peer, msg);
35 else if (msg->type == MESSAGE_SEND_DATA) {
36 xbt_assert(peer->init, "peer_execute_task() failed: got msg_type %u before initialization", msg->type);
37 if (peer->next != NULL)
38 peer_forward_msg(peer, msg);
40 peer->bytes += msg->data_length;
41 if (peer->pieces >= peer->total_pieces) {
42 XBT_DEBUG("%d pieces received", peer->pieces);
47 MSG_task_execute(task);
52 msg_error_t peer_wait_for_message(peer_t peer)
55 msg_comm_t comm = NULL;
56 msg_task_t task = NULL;
60 comm = MSG_task_irecv(&task, peer->me);
61 queue_pending_connection(comm, peer->pending_recvs);
62 int idx = MSG_comm_waitany(peer->pending_recvs);
64 comm = xbt_dynar_get_as(peer->pending_recvs, idx, msg_comm_t);
65 status = MSG_comm_get_status(comm);
66 XBT_DEBUG("peer_wait_for_message: error code = %u", status);
67 xbt_assert(status == MSG_OK, "peer_wait_for_message() failed");
69 task = MSG_comm_get_task(comm);
70 MSG_comm_destroy(comm);
71 xbt_dynar_cursor_rm(peer->pending_recvs, (unsigned int*)&idx);
72 done = peer_execute_task(peer, task);
74 task_message_delete(task);
77 process_pending_connections(peer->pending_sends);
83 void peer_init(peer_t p, int argc, char* argv[])
90 p->pending_recvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
91 p->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
92 /* Set mailbox name: use host number from argv or hostname if no argument given */
94 p->me = bprintf("host%s", argv[1]);
96 p->me = xbt_strdup(MSG_host_get_name(MSG_host_self()));
100 void peer_shutdown(peer_t p)
102 unsigned int size = xbt_dynar_length(p->pending_sends);
104 msg_comm_t* comms = xbt_new(msg_comm_t, size);
106 for (idx = 0; idx < size; idx++) {
107 comms[idx] = xbt_dynar_get_as(p->pending_sends, idx, msg_comm_t);
110 XBT_DEBUG("Waiting for sends to finish before shutdown...");
111 MSG_comm_waitall(comms, size, PEER_SHUTDOWN_DEADLINE);
113 for (idx = 0; idx < size; idx++) {
114 MSG_comm_destroy(comms[idx]);
120 void peer_delete(peer_t p)
122 xbt_dynar_free(&p->pending_recvs);
123 xbt_dynar_free(&p->pending_sends);
131 void peer_print_stats(peer_t p, float elapsed_time)
133 XBT_INFO("### %f %llu bytes (Avg %f MB/s); copy finished (simulated).", elapsed_time, p->bytes,
134 p->bytes / 1024.0 / 1024.0 / elapsed_time);
138 int peer(int argc, char* argv[])
140 peer_t p = xbt_new(s_peer_t, 1);
145 peer_init(p, argc, argv);
146 float start_time = MSG_get_clock();
147 status = peer_wait_for_message(p);
149 float end_time = MSG_get_clock();
150 peer_print_stats(p, end_time - start_time);