Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / tests / test_buffers.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2006 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 2, 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
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr, gru
24 from gnuradio import audio
25 from gnuradio.eng_option import eng_option
26 from optparse import OptionParser
27
28 import time
29 import sys
30
31 # Test script to test setting up the buffers using gr_test
32 # For very large buffers it will fail when you hit the circbuf memory limit.
33 # On linux this limit is shmmax, it will fail when it tries to create a buffer > shmmax.
34 # With a 2.6 or later kernel you can set the shmmax limit manually in a root console
35 #show current shmmax limit
36 #$ cat /proc/sys/kernel/shmmax
37 #33554432
38
39 #set shmmax limit manually to 300MB
40 #echo 300000000 >/proc/sys/kernel/shmmax
41
42 #show new shmmax limit
43 #$ cat /proc/sys/kernel/shmmax
44 #300000000
45
46 class my_graph(gr.flow_graph):
47
48     def __init__(self, seconds,history,output_multiple):
49         gr.flow_graph.__init__(self)
50
51         parser = OptionParser(option_class=eng_option)
52         parser.add_option("-O", "--audio-output", type="string", default="",
53                           help="pcm output device name.  E.g., hw:0,0 or /dev/dsp")
54         parser.add_option("-r", "--sample-rate", type="eng_float", default=48000,
55                           help="set sample rate to RATE (48000)")
56         (options, args) = parser.parse_args ()
57         if len(args) != 0:
58             parser.print_help()
59             raise SystemExit, 1
60
61         sample_rate = int(options.sample_rate)
62         ampl = 0.1
63
64         src0 = gr.sig_source_f (sample_rate, gr.GR_SIN_WAVE, 350, ampl)
65
66         nsamples=int(sample_rate * seconds) #1 seconds
67         # gr.test (const std::string &name=std::string("gr_test"),
68         # int min_inputs=1, int max_inputs=1, unsigned int sizeof_input_item=1,
69         # int min_outputs=1, int max_outputs=1, unsigned int sizeof_output_item=1,
70         # unsigned int history=1,unsigned int output_multiple=1,double relative_rate=1.0,
71         # bool fixed_rate=true,gr_consume_type_t cons_type=CONSUME_NOUTPUT_ITEMS, gr_produce_type_t prod_type=PRODUCE_NOUTPUT_ITEMS);
72         name="gr_test"
73         min_inputs=1
74         max_inputs=1
75         sizeof_input_item=gr.sizeof_float
76         min_outputs=1
77         max_outputs=1
78         sizeof_output_item=gr.sizeof_float
79         #history=1 # problems start at 8150
80         #output_multiple=1 #problems start at 8000 in combination with large history
81         relative_rate=1.0
82         fixed_rate=True
83         consume_type=gr.CONSUME_NOUTPUT_ITEMS
84         produce_type=gr.PRODUCE_NOUTPUT_ITEMS
85         test = gr.test(name, min_inputs,max_inputs,sizeof_input_item,
86                                  min_outputs,max_outputs,sizeof_output_item,
87                                  history,output_multiple,relative_rate,
88                                  fixed_rate, consume_type,produce_type)
89         #test = gr.test("gr_test",1,1,gr.sizeof_float,
90         #                         1,1,gr.sizeof_float,
91         #                         1,1,1.0,
92         #                         True, gr.CONSUME_NOUTPUT_ITEMS,gr.PRODUCE_NOUTPUT_ITEMS)
93         #unsigned int history=1,unsigned int output_multiple=1,double relative_rate=1.0,
94         #bool fixed_rate=false
95         dst = audio.sink (sample_rate, options.audio_output)
96         head= gr.head(gr.sizeof_float, nsamples)
97
98         self.connect (src0,test,head,(dst, 0))
99
100
101 if __name__ == '__main__':
102
103     seconds=5.0
104     output_multiple=1
105     for history in (1,1000,8000,8100,8150,8175,8190,8191,8192,8193,8194,8195,9000,10000,100000,1000000,10000000): #,100000000):
106       sys.stdout.flush()
107       sys.stderr.flush()
108       print 'Test with history=', history, 'output_multiple=',output_multiple
109       sys.stdout.flush()
110       sys.stderr.flush()
111       succeed=True
112       starttime=time.time()
113       try:
114           my_graph(seconds,history,output_multiple).run()
115       except KeyboardInterrupt:
116           pass
117       except:
118           print "\nAn exception has terminated the graph."
119           exception=True
120           succeed=False
121       sys.stdout.flush()
122       sys.stderr.flush()
123       if succeed:
124          print ''
125       endtime=time.time()
126       duration=endtime - starttime
127       if (duration < 0.5*seconds) and (succeed):
128          print "A problem has terminated the graph."
129          succeed=False
130       if (duration > 1.5*seconds) and (succeed):
131          print "Something slowed the graph down considerably."
132          succeed=False
133
134       print 'The test result was:' , succeed
135       print 'Test duration' , duration
136       print ''