ded204fdee9652c90ec7ebb929c7e73be4d2a9e1
1 /*
2 * Copyright (c) 2012-2015 Texas Instruments Incorporated - http://www.ti.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /* =============================================================================
33 * @file MessageQBench.c
34 *
35 * @brief Benchmark application for MessageQ module between MPU/Remote Proc
36 *
37 * ============================================================================
38 */
40 /* Standard headers */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <sys/param.h>
46 /* IPC Headers */
47 #include <ti/ipc/Std.h>
48 #include <ti/ipc/Ipc.h>
49 #include <ti/ipc/MessageQ.h>
50 #include <TransportRpmsg.h>
52 #define MINPAYLOADSIZE (2 * sizeof(UInt32))
54 /* App defines: Must match on remote proc side: */
55 #define HEAPID 0u
56 #define SLAVE_MESSAGEQNAME "SLAVE"
57 #define MPU_MESSAGEQNAME "HOST"
59 #define PROC_ID_DFLT 1 /* Host is zero, remote cores start at 1 */
60 #define NUM_LOOPS_DFLT 1000 /* Number of transfers to be tested. */
62 typedef struct SyncMsg {
63 MessageQ_MsgHeader header;
64 UInt32 numLoops; /* also used for msgId */
65 UInt32 print;
66 } SyncMsg ;
68 long diff(struct timespec start, struct timespec end)
69 {
70 struct timespec temp;
72 if ((end.tv_nsec - start.tv_nsec) < 0) {
73 temp.tv_sec = end.tv_sec - start.tv_sec-1;
74 temp.tv_nsec = 1000000000UL + end.tv_nsec - start.tv_nsec;
75 } else {
76 temp.tv_sec = end.tv_sec - start.tv_sec;
77 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
78 }
80 return (temp.tv_sec * 1000000UL + temp.tv_nsec / 1000);
81 }
83 Int MessageQApp_execute(UInt32 numLoops, UInt32 payloadSize, UInt16 procId)
84 {
85 Int32 status = 0;
86 MessageQ_Msg msg = NULL;
87 MessageQ_Params msgParams;
88 UInt32 i;
89 MessageQ_QueueId queueId = MessageQ_INVALIDMESSAGEQ;
90 MessageQ_Handle msgqHandle;
91 char remoteQueueName[64];
92 struct timespec start, end;
93 long elapsed;
94 UInt32 msgId;
96 printf("Entered MessageQApp_execute\n");
98 /* Create the local Message Queue for receiving. */
99 MessageQ_Params_init(&msgParams);
100 msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
101 if (msgqHandle == NULL) {
102 printf("Error in MessageQ_create\n");
103 goto exit;
104 }
105 else {
106 printf("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
107 }
109 sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
110 MultiProc_getName(procId));
112 /* Poll until remote side has its messageQ created before we send: */
113 do {
114 status = MessageQ_open(remoteQueueName, &queueId);
115 sleep (1);
116 } while (status == MessageQ_E_NOTFOUND);
118 if (status < 0) {
119 printf("Error in MessageQ_open [%d]\n", status);
120 goto cleanup;
121 }
122 else {
123 printf("Remote queueId [0x%x]\n", queueId);
124 }
126 msg = MessageQ_alloc(HEAPID, sizeof(SyncMsg) + payloadSize);
127 if (msg == NULL) {
128 printf("Error in MessageQ_alloc\n");
129 goto close_cleanup;
130 }
132 /* handshake with remote to set the number of loops */
133 MessageQ_setReplyQueue(msgqHandle, msg);
134 ((SyncMsg *)msg)->numLoops = numLoops;
135 ((SyncMsg *)msg)->print = FALSE;
137 status = MessageQ_put(queueId, msg);
138 if (status < 0) {
139 printf("MessageQ_put handshake failed [%d]\n", status);
140 goto free_cleanup;
141 }
143 MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
145 printf("Exchanging %d messages with remote processor %s...\n",
146 numLoops, MultiProc_getName(procId));
148 clock_gettime(CLOCK_REALTIME, &start);
150 for (i = 1 ; i <= numLoops; i++) {
151 ((SyncMsg *)msg)->numLoops = i;
153 /* Have the remote proc reply to this message queue */
154 MessageQ_setReplyQueue(msgqHandle, msg);
156 status = MessageQ_put(queueId, msg);
157 if (status < 0) {
158 printf("Error in MessageQ_put [%d]\n", status);
159 break;
160 }
162 status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
164 if (status < 0) {
165 printf("Error in MessageQ_get [%d]\n", status);
166 break;
167 }
168 else {
169 /* Validate the returned message */
170 msgId = ((SyncMsg *)msg)->numLoops;
171 if ((msg != NULL) && (msgId != i)) {
172 printf("Data integrity failure!\n"
173 " Expected %d\n"
174 " Received %d\n",
175 i, msgId);
176 break;
177 }
178 }
179 }
181 clock_gettime(CLOCK_REALTIME, &end);
182 elapsed = diff(start, end);
184 if (numLoops > 0) {
185 printf("%s: Avg round trip time: %ld usecs\n",
186 MultiProc_getName(procId), elapsed / numLoops);
187 }
189 free_cleanup:
190 MessageQ_free(msg);
191 close_cleanup:
192 MessageQ_close(&queueId);
194 cleanup:
195 status = MessageQ_delete(&msgqHandle);
196 if (status < 0) {
197 printf ("Error in MessageQ_delete [%d]\n", status);
198 }
200 exit:
201 printf("Leaving MessageQApp_execute\n\n");
203 return (status);
204 }
206 int main (int argc, char * argv[])
207 {
208 Int32 status = 0;
209 UInt32 numLoops = NUM_LOOPS_DFLT;
210 UInt32 payloadSize = MINPAYLOADSIZE;
211 UInt16 procId = PROC_ID_DFLT;
213 /* Parse args: */
214 if (argc > 1) {
215 numLoops = strtoul(argv[1], NULL, 0);
216 }
218 if (argc > 2) {
219 payloadSize = MAX(strtoul(argv[2], NULL, 0), MINPAYLOADSIZE);
220 }
222 if (argc > 3) {
223 procId = atoi(argv[3]);
224 }
226 if (argc > 4) {
227 printf("Usage: %s [<numLoops>] [<payloadSize>] [<ProcId>]\n", argv[0]);
228 printf("\tDefaults: numLoops: %d; payloadSize: %d, ProcId: %d\n",
229 NUM_LOOPS_DFLT, MINPAYLOADSIZE, PROC_ID_DFLT);
230 exit(0);
231 }
233 /* configure the transport factory */
234 Ipc_transportConfig(&TransportRpmsg_Factory);
236 /* IPC initialization */
237 status = Ipc_start();
239 if (status < 0) {
240 printf("Error: Ipc_start failed, error=%d\n", status);
241 goto exit;
242 }
244 if (procId >= MultiProc_getNumProcessors()) {
245 printf("ProcId must be less than %d\n", MultiProc_getNumProcessors());
246 Ipc_stop();
247 exit(0);
248 }
249 printf("Using numLoops: %d; payloadSize: %d, procId : %d\n",
250 numLoops, payloadSize, procId);
252 if (status >= 0) {
253 MessageQApp_execute(numLoops, payloadSize, procId);
254 Ipc_stop();
255 }
256 else {
257 printf("Ipc_start failed: status = %d\n", status);
258 }
260 exit:
261 return (status);
262 }