text:
// Monitors execution of a launch file
// Author: Max Schwarz <max.schwarz@uni-bonn.de>

#include "monitor.h"

#include <ros/package.h>
#include <ros/node_handle.h>
#include <ros/param.h>

#include <cstdarg>
#include <cstdio>
#include <fstream>

#include <boost/regex.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem.hpp>

#include <yaml-cpp/yaml.h>

#include <fmt/format.h>

#include "linux_process_info.h"

template<typename... Args>
std::runtime_error error(const char* fmt, const Args& ... args)
{
	return std::runtime_error(fmt::format(fmt, args...));
}

namespace rosmon
{
namespace monitor
{

Monitor::Monitor(launch::LaunchConfig::ConstPtr config, FDWatcher::Ptr watcher)
 : m_config(std::move(config))
 , m_fdWatcher(std::move(watcher))
 , m_ok(true)
{
	for(auto& launchNode : m_config->nodes())
	{
		auto node = std::make_shared<NodeMonitor>(launchNode, m_fdWatcher, m_nh);

		node->logMessageSignal.connect(logMessageSignal);

		if(launchNode->required())
		{
			node->exitedSignal.connect(
				boost::bind(&Monitor::handleRequiredNodeExit, this, _1)
			);
		}

		m_nodes.push_back(node);
	}

#if HAVE_STEADYTIMER
	m_statTimer = m_nh.createSteadyTimer(
#else
	m_statTimer = m_nh.createWallTimer(
#endif
		ros::WallDuration(1.0),
		boost::bind(&Monitor::updateStats, this, _1)
	);
}

void Monitor::setParameters()
{
	{
		std::vector<std::string> paramNames;

		for(auto& node : m_config->nodes())
		{
			if(node->clearParams())
			{
				std::string paramNamespace = node->namespaceString() + "/" + node->name() + "/";

				logTyped(LogEvent::Type::Info, "Deleting parameters in namespace {}", paramNamespace.c_str());

				if(paramNames.empty())
				{
					if(!ros::param::getParamNames(paramNames))
						throw error("Could not get list of parameters for clear_param");
				}

				for(auto& param : paramNames)
				{
					if(param.substr(0, paramNamespace.length()) == paramNamespace)
					{
						ros::param::del(param);
					}
				}
			}
		}
	}

	for(auto& param : m_config->parameters())
		m_nh.setParam(param.first, param.second);
}

void Monitor::start()
{
	for(auto& node : m_nodes)
	{
		node->start();
	}
}

void Monitor::shutdown()
{
	for(auto& node : m_nodes)
		node->shutdown();
}

void Monitor::forceExit()
{
	logTyped(LogEvent::Type::Warning, "Killing the following nodes, which are refusing to exit:\n");
	for(auto& node : m_nodes)
	{
		if(node->running())
		{
			logTyped(LogEvent::Type::Warning, " - {}\n", node->name());
			node->forceExit();
		}
	}
}

bool Monitor::allShutdown()
{
	bool allShutdown = true;
	for(auto& node : m_nodes)
	{
		if(node->running())
			allShutdown = false;
	}

	return allShutdown;
}

double Monitor::shutdownTimeout()
{
	double timeout = 0.0;
	// Find the biggest timeout
	for(auto& node : m_nodes)
		if(node->stopTimeout() > timeout)
			timeout = node->stopTimeout();
	return timeout;
}

void Monitor::handleRequiredNodeExit(const std::string& name)
{
	logTyped(LogEvent::Type::Info, "Required node '{}' exited, shutting down...", name);
	m_ok = false;
}

template<typename... Args>
void Monitor::log(const char* fmt, Args&& ... args)
{
	logMessageSignal({
		"[rosmon]",
		fmt::format(fmt, std::forward<Args>(args)...)
	});
}

template<typename... Args>
void Monitor::logTyped(LogEvent::Type type, const char* fmt, Args&& ... args)
{
	logMessageSignal({
		"[rosmon]",
		fmt::format(fmt, std::forward<Args>(args)...),
		type
	});
}

#if HAVE_STEADYTIMER
void Monitor::updateStats(const ros::SteadyTimerEvent& event)
#else
void Monitor::updateStats(const ros::WallTimerEvent& event)
#endif
{
	namespace fs = boost::filesystem;

	fs::directory_iterator it("/proc");
	fs::directory_iterator end;

	std::map<int, NodeMonitor::Ptr> nodeMap;
	for(auto& node : m_nodes)
	{
		if(node->pid() != -1)
			nodeMap[node->pid()] = node;

		node->beginStatUpdate();
	}

	for(auto& procInfo : m_processInfos)
		procInfo.second.active = false;

	for(; it != end; ++it)
	{
		fs::path statPath = (*it) / "stat";
		try
		{
			if(!fs::exists(statPath))
				continue;
		}
		catch (fs::filesystem_error &e)
		{
			log("boost::filesystem_error", std::string("exception: ") + e.what() + ". Cannot acces to " + statPath.native() );
			continue;
		}

		process_info::ProcessStat stat;
		if(!process_info::readStatFile(statPath.c_str(), &stat))
			continue;

		// Find corresponding node by the process group ID
		// (= process ID of the group leader process)
		auto it = nodeMap.find(stat.pgrp);
		if(it == nodeMap.end())
			continue;

		auto& node = it->second;

		// We need to store the stats and subtract the last one to get a time
		// delta
		auto infoIt = m_processInfos.find(stat.pid);
		if(infoIt == m_processInfos.end())
		{
			ProcessInfo info;
			info.stat = stat;
			info.active = true;
			m_processInfos[stat.pid] = info;
			continue;
		}

		const auto& oldStat = infoIt->second.stat;

		node->addCPUTime(stat.utime - oldStat.utime, stat.stime - oldStat.stime);
		node->addMemory(stat.mem_rss);

		infoIt->second.active = true;
		infoIt->second.stat = stat;
	}

	double elapsedTime = (event.current_real - event.last_real).toSec();

	for(auto& node : m_nodes)
		node->endStatUpdate(elapsedTime * process_info::kernel_hz());

	// Clean up old processes
	for(auto it = m_processInfos.begin(); it != m_processInfos.end();)
	{
		if(!it->second.active)
			it = m_processInfos.erase(it);
		else
			it++;
	}
}

}
}

max_stars_repo_name: RobotnikAutomation/rosmon

max_issues_repo_name: RobotnikAutomation/rosmon

max_forks_repo_name: RobotnikAutomation/rosmon


text:
//  (C) Copyright John Maddock 2015.
//  Use, modification and distribution are subject to the Boost Software License,
//  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt).
//
//  See http://www.boost.org/libs/type_traits for most recent version including documentation.

#ifndef BOOST_TT_IS_DEFAULT_CONSTRUCTIBLE_HPP_INCLUDED
#define BOOST_TT_IS_DEFAULT_CONSTRUCTIBLE_HPP_INCLUDED

#include <cstddef> // size_t
#include <boost/type_traits/integral_constant.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/type_traits/is_complete.hpp>
#include <boost/static_assert.hpp>

#if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40700)
#include <boost/type_traits/is_abstract.hpp>
#endif
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ <= 5)) || (defined(BOOST_MSVC) && (BOOST_MSVC == 1800))
#include <utility> // std::pair
#endif

