switch source package format to 3.0 quilt
[debian/gnuradio] / gcell / apps / benchmark_dma.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2010 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #if defined(HAVE_CONFIG_H)
23 #include <config.h>
24 #endif
25 #include <gcell/gc_job_manager.h>
26 #include <boost/date_time/posix_time/posix_time_types.hpp>
27 #include <getopt.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <boost/scoped_array.hpp>
31 #include <assert.h>
32
33 // handle to embedded SPU executable that contains benchmark routines
34 // (The name of the variable (benchmark_procs) is the name of the spu executable.)
35 extern spe_program_handle_t benchmark_procs;
36
37 static gc_proc_id_t gcp_benchmark_udelay = GCP_UNKNOWN_PROC;
38
39 #define BENCHMARK_PUT           0x1
40 #define BENCHMARK_GET           0x2
41 #define BENCHMARK_GET_PUT       (BENCHMARK_PUT|BENCHMARK_GET)
42
43
44 #if 0
45 static bool
46 power_of_2_p(unsigned long x)
47 {
48   int nbits = sizeof(x) * 8;
49   for (int i = 0; i < nbits; i++)
50     if (x == (1UL << i))
51       return true;
52
53   return false;
54 }
55 #endif
56
57 static void
58 init_jd(gc_job_desc *jd, unsigned int usecs,
59         unsigned char *getbuf, unsigned char *putbuf, size_t buflen,
60         int getput_mask)
61 {
62   jd->proc_id = gcp_benchmark_udelay;
63   jd->input.nargs = 1;
64   jd->input.arg[0].u32 = usecs;
65   jd->output.nargs = 0;
66
67   switch(getput_mask & BENCHMARK_GET_PUT){
68
69   case BENCHMARK_GET:
70     jd->eaa.nargs = 1;
71     jd->eaa.arg[0].direction = GCJD_DMA_GET;
72     jd->eaa.arg[0].ea_addr = ptr_to_ea(getbuf);
73     jd->eaa.arg[0].get_size = buflen;
74     break;
75
76   case BENCHMARK_PUT:
77     jd->eaa.nargs = 1;
78     jd->eaa.arg[0].direction = GCJD_DMA_PUT;
79     jd->eaa.arg[0].ea_addr = ptr_to_ea(putbuf);
80     jd->eaa.arg[0].put_size = buflen;
81     break;
82     
83   case BENCHMARK_GET_PUT:
84     jd->eaa.nargs = 2;
85     jd->eaa.arg[0].direction = GCJD_DMA_GET;
86     jd->eaa.arg[0].ea_addr = ptr_to_ea(getbuf);
87     jd->eaa.arg[0].get_size = buflen;
88     jd->eaa.arg[1].direction = GCJD_DMA_PUT;
89     jd->eaa.arg[1].ea_addr = ptr_to_ea(putbuf);
90     jd->eaa.arg[1].put_size = buflen;
91     break;
92   }
93 }
94
95 static void
96 run_test(unsigned int nspes, unsigned int usecs, unsigned int dma_size, int getput_mask)
97 {
98   using namespace boost::posix_time;
99
100   static const int64_t TOTAL_SIZE_DMA = 5LL << 30;
101   static const int NJDS = 64;
102   unsigned int njobs = (unsigned int)(TOTAL_SIZE_DMA / dma_size);
103   //unsigned int njobs = NJDS * 16;
104   unsigned int nsubmitted = 0;
105   unsigned int ncompleted = 0;
106   gc_job_desc *all_jds[NJDS];
107   gc_job_desc *jds[2][NJDS];
108   unsigned int njds[2];
109   unsigned int ci;              // current index
110   bool done[NJDS];
111   
112   static const unsigned int BUFSIZE = (32 << 10) * NJDS;
113   unsigned char *getbuf = new unsigned char[BUFSIZE];
114   boost::scoped_array<unsigned char> _getbuf(getbuf);
115   unsigned char *putbuf = new unsigned char[BUFSIZE];
116   boost::scoped_array<unsigned char> _putbuf(putbuf);
117   int gbi = 0;
118
119   // touch all pages to force allocation now
120   for (unsigned int i = 0; i < BUFSIZE; i += 4096){
121     getbuf[i] = 0;
122     putbuf[i] = 0;
123   }
124
125   gc_jm_options opts;
126   opts.program_handle = gc_program_handle_from_address(&benchmark_procs);
127   opts.nspes = nspes;
128   //opts.enable_logging = true;
129   //opts.log2_nlog_entries = 13;
130   gc_job_manager_sptr mgr = gc_make_job_manager(&opts);
131
132   if ((gcp_benchmark_udelay = mgr->lookup_proc("benchmark_udelay")) == GCP_UNKNOWN_PROC){
133     fprintf(stderr, "lookup_proc: failed to find \"benchmark_udelay\"\n");
134     return;
135   }
136
137   // allocate and init all job descriptors
138   for (int i = 0; i < NJDS; i++){
139     if (gbi + dma_size > BUFSIZE)
140       gbi = 0;
141
142     all_jds[i] = mgr->alloc_job_desc();
143     if (all_jds[i] == 0){
144       fprintf(stderr, "alloc_job_desc() returned 0\n");
145       return;
146     }
147     init_jd(all_jds[i], usecs, &getbuf[gbi], &putbuf[gbi], dma_size, getput_mask);
148     gbi += dma_size;
149   }
150
151   for (int iter = 0; iter < 1; iter++){
152
153     ptime t_start(microsec_clock::universal_time());
154
155     nsubmitted = 0;
156     ncompleted = 0;
157
158     ci = 0;
159     njds[0] = 0;
160     njds[1] = 0;
161   
162     // submit the first batch
163     for (int i = 0; i < NJDS; i++){
164       if (mgr->submit_job(all_jds[i])){
165         jds[ci][njds[ci]++] = all_jds[i];
166         nsubmitted++;
167       }
168       else {
169         printf("submit_job(jds[%d]) failed, status = %d\n",
170                i, all_jds[i]->status);
171       }
172     }
173   
174     while (ncompleted < njobs){
175       njds[ci^1] = 0;
176       int n = mgr->wait_jobs(njds[ci], jds[ci], done, GC_WAIT_ANY);
177       // printf("%2d\n", n);
178       if (n < 0){
179         fprintf(stderr, "mgr->wait_jobs failed\n");
180         break;
181       }
182       for (unsigned int i = 0; i < njds[ci]; i++){
183         if (!done[i]){  // remember for next iteration
184           jds[ci^1][njds[ci^1]++] = jds[ci][i];
185         }
186         else {
187           ncompleted++;
188           if (jds[ci][i]->status != JS_OK){
189             printf("js_status = %d, job_id = %d, ncompleted = %d\n",
190                    jds[ci][i]->status, jds[ci][i]->sys.job_id, ncompleted);
191           }
192           if (nsubmitted < njobs){                  // submit another one
193             if (mgr->submit_job(jds[ci][i])){
194               jds[ci^1][njds[ci^1]++] = jds[ci][i];  // remember for next iter
195               nsubmitted++;
196             }
197             else {
198               printf("submit_job(jds[%d]) failed, status = %d\n",
199                      i, jds[ci][i]->status);
200             }
201           }
202         }
203       }
204       ci ^= 1;  // toggle current
205     }
206
207     // stop timing
208     ptime t_stop(microsec_clock::universal_time());
209
210     double delta = (t_stop - t_start).total_microseconds() * 1e-6;
211     printf("nspes: %2d  udelay: %4d  elapsed_time: %7.3f  dma_size: %5d  dma_throughput: %7.3e\n",
212            mgr->nspes(), usecs, delta, dma_size,
213            (double) njobs * dma_size / delta * (getput_mask == BENCHMARK_GET_PUT ? 2.0 : 1.0));
214
215   }
216 }
217
218 static void
219 usage()
220 {
221   fprintf(stderr, "usage: benchmark_dma [-p] [-g] [-n <nspes>] [-u <udelay>] [-s <dma_size>]\n");
222   fprintf(stderr, "  you must specify one or both of -p (put) and -g (get)\n");
223 }
224
225
226 int
227 main(int argc, char **argv)
228 {
229   unsigned int nspes = 0;
230   unsigned int usecs = 0;
231   unsigned int dma_size = 32 << 10;
232   int getput_mask = 0;
233   int ch;
234
235   while ((ch = getopt(argc, argv, "n:u:s:pg")) != EOF){
236     switch(ch){
237     case 'n':
238       nspes = strtol(optarg, 0, 0);
239       break;
240
241     case 'u':
242       usecs = strtol(optarg, 0, 0);
243       break;
244
245     case 's':
246       dma_size = strtol(optarg, 0, 0);
247       if (dma_size == 0){
248         fprintf(stderr, "-s <dma_size> must be > 0\n");
249         return 1;
250       }
251       break;
252
253     case 'p':
254       getput_mask |= BENCHMARK_PUT;
255       break;
256
257     case 'g':
258       getput_mask |= BENCHMARK_GET;
259       break;
260       
261     case '?':
262     default:
263       usage();
264       return 1;
265     }
266   }
267
268   if (getput_mask == 0){
269     usage();
270     return 1;
271   }
272
273   run_test(nspes, usecs, dma_size, getput_mask);
274   return 0;
275 }