3 * Copyright 2007,2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
22 #if defined(HAVE_CONFIG_H)
25 #include <gcell/gc_job_manager.h>
26 #include <gnuradio/omni_time.h>
30 #include <boost/scoped_array.hpp>
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;
37 static gc_proc_id_t gcp_benchmark_udelay = GCP_UNKNOWN_PROC;
39 #define BENCHMARK_PUT 0x1
40 #define BENCHMARK_GET 0x2
41 #define BENCHMARK_GET_PUT (BENCHMARK_PUT|BENCHMARK_GET)
46 power_of_2_p(unsigned long x)
48 int nbits = sizeof(x) * 8;
49 for (int i = 0; i < nbits; i++)
58 init_jd(gc_job_desc *jd, unsigned int usecs,
59 unsigned char *getbuf, unsigned char *putbuf, size_t buflen,
62 jd->proc_id = gcp_benchmark_udelay;
64 jd->input.arg[0].u32 = usecs;
67 switch(getput_mask & BENCHMARK_GET_PUT){
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;
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;
83 case BENCHMARK_GET_PUT:
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;
96 run_test(unsigned int nspes, unsigned int usecs, unsigned int dma_size, int getput_mask)
98 static const int64_t TOTAL_SIZE_DMA = 5LL << 30;
99 static const int NJDS = 64;
100 unsigned int njobs = (unsigned int)(TOTAL_SIZE_DMA / dma_size);
101 //unsigned int njobs = NJDS * 16;
102 unsigned int nsubmitted = 0;
103 unsigned int ncompleted = 0;
104 gc_job_desc *all_jds[NJDS];
105 gc_job_desc *jds[2][NJDS];
106 unsigned int njds[2];
107 unsigned int ci; // current index
110 static const unsigned int BUFSIZE = (32 << 10) * NJDS;
111 unsigned char *getbuf = new unsigned char[BUFSIZE];
112 boost::scoped_array<unsigned char> _getbuf(getbuf);
113 unsigned char *putbuf = new unsigned char[BUFSIZE];
114 boost::scoped_array<unsigned char> _putbuf(putbuf);
117 // touch all pages to force allocation now
118 for (unsigned int i = 0; i < BUFSIZE; i += 4096){
124 opts.program_handle = gc_program_handle_from_address(&benchmark_procs);
126 //opts.enable_logging = true;
127 //opts.log2_nlog_entries = 13;
128 gc_job_manager_sptr mgr = gc_make_job_manager(&opts);
130 if ((gcp_benchmark_udelay = mgr->lookup_proc("benchmark_udelay")) == GCP_UNKNOWN_PROC){
131 fprintf(stderr, "lookup_proc: failed to find \"benchmark_udelay\"\n");
135 // allocate and init all job descriptors
136 for (int i = 0; i < NJDS; i++){
137 if (gbi + dma_size > BUFSIZE)
140 all_jds[i] = mgr->alloc_job_desc();
141 if (all_jds[i] == 0){
142 fprintf(stderr, "alloc_job_desc() returned 0\n");
145 init_jd(all_jds[i], usecs, &getbuf[gbi], &putbuf[gbi], dma_size, getput_mask);
149 for (int iter = 0; iter < 1; iter++){
151 omni_time t_start = omni_time::time();
160 // submit the first batch
161 for (int i = 0; i < NJDS; i++){
162 if (mgr->submit_job(all_jds[i])){
163 jds[ci][njds[ci]++] = all_jds[i];
167 printf("submit_job(jds[%d]) failed, status = %d\n",
168 i, all_jds[i]->status);
172 while (ncompleted < njobs){
174 int n = mgr->wait_jobs(njds[ci], jds[ci], done, GC_WAIT_ANY);
175 // printf("%2d\n", n);
177 fprintf(stderr, "mgr->wait_jobs failed\n");
180 for (unsigned int i = 0; i < njds[ci]; i++){
181 if (!done[i]){ // remember for next iteration
182 jds[ci^1][njds[ci^1]++] = jds[ci][i];
186 if (jds[ci][i]->status != JS_OK){
187 printf("js_status = %d, job_id = %d, ncompleted = %d\n",
188 jds[ci][i]->status, jds[ci][i]->sys.job_id, ncompleted);
190 if (nsubmitted < njobs){ // submit another one
191 if (mgr->submit_job(jds[ci][i])){
192 jds[ci^1][njds[ci^1]++] = jds[ci][i]; // remember for next iter
196 printf("submit_job(jds[%d]) failed, status = %d\n",
197 i, jds[ci][i]->status);
202 ci ^= 1; // toggle current
206 omni_time t_stop = omni_time::time();
208 double delta = (t_stop - t_start).double_time();
209 printf("nspes: %2d udelay: %4d elapsed_time: %7.3f dma_size: %5d dma_throughput: %7.3e\n",
210 mgr->nspes(), usecs, delta, dma_size,
211 (double) njobs * dma_size / delta * (getput_mask == BENCHMARK_GET_PUT ? 2.0 : 1.0));
219 fprintf(stderr, "usage: benchmark_dma [-p] [-g] [-n <nspes>] [-u <udelay>] [-s <dma_size>]\n");
220 fprintf(stderr, " you must specify one or both of -p (put) and -g (get)\n");
225 main(int argc, char **argv)
227 unsigned int nspes = 0;
228 unsigned int usecs = 0;
229 unsigned int dma_size = 32 << 10;
233 while ((ch = getopt(argc, argv, "n:u:s:pg")) != EOF){
236 nspes = strtol(optarg, 0, 0);
240 usecs = strtol(optarg, 0, 0);
244 dma_size = strtol(optarg, 0, 0);
246 fprintf(stderr, "-s <dma_size> must be > 0\n");
252 getput_mask |= BENCHMARK_PUT;
256 getput_mask |= BENCHMARK_GET;
266 if (getput_mask == 0){
271 run_test(nspes, usecs, dma_size, getput_mask);