#if !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)

#include <boost/type_traits/detail/yes_no_type.hpp>

namespace lslboost{

   namespace detail{

      struct is_default_constructible_imp
      {
         template<typename _Tp, typename = decltype(_Tp())>
         static lslboost::type_traits::yes_type test(int);

         template<typename>
         static lslboost::type_traits::no_type test(...);
      };
#if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40700)
      template<class T, bool b>
      struct is_default_constructible_abstract_filter
      {
          static const bool value = sizeof(is_default_constructible_imp::test<T>(0)) == sizeof(lslboost::type_traits::yes_type);
      };
      template<class T>
      struct is_default_constructible_abstract_filter<T, true>
      {
          static const bool value = false;
      };
#endif
   }

#if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40700)
   template <class T> struct is_default_constructible : public integral_constant<bool, detail::is_default_constructible_abstract_filter<T, lslboost::is_abstract<T>::value>::value>
   {
      BOOST_STATIC_ASSERT_MSG(lslboost::is_complete<T>::value, "Arguments to is_default_constructible must be complete types");
   };
#else
   template <class T> struct is_default_constructible : public integral_constant<bool, sizeof(detail::is_default_constructible_imp::test<T>(0)) == sizeof(lslboost::type_traits::yes_type)>
   {
      BOOST_STATIC_ASSERT_MSG(lslboost::is_complete<T>::value, "Arguments to is_default_constructible must be complete types");
   };
#endif
   template <class T, std::size_t N> struct is_default_constructible<T[N]> : public is_default_constructible<T>{};
   template <class T> struct is_default_constructible<T[]> : public is_default_constructible<T>{};
   template <class T> struct is_default_constructible<T&> : public integral_constant<bool, false>{};
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ <= 5))|| (defined(BOOST_MSVC) && (BOOST_MSVC == 1800))
   template <class T, class U> struct is_default_constructible<std::pair<T,U> > : public integral_constant<bool, is_default_constructible<T>::value && is_default_constructible<U>::value>{};
#endif
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
   template <class T> struct is_default_constructible<T&&> : public integral_constant<bool, false>{};
#endif
   template <> struct is_default_constructible<void> : public integral_constant<bool, false>{};
   template <> struct is_default_constructible<void const> : public integral_constant<bool, false>{};
   template <> struct is_default_constructible<void volatile> : public integral_constant<bool, false>{};
   template <> struct is_default_constructible<void const volatile> : public integral_constant<bool, false>{};

#else

#include <boost/type_traits/is_pod.hpp>

namespace lslboost{

   // We don't know how to implement this, note we can not use has_trivial_constructor here
   // because the correct implementation of that trait requires this one:
   template <class T> struct is_default_constructible : public is_pod<T>{};
   template <> struct is_default_constructible<void> : public integral_constant<bool, false>{};
   template <> struct is_default_constructible<void const> : public integral_constant<bool, false>{};
   template <> struct is_default_constructible<void volatile> : public integral_constant<bool, false>{};
   template <> struct is_default_constructible<void const volatile> : public integral_constant<bool, false>{};

#endif

} // namespace lslboost

#endif // BOOST_TT_IS_DEFAULT_CONSTRUCTIBLE_HPP_INCLUDED

ext: hpp

max_stars_repo_name: samuelpowell/liblsl

max_issues_repo_name: samuelpowell/liblsl

max_forks_repo_name: samuelpowell/liblsl


text:
#include "WordsApiJsonResponseDeserializer.h"

#include "gtest/gtest.h"
#include <boost/optional/optional_io.hpp>

using namespace glossary;
using namespace wordDescriptionDownloader;
using namespace wordDescriptionRepository;
using namespace ::testing;

namespace
{
const std::string invalidJson{"{."};
const std::string definitionsJson =
    R"({"word":"fetch",
"definitions":[
{"definition":"be sold for a certain price","partOfSpeech":"verb"},
{"definition":"go or come after and bring or take back","partOfSpeech":"verb"},
{"definition":"take away or remove","partOfSpeech":"verb"},
{"definition":"the action of fetching","partOfSpeech":"noun"}
]})";
const std::string examplesJson =
    R"({"word":"fetch",
"examples":[
"The old print fetched a high price at the auction",
"The dog fetched the hat",
"The devil will fetch you!"
]})";
const std::string synonymsJson =
    R"({"word":"fetch",
"synonyms":[
"bring",
"bring in",
"convey",
"get"
]})";
const std::string definitionsJsonWithoutDefinitionsField =
    R"({"word":"fetch",
"":[
{"definition":"be sold for a certain price","partOfSpeech":"verb"},
{"definition":"go or come after and bring or take back","partOfSpeech":"verb"},
{"definition":"take away or remove","partOfSpeech":"verb"},
{"definition":"the action of fetching","partOfSpeech":"noun"}
]})";
const std::string examplesJsonWithoutExamplesField =
    R"({"word":"fetch",
"":[
"The old print fetched a high price at the auction",
"The dog fetched the hat",
"The devil will fetch you!"
]})";
const std::string synonymsJsonWithoutSynonymsField =
    R"({"word":"fetch",
"":[
"bring",
"bring in",
"convey",
"get"
]})";
const Definitions expectedDefinitions{"be sold for a certain price",
                                      "go or come after and bring or take back", "take away or remove",
                                      "the action of fetching"};
const Examples expectedExamples{"The old print fetched a high price at the auction",
                                "The dog fetched the hat", "The devil will fetch you!"};
const Synonyms expectedSynonyms{"bring", "bring in", "convey", "get"};
}

class WordsApiJsonResponseDeserializerTest : public Test
{
public:
    WordsApiJsonResponseDeserializer deserializer;
};

TEST_F(WordsApiJsonResponseDeserializerTest, invalidJson_shouldReturnEmptyDefinitions)
{
    const auto actualDefinitions = deserializer.deserializeDefinitions(invalidJson);

    ASSERT_TRUE(actualDefinitions.empty());
}

TEST_F(WordsApiJsonResponseDeserializerTest, jsonWithoutDefinitionsField_shouldReturnEmptyDefinitions)
{
    const auto actualDefinitions =
        deserializer.deserializeDefinitions(definitionsJsonWithoutDefinitionsField);

    ASSERT_TRUE(actualDefinitions.empty());
}

