switch source package format to 3.0 quilt
[debian/gnuradio] / gcell / apps / benchmark_roundtrip.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2009,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,
97          int getput_mask, int njobs_at_once)
98 {
99   using namespace boost::posix_time;
100
101   int NJDS = njobs_at_once;
102   gc_job_desc *all_jds[NJDS];
103   bool done[NJDS];
104   
105   static const unsigned int BUFSIZE = (32 << 10) * NJDS;
106   unsigned char *getbuf = new unsigned char[BUFSIZE];
107   boost::scoped_array<unsigned char> _getbuf(getbuf);
108   unsigned char *putbuf = new unsigned char[BUFSIZE];
109   boost::scoped_array<unsigned char> _putbuf(putbuf);
110   int gbi = 0;
111
112   // touch all pages to force allocation now
113   for (unsigned int i = 0; i < BUFSIZE; i += 4096){
114     getbuf[i] = 0;
115     putbuf[i] = 0;
116   }
117
118   gc_jm_options opts;
119   opts.program_handle = gc_program_handle_from_address(&benchmark_procs);
120   opts.nspes = nspes;
121   //opts.enable_logging = true;
122   //opts.log2_nlog_entries = 13;
123   gc_job_manager_sptr mgr = gc_make_job_manager(&opts);
124
125   if ((gcp_benchmark_udelay = mgr->lookup_proc("benchmark_udelay")) == GCP_UNKNOWN_PROC){
126     fprintf(stderr, "lookup_proc: failed to find \"benchmark_udelay\"\n");
127     return;
128   }
129
130   // allocate and init all job descriptors
131   for (int i = 0; i < NJDS; i++){
132     if (gbi + dma_size > BUFSIZE)
133       gbi = 0;
134
135     all_jds[i] = mgr->alloc_job_desc();
136     if (all_jds[i] == 0){
137       fprintf(stderr, "alloc_job_desc() returned 0\n");
138       return;
139     }
140     init_jd(all_jds[i], usecs, &getbuf[gbi], &putbuf[gbi], dma_size, getput_mask);
141     gbi += dma_size;
142   }
143
144   int niter = 100000;
145   ptime t_start(microsec_clock::universal_time());
146
147   for (int iter = 0; iter < niter; iter++){
148
149     // submit the jobs
150     for (int i = 0; i < NJDS; i++){
151       if (!mgr->submit_job(all_jds[i])){
152         printf("submit_job(jds[%d]) failed, status = %d\n",
153                i, all_jds[i]->status);
154       }
155     }
156   
157     int n = mgr->wait_jobs(NJDS, all_jds, done, GC_WAIT_ALL);
158     if (n < 0){
159       fprintf(stderr, "mgr->wait_jobs failed\n");
160       break;
161     }
162     if (n != NJDS){
163       fprintf(stderr, "mgr->wait_jobs returned short count.  Expected %d, got %d\n",
164               NJDS, n);
165     }
166   }
167
168   // stop timing
169   ptime t_stop(microsec_clock::universal_time());
170   double delta = (t_stop - t_start).total_microseconds() * 1e-6;
171   printf("nspes: %2d  udelay: %4d  elapsed_time: %7.3f  dma_size: %5d  dma_throughput: %7.3e  round_trip: %gus\n",
172          mgr->nspes(), usecs, delta, dma_size,
173          (double) NJDS * niter * dma_size / delta * (getput_mask == BENCHMARK_GET_PUT ? 2.0 : 1.0),
174          delta / niter * 1e6);
175 }
176
177 static void
178 usage()
179 {
180   fprintf(stderr, "usage: benchmark_dma [-p] [-g] [-n <nspes>] [-u <udelay>] [-s <dma_size>] [-N <njobs_at_a_time>]\n");
181   fprintf(stderr, "  you must specify one or both of -p (put) and -g (get)\n");
182 }
183
184
185 int
186 main(int argc, char **argv)
187 {
188   unsigned int nspes = 0;
189   unsigned int usecs = 0;
190   unsigned int dma_size = 32 << 10;
191   int njobs_at_once = -1;
192   int getput_mask = 0;
193   int ch;
194
195   while ((ch = getopt(argc, argv, "n:u:s:pgN:")) != EOF){
196     switch(ch){
197     case 'n':
198       nspes = strtol(optarg, 0, 0);
199       break;
200
201     case 'u':
202       usecs = strtol(optarg, 0, 0);
203       break;
204
205     case 'N':
206       njobs_at_once = strtol(optarg, 0, 0);
207       break;
208
209     case 's':
210       dma_size = strtol(optarg, 0, 0);
211       if (dma_size == 0){
212         fprintf(stderr, "-s <dma_size> must be > 0\n");
213         return 1;
214       }
215       break;
216
217     case 'p':
218       getput_mask |= BENCHMARK_PUT;
219       break;
220
221     case 'g':
222       getput_mask |= BENCHMARK_GET;
223       break;
224       
225     case '?':
226     default:
227       usage();
228       return 1;
229     }
230   }
231
232   if (njobs_at_once < 0)
233     njobs_at_once = nspes;
234
235   if (getput_mask == 0){
236     usage();
237     return 1;
238   }
239
240   run_test(nspes, usecs, dma_size, getput_mask, njobs_at_once);
241   return 0;
242 }