Configuration.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 extern "C" {
15  #include <sched.h>
16  #include <sys/mman.h>
17  #include <sys/utsname.h>
18 }
19 
20 // Custom includes
21 #include <senf/Utils/membind.hh>
22 #include <senf/Utils/hexdump.hh>
23 #include <senf/Utils/Logger.hh>
24 #include <senf/Utils/Console.hh>
26 #include "Configuration.hh"
27 
28 #define prefix_
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
32  verbose( false),
33  numPackets(10),
34  pktSize(1000),
35  sendBuffer(256*1024),
36  interface( "eth0"),
37  destination(senf::MACAddress::None),
38  sessionId(1)
39 {
40  namespace fty = senf::console::factory;
41  namespace kw = senf::console::kw;
42  senf::console::DirectoryNode & initDir (senf::console::root().add("init", fty::Directory()));
43  initDir.add("help", fty::Command( &Configuration::help, this));
44  initDir.add("version", fty::Command( &Configuration::version, this));
45  initDir.add("realtime-scheduling", fty::Command( &Configuration::enableRealtimeScheduling, this));
46  initDir.add("verbose", fty::Variable( verbose));
47  initDir.add("num-packets", fty::Variable( numPackets));
48  initDir.add("packet-size", fty::Variable( pktSize));
49  initDir.add("send-buffer", fty::Variable( sendBuffer));
50  initDir.add("interface", fty::Variable( interface));
51  initDir.add("destination", fty::Variable( destination));
52  initDir.add("sessionId", fty::Variable( sessionId));
53 
54  // always turn those on, where available
55  enableHighresTimers();
56  enableRealtimeScheduling();
57 }
58 
60 {
61  senf::console::DirectoryNode::ChildrenRange range(senf::console::root()["init"].children());
62 
63  senf::console::DirectoryNode::ChildrenRange::iterator it;
64  for (it = range.begin(); it != range.end(); it++) {
65  std::cerr << it->first << ": ";
66  it->second->help( std::cout);
67  }
68 
69  exit(0);
70 }
71 
73 {
74  std::cerr << "0.1" << std::endl;
75  exit(0);
76 }
77 
79 {
80  // switching to scheduler highres timer
81  if (senf::scheduler::haveScalableHiresTimers())
82  SENF_LOG((senf::log::IMPORTANT)("using 'timerfd' hi-res timer."));
83  else
84  SENF_LOG((senf::log::IMPORTANT)("using 'posix' hi-res timer."));
85  senf::scheduler::hiresTimers();
86 }
87 
89 {
90  // switching to kernel realtime scheduling
91  struct sched_param param;
92  param.sched_priority = sched_get_priority_max(SCHED_FIFO);
93  if (sched_setscheduler(0, SCHED_FIFO, &param) !=0 ) {
94  SENF_LOG((senf::log::IMPORTANT)("WARNING: unable to switch to realtime mode "));
95  } else {
96  mlockall(MCL_FUTURE);
97  SENF_LOG((senf::log::IMPORTANT) ("switching to realtime mode (SCHED_FIFO)"));
98  }
99 }
100 
101 prefix_ bool Configuration::parse( int argc, char const *argv[])
102 {
103  namespace fty = senf::console::factory;
104  namespace kw = senf::console::kw;
105  senf::console::DirectoryNode & initDir (senf::console::root()["init"]);
106 
107  senf::console::ProgramOptions cmdlineOptions ( argc, argv, initDir);
108 
109  try {
110  // now we parse the filled up configBundle
111  cmdlineOptions.parse( initDir);
112  } catch (senf::console::SyntaxErrorException const & e) {
113  std::cerr << e.message() << std::endl;
114  return false;
115  }
116  catch (senf::ExceptionMixin & ex) {
117  std::cerr << "Exception occurred during configuration parsing: "
118  << ex.message() << std::endl
119  << ex.backtrace() << std::endl;
120  return false;
121  }
122 
123  return true;
124 }
#define prefix_
std::string backtrace() const
boost::iterator_range< ChildMap::const_iterator > ChildrenRange
NodeType & add(std::string const &name, boost::shared_ptr< NodeType > node)
bool parse(int argc, char const *argv[])
void enableHighresTimers()
std::string message() const
#define SENF_LOG(args)
void enableRealtimeScheduling()