TEST_F(WordsApiJsonResponseDeserializerTest, jsonWithDefinitionsField_shouldReturnDefinitions)
{
    const auto actualDefinitions = deserializer.deserializeDefinitions(definitionsJson);

    ASSERT_EQ(actualDefinitions, expectedDefinitions);
}

TEST_F(WordsApiJsonResponseDeserializerTest, invalidJson_shouldReturnEmptyExamples)
{
    const auto actualExamples = deserializer.deserializeExamples(invalidJson);

    ASSERT_TRUE(actualExamples.empty());
}

TEST_F(WordsApiJsonResponseDeserializerTest, jsonWithoutExamplesField_shouldReturnEmptyExamples)
{
    const auto actualExamples = deserializer.deserializeExamples(examplesJsonWithoutExamplesField);

    ASSERT_TRUE(actualExamples.empty());
}

TEST_F(WordsApiJsonResponseDeserializerTest, jsonWithExamplesField_shouldReturnExamples)
{
    const auto actualExamples = deserializer.deserializeExamples(examplesJson);

    ASSERT_EQ(actualExamples, expectedExamples);
}

TEST_F(WordsApiJsonResponseDeserializerTest, invalidJson_shouldReturnEmptySynonyms)
{
    const auto actualSynonyms = deserializer.deserializeSynonyms(invalidJson);

    ASSERT_TRUE(actualSynonyms.empty());
}

TEST_F(WordsApiJsonResponseDeserializerTest, jsonWithoutSynonymsField_shouldReturnEmptySynonyms)
{
    const auto actualSynonyms = deserializer.deserializeSynonyms(synonymsJsonWithoutSynonymsField);

    ASSERT_TRUE(actualSynonyms.empty());
}

TEST_F(WordsApiJsonResponseDeserializerTest, jsonWithSynonymsField_shouldReturnSynonyms)
{
    const auto actualSynonyms = deserializer.deserializeSynonyms(synonymsJson);

    ASSERT_EQ(actualSynonyms, expectedSynonyms);
}

ext: cpp

max_stars_repo_name: michalovsky/Glossary

max_issues_repo_name: michalovsky/Glossary

max_forks_repo_name: michalovsky/Glossary


text:
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/detail/fast_and.hpp>
namespace hana = boost::hana;

static_assert(hana::detail::fast_and<>::value, "");
static_assert(hana::detail::fast_and<true>::value, "");
static_assert(!hana::detail::fast_and<false>::value, "");

static_assert(hana::detail::fast_and<true, true>::value, "");
static_assert(!hana::detail::fast_and<true, false>::value, "");
static_assert(!hana::detail::fast_and<false, true>::value, "");
static_assert(!hana::detail::fast_and<false, false>::value, "");

static_assert(hana::detail::fast_and<true, true, true>::value, "");
static_assert(!hana::detail::fast_and<true, true, false>::value, "");
static_assert(!hana::detail::fast_and<true, false, true>::value, "");
static_assert(!hana::detail::fast_and<true, false, false>::value, "");
static_assert(!hana::detail::fast_and<false, true, true>::value, "");
static_assert(!hana::detail::fast_and<false, true, false>::value, "");
static_assert(!hana::detail::fast_and<false, false, true>::value, "");
static_assert(!hana::detail::fast_and<false, false, false>::value, "");

static_assert(hana::detail::fast_and<true, true, true, true, true, true>::value, "");
static_assert(!hana::detail::fast_and<true, true, true, false, true, true>::value, "");
static_assert(!hana::detail::fast_and<true, true, true, true, true, false>::value, "");
static_assert(!hana::detail::fast_and<false, true, true, true, true, true>::value, "");

int main() { }


text:
#include <idmlib/wiki/ner_extractor.h>

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <util/ustring/UString.h>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <idmlib/util/idm_analyzer.h>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/regex.hpp>
using namespace idmlib::wiki;

bool NerExtractor::extract_from_xml(const std::string& input_xml, const std::string& title_regex)
{
  std::ifstream ifs(input_xml.c_str());
  std::string line;
  izenelib::util::UString left("[[", izenelib::util::UString::UTF_8);
  izenelib::util::UString right("]]", izenelib::util::UString::UTF_8);
  izenelib::util::UString spliter("|", izenelib::util::UString::UTF_8);
  izenelib::util::UString colon(":", izenelib::util::UString::UTF_8);
  std::string page_end = "</page>";
  std::string title = "";
  uint32_t line_num = 0;
  bool valid_title = false;
  boost::regex reg_exp(title_regex);
  while(getline( ifs, line))
  {
    line_num++;
    if(line_num%100000==0)
    {
//       std::cout<<"Processing line : "<<line_num<<std::endl;
    }
    boost::algorithm::trim(line);
    if(line == page_end )
    {
      title = "";
      valid_title = false;
      continue;
    }
    if( boost::starts_with(line, "<title>") && boost::ends_with(line, "</title>") )
    {
      title = line.substr(7, line.length()-15);
//       std::cout<<"find title : "<<title<<std::endl;
      if( boost::regex_match(title, reg_exp) )
      {
        std::cout<<"Match! : "<<title<<std::endl;
        valid_title = true;
      }
      else
      {
        valid_title = false;
      }
    }
    if(!valid_title) continue;
    izenelib::util::UString text(line, izenelib::util::UString::UTF_8);
    std::size_t pos = 0;
    while(true)
    {
      uint32_t left_pos = text.find(left, pos);
      if( left_pos==(uint32_t)-1 ) break;
//       std::cout<<"find left in line "<<line<<" @@@ "<<left_pos<<std::endl;
      uint32_t right_pos = text.find(right, left_pos);
      if( right_pos==(uint32_t)-1 ) break;
      izenelib::util::UString left_context = text.substr(pos, left_pos-pos);
      izenelib::util::UString right_context = text.substr(right_pos+2, text.length()-right_pos-2);
      izenelib::util::UString entity = text.substr(left_pos+2, right_pos-left_pos-2);
      pos = right_pos;
      if(entity.find(colon)!=(uint32_t)-1) continue;

      izenelib::util::UString surface = entity;
      izenelib::util::UString ref = entity;

      uint32_t entity_split = entity.find(spliter);
      if(entity_split!=(uint32_t)-1)
      {
        ref = entity.substr(0, entity_split);
        surface = entity.substr(entity_split+1, entity.length()-entity_split-1);
      }
      //TODO write
      if(surface.length()>1)
      {
        std::string surface_str;
        surface.convertString(surface_str, izenelib::util::UString::UTF_8);
        if(boost::ends_with(surface_str, "列表")) continue;
        std::string ref_str;
        ref.convertString(ref_str, izenelib::util::UString::UTF_8);
        std::cout<<surface_str<<","<<ref_str<<std::endl;
      }

    }

  }
  ifs.close();
  return true;
}

