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 /*
41 * opkg --tmp-dir ~/tmp install util-linux-ng
42 * chrt -f ./MessageQBench
43 */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <time.h>
47 #include <sys/param.h>
49 #include <Std.h>
50 #include <ti/ipc/Ipc.h>
51 #include <ti/ipc/MessageQ.h>
53 #define MessageQ_payload(m) ((void *)((char *)(m) + sizeof(MessageQ_MsgHeader)))
54 #define MINPAYLOADSIZE (2 * sizeof(UInt32))
56 /* App defines: Must match on remote proc side: */
57 #define NUM_LOOPS_DFLT 1000 /* Number of transfers to be tested. */
58 #define HEAPID 0u
59 #define PROC_ID_DFLT 1 /* Host is zero, remote cores start at 1 */
60 #define SLAVE_MESSAGEQNAME "SLAVE"
61 #define MPU_MESSAGEQNAME "HOST"
64 long diff(struct timespec start, struct timespec end)
65 {
66 struct timespec temp;
68 if ((end.tv_nsec - start.tv_nsec) < 0) {
69 temp.tv_sec = end.tv_sec - start.tv_sec-1;
70 temp.tv_nsec = 1000000000UL + end.tv_nsec - start.tv_nsec;
71 } else {
72 temp.tv_sec = end.tv_sec - start.tv_sec;
73 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
74 }
76 return (temp.tv_sec * 1000000UL + temp.tv_nsec / 1000);
77 }
79 Int MessageQApp_execute(UInt32 numLoops, UInt32 payloadSize, UInt16 procId)
80 {
81 Int32 status = 0;
82 MessageQ_Msg msg = NULL;
83 MessageQ_Params msgParams;
84 UInt16 i;
85 MessageQ_QueueId queueId = MessageQ_INVALIDMESSAGEQ;
86 MessageQ_Handle msgqHandle;
87 char remoteQueueName[64];
88 struct timespec start, end;
89 long elapsed;
90 UInt32 *params;
92 printf ("Entered MessageQApp_execute\n");
94 /* Create the local Message Queue for receiving. */
95 MessageQ_Params_init(&msgParams);
96 msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
97 if (msgqHandle == NULL) {
98 printf ("Error in MessageQ_create\n");
99 goto exit;
100 }
101 else {
102 printf ("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
103 }
105 sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
106 MultiProc_getName(procId));
108 /* Poll until remote side has its messageQ created before we send: */
109 do {
110 status = MessageQ_open(remoteQueueName, &queueId);
111 sleep (1);
112 } while (status == MessageQ_E_NOTFOUND);
114 if (status < 0) {
115 printf("Error in MessageQ_open [%d]\n", status);
116 goto cleanup;
117 }
119 printf("Remote queueId [0x%x]\n", queueId);
121 msg = MessageQ_alloc(HEAPID, sizeof(MessageQ_MsgHeader) + payloadSize);
122 if (msg == NULL) {
123 printf("Error in MessageQ_alloc\n");
124 MessageQ_close(&queueId);
125 goto cleanup;
126 }
128 /* handshake with remote to set the number of loops */
129 MessageQ_setReplyQueue(msgqHandle, msg);
130 params = MessageQ_payload(msg);
131 params[0] = numLoops;
132 params[1] = FALSE;
133 MessageQ_put(queueId, msg);
134 MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
136 printf("Exchanging %d messages with remote processor %s...\n",
137 numLoops, MultiProc_getName(procId));
139 clock_gettime(CLOCK_REALTIME, &start);
141 for (i = 0; i < numLoops; i++) {
142 MessageQ_setMsgId(msg, i);
144 /* Have the remote proc reply to this message queue */
145 MessageQ_setReplyQueue(msgqHandle, msg);
147 status = MessageQ_put(queueId, msg);
148 if (status < 0) {
149 printf("Error in MessageQ_put [%d]\n", status);
150 break;
151 }
153 status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
154 if (status < 0) {
155 printf("Error in MessageQ_get [%d]\n", status);
156 break;
157 }
158 else {
159 /* Validate the returned message. */
160 if ((msg != NULL) && (MessageQ_getMsgId(msg) != i)) {
161 printf ("Data integrity failure!\n"
162 " Expected %d\n"
163 " Received %d\n",
164 i, MessageQ_getMsgId (msg));
165 break;
166 }
167 }
168 }
170 clock_gettime(CLOCK_REALTIME, &end);
171 elapsed = diff(start, end);
173 if (numLoops > 0) {
174 printf("%s: Avg round trip time: %ld usecs\n",
175 MultiProc_getName(procId), elapsed / numLoops);
176 }
178 MessageQ_free(msg);
179 MessageQ_close(&queueId);
181 cleanup:
182 status = MessageQ_delete(&msgqHandle);
183 if (status < 0) {
184 printf ("Error in MessageQ_delete [%d]\n", status);
185 }
187 exit:
188 printf("Leaving MessageQApp_execute\n\n");
190 return (status);
191 }
193 int main (int argc, char * argv[])
194 {
195 Int32 status = 0;
196 UInt32 numLoops = NUM_LOOPS_DFLT;
197 UInt32 payloadSize = MINPAYLOADSIZE;
198 UInt16 procId = PROC_ID_DFLT;
199 UInt32 argul = strtoul(argv[2], NULL, 0);
201 /* Parse args: */
202 if (argc > 1) {
203 numLoops = strtoul(argv[1], NULL, 0);
204 }
206 if (argc > 2) {
207 payloadSize = ((argul >= MINPAYLOADSIZE) ? argul : MINPAYLOADSIZE);
208 }
210 if (argc > 3) {
211 procId = atoi(argv[3]);
212 }
214 if (argc > 4) {
215 printf("Usage: %s [<numLoops>] [<payloadSize>] [<ProcId>]\n", argv[0]);
216 printf("\tDefaults: numLoops: %d; payloadSize: %d, ProcId: %d\n",
217 NUM_LOOPS_DFLT, MINPAYLOADSIZE, PROC_ID_DFLT);
218 exit(0);
219 }
221 status = Ipc_start();
223 if (status >= 0) {
224 if (procId >= MultiProc_getNumProcessors()) {
225 printf("ProcId must be less than %d\n",
226 MultiProc_getNumProcessors());
227 Ipc_stop();
228 exit(0);
229 }
230 printf("Using numLoops: %d; payloadSize: %d, procId : %d\n",
231 numLoops, payloadSize, procId);
233 MessageQApp_execute(numLoops, payloadSize, procId);
234 Ipc_stop();
235 }
236 else {
237 fprintf(stderr, "Ipc_start failed: status = %d\n", status);
238 }
240 return (status);
241 }