From 8827ecc87eca9a1e970dd2ba5da0731cd9db0e98 Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Sat, 16 Feb 2013 14:39:28 -0500 Subject: core: Making an all C++ version of the preferences/config file readers. This replaces the Python config parser stuff; should have same functionality that we need. --- gnuradio-core/src/lib/general/gr_prefs.cc | 128 +++++++++++++++++++++-- gnuradio-core/src/lib/general/gr_prefs.h | 10 ++ gnuradio-core/src/lib/general/gr_prefs.i | 3 - gnuradio-core/src/python/gnuradio/gr/__init__.py | 2 +- 4 files changed, 133 insertions(+), 10 deletions(-) (limited to 'gnuradio-core') diff --git a/gnuradio-core/src/lib/general/gr_prefs.cc b/gnuradio-core/src/lib/general/gr_prefs.cc index 20aead8e3..40601f7c4 100644 --- a/gnuradio-core/src/lib/general/gr_prefs.cc +++ b/gnuradio-core/src/lib/general/gr_prefs.cc @@ -25,6 +25,13 @@ #endif #include +#include +#include + +#include +#include +#include +namespace fs = boost::filesystem; /* * Stub implementations @@ -45,44 +52,153 @@ gr_prefs::set_singleton(gr_prefs *p) s_singleton = p; } +gr_prefs::gr_prefs() +{ + _read_files(); +} + gr_prefs::~gr_prefs() { // nop } +std::vector +gr_prefs::_sys_prefs_filenames() +{ + fs::path dir = gr_prefsdir(); + if(!fs::is_directory(dir)) + std::runtime_error("gr_prefs: preference path does not exist.\n"); + + std::vector fnames; + fs::directory_iterator diritr(dir); + while(diritr != fs::directory_iterator()) { + fs::path p = *diritr++; + fnames.push_back(p.string()); + } + std::sort(fnames.begin(), fnames.end()); + return fnames; +} + +void +gr_prefs::_read_files() +{ + std::vector filenames = _sys_prefs_filenames(); + std::vector::iterator sitr; + char tmp[1024]; + for(sitr = filenames.begin(); sitr != filenames.end(); sitr++) { + fs::ifstream fin(*sitr); + while(!fin.eof()) { + fin.getline(tmp, 1024); + std::string t(tmp); + // ignore empty lines or lines of just comments + if((t.size() > 0) && (t[0] != '#')) { + // remove any comments in the line + size_t hash = t.find("#"); + + // Use hash marks at the end of each segment as a delimiter + d_configs += t.substr(0, hash) + '#'; + } + } + fin.close(); + } + + // Remove all whitespace + d_configs.erase(std::remove_if(d_configs.begin(), d_configs.end(), ::isspace), d_configs.end()); +} + bool gr_prefs::has_section(const std::string section) { - return false; + size_t t = d_configs.find("[" + section + "]#"); + return t != std::string::npos; } bool gr_prefs::has_option(const std::string section, const std::string option) { - return false; + if(has_section(section)) { + size_t sec = d_configs.find("[" + section + "]#"); + size_t opt = d_configs.find("#" + option + "=", sec); + return opt != std::string::npos; + } + else { + return false; + } } const std::string gr_prefs::get_string(const std::string section, const std::string option, const std::string default_val) { - return default_val; + if(has_option(section, option)) { + std::string optname = "#" + option + "="; + size_t sec = d_configs.find("[" + section + "]#"); + size_t opt = d_configs.find(optname, sec); + + size_t start = opt + optname.size(); + size_t end = d_configs.find("#", start); + size_t len = end - start; + + return d_configs.substr(start, len); + } + else { + return default_val; + } } bool gr_prefs::get_bool(const std::string section, const std::string option, bool default_val) { - return default_val; + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + if(str == "true") + return true; + else if(str == "false") + return false; + else + return default_val; + } + else { + return default_val; + } } long gr_prefs::get_long(const std::string section, const std::string option, long default_val) { - return default_val; + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::stringstream sstr(str); + long n; + sstr >> n; + return n; + } + else { + return default_val; + } } double gr_prefs::get_double(const std::string section, const std::string option, double default_val) { - return default_val; + if(has_option(section, option)) { + std::string str = get_string(section, option, ""); + if(str == "") { + return default_val; + } + std::stringstream sstr(str); + double n; + sstr >> n; + return n; + } + else { + return default_val; + } } diff --git a/gnuradio-core/src/lib/general/gr_prefs.h b/gnuradio-core/src/lib/general/gr_prefs.h index b1c354bd3..90d602741 100644 --- a/gnuradio-core/src/lib/general/gr_prefs.h +++ b/gnuradio-core/src/lib/general/gr_prefs.h @@ -24,6 +24,7 @@ #include #include +#include /*! * \brief Base class for representing user preferences a la windows INI files. @@ -39,6 +40,7 @@ public: static gr_prefs *singleton(); static void set_singleton(gr_prefs *p); + gr_prefs(); virtual ~gr_prefs(); /*! @@ -78,6 +80,14 @@ public: virtual double get_double(const std::string section, const std::string option, double default_val); + + protected: + virtual std::vector _sys_prefs_filenames(); + virtual void _read_files(); + + private: + gruel::mutex d_mutex; + std::string d_configs; }; diff --git a/gnuradio-core/src/lib/general/gr_prefs.i b/gnuradio-core/src/lib/general/gr_prefs.i index f44dcc944..cfb4cdb4e 100644 --- a/gnuradio-core/src/lib/general/gr_prefs.i +++ b/gnuradio-core/src/lib/general/gr_prefs.i @@ -20,9 +20,6 @@ * Boston, MA 02110-1301, USA. */ -// Generate SWIG directors for gr_prefs. -%feature("director") gr_prefs; - class gr_prefs { public: diff --git a/gnuradio-core/src/python/gnuradio/gr/__init__.py b/gnuradio-core/src/python/gnuradio/gr/__init__.py index f1b971e62..1c2c4c837 100644 --- a/gnuradio-core/src/python/gnuradio/gr/__init__.py +++ b/gnuradio-core/src/python/gnuradio/gr/__init__.py @@ -36,7 +36,7 @@ serial_to_parallel = stream_to_vector parallel_to_serial = vector_to_stream # Force the preference database to be initialized -from prefs import prefs +prefs = gr_prefs.singleton #alias old gr_add_vXX and gr_multiply_vXX add_vcc = add_cc -- cgit From 55028034dbad37c07c4675c88fccb460218ee924 Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Sat, 16 Feb 2013 14:45:43 -0500 Subject: core: adding and using a PerfCounters section to the config files. gr_block_executor will read the config files to determine if the perf counters are on or off. --- gnuradio-core/gnuradio-core.conf | 3 +++ gnuradio-core/src/lib/runtime/gr_block_executor.cc | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'gnuradio-core') diff --git a/gnuradio-core/gnuradio-core.conf b/gnuradio-core/gnuradio-core.conf index 178b288e8..70eb00236 100644 --- a/gnuradio-core/gnuradio-core.conf +++ b/gnuradio-core/gnuradio-core.conf @@ -5,3 +5,6 @@ [DEFAULT] verbose = False + +[PerfCounters] +on = False diff --git a/gnuradio-core/src/lib/runtime/gr_block_executor.cc b/gnuradio-core/src/lib/runtime/gr_block_executor.cc index 27f591452..6258d99a8 100644 --- a/gnuradio-core/src/lib/runtime/gr_block_executor.cc +++ b/gnuradio-core/src/lib/runtime/gr_block_executor.cc @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -179,6 +180,8 @@ gr_block_executor::~gr_block_executor () gr_block_executor::state gr_block_executor::run_one_iteration() { + gr_prefs *prefs = gr_prefs::singleton(); + int noutput_items; int max_items_avail; int max_noutput_items = d_max_noutput_items; @@ -420,7 +423,8 @@ gr_block_executor::run_one_iteration() d_start_nitems_read[i] = d->nitems_read(i); #ifdef GR_PERFORMANCE_COUNTERS - d->start_perf_counters(); + if(prefs->get_bool("PerfCounters", "on", false)) + d->start_perf_counters(); #endif /* GR_PERFORMANCE_COUNTERS */ // Do the actual work of the block @@ -428,7 +432,8 @@ gr_block_executor::run_one_iteration() d_input_items, d_output_items); #ifdef GR_PERFORMANCE_COUNTERS - d->stop_perf_counters(noutput_items, n); + if(prefs->get_bool("PerfCounters", "on", false)) + d->stop_perf_counters(noutput_items, n); #endif /* GR_PERFORMANCE_COUNTERS */ LOG(*d_log << " general_work: noutput_items = " << noutput_items -- cgit From 1ddc990d1aded4cda9c1dfde596a1be2599aaf0d Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Sat, 16 Feb 2013 15:41:56 -0500 Subject: core: only query preferences database once at start of block_executor. --- gnuradio-core/src/lib/runtime/gr_block_executor.cc | 11 +++++++---- gnuradio-core/src/lib/runtime/gr_block_executor.h | 4 ++++ 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'gnuradio-core') diff --git a/gnuradio-core/src/lib/runtime/gr_block_executor.cc b/gnuradio-core/src/lib/runtime/gr_block_executor.cc index 6258d99a8..e070f3c50 100644 --- a/gnuradio-core/src/lib/runtime/gr_block_executor.cc +++ b/gnuradio-core/src/lib/runtime/gr_block_executor.cc @@ -166,6 +166,11 @@ gr_block_executor::gr_block_executor (gr_block_sptr block, int max_noutput_items << d_block << std::endl; } +#ifdef GR_PERFORMANCE_COUNTERS + gr_prefs *prefs = gr_prefs::singleton(); + d_use_pc = prefs->get_bool("PerfCounters", "on", false); +#endif /* GR_PERFORMANCE_COUNTERS */ + d_block->start(); // enable any drivers, etc. } @@ -180,8 +185,6 @@ gr_block_executor::~gr_block_executor () gr_block_executor::state gr_block_executor::run_one_iteration() { - gr_prefs *prefs = gr_prefs::singleton(); - int noutput_items; int max_items_avail; int max_noutput_items = d_max_noutput_items; @@ -423,7 +426,7 @@ gr_block_executor::run_one_iteration() d_start_nitems_read[i] = d->nitems_read(i); #ifdef GR_PERFORMANCE_COUNTERS - if(prefs->get_bool("PerfCounters", "on", false)) + if(d_use_pc) d->start_perf_counters(); #endif /* GR_PERFORMANCE_COUNTERS */ @@ -432,7 +435,7 @@ gr_block_executor::run_one_iteration() d_input_items, d_output_items); #ifdef GR_PERFORMANCE_COUNTERS - if(prefs->get_bool("PerfCounters", "on", false)) + if(d_use_pc) d->stop_perf_counters(noutput_items, n); #endif /* GR_PERFORMANCE_COUNTERS */ diff --git a/gnuradio-core/src/lib/runtime/gr_block_executor.h b/gnuradio-core/src/lib/runtime/gr_block_executor.h index 0ae5affba..fb7f9c269 100644 --- a/gnuradio-core/src/lib/runtime/gr_block_executor.h +++ b/gnuradio-core/src/lib/runtime/gr_block_executor.h @@ -53,6 +53,10 @@ protected: std::vector d_returned_tags; int d_max_noutput_items; +#ifdef GR_PERFORMANCE_COUNTERS + bool d_use_pc; +#endif /* GR_PERFORMANCE_COUNTERS */ + public: gr_block_executor(gr_block_sptr block, int max_noutput_items=100000); ~gr_block_executor (); -- cgit From 4f2481e1310dd9e936ed110d60679e71ca2059a8 Mon Sep 17 00:00:00 2001 From: Tom Rondeau Date: Sat, 16 Feb 2013 15:42:48 -0500 Subject: core: add ability to override config files with environmental variables. Env variables are named GR_CONF_
_