bool NerExtractor::extract_from_catelink(const std::string& input_file, const std::string& category_regex, const std::string& page_regex)
{
  std::ifstream ifs(input_file.c_str());
  std::string line;
  izenelib::util::UString left("[[", izenelib::util::UString::UTF_8);
  izenelib::util::UString right("]]", izenelib::util::UString::UTF_8);
  izenelib::util::UString spliter("|", izenelib::util::UString::UTF_8);
  izenelib::util::UString colon(":", izenelib::util::UString::UTF_8);
  uint32_t line_num = 0;
  boost::regex reg_exp(category_regex);
  boost::regex* page_reg = NULL;
  if(page_regex!="")
  {
    page_reg = new boost::regex(page_regex);
  }
  while(getline( ifs, line))
  {
    line_num++;
    if(line_num%100000==0)
    {
//       std::cout<<"Processing line : "<<line_num<<std::endl;
    }
    std::vector<std::string> vec_value;
    boost::algorithm::split( vec_value, line, boost::algorithm::is_any_of("\t") );
    if(vec_value.size()!=2)
    {
      std::cout<<"invalid row : "<<line<<std::endl;
      continue;
    }
    std::string page = vec_value[0];
    if(boost::ends_with(page, "列表")) continue;
    izenelib::util::UString upage(page, izenelib::util::UString::UTF_8);
    if( page.find(":")!=std::string::npos ) continue;
    if(upage.length()<2) continue;
    if(upage.isAlphaChar(0)) continue;
    std::string category = vec_value[1];
    if( boost::regex_match(category, reg_exp) )
    {
      if(page_reg!=NULL)
      {
        if( boost::regex_match(page, *page_reg) )
        {
          std::cout<<page<<std::endl;
        }
      }

    }
  }
  ifs.close();
  return true;
}

ext: cc

max_stars_repo_name: izenecloud/idmlib

max_issues_repo_name: izenecloud/idmlib

max_forks_repo_name: izenecloud/idmlib


text:
/*
 * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
 */

#include "control_node.h"

#include <boost/assign.hpp>
#include <sys/resource.h>

