63175953c5353c8439e3c85761ad5119145698c2
1 /*
2 * Copyright (c) 2012-2013, Texas Instruments Incorporated
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>
51 #define MINPAYLOADSIZE (2 * sizeof(UInt32))
53 /* App defines: Must match on remote proc side: */
54 #define HEAPID 0u
55 #define SLAVE_MESSAGEQNAME "SLAVE"
56 #define MPU_MESSAGEQNAME "HOST"
58 #define PROC_ID_DFLT 1 /* Host is zero, remote cores start at 1 */
59 #define NUM_LOOPS_DFLT 1000 /* Number of transfers to be tested. */
61 typedef struct SyncMsg {
62 MessageQ_MsgHeader header;
63 UInt32 numLoops; /* also used for msgId */
64 UInt32 print;
65 } SyncMsg ;
67 long diff(struct timespec start, struct timespec end)
68 {
69 struct timespec temp;
71 if ((end.tv_nsec - start.tv_nsec) < 0) {
72 temp.tv_sec = end.tv_sec - start.tv_sec-1;
73 temp.tv_nsec = 1000000000UL + end.tv_nsec - start.tv_nsec;
74 } else {
75 temp.tv_sec = end.tv_sec - start.tv_sec;
76 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
77 }
79 return (temp.tv_sec * 1000000UL + temp.tv_nsec / 1000);
80 }
82 Int MessageQApp_execute(UInt32 numLoops, UInt32 payloadSize, UInt16 procId)
83 {
84 Int32 status = 0;
85 MessageQ_Msg msg = NULL;
86 MessageQ_Params msgParams;
87 UInt32 i;
88 MessageQ_QueueId queueId = MessageQ_INVALIDMESSAGEQ;
89 MessageQ_Handle msgqHandle;
90 char remoteQueueName[64];
91 struct timespec start, end;
92 long elapsed;
93 UInt32 msgId;
95 printf("Entered MessageQApp_execute\n");
97 /* Create the local Message Queue for receiving. */
98 MessageQ_Params_init(&msgParams);
99 msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
100 if (msgqHandle == NULL) {
101 printf("Error in MessageQ_create\n");
102 goto exit;
103 }
104 else {
105 printf("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
106 }
108 sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
109 MultiProc_getName(procId));
111 /* Poll until remote side has its messageQ created before we send: */
112 do {
113 status = MessageQ_open(remoteQueueName, &queueId);
114 sleep (1);
115 } while (status == MessageQ_E_NOTFOUND);
117 if (status < 0) {
118 printf("Error in MessageQ_open [%d]\n", status);
119 goto cleanup;
120 }
121 else {
122 printf("Remote queueId [0x%x]\n", queueId);
123 }
125 msg = MessageQ_alloc(HEAPID, sizeof(SyncMsg) + payloadSize);
126 if (msg == NULL) {
127 printf("Error in MessageQ_alloc\n");
128 MessageQ_close(&queueId);
129 goto 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;
136 MessageQ_put(queueId, msg);
137 MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
139 printf("Exchanging %d messages with remote processor %s...\n",
140 numLoops, MultiProc_getName(procId));
142 clock_gettime(CLOCK_REALTIME, &start);
144 for (i = 1 ; i <= numLoops; i++) {
145 ((SyncMsg *)msg)->numLoops = i;
147 /* Have the remote proc reply to this message queue */
148 MessageQ_setReplyQueue(msgqHandle, msg);
150 status = MessageQ_put(queueId, msg);
151 if (status < 0) {
152 printf("Error in MessageQ_put [%d]\n", status);
153 break;
154 }
156 status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
158 if (status < 0) {
159 printf("Error in MessageQ_get [%d]\n", status);
160 break;
161 }
162 else {
163 /* Validate the returned message */
164 msgId = ((SyncMsg *)msg)->numLoops;
165 if ((msg != NULL) && (msgId != i)) {
166 printf("Data integrity failure!\n"
167 " Expected %d\n"
168 " Received %d\n",
169 i, msgId);
170 break;
171 }
172 }
173 }
175 clock_gettime(CLOCK_REALTIME, &end);
176 elapsed = diff(start, end);
178 if (numLoops > 0) {
179 printf("%s: Avg round trip time: %ld usecs\n",
180 MultiProc_getName(procId), elapsed / numLoops);
181 }
183 MessageQ_free(msg);
184 MessageQ_close(&queueId);
186 cleanup:
187 status = MessageQ_delete(&msgqHandle);
188 if (status < 0) {
189 printf ("Error in MessageQ_delete [%d]\n", status);
190 }
192 exit:
193 printf("Leaving MessageQApp_execute\n\n");
195 return (status);
196 }
198 int main (int argc, char * argv[])
199 {
200 Int32 status = 0;
201 UInt32 numLoops = NUM_LOOPS_DFLT;
202 UInt32 payloadSize = MINPAYLOADSIZE;
203 UInt16 procId = PROC_ID_DFLT;
205 /* Parse args: */
206 if (argc > 1) {
207 numLoops = strtoul(argv[1], NULL, 0);
208 }
210 if (argc > 2) {
211 payloadSize = MAX(strtoul(argv[2], NULL, 0), MINPAYLOADSIZE);
212 }
214 if (argc > 3) {
215 procId = atoi(argv[3]);
216 }
218 if (argc > 4) {
219 printf("Usage: %s [<numLoops>] [<payloadSize>] [<ProcId>]\n", argv[0]);
220 printf("\tDefaults: numLoops: %d; payloadSize: %d, ProcId: %d\n",
221 NUM_LOOPS_DFLT, MINPAYLOADSIZE, PROC_ID_DFLT);
222 exit(0);
223 }
225 status = Ipc_start();
227 if (procId >= MultiProc_getNumProcessors()) {
228 printf("ProcId must be less than %d\n", MultiProc_getNumProcessors());
229 Ipc_stop();
230 exit(0);
231 }
232 printf("Using numLoops: %d; payloadSize: %d, procId : %d\n",
233 numLoops, payloadSize, procId);
235 if (status >= 0) {
236 MessageQApp_execute(numLoops, payloadSize, procId);
237 Ipc_stop();
238 }
239 else {
240 printf("Ipc_start failed: status = %d\n", status);
241 }
243 return (status);
244 }