I find a paper about GNURADIO COREand think it is a good reference. I will use Chinese to introduceit.
我会用中文主要介绍一下这个GNURADIO COREWORK主要讲什么东西。
Contents
1 How the GNU Radio scheduler iscalled and what it does 1
2 How a thread of each block works 5
1. GNU Radio 调度器怎样调用 和怎么工作
2. 每个模块的线程怎么工作
1 How the GNU Radio scheduler is called and what itdoes
As we become quite familiar with GNU Radio Python codes now,it is essential to figure out
what's going on behind the plain Python codes; that is, howGNU radio core works. A typical
example of GNU Radio 3 is as follows. (There is an olddial-tone example using flow_graph(),
which is obsolete! )
上面的话没太多意思,就是让我们从dial-tone开始去探索GNURadio怎么工作
Besides some routine connections for blocks, the GNU Radiorunning thread starts at
my_top_block().run()
We are going to figure out what's going on after thiscode.
Since Python and C++ classes are 1-1corresponding via SWIG, We have to go deeper
andto find the run() function in C++class
gr_top_block.
这里告诉你,PYTHON和C++通过SWIG对应起来,我们要了解 python里run()这个函数,就要深入C++去了解。
Infile:
gr top block.cc
void
gr_top_block::run()
{
}
Then, go to
start();
先找到run(),看看代码,发现start(),去看start()
In file:
grtop block.cc
void
gr_top_block::start()
{
}
d_impl is a member thatpoints to the class gr_top_block_impl with the followingcodes:
看到start() 其实是调用了d_impl->start(),去classgr_top_block_impl 再去看
In file
: grtop block impl.cc
void
gr_top_block_impl::start()
{
}
The codes do some sanitycheck and then create the GNU Radio Scheduler bycalling
d_scheduler =make_scheduler(d_ffg);
Let go to function make_scheduler()as follows
上面的代码通过调用 d_scheduler = make_scheduler(d_ffg);很严谨的检查并且创建了GNURadio的调度器。让我们去make_scheduler(d_ffg)看看
In file : gr top blockimpl.cc
static gr_scheduler_sptr
make_scheduler(gr_flat_flowgraph_sptr ffg)
{
if (strcmp(v, scheduler_table[i].name) == 0){
}
std::cerr << "warning: InvalidGR_SCHEDULER environment variable value ""
factory = scheduler_table[0].f;
}
In the above program, whatis the variable static scheduler_makerfactory
we can look into the file and find,
在上面的程序中,scheduler_maker factory是什么玩意?我们在文件中找找
typedef gr_scheduler_sptr(*scheduler_maker) (gr_flat_flowgraph_sptr ffg);
Well, factory is a function pointer!Where does it points to? We can find
factory是一个函数指针,指向哪里?找找
factory =schedulertable[i].f
OK. Let us find what is inscheduler_table:
好了,让我们继续找找什么是scheduler_table
static struct scheduler_table {
} scheduler_table[] = {
};
Great! It points to a memberfunction, make, in a scheduler's class. Then theprogram
is veryeasy to understand: it checks whether there exists a Linuxenvironment variable:
GR_SCHEDULER. If no, use thedefault scheduler; otherwise, use user's choice. And we donot
have somany choices on the scheduler:
好耶!它指向一个在scheduler类里边的成员函数,make。简单理解:检查是否存在Linux环境变量:GR_SCHEDULER。如果没有,就是用默认的调度器;否则,是用用户选择的。其实我们也没多少选择不是么,就下边两个:
1. TPB (default): multi-threadedscheduler.
2. STS: single-threadedscheduler.
By default, gr_scheduler_tpb::make will becalled.
In file: gr scheduler_tpb.cc
gr_scheduler_sptr
gr_scheduler_tpb::make(gr_flat_flowgraph_sptr ffg)
{
}
The constructor of
gr_scheduler_tpb
is called.
In file
: grscheduler tpb.cc
gr_scheduler_tpb::gr_scheduler_tpb(gr_flat_flowgraph_sptrffg)
{
}
Nothing strange here, theonly thing needs to mention is
thread_body_wrapper wrapsthe main thread with the block name. Then, thethread
begins from thread_body_wrapper(). Let ussee part of the program to find what happens.
thread_body_wrapper把每个block的主要线程都包住了。然后,线程从thread_body_wrapper()开始。让我们看看到底这部分程序发生了什么。
In file: thread_body_wrapper.h
namespacegruel
{
d_f();
std::cerr<< "thread["<< d_name<< "]: "
std::cerr<< "thread["<< d_name<< "]: "
}
See the overloading ofoperate(), actually, d_f() is called, which is explicitly linkedto
tpb_container(). So go to the code of tpb_container:
看到重载operate(),实际上调用了d_f(),这个函数清晰的链接到tpb_container()。
In file
:gr_scheduler_tpb.cc
class tpb_container
{
public:
};
Well. the overloading ofoperate() just constructs another class,gr_tpb_thread_body,
with theblock pointer. From here, the scheduler's work isdone.
Let's briefly summarize whatis going on with the GNU Radio scheduler.
1. Analyzeused blocks in gr_top_block.
2. Defaultscheduler is TPB, which creates multi-threads forblocks.
3. Thescheduler creates one concurrent thread for eachblock.
4. For eachblock, the thread's entry is gr_tpb_thread_bodybody(d_block).
总结GNU RADIO调度器作用:
1. 分析在gr_top_block中每个block
2.默认调度器是TPB,用来创造每个blocks里边的多线程
3.调度器为每个block创建线程
4.对于每个block,线程入口都在gr_tpb_thread_body 里边
2 How a thread of each blockworks
As we discussed, the TPB scheduler generatesa thread for each block whose entry starts from
theconstructor of class gr_tpb_thread_body. Let us go over theconstructor:
正如我们讨论的,TPB调度器为每个block产生一个线程,这些线程的入口来自一个gr_tpb_thread_body类的构造器。
In file
:gr_tpb_thread_body.cc
gr_tpb_thread_body::gr_tpb_thread_body(gr_block_sptrblock)
{
//Here startsthe main loop of the thread.
//开始线程的循环
//First, thethread processes all signals
//首先,线程处理所有信号
gruel::scoped_lockguard(d->d_tpb.mutex);
while(!d->d_tpb.input_changed){
}
gruel::scoped_lockguard(d->d_tpb.mutex);
while(!d->d_tpb.output_changed){
}
}
So far so good. We can seerun_one_iteration() is the key in the whole thread,it
includes the major functionality of theblock. Go to its source. Woo! it is a littlelong.
很好,我们看到run_one_iteration(),它是整个线程的核心,它包括block的主要功能。去看看那些源代码吧!哇!!有一点点长噢。
In file
:gr_block_executor.cc
gr_block_executor::state
gr_block_executor::run_one_iteration()
{
// Do the actual work of the block int n =m->general_work (noutput_items, d_ninput_items,d_input_items, d_output_items); LOG(*d_log<< " general_work: noutput_items = "<< noutput_items<< " result = "<< n <<std::endl);
}
Overall,the code first checks
1. whether thereexists
sufficient
dataspace for output. No → returnBLKD_OUT
2. whether thereare
sufficient
input data available,No →
returnBLKD_IN
总体上,这些代码首先检查:
1.是否存在足够的输出数据空间
2.是否存在足够的可靠输入数据
If thereare sufficient input data and sufficient output space, the coderuns the actual work
of the block: general_work().
So up to now, we can know how each thread inthe GNU radio core works. Let us briefly
如果这里有足够的输入数据和足够的输出空间,代码就会执行block里边的work咯:gnerral_work()
现在,我们可以知道在GNURadio里的每个线程都怎么工作了噢,所以短暂的总结一下:
summarize
1. A thread for each block has a while (1)loop.
2. The loop processes signals and run the key functionrun_one_iteration().
3. run_one_iteration() checks if there are sufficientdata for input and sufficient available
space for output for theblock.
4. If yes, call general_work() to run the mainfunctionality of the block. Otherwise,return
BLKD_OUT, BLKD_IN, orothers.
1. 每个block的线程都有一个while(1)循环
2. 循环处理信号并且运行主要函数 run_one_iteration()
3. run_one_iteration() 检查是否有足够的数据输入和为这个block输出足够的可靠的空间
4. 如果都可以,调用general_work() 去跑这个block主要的功能。否则,返回BLKD_OUT,BLKD_IN,或者其他。