#include "base/task.h"
#include "db/db.h"
#include "config_client_manager.h"
//
// Default scheduler policy for control-node daemon and test processes.
//
void ControlNode::SetDefaultSchedulingPolicy() {
    static bool policy_set;

    if (policy_set)
        return;
    policy_set = true;

    TaskScheduler *scheduler = TaskScheduler::GetInstance();

    // Policy for bgp::Config Task.
    TaskPolicy config_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::EvpnSegment")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RTFilter")))
        (TaskExclusion(scheduler->GetTaskId("bgp::SendUpdate")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StateMachine")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")))
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("db::IFMapTable")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("io::ReaderTask")))
        (TaskExclusion(scheduler->GetTaskId("ifmap::StateMachine")))
        (TaskExclusion(scheduler->GetTaskId("xmpp::StateMachine")))
        (TaskExclusion(scheduler->GetTaskId("timer::TimerTask")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ShowCommand")))
        (TaskExclusion(scheduler->GetTaskId("bgp::SendReadyTask")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RouteAggregation")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverPath")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverNexthop")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::Config"), config_policy);

    // Policy for bgp::ConfigHelper Task.
    // Same as that for bgp:Config Task except that bgp:ConfigHelper
    // is not exclusive with db::IFMapTable and ifmap::StateMachine.
    TaskPolicy config_helper_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::EvpnSegment")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RTFilter")))
        (TaskExclusion(scheduler->GetTaskId("bgp::SendUpdate")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StateMachine")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")))
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("io::ReaderTask")))
        (TaskExclusion(scheduler->GetTaskId("xmpp::StateMachine")))
        (TaskExclusion(scheduler->GetTaskId("timer::TimerTask")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ShowCommand")))
        (TaskExclusion(scheduler->GetTaskId("bgp::SendReadyTask")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RouteAggregation")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverPath")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverNexthop")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::ConfigHelper"),
        config_helper_policy);

    // Policy for bgp::ServiceChain and bgp::StaticRoute Tasks.
    TaskPolicy static_service_chain_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::EvpnSegment")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverPath")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RouteAggregation")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::ServiceChain"),
        static_service_chain_policy);
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::StaticRoute"),
        static_service_chain_policy);

    // Policy for bgp::StateMachine and xmpp::StateMachine Tasks.
    // Add policy to provision exclusion between io::Reader and
    // bgp/xmpp StateMachine tasks with the same task instance.
    TaskPolicy sm_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ShowCommand")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RTFilter")));
    for (int idx = 0; idx < scheduler->HardwareThreadCount(); ++idx) {
        sm_policy.push_back(
            (TaskExclusion(scheduler->GetTaskId("io::ReaderTask"), idx)));
    }
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::StateMachine"),
        sm_policy);
    scheduler->SetPolicy(scheduler->GetTaskId("xmpp::StateMachine"),
        sm_policy);

    // Policy for bgp::PeerMembership Task.
    TaskPolicy peer_membership_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::EvpnSegment")))
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("bgp::SendUpdate")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ShowCommand")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StateMachine")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")))
        (TaskExclusion(scheduler->GetTaskId("xmpp::StateMachine")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::PeerMembership"),
        peer_membership_policy);

    // Policy for bgp::SendUpdate Task.
    // Add policy to provision exclusion between db::DBTable and
    // bgp::SendUpdate tasks with the same task instance.
    TaskPolicy send_update_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")))
        (TaskExclusion(scheduler->GetTaskId("bgp::SendReadyTask")));
    for (int idx = 0; idx < DB::PartitionCount(); ++idx) {
        send_update_policy.push_back(
            (TaskExclusion(scheduler->GetTaskId("db::DBTable"), idx)));
    }
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::SendUpdate"),
        send_update_policy);

    // Policy for bgp::SendReadyTask Task.
    TaskPolicy send_ready_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("bgp::SendUpdate")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::SendReadyTask"),
        send_ready_policy);

    // Policy for bgp::EvpnSegment Task.
    TaskPolicy evpn_segment_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverPath")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RouteAggregation")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::EvpnSegment"),
        evpn_segment_policy);

    // Policy for bgp::RTFilter Task.
    TaskPolicy rtfilter_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StateMachine")))
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::RTFilter"),
        rtfilter_policy);

    // Policy for bgp::ResolverPath Task.
    TaskPolicy resolver_path_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::EvpnSegment")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverNexthop")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RouteAggregation")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::ResolverPath"),
        resolver_path_policy);

    // Policy for bgp::ResolverNexthop Task.
    TaskPolicy resolver_nexthop_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverPath")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RouteAggregation")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::ResolverNexthop"),
        resolver_nexthop_policy);

    // Policy for bgp::RouteAggregation Task.
    TaskPolicy route_aggregation_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")))
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::EvpnSegment")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverNexthop")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverPath")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")));
    scheduler->SetPolicy(scheduler->GetTaskId("bgp::RouteAggregation"),
        route_aggregation_policy);

    // Policy for db::IFMapTable Task.
    TaskPolicy db_ifmap_policy = boost::assign::list_of
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("db::Walker")));
    scheduler->SetPolicy(scheduler->GetTaskId("db::IFMapTable"),
        db_ifmap_policy);

    // Policy for db::Walker Task.
    // Rules:
    // 1. All tasks that trigger WalkTable should be mutually exclusive to
    // db::Walker task
    // 2. All tasks that updates the db table partition should be mutually
    // mutually exclusive
    TaskPolicy walker_policy = boost::assign::list_of
        // Following tasks trigger WalkTable
        (TaskExclusion(scheduler->GetTaskId("bgp::Config")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ConfigHelper")))
        (TaskExclusion(scheduler->GetTaskId("bgp::PeerMembership")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RTFilter")))
        // Following tasks updates db table partition
        (TaskExclusion(scheduler->GetTaskId("db::DBTable")))
        (TaskExclusion(scheduler->GetTaskId("db::IFMapTable")))
        (TaskExclusion(scheduler->GetTaskId("bgp::EvpnSegment")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ResolverPath")))
        (TaskExclusion(scheduler->GetTaskId("bgp::RouteAggregation")))
        (TaskExclusion(scheduler->GetTaskId("bgp::ServiceChain")))
        (TaskExclusion(scheduler->GetTaskId("bgp::StaticRoute")));
    scheduler->SetPolicy(scheduler->GetTaskId("db::Walker"), walker_policy);
}

// Exit control-node without dumping any core-file.
void ControlNode::Exit(int status, bool do_assert) {
    rlimit new_core_limit;
    new_core_limit.rlim_cur = 0;
    new_core_limit.rlim_max = 0;
    setrlimit(RLIMIT_CORE, &new_core_limit);
    if (do_assert)
        assert(false);
    exit(status);
}

max_stars_repo_name: kaweue/contrail-controller

max_issues_repo_name: kaweue/contrail-controller

max_forks_repo_name: kaweue/contrail-controller


text:
/**
 *-----------------------------------------------------------------------------
 * Title         : PRBS Receive And Transmit Class
 * ----------------------------------------------------------------------------
 * File          : Prbs.cpp
 * Author        : Ryan Herbst <rherbst@slac.stanford.edu>
 * Created       : 09/17/2016
 * Last update   : 09/17/2016
 *-----------------------------------------------------------------------------
 * Description :
 *    Class used to generate and receive PRBS test data.
 *-----------------------------------------------------------------------------
 * This file is part of the rogue software platform. It is subject to
 * the license terms in the LICENSE.txt file found in the top-level directory
 * of this distribution and at:
    * https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
 * No part of the rogue software platform, including this file, may be
 * copied, modified, propagated, or distributed except according to the terms
 * contained in the LICENSE.txt file.
 *-----------------------------------------------------------------------------
**/
#include <unistd.h>
#include <stdarg.h>
#include <rogue/interfaces/stream/Slave.h>
#include <rogue/interfaces/stream/Master.h>
#include <rogue/interfaces/stream/Frame.h>
#include <rogue/interfaces/stream/FrameLock.h>
#include <rogue/interfaces/stream/FrameIterator.h>
#include <rogue/interfaces/stream/Buffer.h>
#include <rogue/utilities/Prbs.h>
#include <memory>
#include <rogue/GilRelease.h>
#include <rogue/GeneralError.h>
#include <rogue/Logging.h>
#include <rogue/GeneralError.h>
#include <sys/time.h>
#include <string.h>

namespace ris = rogue::interfaces::stream;
namespace ru  = rogue::utilities;

#ifndef NO_PYTHON
#define BOOST_BIND_GLOBAL_PLACEHOLDERS
#include <boost/python.hpp>
namespace bp = boost::python;
#endif

//! Class creation
ru::PrbsPtr ru::Prbs::create () {
   ru::PrbsPtr p = std::make_shared<ru::Prbs>();
   return(p);
}

//! Creator with default taps and size
ru::Prbs::Prbs() {
   txThread_   = NULL;
   rxSeq_      = 0;
   rxErrCount_ = 0;
   rxCount_    = 0;
   rxBytes_    = 0;
   rxEnable_   = true;
   txSeq_      = 0;
   txSize_     = 0;
   txErrCount_ = 0;
   txCount_    = 0;
   txBytes_    = 0;
   checkPl_    = true;
   genPl_      = true;
   rxLog_      = rogue::Logging::create("prbs.rx");
   txLog_      = rogue::Logging::create("prbs.tx");

   // Init width = 32
   width_     = 32;
   byteWidth_ = 4;
   minSize_   = 12;
   sendCount_ = false;

   // Init 4 taps
   tapCnt_  = 4;
   taps_    = (uint8_t *)malloc(sizeof(uint8_t)*tapCnt_);
   taps_[0] = 1;
   taps_[1] = 2;
   taps_[2] = 6;
   taps_[3] = 31;

   gettimeofday(&lastRxTime_,NULL);
   gettimeofday(&lastTxTime_,NULL);

   lastRxCount_ = 0;
   lastRxBytes_ = 0;
   rxRate_ = 0.0;
   rxBw_ = 0.0;

   lastTxCount_ = 0;
   lastTxBytes_ = 0;
   txRate_ = 0.0;
   txBw_ = 0.0;
}

//! Deconstructor
ru::Prbs::~Prbs() {
   free(taps_);
}

//! Compute period
double ru::Prbs::updateTime ( struct timeval *last ) {
   struct timeval cmp;
   struct timeval now;
   struct timeval per;
   double ret;

   cmp.tv_sec  = 1;
   cmp.tv_usec = 0;

   gettimeofday(&now,NULL);

   timersub(&now,last,&per);

   if ( timercmp(&per,&cmp,>) ) {
      ret = (float)per.tv_sec + (float(per.tv_usec) / 1e6);
      gettimeofday(last,NULL);
   }
   else ret = 0.0;

   return ret;
}

//! Set width
void ru::Prbs::setWidth(uint32_t width) {
   if ( (width > (MaxBytes*8)) || (width % 32) != 0)
      throw(rogue::GeneralError("Prbs::setWidth","Invalid width."));

   rogue::GilRelease noGil;
   std::lock_guard<std::mutex> lockT(pMtx_);

   width_     = width;
   byteWidth_ = width / 8;
   minSize_   = byteWidth_ * 3;
}

//! Set taps
void ru::Prbs::setTaps(uint32_t tapCnt, uint8_t * taps) {
   uint32_t i;

   std::lock_guard<std::mutex> lockT(pMtx_);

   free(taps_);
   tapCnt_ = tapCnt;
   taps_   = (uint8_t *)malloc(sizeof(uint8_t)*tapCnt);

   for (i=0; i < tapCnt_; i++) taps_[i] = taps[i];
}

#ifndef NO_PYTHON

//! Set taps, python
void ru::Prbs::setTapsPy(boost::python::object p) {
   Py_buffer pyBuf;

   if ( PyObject_GetBuffer(p.ptr(),&pyBuf,PyBUF_SIMPLE) < 0 )
      throw(rogue::GeneralError("Prbs::setTapsPy","Python Buffer Error"));

   setTaps(pyBuf.len,(uint8_t *)pyBuf.buf);
   PyBuffer_Release(&pyBuf);
}

#endif

//! Send counter value
void ru::Prbs::sendCount(bool state) {
   rogue::GilRelease noGil;
   std::lock_guard<std::mutex> lockT(pMtx_);

   sendCount_ = state;
}

void ru::Prbs::flfsr(uint8_t * data) {
   uint32_t iByte;
   uint32_t iBit;
   uint32_t x;
   bool msbOut;
   bool lsbIn = false;

   for (x=0; x < tapCnt_; x++) {
      iByte = taps_[x] / 8;
      iBit  = taps_[x] % 8;
      lsbIn ^= ((data[iByte] >> iBit) & 0x1);
   }

   for (x=0; x < byteWidth_; x++) {
      msbOut = data[x] & 0x80;
      data[x] <<= 1;
      data[x] |= lsbIn;
      lsbIn = msbOut;
   }
}

//! Thread background
void ru::Prbs::runThread() {
   txLog_->logThreadId();

   while(threadEn_) {
      genFrame(txSize_);
   }
}

//! Auto run data generation
void ru::Prbs::enable(uint32_t size) {

   // Verify size first
   if ((( size % byteWidth_ ) != 0) || size < minSize_ )
      throw rogue::GeneralError("Prbs::enable","Invalid frame size");

   if ( txThread_ == NULL ) {
      txSize_ = size;
      threadEn_ = true;
      txThread_ = new std::thread(&Prbs::runThread, this);

      // Set a thread name
#ifndef __MACH__
      pthread_setname_np( txThread_->native_handle(), "PrbsTx" );
#endif
   }
}

//! Disable auto generation
void ru::Prbs::disable() {
   if ( txThread_ != NULL ) {
      rogue::GilRelease noGil;
      threadEn_ = false;
      txThread_->join();
      delete txThread_;
      txThread_ = NULL;
   }
}

//! Get rx enable
bool ru::Prbs::getRxEnable() {
   return rxEnable_;
}

//! Set rx enable
void ru::Prbs::setRxEnable(bool en) {
   rogue::GilRelease noGil;
   pMtx_.lock();
   rxEnable_ = en;
   pMtx_.unlock();
}

//! Get RX errors
uint32_t ru::Prbs::getRxErrors() {
   return(rxErrCount_);
}

//! Get rx count
uint32_t ru::Prbs::getRxCount() {
   return(rxCount_);
}

//! Get rx bytes
uint32_t ru::Prbs::getRxBytes() {
   return(rxBytes_);
}

//! Get TX errors
uint32_t ru::Prbs::getTxErrors() {
   return(txErrCount_);
}

//! Get TX count
uint32_t ru::Prbs::getTxCount() {
   return(txCount_);
}

//! Get rx rate
double ru::Prbs::getRxRate() {
   return rxRate_;
}

//! Get rx bw
double ru::Prbs::getRxBw() {
   return rxBw_;
}

//! Get tx rate
double ru::Prbs::getTxRate() {
   return txRate_;
}

//! Get tx bw
double ru::Prbs::getTxBw() {
   return txBw_;
}

//! Get TX bytes
uint32_t ru::Prbs::getTxBytes() {
   return(txBytes_);
}

//! Set check payload flag, default = true
void ru::Prbs::checkPayload(bool state) {
   checkPl_ = state;
}

//! Set generate payload flag, default = true
void ru::Prbs::genPayload(bool state) {
   genPl_ = state;
}

//! Reset counters
// Counters should really be locked!
void ru::Prbs::resetCount() {
   pMtx_.lock();
   txErrCount_ = 0;
   txCount_    = 0;
   txBytes_    = 0;
   rxErrCount_ = 0;
   rxCount_    = 0;
   rxBytes_    = 0;
   pMtx_.unlock();
}

//! Generate a data frame
void ru::Prbs::genFrame (uint32_t size) {
   ris::FrameIterator frIter;
   ris::FrameIterator frEnd;
   uint32_t      frSeq[MaxBytes/4];
   uint32_t      frSize[MaxBytes/4];
   uint32_t      wCount[MaxBytes/4];
   uint8_t       data[MaxBytes];
   double        per;
   ris::FramePtr fr;

   rogue::GilRelease noGil;
   std::lock_guard<std::mutex> lock(pMtx_);

   // Verify size first
   if ((( size % byteWidth_ ) != 0) || size < minSize_ )
      throw rogue::GeneralError("Prbs::genFrame","Invalid frame size");

   // Setup size
   memset(frSize,0,MaxBytes);
   frSize[0] = (size / byteWidth_) - 1;

   // Setup sequence
   memset(frSeq,0,MaxBytes);
   frSeq[0]  = txSeq_;

   // Setup counter
   memset(wCount,0,MaxBytes);

   // Get frame
   fr = reqFrame(size,true);
   fr->setPayload(size);

   frIter = fr->begin();
   frEnd  = frIter + size;

   // First word is sequence
   ris::toFrame(frIter,byteWidth_,frSeq);
   ++wCount[0];

   // Second word is size
   ris::toFrame(frIter,byteWidth_,frSize);
   ++wCount[0];

   if ( genPl_ ) {

      // Init data
      std::memcpy(data,frSeq,byteWidth_);

      // Generate payload
      while ( frIter != frEnd ) {

         if ( sendCount_ ) ris::toFrame(frIter,byteWidth_,wCount);
         else {
            flfsr(data);
            ris::toFrame(frIter, byteWidth_, data);
         }
         ++wCount[0];
      }
   }

   sendFrame(fr);

   // Update counters
   txSeq_++;
   txCount_++;
   txBytes_ += size;

   if ( (per = updateTime(&lastTxTime_)) > 0.0 ) {
      txRate_ = (float)(txCount_ - lastTxCount_) / per;
      txBw_   = (float)(txBytes_ - lastTxBytes_) / per;
      lastTxCount_ = txCount_;
      lastTxBytes_ = txBytes_;
   }
}

//! Accept a frame from master
void ru::Prbs::acceptFrame ( ris::FramePtr frame ) {
   ris::FrameIterator frIter;
   ris::FrameIterator frEnd;
   uint32_t      frSeq[MaxBytes/4];
   uint32_t      frSize[MaxBytes/4];
   uint32_t      expSeq;
   uint32_t      expSize;
   uint32_t      size;
   uint32_t      pos;
   uint32_t      x;
   uint8_t       expData[MaxBytes];
   double        per;
   char          debugA[10000];
   char          debugB[1000];

   rogue::GilRelease noGil;

   while (not rxEnable_) usleep(10000);

   ris::FrameLockPtr fLock = frame->lock();
   std::lock_guard<std::mutex> lock(pMtx_);

   size = frame->getPayload();
   frIter = frame->begin();
   frEnd  = frame->end();

   // Check for frame errors
   if ( frame->getError() ) {
      rxLog_->warning("Frame error field is set: 0x%x",frame->getError());
      rxErrCount_++;
      return;
   }

   // Verify size
   if ((( size % byteWidth_ ) != 0) || size < minSize_ ) {
      rxLog_->warning("Size violation size=%i, count=%i",size,rxCount_);
      rxErrCount_++;
      return;
   }

   // First word is sequence
   ris::fromFrame(frIter,byteWidth_,frSeq);
   expSeq = rxSeq_;
   rxSeq_ = frSeq[0] + 1;

   // Second word is size
   ris::fromFrame(frIter,byteWidth_,frSize);
   expSize = (frSize[0] + 1) * byteWidth_;

   // Check size and sequence
   // Accept any sequence if our local count is zero
   // incoming frames with seq = 0 never cause errors and treated as a restart
   if ( ( expSize != size ) || ( frSeq[0] != 0 && expSeq != 0 && frSeq[0] != expSeq ) ) {
      rxLog_->warning("Bad header. expSize=%u gotSize=%u expSeq=%u gotSeq=%u nxtSeq=%u count=%i",
            expSize,size,expSeq,frSeq[0],rxSeq_,rxCount_);
      rxErrCount_++;
      return;
   }

   // Is payload checking enabled
   if ( checkPl_ ) {

      // Init data
      std::memcpy(expData,frSeq,byteWidth_);
      pos = 0;

      // Read payload
      while ( frIter != frEnd ) {
         flfsr(expData);

         if ( ! std::equal(frIter,frIter+byteWidth_,expData ) ) {
            sprintf(debugA,"Bad value at index %i. count=%i, size=%i",pos,rxCount_,(size/byteWidth_)-1);
            for (x=0; x < byteWidth_; x++) {
               sprintf(debugB,"\n   %i:%i Got=0x%x Exp=0x%x",pos,x,*(frIter+x),*(expData+x));
               strcat(debugA,debugB);
            }
            rxLog_->warning(debugA);
            rxErrCount_++;
            return;
         }
         frIter += byteWidth_;
         ++pos;
      }
   }

   rxCount_++;
   rxBytes_ += size;

   if ( (per = updateTime(&lastRxTime_)) > 0.0 ) {
      rxRate_ = (float)(rxCount_ - lastRxCount_) / per;
      rxBw_   = (float)(rxBytes_ - lastRxBytes_) / per;
      lastRxCount_ = rxCount_;
      lastRxBytes_ = rxBytes_;
   }
}

void ru::Prbs::setup_python() {
#ifndef NO_PYTHON

   bp::class_<ru::Prbs, ru::PrbsPtr, bp::bases<ris::Master,ris::Slave>, boost::noncopyable >("Prbs",bp::init<>())
      .def("genFrame",       &ru::Prbs::genFrame)
      .def("enable",         &ru::Prbs::enable)
      .def("disable",        &ru::Prbs::disable)
      .def("setWidth",       &ru::Prbs::setWidth)
      .def("setTaps",        &ru::Prbs::setTaps)
      .def("getRxEnable",    &ru::Prbs::getRxEnable)
      .def("setRxEnable",    &ru::Prbs::setRxEnable)
      .def("getRxErrors",    &ru::Prbs::getRxErrors)
      .def("getRxCount",     &ru::Prbs::getRxCount)
      .def("getRxRate",      &ru::Prbs::getRxRate)
      .def("getRxBw",        &ru::Prbs::getRxBw)
      .def("getRxBytes",     &ru::Prbs::getRxBytes)
      .def("getTxErrors",    &ru::Prbs::getTxErrors)
      .def("getTxCount",     &ru::Prbs::getTxCount)
      .def("getTxBytes",     &ru::Prbs::getTxBytes)
      .def("getTxRate",      &ru::Prbs::getTxRate)
      .def("getTxBw",        &ru::Prbs::getTxBw)
      .def("checkPayload",   &ru::Prbs::checkPayload)
      .def("genPayload",     &ru::Prbs::genPayload)
      .def("resetCount",     &ru::Prbs::resetCount)
      .def("sendCount",      &ru::Prbs::sendCount)
   ;

   bp::implicitly_convertible<ru::PrbsPtr, ris::SlavePtr>();
   bp::implicitly_convertible<ru::PrbsPtr, ris::MasterPtr>();
#endif
}

max_stars_repo_name: slacrherbst/rogue

max_issues_repo_name: slacrherbst/rogue

max_forks_repo_name: slacrherbst/rogue


text:
//          Copyright Gavin Band 2008 - 2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <vector>
#include <set>
#include <fstream>
#include <boost/variant/variant.hpp>
#include <boost/bind.hpp>
#include "genfile/CohortIndividualSource.hpp"
#include "genfile/FromFileCohortIndividualSource.hpp"
#include "genfile/TraditionalStrictCohortIndividualSource.hpp"

namespace genfile {
	// Read a sample file in the format described here:
	// http://www.stats.ox.ac.uk/%7Emarchini/software/gwas/file_format_new.html
	TraditionalStrictCohortIndividualSource::TraditionalStrictCohortIndividualSource( std::string const& filename, std::string const& missing_values ):
		FromFileCohortIndividualSource(
			filename,
			string_utils::split_and_strip_discarding_empty_entries( missing_values, ",", " \t" ),
			&TraditionalStrictCohortIndividualSource::get_entry_from_string
		)
	{
		check_sample_ids() ;
	}

	TraditionalStrictCohortIndividualSource::TraditionalStrictCohortIndividualSource( std::istream& stream, std::string const& missing_values ):
		FromFileCohortIndividualSource(
			stream,
			string_utils::split_and_strip_discarding_empty_entries( missing_values, ",", " \t" ),
			&TraditionalStrictCohortIndividualSource::get_entry_from_string
		)
	{
		check_sample_ids() ;
	}

	CohortIndividualSource::Entry TraditionalStrictCohortIndividualSource::get_entry_from_string( std::string const& entry_as_string, ColumnType column_type ) {
		switch( column_type ) {
			case e_ID_COLUMN:
				return Entry( entry_as_string ) ;
				break ;
			case e_MISSINGNESS_COLUMN:
			case e_CONTINUOUS_COVARIATE:
			case e_CONTINUOUS_PHENOTYPE:
				return Entry( string_utils::to_repr< double >( entry_as_string )) ;
				break ;
			case e_DISCRETE_COVARIATE: {
				int value = string_utils::to_repr< int >( entry_as_string ) ;
				if( value <= 0 ) {
					throw string_utils::StringConversionError() ;
				}
				return Entry( value ) ;
				break ;
			}
			case e_BINARY_PHENOTYPE: {
				int value = string_utils::to_repr< int >( entry_as_string ) ;
				if( value != 0 && value != 1 ) {
					throw string_utils::StringConversionError() ;
				}
				return Entry( value ) ;
				break ;
			}
			default:
				assert(0) ;
		}
	}

	void TraditionalStrictCohortIndividualSource::check_sample_ids() const {
		// each of id_1 and id_2 fields should be unique.
		{
			std::set< std::string > sample_ids ;
			for( std::size_t i = 0; i < get_number_of_individuals(); ++i ) {
				std::string id = get_entry( i, "id_1" ).as< std::string >() ;
				if( sample_ids.find( id ) != sample_ids.end() ) {
					throw DuplicateIndividualError( get_filename(), id, get_entry( i, "id_2" ).as< std::string >(), i + 2 ) ;
				}
				sample_ids.insert( id ) ;
			}
		}

		{
			std::set< std::string > sample_ids ;
			for( std::size_t i = 0; i < get_number_of_individuals(); ++i ) {
				std::string id = get_entry( i, "id_2" ).as< std::string >() ;
				if( sample_ids.find( id ) != sample_ids.end() ) {
					throw DuplicateIndividualError( get_filename(),  get_entry( i, "id_1" ).as< std::string >(), id, i + 2 ) ;
				}
				sample_ids.insert( id ) ;
			}
		}
	}

}

max_stars_repo_name: gavinband/bingwa

max_issues_repo_name: gavinband/bingwa

max_forks_repo_name: gavinband/qctool


text:
//==============================================================================
//         Copyright 2009 - 2013 LRI    UMR 8623 CNRS/Univ Paris Sud XI
//         Copyright 2012 - 2013 MetaScale SAS
//
//          Distributed under the Boost Software License, Version 1.0.
//                 See accompanying file LICENSE.txt or copy at
//                     http://www.boost.org/LICENSE_1_0.txt
//==============================================================================
#ifndef BOOST_SIMD_BITWISE_FUNCTIONS_SIMD_COMMON_RROL_HPP_INCLUDED
#define BOOST_SIMD_BITWISE_FUNCTIONS_SIMD_COMMON_RROL_HPP_INCLUDED

#include <boost/simd/bitwise/functions/rrol.hpp>
#include <boost/simd/include/functions/simd/bitwise_cast.hpp>
#include <boost/simd/include/functions/simd/unary_minus.hpp>
#include <boost/simd/include/functions/simd/if_else.hpp>
#include <boost/simd/include/functions/simd/is_gtz.hpp>
#include <boost/simd/include/functions/simd/ror.hpp>
#include <boost/simd/include/functions/simd/rol.hpp>
#include <boost/simd/sdk/meta/cardinal_of.hpp>
#include <boost/mpl/equal_to.hpp>

#ifndef NDEBUG
#include <boost/simd/include/functions/simd/max.hpp>
#include <boost/simd/include/constants/zero.hpp>
#endif

namespace boost { namespace simd { namespace ext
{
  BOOST_DISPATCH_IMPLEMENT_IF         ( rrol_, tag::cpu_
                                      , (A0)(A1)(X)
                                      , (boost::mpl::equal_to
                                          < boost::simd::meta::cardinal_of<A0>
                                          , boost::simd::meta::cardinal_of<A1>
                                          >
                                        )
                                      , ((simd_< arithmetic_<A0>, X >))
                                        ((simd_< int_<A1>, X >))
                                      )
  {
    typedef A0 result_type;
    BOOST_FORCEINLINE BOOST_SIMD_FUNCTOR_CALL(2)
    {
      #ifndef NDEBUG
      return bitwise_cast<result_type>( if_else ( is_gtz(a1)
                                                , rol ( bitwise_cast<A1>(a0)
                                                      , max(Zero<A1>(),a1)
                                                      )
                                                , ror ( bitwise_cast<A1>(a0)
                                                      , max(Zero<A1>(),-a1)
                                                      )
                                                )
                                      );
      #else
      return bitwise_cast<result_type>( if_else ( is_gtz(a1)
                                                , rol(bitwise_cast<A1>(a0), a1)
                                                , ror(bitwise_cast<A1>(a0),-a1)
                                                )
                                      );
      #endif
    }
  };

  BOOST_DISPATCH_IMPLEMENT_IF         ( rrol_, tag::cpu_
                                      , (A0)(A1)(X)
                                      , (boost::mpl::equal_to
                                          < boost::simd::meta::cardinal_of<A0>
                                          , boost::simd::meta::cardinal_of<A1>
                                          >
                                        )
                                      , ((simd_< arithmetic_<A0>, X >))
                                        ((simd_< uint_<A1>, X >))
                                      )
  {
    typedef A0 result_type;

    BOOST_FORCEINLINE BOOST_SIMD_FUNCTOR_CALL(2)
    {
      return bitwise_cast<result_type>( rol( bitwise_cast<A1>(a0), a1 ) );
    }
  };
} } }

#endif


text:
//==============================================================================
//         Copyright 2003 - 2012   LASMEA UMR 6602 CNRS/Univ. Clermont II
//         Copyright 2009 - 2012   LRI    UMR 8623 CNRS/Univ Paris Sud XI
//
//          Distributed under the Boost Software License, Version 1.0.
//                 See accompanying file LICENSE.txt or copy at
//                     http://www.boost.org/LICENSE_1_0.txt
//==============================================================================
#ifndef BOOST_SIMD_SDK_SIMD_META_IS_NATIVE_HPP_INCLUDED
#define BOOST_SIMD_SDK_SIMD_META_IS_NATIVE_HPP_INCLUDED

#include <boost/mpl/bool.hpp>
#include <boost/simd/sdk/simd/native_fwd.hpp>

namespace boost { namespace simd { namespace meta
{
  template<class T> struct is_native : boost::mpl::false_ {};

  template<class T, class X>
  struct is_native<boost::simd::native<T,X> >: boost::mpl::true_ {};
} } }

#endif