text:
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2014-2019 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "base58.h"
#include "clientversion.h"
#include "init.h"
#include "key_io.h"
#include "net.h"
#include "netbase.h"
#include "rpc/server.h"
#include "timedata.h"
#include "txmempool.h"
#include "util/system.h"
#include "util/strencodings.h"
#include "validation.h"
#ifdef ENABLE_WALLET
#include "wallet/rpcwallet.h"
#include "wallet/wallet.h"
#include "wallet/walletdb.h"
#endif

#include "masternode-sync.h"
#include "spork.h"

#include <stdint.h>

#include <boost/assign/list_of.hpp>
#include <boost/algorithm/string.hpp>

#include <univalue.h>

extern int64_t nLastCoinStakeSearchInterval;

class CWallet;

/*
    Used for updating/reading spork settings on the network
*/
UniValue spork(const JSONRPCRequest& request)
{
    if (request.params.size() == 1) {
        // basic mode, show info
        std:: string strCommand = request.params[0].get_str();
        if (strCommand == "show") {
            UniValue ret(UniValue::VOBJ);
            for (const auto& sporkDef : sporkDefs) {
                ret.push_back(Pair(sporkDef.name, sporkManager.GetSporkValue(sporkDef.sporkId)));
            }
            return ret;
        } else if(strCommand == "active"){
            UniValue ret(UniValue::VOBJ);
            for (const auto& sporkDef : sporkDefs) {
                ret.push_back(Pair(sporkDef.name, sporkManager.IsSporkActive(sporkDef.sporkId)));
            }
            return ret;
        }
    }

    if (request.fHelp || request.params.size() != 2) {
        // default help, for basic mode
        throw std::runtime_error(
            "spork \"command\"\n"
            "\nShows information about current state of sporks\n"
            "\nArguments:\n"
            "1. \"command\"                     (string, required) 'show' to show all current spork values, 'active' to show which sporks are active\n"
            "\nResult:\n"
            "For 'show':\n"
            "{\n"
            "  \"SPORK_NAME\" : spork_value,    (number) The value of the specific spork with the name SPORK_NAME\n"
            "  ...\n"
            "}\n"
            "For 'active':\n"
            "{\n"
            "  \"SPORK_NAME\" : true|false,     (boolean) 'true' for time-based sporks if spork is active and 'false' otherwise\n"
            "  ...\n"
            "}\n"
            "\nExamples:\n"
            + HelpExampleCli("spork", "show")
            + HelpExampleRpc("spork", "\"show\""));
    } else {
        // advanced mode, update spork values
        SporkId nSporkID = sporkManager.GetSporkIDByName(request.params[0].get_str());
        if(nSporkID == SPORK_INVALID)
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid spork name");

        if (!g_connman)
            throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");

        // SPORK VALUE
        int64_t nValue = request.params[1].get_int64();

        //broadcast new spork
        if(sporkManager.UpdateSpork(nSporkID, nValue, *g_connman)){
            return "success";
        } else {
            throw std::runtime_error(
                "spork \"name\" value\n"
                "\nUpdate the value of the specific spork. Requires \"-sporkkey\" to be set to sign the message.\n"
                "\nArguments:\n"
                "1. \"name\"              (string, required) The name of the spork to update\n"
                "2. value               (number, required) The new desired value of the spork\n"
                "\nResult:\n"
                "  result               (string) \"success\" if spork value was updated or this help otherwise\n"
                "\nExamples:\n"
                + HelpExampleCli("spork", "SPORK_2_INSTANTSEND_ENABLED 4070908800")
                + HelpExampleRpc("spork", "\"SPORK_2_INSTANTSEND_ENABLED\", 4070908800"));
        }
    }

}

UniValue mnsync(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() != 1)
        throw std::runtime_error(
            "mnsync [status|next|reset]\n"
            "Returns the sync status, updates to the next step or resets it entirely.\n"
        );

    std::string strMode = request.params[0].get_str();

    if(strMode == "status") {
        UniValue objStatus(UniValue::VOBJ);
        objStatus.push_back(Pair("AssetID", masternodeSync.GetAssetID()));
        objStatus.push_back(Pair("AssetName", masternodeSync.GetAssetName()));
        objStatus.push_back(Pair("AssetStartTime", masternodeSync.GetAssetStartTime()));
        objStatus.push_back(Pair("Attempt", masternodeSync.GetAttempt()));
        objStatus.push_back(Pair("IsBlockchainSynced", masternodeSync.IsBlockchainSynced()));
        objStatus.push_back(Pair("IsSynced", masternodeSync.IsSynced()));
        objStatus.push_back(Pair("IsFailed", masternodeSync.IsFailed()));
        return objStatus;
    }

    if(strMode == "next")
    {
        masternodeSync.SwitchToNextAsset(*g_connman);
        return "sync updated to " + masternodeSync.GetAssetName();
    }

    if(strMode == "reset")
    {
        masternodeSync.Reset();
        masternodeSync.SwitchToNextAsset(*g_connman);
        return "success";
    }
    return "failure";
}

#ifdef ENABLE_WALLET
class DescribeAddressVisitor : public boost::static_visitor<UniValue>
{
public:
    CWallet * const pwallet;

    DescribeAddressVisitor(CWallet *_pwallet) : pwallet(_pwallet) {}

    UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); }

    UniValue operator()(const CKeyID &keyID) const {
        UniValue obj(UniValue::VOBJ);
        CPubKey vchPubKey;
        obj.push_back(Pair("isscript", false));
        if (pwallet && pwallet->GetPubKey(keyID, vchPubKey)) {
            obj.push_back(Pair("pubkey", HexStr(vchPubKey)));
            obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed()));
        }
        return obj;
    }

    UniValue operator()(const CScriptID &scriptID) const {
        UniValue obj(UniValue::VOBJ);
        CScript subscript;
        obj.push_back(Pair("isscript", true));
        if (pwallet && pwallet->GetCScript(scriptID, subscript)) {
            std::vector<CTxDestination> addresses;
            txnouttype whichType;
            int nRequired;
            ExtractDestinations(subscript, whichType, addresses, nRequired);
            obj.push_back(Pair("script", GetTxnOutputType(whichType)));
            obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end())));
            UniValue a(UniValue::VARR);
            BOOST_FOREACH(const CTxDestination& addr, addresses)
                a.push_back(CBitcoinAddress(addr).ToString());
            obj.push_back(Pair("addresses", a));
            if (whichType == TX_MULTISIG)
                obj.push_back(Pair("sigsrequired", nRequired));
        }
        return obj;
    }
};
#endif

/**
 * Used by addmultisigaddress / createmultisig:
 */
CScript _createmultisig_redeemScript(CWallet * const pwallet, const UniValue& params)
{
    int nRequired = params[0].get_int();
    const UniValue& keys = params[1].get_array();

    // Gather public keys
    if (nRequired < 1)
        throw std::runtime_error("a multisignature address must require at least one key to redeem");
    if ((int)keys.size() < nRequired)
        throw std::runtime_error(
            strprintf("not enough keys supplied "
                      "(got %u keys, but need at least %d to redeem)", keys.size(), nRequired));
    if (keys.size() > 16)
        throw std::runtime_error("Number of addresses involved in the multisignature address creation > 16\nReduce the number");
    std::vector<CPubKey> pubkeys;
    pubkeys.resize(keys.size());
    for (unsigned int i = 0; i < keys.size(); i++)
    {
        const std::string& ks = keys[i].get_str();
#ifdef ENABLE_WALLET
        // Case 1: Dash address and we have full public key:
        CBitcoinAddress address(ks);
        if (pwallet && address.IsValid()) {
            CKeyID keyID;
            if (!address.GetKeyID(keyID))
                throw std::runtime_error(
                    strprintf("%s does not refer to a key",ks));
            CPubKey vchPubKey;
            if (!pwallet->GetPubKey(keyID, vchPubKey)) {
                throw std::runtime_error(
                    strprintf("no full public key for address %s",ks));
            }
            if (!vchPubKey.IsFullyValid())
                throw std::runtime_error(" Invalid public key: "+ks);
            pubkeys[i] = vchPubKey;
        }

        // Case 2: hex public key
        else
#endif
        if (IsHex(ks))
        {
            CPubKey vchPubKey(ParseHex(ks));
            if (!vchPubKey.IsFullyValid())
                throw std::runtime_error(" Invalid public key: "+ks);
            pubkeys[i] = vchPubKey;
        }
        else
        {
            throw std::runtime_error(" Invalid public key: "+ks);
        }
    }
    CScript result = GetScriptForMultisig(nRequired, pubkeys);

    if (result.size() > MAX_SCRIPT_ELEMENT_SIZE)
        throw std::runtime_error(
                strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE));

    return result;
}

UniValue createmultisig(const JSONRPCRequest& request)
{
#ifdef ENABLE_WALLET
    std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
    CWallet * const pwallet = wallet.get();
#else
    CWallet * const pwallet = NULL;
#endif

    if (request.fHelp || request.params.size() < 2 || request.params.size() > 2)
    {
        std::string msg = "createmultisig nrequired [\"key\",...]\n"
            "\nCreates a multi-signature address with n signature of m keys required.\n"
            "It returns a json object with the address and redeemScript.\n"

            "\nArguments:\n"
            "1. nrequired      (numeric, required) The number of required signatures out of the n keys or addresses.\n"
            "2. \"keys\"       (string, required) A json array of keys which are zentoshi addresses or hex-encoded public keys\n"
            "     [\n"
            "       \"key\"    (string) zentoshi address or hex-encoded public key\n"
            "       ,...\n"
            "     ]\n"

            "\nResult:\n"
            "{\n"
            "  \"address\":\"multisigaddress\",  (string) The value of the new multisig address.\n"
            "  \"redeemScript\":\"script\"       (string) The string value of the hex-encoded redemption script.\n"
            "}\n"

            "\nExamples:\n"
            "\nCreate a multisig address from 2 addresses\n"
            + HelpExampleCli("createmultisig", "2 \"[\\\"Xt4qk9uKvQYAonVGSZNXqxeDmtjaEWgfrs\\\",\\\"XoSoWQkpgLpppPoyyzbUFh1fq2RBvW6UK1\\\"]\"") +
            "\nAs a json rpc call\n"
            + HelpExampleRpc("createmultisig", "2, \"[\\\"Xt4qk9uKvQYAonVGSZNXqxeDmtjaEWgfrs\\\",\\\"XoSoWQkpgLpppPoyyzbUFh1fq2RBvW6UK1\\\"]\"")
        ;
        throw std::runtime_error(msg);
    }

    // Construct using pay-to-script-hash:
    CScript inner = _createmultisig_redeemScript(pwallet, request.params);
    CScriptID innerID(inner);
    CBitcoinAddress address(innerID);

    UniValue result(UniValue::VOBJ);
    result.push_back(Pair("address", address.ToString()));
    result.push_back(Pair("redeemScript", HexStr(inner.begin(), inner.end())));

    return result;
}

UniValue verifymessage(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() != 3)
        throw std::runtime_error(
            "verifymessage \"address\" \"signature\" \"message\"\n"
            "\nVerify a signed message\n"
            "\nArguments:\n"
            "1. \"address\"         (string, required) The zentoshi address to use for the signature.\n"
            "2. \"signature\"       (string, required) The signature provided by the signer in base 64 encoding (see signmessage).\n"
            "3. \"message\"         (string, required) The message that was signed.\n"
            "\nResult:\n"
            "true|false   (boolean) If the signature is verified or not.\n"
            "\nExamples:\n"
            "\nUnlock the wallet for 30 seconds\n"
            + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") +
            "\nCreate the signature\n"
            + HelpExampleCli("signmessage", "\"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\" \"my message\"") +
            "\nVerify the signature\n"
            + HelpExampleCli("verifymessage", "\"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\" \"signature\" \"my message\"") +
            "\nAs json rpc\n"
            + HelpExampleRpc("verifymessage", "\"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\", \"signature\", \"my message\"")
        );

    LOCK(cs_main);

    std::string strAddress  = request.params[0].get_str();
    std::string strSign     = request.params[1].get_str();
    std::string strMessage  = request.params[2].get_str();

    CBitcoinAddress addr(strAddress);
    if (!addr.IsValid())
        throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");

    CKeyID keyID;
    if (!addr.GetKeyID(keyID))
        throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");

    bool fInvalid = false;
    std::vector<unsigned char> vchSig = DecodeBase64(strSign.c_str(), &fInvalid);

    if (fInvalid)
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding");

    CHashWriter ss(SER_GETHASH, 0);
    ss << strMessageMagic;
    ss << strMessage;

    CPubKey pubkey;
    CPubKey::InputScriptType inputScriptType;
    if (!pubkey.RecoverCompact(ss.GetHash(), vchSig, inputScriptType))
        return false;

    return (pubkey.GetID() == keyID);
}

UniValue signmessagewithprivkey(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() != 2)
        throw std::runtime_error(
            "signmessagewithprivkey \"privkey\" \"message\"\n"
            "\nSign a message with the private key of an address\n"
            "\nArguments:\n"
            "1. \"privkey\"         (string, required) The private key to sign the message with.\n"
            "2. \"message\"         (string, required) The message to create a signature of.\n"
            "\nResult:\n"
            "\"signature\"          (string) The signature of the message encoded in base 64\n"
            "\nExamples:\n"
            "\nCreate the signature\n"
            + HelpExampleCli("signmessagewithprivkey", "\"privkey\" \"my message\"") +
            "\nVerify the signature\n"
            + HelpExampleCli("verifymessage", "\"XwnLY9Tf7Zsef8gMGL2fhWA9ZmMjt4KPwG\" \"signature\" \"my message\"") +
            "\nAs json rpc\n"
            + HelpExampleRpc("signmessagewithprivkey", "\"privkey\", \"my message\"")
        );

    std::string strPrivkey = request.params[0].get_str();
    std::string strMessage = request.params[1].get_str();

    CBitcoinSecret vchSecret;
    bool fGood = vchSecret.SetString(strPrivkey);
    if (!fGood)
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
    CKey key = vchSecret.GetKey();
    if (!key.IsValid())
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");

    CHashWriter ss(SER_GETHASH, 0);
    ss << strMessageMagic;
    ss << strMessage;

    std::vector<unsigned char> vchSig;
    if (!key.SignCompact(ss.GetHash(), vchSig))
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");

    return EncodeBase64(&vchSig[0], vchSig.size());
}

UniValue setmocktime(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() != 1)
        throw std::runtime_error(
            "setmocktime timestamp\n"
            "\nSet the local time to given timestamp (-regtest only)\n"
            "\nArguments:\n"
            "1. timestamp  (integer, required) Unix seconds-since-epoch timestamp\n"
            "   Pass 0 to go back to using the system time."
        );

    if (!Params().MineBlocksOnDemand())
        throw std::runtime_error("setmocktime for regression testing (-regtest mode) only");

    // For now, don't change mocktime if we're in the middle of validation, as
    // this could have an effect on mempool time-based eviction, as well as
    // IsCurrentForFeeEstimation() and IsInitialBlockDownload().
    // TODO: figure out the right way to synchronize around mocktime, and
    // ensure all call sites of GetTime() are accessing this safely.
    LOCK(cs_main);

    RPCTypeCheck(request.params, boost::assign::list_of(UniValue::VNUM));
    SetMockTime(request.params[0].get_int64());

    return NullUniValue;
}

static UniValue RPCLockedMemoryInfo()
{
    LockedPool::Stats stats = LockedPoolManager::Instance().stats();
    UniValue obj(UniValue::VOBJ);
    obj.push_back(Pair("used", uint64_t(stats.used)));
    obj.push_back(Pair("free", uint64_t(stats.free)));
    obj.push_back(Pair("total", uint64_t(stats.total)));
    obj.push_back(Pair("locked", uint64_t(stats.locked)));
    obj.push_back(Pair("chunks_used", uint64_t(stats.chunks_used)));
    obj.push_back(Pair("chunks_free", uint64_t(stats.chunks_free)));
    return obj;
}

UniValue getmemoryinfo(const JSONRPCRequest& request)
{
    /* Please, avoid using the word "pool" here in the RPC interface or help,
     * as users will undoubtedly confuse it with the other "memory pool"
     */
    if (request.fHelp || request.params.size() != 0)
        throw std::runtime_error(
            "getmemoryinfo\n"
            "Returns an object containing information about memory usage.\n"
            "\nResult:\n"
            "{\n"
            "  \"locked\": {               (json object) Information about locked memory manager\n"
            "    \"used\": xxxxx,          (numeric) Number of bytes used\n"
            "    \"free\": xxxxx,          (numeric) Number of bytes available in current arenas\n"
            "    \"total\": xxxxxxx,       (numeric) Total number of bytes managed\n"
            "    \"locked\": xxxxxx,       (numeric) Amount of bytes that succeeded locking. If this number is smaller than total, locking pages failed at some point and key data could be swapped to disk.\n"
            "    \"chunks_used\": xxxxx,   (numeric) Number allocated chunks\n"
            "    \"chunks_free\": xxxxx,   (numeric) Number unused chunks\n"
            "  }\n"
            "}\n"
            "\nExamples:\n"
            + HelpExampleCli("getmemoryinfo", "")
            + HelpExampleRpc("getmemoryinfo", "")
        );
    UniValue obj(UniValue::VOBJ);
    obj.push_back(Pair("locked", RPCLockedMemoryInfo()));
    return obj;
}

UniValue echo(const JSONRPCRequest& request)
{
    if (request.fHelp)
        throw std::runtime_error(
            "echo|echojson \"message\" ...\n"
            "\nSimply echo back the input arguments. This command is for testing.\n"
            "\nThe difference between echo and echojson is that echojson has argument conversion enabled in the client-side table in"
            "bitcoin-cli and the GUI. There is no server-side difference."
        );

    return request.params;
}

UniValue getstakingstatus(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() != 0)
        throw std::runtime_error(
            "getstakingstatus\n"
            "Returns an object containing various staking information.\n"
            "\nResult:\n"
            "{\n"
            "  \"validtime\": true|false,          (boolean) if the chain tip is within staking phases\n"
            "  \"haveconnections\": true|false,    (boolean) if network connections are present\n"
            "  \"walletunlocked\": true|false,     (boolean) if the wallet is unlocked\n"
            "  \"mintablecoins\": true|false,      (boolean) if the wallet has mintable coins\n"
            "  \"enoughcoins\": true|false,        (boolean) if available coins are greater than reserve balance\n"
            "  \"mnsync\": true|false,             (boolean) if masternode data is synced\n"
            "  \"staking status\": true|false,     (boolean) if the wallet is staking or not\n"
            "}\n"
            "\nExamples:\n" +
            HelpExampleCli("getstakingstatus", "") + HelpExampleRpc("getstakingstatus", ""));

    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);

    UniValue obj(UniValue::VOBJ);
    obj.pushKV("validtime", true);
    obj.pushKV("haveconnections", g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) > 0);
    if (pwallet) {
        obj.pushKV("walletunlocked", !pwallet->IsLocked());
        obj.pushKV("mintablecoins", pwallet->MintableCoins());
        obj.pushKV("enoughcoins", pwallet->GetBalance() > 0);
    }
    obj.pushKV("mnsync", masternodeSync.IsSynced());

    bool nStaking = false;

    if (nLastCoinStakeSearchInterval > 0)
        nStaking = true;

    obj.pushKV("staking status", nStaking);

    return obj;
}

static const CRPCCommand commands[] =
{ //  category              name                      actor (function)         okSafeMode
  //  --------------------- ------------------------  -----------------------  ----------
    { "control",            "getmemoryinfo",          &getmemoryinfo,          {} },
    { "util",               "createmultisig",         &createmultisig,         {"nrequired","keys"} },
    { "util",               "verifymessage",          &verifymessage,          {"address","signature","message"} },
    { "util",               "signmessagewithprivkey", &signmessagewithprivkey, {"privkey","message"} },
    { "util",               "getstakingstatus",       &getstakingstatus,       {} },

    /* Dash features */
    { "zentoshi",           "mnsync",                 &mnsync,                 {} },
    { "zentoshi",           "spork",                   &spork,                    {"mode"} },

    /* Not shown in help */
    { "hidden",             "setmocktime",            &setmocktime,            {"timestamp"}},
    { "hidden",             "echo",                   &echo,                   {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
    { "hidden",             "echojson",               &echo,                   {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
};

void RegisterMiscRPCCommands(CRPCTable &t)
{
    for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
        t.appendCommand(commands[vcidx].name, &commands[vcidx]);
}


text:
//==================================================================================================
/*!

  Copyright 2015 NumScale SAS

  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/simd/function/scalar/is_eq.hpp>
#include <simd_test.hpp>
#include <boost/dispatch/meta/as_integer.hpp>
#include <boost/simd/constant/inf.hpp>
#include <boost/simd/constant/minf.hpp>
#include <boost/simd/constant/mone.hpp>
#include <boost/simd/constant/nan.hpp>
#include <boost/simd/constant/one.hpp>
#include <boost/simd/constant/zero.hpp>
#include <boost/simd/constant/true.hpp>
#include <boost/simd/constant/false.hpp>
#include <boost/simd/logical.hpp>

STF_CASE_TPL (" is_eq integer",  STF_INTEGRAL_TYPES)
{
  namespace bs = boost::simd;
  using bs::is_eq;
  using r_t = decltype(is_eq(T(), T()));

   // return type conformity test
  STF_TYPE_IS(r_t,bs::logical<T>);

  // specific values tests
  STF_EQUAL(is_eq(bs::One<T>(), bs::One<T>()), r_t(true));
  STF_EQUAL(is_eq(bs::One<T>(),bs::Zero<T>()), r_t(false));
  STF_EQUAL(is_eq(bs::Zero<T>(), bs::Zero<T>()), r_t(true));
} // end of test for integer_

STF_CASE_TPL (" is_eq real",  STF_IEEE_TYPES)
{
  namespace bs = boost::simd;
  using bs::is_eq;
  using r_t = decltype(is_eq(T(), T()));

   // return type conformity test
  STF_TYPE_IS(r_t,bs::logical<T>);

  // specific values tests
  STF_EQUAL(is_eq(bs::Inf<T>(), bs::Inf<T>()), r_t(true));
  STF_EQUAL(is_eq(bs::Minf<T>(), bs::Minf<T>()), r_t(true));
  STF_EQUAL(is_eq(bs::Nan<T>(), bs::Nan<T>()), r_t(false));
  STF_EQUAL(is_eq(bs::One<T>(),bs::Zero<T>()), r_t(false));
  STF_EQUAL(is_eq(bs::Zero<T>(), bs::Zero<T>()), r_t(true));
} // end of test for floating_

STF_CASE_TPL (" is_eq logical",  STF_IEEE_TYPES)
{
  namespace bs = boost::simd;

  using bs::is_eq;
  using r_t = decltype(is_eq(T(), T()));

  // return type conformity test
  STF_TYPE_IS(r_t, bs::logical<T>);

  STF_EQUAL(is_eq(bs::True< bs::logical<T> >(), bs::True< bs::logical<T> >()), r_t(true));
  STF_EQUAL(is_eq(bs::False< bs::logical<T> >(), bs::False< bs::logical<T> >()), r_t(true));
  STF_EQUAL(is_eq(bs::True< bs::logical<T> >(), bs::False< bs::logical<T> >()), r_t(false));
  STF_EQUAL(is_eq(bs::False< bs::logical<T> >(), bs::True< bs::logical<T> >()), r_t(false));
}

STF_CASE ( "is_eq bool")
{
  namespace bs = boost::simd;
  using bs::is_eq;

  // return type conformity test
  STF_EXPR_IS(is_eq(bool(), bool()), bool);

  // specific values tests
  STF_EQUAL(is_eq(true, false), false);
  STF_EQUAL(is_eq(false, true), false);
  STF_EQUAL(is_eq(true, true), true);
  STF_EQUAL(is_eq(false, false), true);
}

ext: cpp


text:
//---------------------------------------------------------- -*- Mode: C++ -*-
// $Id$
//
// Created 2008/08/27
//
// Author: Sriram Rao
//
// Copyright 2008-2012 Quantcast Corp.
//
// This file is part of Kosmos File System (KFS).
//
// Licensed under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// \brief Emulator for the layout manager: read in a chunk->location
// map; we can then migrate blocks around to experiment with placement algorithms.
//
//----------------------------------------------------------------------------

#include "LayoutEmulator.h"
#include "ChunkServerEmulator.h"
#include "common/kfstypes.h"
#include "common/MsgLogger.h"
#include "common/RequestParser.h"
#include "common/StBuffer.h"
#include "meta/kfstree.h"
#include "meta/util.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <algorithm>
#include <cerrno>
#include <fstream>
#include <boost/bind.hpp>

namespace KFS
{

using std::string;
using std::ifstream;
using std::for_each;
using std::ofstream;
using boost::bind;

static inline ChunkServerEmulator&
GetCSEmulator(ChunkServer& server)
{
    return static_cast<ChunkServerEmulator&>(server);
}

int
LayoutEmulator::LoadChunkmap(
    const string& chunkLocationFn, bool addChunksToReplicationChecker)
{
    ifstream file(chunkLocationFn.c_str());
    if (! file) {
        const int err = errno;
        KFS_LOG_STREAM_INFO << chunkLocationFn << ": " << strerror(err) <<
        KFS_LOG_EOM;
        return (err > 0 ? -err : -1);
    }
    const size_t       kMaxLineSize = 256 << 10;
    StBufferT<char, 1> buf;
    char* const        line         = buf.Resize(kMaxLineSize);
    size_t             lineno       = 1;
    size_t             len          = 0;
    ServerLocation     loc;
    line[0] = 0;
    while (file.getline(line, kMaxLineSize) &&
            (len = file.gcount()) < kMaxLineSize - 1 &&
            Parse(line, len, addChunksToReplicationChecker, loc)) {
        lineno++;
    }
    const bool badFlag = file.bad();
    if (! badFlag && file.eof()) {
        return 0;
    }
    const int err = badFlag ? errno : EINVAL;
    KFS_LOG_STREAM_ERROR << chunkLocationFn << ":" << lineno <<
        (badFlag ? " " : " malformed: ") <<
        (badFlag ? strerror(err) : line ) <<
    KFS_LOG_EOM;
    return (err > 0 ? -err : -1);
}

bool
LayoutEmulator::Parse(
    const char*       line,
    size_t            size,
    bool              addChunksToReplicationChecker,
    ServerLocation&   loc)
{
    // format of the file:
    // <chunkid> <fileid> <# of servers> [server location]
    // \n
    // where, <server location>: replica-size name port rack#
    // and we have as many server locations as # of servers

    kfsChunkId_t       cid;
    fid_t              fid;
    int                numServers;
    const char*        p   = line;
    const char* const  end = p + size;
    if (! DecIntParser::Parse(p, end - p, cid) ||
            ! DecIntParser::Parse(p, end - p, fid) ||
            ! DecIntParser::Parse(p, end - p, numServers)) {
        return false;
    }
    CSMap::Entry* const ci = mChunkToServerMap.Find(cid);
    if (! ci) {
        KFS_LOG_STREAM_ERROR << "no such chunk: " << cid << KFS_LOG_EOM;
        return true;
    }
    for (int i = 0; i < numServers; i++) {
        while (p < end && (*p & 0xFF) <= ' ') {
            p++;
        }
        if (p >= end) {
            return false;
        }
        const char* const host = p;
        while (p < end && (*p & 0xFF) > ' ') {
            p++;
        }
        if (p >= end) {
            return false;
        }
        loc.hostname.assign(host, p - host);
        if (! DecIntParser::Parse(p, end - p, loc.port)) {
            return false;
        }
        while (p < end && (*p & 0xFF) <= ' ') {
            p++;
        }
        if (p >= end) {
            return false;
        }
        // const char* const rack = p;
        while (p < end && (*p & 0xFF) > ' ') {
            p++;
        }
        Loc2Server::const_iterator const it = mLoc2Server.find(loc);
        if (it == mLoc2Server.end()) {
            KFS_LOG_STREAM_ERROR <<
                "chunk: " << cid <<
                " no such server: "  << loc <<
            KFS_LOG_EOM;
            continue;
        }
        if (! AddReplica(*ci, it->second)) {
            KFS_LOG_STREAM_ERROR <<
                "chunk: "        << cid <<
                " add server: "  << loc <<
                " failed" <<
            KFS_LOG_EOM;
            continue;
        }
        GetCSEmulator(*(it->second)).HostingChunk(cid, GetChunkSize(*ci));
    }
    if (addChunksToReplicationChecker) {
        CheckChunkReplication(*ci);
    }
    return true;
}

// override what is in the layout manager (only for the emulator code)
bool
LayoutEmulator::ChunkReplicationDone(MetaChunkReplicate* req)
{
    mOngoingReplicationStats->Update(-1);
    // Book-keeping....
    if (mNumOngoingReplications > 0) {
        mNumOngoingReplications--;
    }
    req->server->ReplicateChunkDone(req->chunkId);
    if (req->srcLocation.IsValid() && req->dataServer) {
        req->dataServer->UpdateReplicationReadLoad(-1);
    }
    req->dataServer.reset();
    if (req->status != 0) {
        // Replication failed...we will try again later
        KFS_LOG_STREAM_ERROR <<
            "replication failed"
            " chunk: "  << req->chunkId <<
            " status: " << req->status <<
            " server: " << req->server->GetServerLocation() <<
        KFS_LOG_EOM;
        mFailedReplicationStats->Update(1);
        return false;
    }
    mNumBlksRebalanced++;
    // replication succeeded: book-keeping
    CSMap::Entry* const ci = mChunkToServerMap.Find(req->chunkId);
    if (! ci) {
        KFS_LOG_STREAM_ERROR <<
            "replication completion: no such chunk: " << req->chunkId <<
        KFS_LOG_EOM;
        return false;
    }
    const bool addedFlag = AddReplica(*ci, req->server);
    if (addedFlag) {
        GetCSEmulator(*(req->server)).HostingChunk(
            req->chunkId, GetChunkSize(*ci));
    } else {
            KFS_LOG_STREAM_ERROR <<
                "chunk: "        << req->chunkId <<
                " add server: "  << req->server->GetServerLocation() <<
                " failed" <<
            KFS_LOG_EOM;
    }
    CheckChunkReplication(*ci);
    return addedFlag;
}

void
LayoutEmulator::MarkServerDown(const ServerLocation& loc)
{
    Loc2Server::iterator const it = mLoc2Server.find(loc);
    if (it == mLoc2Server.end()) {
        KFS_LOG_STREAM_ERROR <<
            "server down: no such server: " << loc <<
        KFS_LOG_EOM;
        return;
    }
    ServerDown(it->second);
    mLoc2Server.erase(it);
    KFS_LOG_STREAM_INFO << "server down: " << loc <<
    KFS_LOG_EOM;
}

seq_t
LayoutEmulator::GetChunkversion(chunkId_t cid) const
{
    const CSMap::Entry* const ci = mChunkToServerMap.Find(cid);
    return (ci ? ci->GetChunkInfo()->chunkVersion : -1);
}

size_t
LayoutEmulator::GetChunkSize(chunkId_t cid) const
{
    const CSMap::Entry* const ci = mChunkToServerMap.Find(cid);
    if (! ci) {
        return 0;
    }
    return GetChunkSize(*ci);
}

size_t
LayoutEmulator::GetChunkSize(const CSMap::Entry& ci) const
{
    // Assume that the file isn't sparse.
    const MetaFattr* const fa = ci.GetFattr();
    if (fa->chunkcount() <= 0) {
        return 0;
    }
    const chunkOff_t pos   = ci.GetChunkInfo()->offset;
    const chunkOff_t fsize = metatree.getFileSize(fa);
    if (! fa->IsStriped()) {
        if (fsize < pos) {
            return CHUNKSIZE;
        }
        return min(CHUNKSIZE, size_t(fsize - pos));
    }

    const chunkOff_t blkSize = fa->numStripes * (chunkOff_t)CHUNKSIZE;
    const chunkOff_t blkPos  = fa->ChunkPosToChunkBlkFileStartPos(pos);
    const chunkOff_t size    = fsize - blkPos;
    if (size <= 0 || size >= blkSize) {
        return CHUNKSIZE;
    }
    const int        chunkStripeIdx = (int)(pos / (chunkOff_t)CHUNKSIZE %
        (fa->numStripes + fa->numRecoveryStripes));
    const chunkOff_t strideSize     = fa->stripeSize * fa->numStripes;
    const chunkOff_t strideCount    = size / strideSize;
    const chunkOff_t strideHead     = size % strideSize;
    const chunkOff_t stripeIdx      = strideHead / fa->stripeSize;
    const int        idx            =
            chunkStripeIdx < (int)fa->numStripes ? chunkStripeIdx : 0;
    chunkOff_t       chunkSize      = strideCount * fa->stripeSize;
    if (idx < stripeIdx) {
        chunkSize += fa->stripeSize;
    } else if (idx == stripeIdx) {
        chunkSize += strideHead % fa->stripeSize;
    }
    return (size_t)chunkSize;
}

void
LayoutEmulator::AddServer(
    const ServerLocation& loc,
    int                   rack,
    uint64_t              totalSpace,
    uint64_t              usedSpace)
{
    ostringstream os;
    os << loc.hostname << ":" << loc.port;
    const string peerName = os.str();
    ChunkServerPtr c;
    ChunkServerEmulator& srv = *(new ChunkServerEmulator(loc, rack, peerName));
    c.reset(&srv);

    srv.InitSpace(totalSpace, usedSpace, mUseFsTotalSpaceFlag);

    mChunkToServerMap.AddServer(c);
    mLoc2Server.insert(make_pair(loc, c));
    mChunkServers.push_back(c);
    RackInfos::iterator const it = find_if(
        mRacks.begin(), mRacks.end(),
        bind(&RackInfo::id, _1) == rack);
    if (it != mRacks.end()) {
        it->addServer(c);
    } else if (rack >= 0) {
        mRacks.push_back(RackInfo(rack, 1.0, c));
    }
    UpdateSrvLoadAvg(srv, 0, 0);
    UpdateReplicationsThreshold();

    KFS_LOG_STREAM_INFO <<
        "added:"
        " server: "      << srv.GetServerLocation() <<
        " rack: "        << srv.GetRack() <<
        " space:"
        " total: "       << srv.GetTotalSpace(mUseFsTotalSpaceFlag) <<
        " utilization: " << srv.GetSpaceUtilization(mUseFsTotalSpaceFlag) <<
    KFS_LOG_EOM;
}

int
LayoutEmulator::SetRebalancePlanOutFile(const string &rebalancePlanFn)
{
    mPlanFile.close();
    mPlanFile.open(rebalancePlanFn.c_str(), ofstream::out);
    mPlanFile.setf(istream::hex);
    if (! mPlanFile) {
        const int err = errno;
        KFS_LOG_STREAM_ERROR << rebalancePlanFn << ": " << strerror(err) <<
        KFS_LOG_EOM;
        return -1;
    }
    for (Servers::iterator i = mChunkServers.begin();
            i != mChunkServers.end();
            i++) {
        GetCSEmulator(**i).SetRebalancePlanOutFd(&mPlanFile);
    }
    return 0;
}

size_t
LayoutEmulator::RunChunkserverOps()
{
    size_t opsCount = 0;
    for (size_t i = 0; i < mChunkServers.size(); i++) {
        ChunkServer& srv = *mChunkServers[i];
        opsCount += GetCSEmulator(srv).Dispatch();
        // Handle the case where the chunk server might go away as result of
        // executing op.
        if (mChunkServers.size() <= i) {
            break;
        }
        if (&*mChunkServers[i] == &srv) {
            UpdateSrvLoadAvg(srv, 0, 0);
        }
    }
    return opsCount;
}

void
LayoutEmulator::CalculateRebalaceThresholds()
{
    double avgSpaceUtil = 0;
    for (Servers::iterator it = mChunkServers.begin();
            it != mChunkServers.end();
            it++) {
        avgSpaceUtil += (*it)->GetSpaceUtilization(mUseFsTotalSpaceFlag);
    }
    const size_t cnt = mChunkServers.size();
    if (cnt > 0) {
        avgSpaceUtil /= cnt;
    }
    if (mVariationFromMean > 0) {
        // Take the average utilizaiton in the cluster; any node that has
        // utilizaiton outside the average is candidate for rebalancing
        mMinRebalanceSpaceUtilThreshold =
            max(0.0, avgSpaceUtil - mVariationFromMean * 0.5);
        mMaxRebalanceSpaceUtilThreshold =
            min(1.0, avgSpaceUtil + mVariationFromMean * 0.5);
    }
    KFS_LOG_STREAM_INFO <<
        "chunk servers: " << cnt <<
        " racks: "        << mRacks.size() <<
        " rebalance thresholds:"
        " average: "      << avgSpaceUtil <<
        " min: "          << mMinRebalanceSpaceUtilThreshold <<
        " max: "          << mMaxRebalanceSpaceUtilThreshold <<
    KFS_LOG_EOM;
}

void
LayoutEmulator::PrepareRebalance(bool enableRebalanceFlag)
{
    ToggleRebalancing(enableRebalanceFlag);

    if (enableRebalanceFlag) {
        CalculateRebalaceThresholds();
    }
    const int64_t kMicroseconds = int64_t(1000) * 1000;
    const int64_t kMaxRunTime   = kMicroseconds * 15;
    mRebalanceRunInterval             = -1;
    mMinChunkReplicationCheckInterval = -1;
    mMaxRebalanceRunTime             = max(mMaxRebalanceRunTime, kMaxRunTime);
    mMaxTimeForChunkReplicationCheck =
        max(mMaxTimeForChunkReplicationCheck, kMaxRunTime);

    if (mMaxConcurrentWriteReplicationsPerNode < 1) {
        mMaxConcurrentWriteReplicationsPerNode = 1;
        UpdateReplicationsThreshold();
    }
    if (mMaxConcurrentReadReplicationsPerNode < 1) {
        mMaxConcurrentReadReplicationsPerNode = 1;
    }
}

void
LayoutEmulator::BuildRebalancePlan()
{
    PrepareRebalance(true);

    RebalanceCtrs::Counter round       = mRebalanceCtrs.GetRoundCount() + 1;
    RebalanceCtrs::Counter nextScanned = 0;
    const size_t kThreshUpdateInterval = 4 << 10;
    size_t       updateThreshOpsCnt    = kThreshUpdateInterval;
    for (int prev = mNumBlksRebalanced; ;) {
        if (mCleanupScheduledFlag) {
            ScheduleCleanup();
        }
        ChunkReplicationChecker();
        const size_t opsCount = RunChunkserverOps();
        if (prev != mNumBlksRebalanced) {
            prev  = mNumBlksRebalanced;
            round = mRebalanceCtrs.GetRoundCount() + 1;
        }
        const bool doneFlag =
            mStopFlag ||
            mChunkServers.empty() ||
            (opsCount <= 0 &&
            ! mCleanupScheduledFlag &&
            mRebalanceCtrs.GetRoundCount() > round &&
            ! mChunkToServerMap.Front(CSMap::Entry::kStateCheckReplication));
        RebalanceCtrs::Counter const scanned = mRebalanceCtrs.GetTotalScanned();
        if (doneFlag || nextScanned < scanned) {
            KFS_LOG_STREAM_START(MsgLogger::kLogLevelINFO, logStream);
                ostream& os = logStream.GetStream();
                os << "=== rebalance counters: ";
                mRebalanceCtrs.Show(os, ": ", " ");
            KFS_LOG_STREAM_END;
            nextScanned = scanned + 1000 * 1000;
        }
        if (doneFlag) {
            break;
        }
        if (mVariationFromMean > 0) {
            if (updateThreshOpsCnt <= opsCount) {
                CalculateRebalaceThresholds();
                updateThreshOpsCnt = kThreshUpdateInterval;
            } else {
                updateThreshOpsCnt -= opsCount;
            }
        }
    }
}

void
LayoutEmulator::ExecuteRebalancePlan()
{
    // the plan has already been worked out; we just execute
    PrepareRebalance(false);

    RebalanceCtrs::Counter nextScanned = 0;
    for (; ;) {
        if (mCleanupScheduledFlag) {
            ScheduleCleanup();
        }
        ChunkReplicationChecker();
        const bool doneFlag =
            mStopFlag ||
            mChunkServers.empty() ||
            (RunChunkserverOps() <= 0 &&
            ! mChunkToServerMap.Front(CSMap::Entry::kStateCheckReplication) &&
            ! mIsExecutingRebalancePlan &&
            ! mCleanupScheduledFlag);
        RebalanceCtrs::Counter const scanned = mRebalanceCtrs.GetTotalScanned();
        if (doneFlag || nextScanned < scanned) {
            KFS_LOG_STREAM_START(MsgLogger::kLogLevelINFO, logStream);
                ostream& os = logStream.GetStream();
                os << "=== rebalance counters: ";
                mRebalanceCtrs.Show(os, ": ", " ");
            KFS_LOG_STREAM_END;
            nextScanned = scanned + 1000 * 1000;
        }
        if (doneFlag) {
            break;
        }
    }
}

class PrintBlockCount
{
    ostream&   mOs;
    const bool mUseFsTotalSpaceFlag;
public:
    PrintBlockCount(ostream& os, bool f)
        : mOs(os),
          mUseFsTotalSpaceFlag(f)
        {}
    void operator()(const ChunkServerPtr &c) const
    {
        ChunkServer& cse = *c;
        mOs << cse.GetServerLocation() <<
            ' ' << cse.GetNumChunks() <<
            ' ' << cse.GetUsedSpace() <<
            ' ' << cse.GetSpaceUtilization(mUseFsTotalSpaceFlag) <<
        '\n';
    }
};

void
LayoutEmulator::PrintChunkserverBlockCount(ostream& os) const
{
    for_each(mChunkServers.begin(), mChunkServers.end(),
        PrintBlockCount(os, mUseFsTotalSpaceFlag));
}

int
LayoutEmulator::ReadNetworkDefn(const string& networkFn)
{
    ifstream file(networkFn.c_str());
    if (! file) {
        const int err = errno;
        KFS_LOG_STREAM_ERROR << networkFn << ": " << strerror(err) <<
        KFS_LOG_EOM;
        return (err > 0 ? -err : -1);
    }

    ServerLocation    loc;
    BufferInputStream bis;
    const size_t      kMaxLineSize = 4 << 10;
    char              line[kMaxLineSize];
    size_t            lineno = 1;
    size_t            len;
    line[0] = 0;
    while (file.getline(line, kMaxLineSize) &&
            (len = file.gcount()) < kMaxLineSize - 1) {
        istream& is = bis.Set(line, len);
        int      rack;
        uint64_t totalSpace;
        uint64_t usedSpace;
        if (! (is >> loc.hostname >> loc.port >> rack >>
                totalSpace >> usedSpace)) {
            KFS_LOG_STREAM_ERROR << networkFn << ":" << lineno <<
                " malformed: " << line <<
            KFS_LOG_EOM;
            return -EINVAL;
        }
        lineno++;
        AddServer(loc, rack, totalSpace, usedSpace);
    }
    const bool badFlag = file.bad();
    if (badFlag || ! file.eof()) {
        const int err = badFlag ? errno : EINVAL;
        KFS_LOG_STREAM_ERROR << networkFn << ":" << lineno <<
            (badFlag ? " " : " malformed: ") <<
            (badFlag ? strerror(err) : line ) <<
        KFS_LOG_EOM;
        return (err > 0 ? -err : -1);
    }

    const size_t cnt = mChunkServers.size();
    KFS_LOG_STREAM_INFO <<
        "chunk servers: " << cnt <<
        " racks: "        << mRacks.size() <<
    KFS_LOG_EOM;
    for (RackInfos::iterator it = mRacks.begin();
            it != mRacks.end();
            ++it) {
        KFS_LOG_STREAM_INFO <<
            "rack: "                 << it->id() <<
            " weight: "              << it->getWeight() <<
            " servers: "             << it->getServers().size() <<
            " candidates: "          << it->getPossibleCandidatesCount() <<
            " weighted candidates: " <<
                it->getWeightedPossibleCandidatesCount() <<
        KFS_LOG_EOM;
    }

    return 0;
}

class LayoutEmulator::PlacementVerifier
{
public:
    int           sameRack;
    int           underReplicated;
    int           overReplicated;
    int           missing;
    int           sameNode;
    int           stripeSameNode;
    const int64_t startTime;

    PlacementVerifier()
        : sameRack(0),
          underReplicated(0),
          overReplicated(0),
          missing(0),
          sameNode(0),
          stripeSameNode(0),
          startTime(microseconds())
        {}
    ostream& report(ostream& os, size_t chunkCount)
    {
        os <<
        "************************************************\n"
        " Total chunks : "                  << chunkCount         << "\n"
        " Total chunks missing : "          << missing            << "\n"
        " Total chunks on same rack : "     << sameRack           << "\n"
        " Total chunks under replicated : " << underReplicated    << "\n"
        " Total chunks over replicated : "  << overReplicated     << "\n"
        " Total stripes on same node : "    << stripeSameNode     << "\n"
        " Run time : " << ((microseconds() - startTime) * 1e-6)   << "\n"
        "************************************************\n"
        ;
        return os;
    }
    bool IsHealthy() const
        { return (missing <= 0); }
};

inline const string&
GetFileName(const MetaFattr* fa, string& fileName)
{
    if (fileName.empty()) {
        fileName = metatree.getPathname(fa);
    }
    return fileName;
}

class DisplayFileType
{
public:
    DisplayFileType(const MetaFattr* a)
        : fa(a)
        {}
    ostream& Display(ostream& os) const
    {
        if (! fa->IsStriped()) {
            return (os << "r " << fa->numReplicas);
        }
        return (os << "rs " << fa->numReplicas << "," <<
            fa->numStripes << "+" << fa->numRecoveryStripes);
    }
private:
    const MetaFattr* const fa;
};

ostream&
operator<<(ostream& os, const DisplayFileType& d) {
    return d.Display(os);
}

void
LayoutEmulator::ShowPlacementError(
    ostream&            os,
    const CSMap::Entry& c,
    const ChunkServer*  srv,
    string&             fileName,
    size_t              replicas,
    const char*         reason)
{
    const MetaFattr* const fa  = c.GetFattr();
    const chunkOff_t       pos = c.GetChunkInfo()->offset;
    os <<
        reason <<
        " chunk: "    << c.GetChunkId() <<
        " pos: "      << pos <<
        " size: "     << GetChunkSize(c) <<
        " block: "    << fa->ChunkPosToChunkBlkIndex(pos) <<
        " file: "     << c.GetFileId() <<
        " type: "     << DisplayFileType(fa) <<
        " node: "     << (srv ? srv->GetServerLocation() : ServerLocation()) <<
        " rack: "     << (srv ? srv->GetRack() : -1) <<
        " replicas: " << fa->numReplicas <<
        " actual: "   << replicas <<
        " "           << GetFileName(fa, fileName) <<
    "\n";
}

void
LayoutEmulator::VerifyPlacement(
    const CSMap::Entry&                c,
    const LayoutEmulator::Servers&     servers,
    const vector<MetaChunkInfo*>&      cblk,
    LayoutEmulator::ChunkPlacement&    placement,
    ostream&                           os,
    bool                               verboseFlag,
    bool                               reportAllFlag,
    LayoutEmulator::PlacementVerifier& verifier)
{
    const MetaFattr* const fa  = c.GetFattr();
    string                 fileName;

    if (servers.empty()) {
        if (verboseFlag) {
            ShowPlacementError(os, c, 0, fileName, servers.size(),
                "no replicas");
        }
        verifier.missing++;
        return;
    }
    for (Servers::const_iterator it = servers.begin();
            it != servers.end();
            ++it) {
        ChunkServer& srv = **it;
        if (placement.IsServerExcluded(srv)) {
            if (! fa->IsStriped() ||
                    find(it + 1, servers.end(), *it) !=
                        servers.end()) {
                verifier.sameNode++;
                ShowPlacementError(os, c, &srv, fileName, servers.size(),
                    "duplicate server");
            } else if (reportAllFlag ||
                    placement.GetExcludedServersCount() <
                    mChunkServers.size()) {
                verifier.stripeSameNode++;
                if (verboseFlag) {
                    ShowPlacementError(os, c, &srv, fileName, servers.size(),
                        "same node");
                }
            }
            placement.ExcludeServerAndRack(srv, c.GetChunkId());
        } else if (! placement.ExcludeServerAndRack(srv, c.GetChunkId()) &&
                (reportAllFlag || placement.HasCandidateRacks())) {
            verifier.sameRack++;
            if (verboseFlag) {
                ShowPlacementError(os, c, &srv, fileName, servers.size(),
                    "same rack");
            }
        }
    }
    if (! servers.empty() &&
            servers.size() != (size_t)fa->numReplicas) {
        const bool underReplicatedFlag =
            servers.size() < (size_t)fa->numReplicas;
        if (underReplicatedFlag) {
            verifier.underReplicated++;
        } else {
            verifier.overReplicated++;
        }
        if (verboseFlag) {
            ShowPlacementError(os, c, 0, fileName, servers.size(),
                (underReplicatedFlag ? " under replicated" :
                    " over replicated"));
        }
    }
}

int
LayoutEmulator::VerifyRackAwareReplication(
    bool reportAllFlag, bool verboseFlag, ostream& os)
{
    os <<
    "************************************************\n"
    " KFS Replica Checker\n"
    "************************************************\n"
    ;
    StTmp<Servers>                 serversTmp(mServersTmp);
    StTmp<ChunkPlacement>          placementTmp(mChunkPlacementTmp);
    StTmp<vector<MetaChunkInfo*> > cinfoTmp(mChunkInfosTmp);
    PlacementVerifier     verifier;
    const bool kIncludeThisChunkFlag             = false;
    const bool kStopIfHasAnyReplicationsInFlight = false;
    mChunkToServerMap.First();
    for (const CSMap::Entry* p; (p = mChunkToServerMap.Next()); ) {
        ChunkPlacement& placement = placementTmp.Get();
        Servers& servers = serversTmp.Get();
        mChunkToServerMap.GetServers(*p, servers);
        vector<MetaChunkInfo*>& cblk = cinfoTmp.Get();
        if (! servers.empty()) {
            GetPlacementExcludes(*p, placement, kIncludeThisChunkFlag,
                kStopIfHasAnyReplicationsInFlight, &cblk);
        }
        VerifyPlacement(*p, servers, cblk, placement,
            os, verboseFlag, reportAllFlag, verifier);
    }
    verifier.report(os, mChunkToServerMap.Size());
    return (verifier.IsHealthy() ? 0 : 1);
}

int
LayoutEmulator::RunFsck(
    const string& fileName)
{
    const string kStdout("-");
    const int    outfd = fileName == kStdout ?
        fileno(stdout) :
        open(fileName.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644);
    if (outfd < 0) {
        const int err = errno;
        KFS_LOG_STREAM_ERROR << "failed to create temporary file: " <<
            fileName << ": " << strerror(err) <<
        KFS_LOG_EOM;
        return (err > 0 ? -err : -1);
    }
    const bool kReportAbandonedFilesFlag = true;
    const int              cnt       = FsckStreamCount(
        kReportAbandonedFilesFlag);
    const char* const      suffix    = ".XXXXXX";
    const size_t           suffixLen = strlen(suffix);
    ofstream* const        streams   = new ofstream[cnt];
    StBufferT<char, 128>   buf;
    StBufferT<ostream*, 8> osbuf;
    ostream** const        osptr     = osbuf.Resize(cnt + 1);
    vector<int>            fd;
    fd.reserve(cnt);
    for (int i = 0; i < cnt; i++) {
        char* const ptr = buf.Resize(fileName.length() + suffixLen + 1);
        memcpy(ptr, fileName.data(), fileName.size());
        strcpy(ptr + fileName.size(), suffix);
        int tfd = mkstemp(ptr);
        int err = errno;
        if (tfd > 0) {
            streams[i].open(ptr);
            err = errno;
            unlink(ptr);
            if (! streams[i]) {
                close(tfd);
                tfd = -1;
            }
        }
        if (tfd < 0) {
            KFS_LOG_STREAM_ERROR << "failed to create temporary file: " <<
                ptr << ": " << strerror(err) <<
            KFS_LOG_EOM;
            while (--i >= 0) {
                streams[i].close();
                close(fd[i]);
            }
            delete [] streams;
            close(outfd);
            return (err > 0 ? -err : -1);
        }
        fd.push_back(tfd);
        osptr[i] = streams + i;
    }
    osptr[cnt] = 0;
    Fsck(osptr, kReportAbandonedFilesFlag);
    int err = 0;
    for (int i = 0; i < cnt; i++) {
        streams[i].close();
        if (! streams[i]) {
            err = errno;
            KFS_LOG_STREAM_ERROR << "failed to close temporary file: " <<
                strerror(err) <<
            KFS_LOG_EOM;
            err = err > 0 ? -err : -1;
        }
    }
    int i = 0;
    if (err == 0) {
        const size_t sz  = 1 << 20;
        char* const  ptr = buf.Resize(sz);
        for (i = 0; err == 0 && i < cnt; i++) {
            ssize_t nrd = 0;
            while (err == 0 && (nrd = read(fd[i], ptr, sz)) > 0) {
                const char*       p = ptr;
                const char* const e = p + nrd;
                ssize_t           nwr;
                while (p < e && (nwr = write(outfd, p, e - p)) > 0) {
                    p += nwr;
                }
                if (p < e) {
                    err = errno;
                    KFS_LOG_STREAM_ERROR << fileName << ": " <<
                        strerror(err) <<
                    KFS_LOG_EOM;
                    err = err > 0 ? -err : -1;
                    break;
                }
            }
            if (nrd < 0) {
                err = errno;
                KFS_LOG_STREAM_ERROR << "read failure: " <<
                    strerror(err) <<
                KFS_LOG_EOM;
                err = err > 0 ? -err : -1;
            }
            close(fd[i]);
        }
    }
    for ( ; i < cnt; i++) {
        close(fd[i]);
    }
    if (fileName != kStdout && close(outfd)) {
        err = errno;
        KFS_LOG_STREAM_ERROR << fileName << ": " <<
            strerror(err) <<
        KFS_LOG_EOM;
        err = err > 0 ? -err : -1;
    }
    delete [] streams;
    return err;
}

LayoutEmulator gLayoutEmulator;
LayoutManager& gLayoutManager = gLayoutEmulator;
const UserAndGroup& MetaUserAndGroup::sUserAndGroup =
    gLayoutManager.GetUserAndGroup();

} // namespace KFS

ext: cc

max_stars_repo_name: chanwit/qfs


text:
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "pidloop.hpp"

#include "pid/pidcontroller.hpp"
#include "pid/tuning.hpp"
#include "sensors/sensor.hpp"

#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <map>
#include <memory>
#include <thread>
#include <vector>

static void processThermals(PIDZone* zone)
{
    // Get the latest margins.
    zone->updateSensors();
    // Zero out the RPM set point goals.
    zone->clearRPMSetPoints();
    zone->clearRPMCeilings();
    // Run the margin PIDs.
    zone->processThermals();
    // Get the maximum RPM setpoint.
    zone->determineMaxRPMRequest();
}

void pidControlLoop(PIDZone* zone, boost::asio::steady_timer& timer, bool first,
                    int ms100cnt)
{
    if (first)
    {
        if (tuningLoggingEnabled)
        {
            zone->initializeLog();
        }

        zone->initializeCache();
        processThermals(zone);
    }

    timer.expires_after(std::chrono::milliseconds(100));
    timer.async_wait(
        [zone, &timer, ms100cnt](const boost::system::error_code& ec) mutable {
            /*
             * This should sleep on the conditional wait for the listen thread
             * to tell us it's in sync.  But then we also need a timeout option
             * in case phosphor-hwmon is down, we can go into some weird failure
             * more.
             *
             * Another approach would be to start all sensors in worst-case
             * values, and fail-safe mode and then clear out of fail-safe mode
             * once we start getting values.  Which I think it is a solid
             * approach.
             *
             * For now this runs before it necessarily has any sensor values.
             * For the host sensors they start out in fail-safe mode.  For the
             * fans, they start out as 0 as input and then are adjusted once
             * they have values.
             *
             * If a fan has failed, it's value will be whatever we're told or
             * however we retrieve it.  This program disregards fan values of 0,
             * so any code providing a fan speed can set to 0 on failure and
             * that fan value will be effectively ignored.  The PID algorithm
             * will be unhappy but nothing bad will happen.
             *
             * TODO(venture): If the fan value is 0 should that loop just be
             * skipped? Right now, a 0 value is ignored in
             * FanController::inputProc()
             */

            // Check if we should just go back to sleep.
            if (zone->getManualMode())
            {
                pidControlLoop(zone, timer, false, ms100cnt);
                return;
            }

            // Get the latest fan speeds.
            zone->updateFanTelemetry();

            if (10 <= ms100cnt)
            {
                ms100cnt = 0;

                processThermals(zone);
            }

            // Run the fan PIDs every iteration.
            zone->processFans();

            if (tuningLoggingEnabled)
            {
                zone->getLogHandle() << "," << zone->getFailSafeMode();
                zone->getLogHandle() << std::endl;
            }

            ms100cnt += 1;

            pidControlLoop(zone, timer, false, ms100cnt);
        });
}

max_stars_repo_name: quanta-bmc/phosphor-pid-control

max_issues_repo_name: quanta-bmc/phosphor-pid-control

max_forks_repo_name: quanta-bmc/phosphor-pid-control


text:
//==================================================================================================
/*!
  @file

  @copyright 2016 NumScale SAS

  Distributed under the Boost Software License, Version 1.0.
  (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
//==================================================================================================
#ifndef BOOST_SIMD_FUNCTION_IF_ALLBITS_ELSE_HPP_INCLUDED
#define BOOST_SIMD_FUNCTION_IF_ALLBITS_ELSE_HPP_INCLUDED

#if defined(DOXYGEN_ONLY)
namespace boost { namespace simd
{

 /*!

    @ingroup group-boolean
    Function object implementing if_allbits_else capabilities

    If @c c is @ref True returns @ref Allbits else returns @c f

    @par Semantic:

    For every parameters @c c of type @c C and @c f of type @c T:

    @code
    T r = if_allbits_else(c, f);
    @endcode

    is similar to:

    @code
    T r = c ? Allbits<T>() : f;
    @endcode

    @par Alias:
    @c if_nan_else,

    @see genmask, Allbits, Nan

  **/
  Value if_allbits_else(Value const& c, Value const& f);

  //@overload
  Value if_allbits_else(LogicalValue const& c, Value const& f);
} }
#endif

#include <boost/simd/function/scalar/if_allbits_else.hpp>
#include <boost/simd/function/simd/if_allbits_else.hpp>

#endif

ext: hpp

max_stars_repo_name: nickporubsky/boost-simd-clone

max_issues_repo_name: nickporubsky/boost-simd-clone

max_forks_repo_name: nickporubsky/boost-simd-clone


text:
// Copyright (c) 2014-2019 The Dash Core Developers, The DAC Core Developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "smartcontract-client.h"
#include "smartcontract-server.h"
#include "util.h"
#include "utilmoneystr.h"
#include "rpcpodc.h"
#include "rpcpog.h"
#include "init.h"
#include "activemasternode.h"
#include "governance-classes.h"
#include "governance.h"
#include "masternode-sync.h"
#include "masternode-payments.h"
#include "masternodeconfig.h"
#include "messagesigner.h"
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
#include <boost/algorithm/string.hpp> // for trim()
#include <boost/date_time/posix_time/posix_time.hpp> // for StringToUnixTime()
#include <math.h>       /* round, floor, ceil, trunc */
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <openssl/crypto.h>
#include <stdint.h>
#include <univalue.h>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>

#ifdef ENABLE_WALLET
extern CWallet* pwalletMain;
#endif // ENABLE_WALLET

//////////////////////////////////////////////////////////////////// DAC - SMART CONTRACTS - CLIENT SIDE ///////////////////////////////////////////////////////////////////////////////////////////////

max_stars_repo_name: Drusuilia/ImageCoin

max_issues_repo_name: Drusuilia/ImageCoin

max_forks_repo_name: Drusuilia/ImageCoin


text:
#include <iostream>
#include <math.h>
#include <deque>
#include <sdf/sdf.hh>

#include "gazebo/gazebo.hh"
#include "gazebo/common/common.hh"
#include "gazebo/math/Vector3.hh"
#include "gazebo/transport/transport.hh"
#include "gazebo/physics/physics.hh"
#include "gazebo/msgs/msgs.hh"

#include "map_request.pb.h"
#include "vector2d.pb.h"

#include <ros/ros.h>
#include <boost/filesystem/path.hpp>

using namespace std;

bool createVector(const char * vectorString,
                       gazebo::msgs::Vector2d* origin)
{
    string cornersStr = vectorString;
    size_t opening=0;
    size_t closing=0;

    opening = cornersStr.find('(', closing);
    closing = cornersStr.find(')', opening);
    if (opening == string::npos || closing == string::npos)
    {
        ROS_ERROR("Poorly formed string: %s", cornersStr.c_str());
        ROS_ERROR("( found at: %ld ) found at: %ld", opening, closing);
        return false;
    }
    string oneCornerStr = cornersStr.substr(opening + 1, closing - opening - 1);
    size_t commaLoc = oneCornerStr.find(",");
    string x = oneCornerStr.substr(0,commaLoc);
    string y = oneCornerStr.substr(commaLoc + 1, oneCornerStr.length() - commaLoc);
    origin->set_x(atof(x.c_str()));
    origin->set_y(atof(y.c_str()));

    return true;
}

int main(int argc, char * argv[])
{
    if (argc > 6)
    {
        dronedoc::msgs::MapRequest request;
        gazebo::msgs::Vector2d* origin = request.mutable_origin();

        if (!createVector(argv[3], origin))
        {
            ROS_ERROR("Failed to create rasterize region.");
            return -1;
        }

        request.set_height(atof(argv[1]));
        request.set_resolution(atof(argv[2]));
        request.set_map_width(atof(argv[4]));
        request.set_map_height(atof(argv[5]));

        std::string filename;
        if(argc < 7){
            filename = "map.png";
        } else {
            filename = argv[6];
        }
        boost::filesystem::path fname(filename);
        fname = boost::filesystem::absolute(fname);
        request.set_filename(fname.string());

        if (argc == 8)
        {
            request.set_threshold(atoi(argv[7]));
        }

        gazebo::transport::init();
        gazebo::transport::run();
        gazebo::transport::NodePtr node(new gazebo::transport::Node());
        node->Init("default");

        ROS_INFO("\nRequest: \n"
                 " origin.x: %f origin.y: %f\n"
                 " map_width: %f map_height: %f\n"
                 " Height: %f\n Resolution: %f\n Filename: %s\n Threshold: %d",
                 request.origin().x(),
                 request.origin().y(),
                 request.map_width(),
                 request.map_height(),
                 request.height(),
                 request.resolution(),
                 request.filename().c_str(),
                 request.threshold());

        gazebo::transport::PublisherPtr imagePub =
                node->Advertise<dronedoc::msgs::MapRequest>("~/collision_map/command");
        imagePub->WaitForConnection();
        imagePub->Publish(request);

        gazebo::transport::fini();
        return 0;
    }

    std::cout << "Usage: " <<
                 "rosrun dronedoc request_publisher a1 a2 a3 a4 a5 [a6 a7]\n" <<
                 "\ta1: height\n" <<
                 "\ta2: resolution\n" <<
                 "\ta3: \"(origin.x, origin.y)\"\n" <<
                 "\t  Origin is the point on lower left corner of map image\n"
                 "\ta4: map_width\n" <<
                 "\ta5: map_height\n" <<
                 "\ta6: filename\t[default=\"map\"]\n" <<
                 "\ta7: threshold\t[default=255]\n" << std::endl;

    return -1;
}

max_stars_repo_name: hansbonini/dronedoc

max_issues_repo_name: weiLiangHare/dronedoc

max_forks_repo_name: weiLiangHare/dronedoc


text:
//
// Copyright (c) 2019-2021 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// 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 "boost/mysql/value.hpp"
#include "test_common.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_suite.hpp>
#include <cstdint>

using boost::mysql::value;
using boost::mysql::null_t;
using boost::mysql::datetime;
using boost::mysql::date;
using boost::mysql::days;
using vt = boost::mysql::value::variant_type;

#ifndef BOOST_NO_CXX14_CONSTEXPR

BOOST_AUTO_TEST_SUITE(test_value_constexpr)

#ifndef _MSC_VER // for some reason, MSVC doesn't like these tests

BOOST_AUTO_TEST_CASE(null_type)
{
    constexpr value v;
    constexpr value v2 (nullptr);
    static_assert(v.is_null(), "");
    static_assert(v.is<null_t>(), "");
    static_assert(v.is_convertible_to<null_t>(), "");
    static_assert(v.to_variant() == vt(null_t()), "");
    static_assert(v == v2, "");
    static_assert(v != value(10), "");
    static_assert(v < value(10), "");
    static_assert(v <= value(10), "");
    static_assert(v >= v2, "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<null_t>(), "");
    #endif
}

#endif

BOOST_AUTO_TEST_CASE(int64_t_type)
{
    constexpr value v (std::int64_t(60));
    constexpr value v2 (-4); // from other int type
    static_assert(!v.is_null(), "");
    static_assert(v.is<std::int64_t>(), "");
    static_assert(v.is_convertible_to<std::int64_t>(), "");
    static_assert(v.is_convertible_to<std::uint64_t>(), "");
    static_assert(v.to_variant() == vt(std::int64_t(60)), "");
    static_assert(v == v, "");
    static_assert(v != v2, "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<std::int64_t>(), "");
    #endif
}

BOOST_AUTO_TEST_CASE(uint64_t_type)
{
    constexpr value v (std::uint64_t(60));
    static_assert(!v.is_null(), "");
    static_assert(v.is<std::uint64_t>(), "");
    static_assert(v.is_convertible_to<std::int64_t>(), "");
    static_assert(v.is_convertible_to<std::uint64_t>(), "");
    static_assert(v.to_variant() == vt(std::uint64_t(60)), "");
    static_assert(v == v, "");
    static_assert(v != value(std::int64_t(0)), "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<std::uint64_t>(), "");
    #endif
}

BOOST_AUTO_TEST_CASE(string_view_type)
{
    constexpr value v (boost::string_view("test", 4));
    static_assert(!v.is_null(), "");
    static_assert(v.is<boost::string_view>(), "");
    static_assert(v.is_convertible_to<boost::string_view>(), "");
    // Equality is not constexpr for strings
    static_assert(v != value(std::int64_t(0)), "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<boost::string_view>(), "");
    #endif
}

BOOST_AUTO_TEST_CASE(float_type)
{
    constexpr value v (3.14f);
    static_assert(!v.is_null(), "");
    static_assert(v.is<float>(), "");
    static_assert(v.is_convertible_to<float>(), "");
    static_assert(v.is_convertible_to<double>(), "");
    static_assert(v.to_variant() == vt(3.14f), "");
    static_assert(v == v, "");
    static_assert(v != value(std::int64_t(0)), "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<float>(), "");
    #endif
}

BOOST_AUTO_TEST_CASE(double_type)
{
    constexpr value v (3.14);
    static_assert(!v.is_null(), "");
    static_assert(v.is<double>(), "");
    static_assert(v.is_convertible_to<double>(), "");
    static_assert(v.to_variant() == vt(3.14), "");
    static_assert(v == v, "");
    static_assert(v != value(std::int64_t(0)), "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<double>(), "");
    #endif
}

BOOST_AUTO_TEST_CASE(date_type)
{
    constexpr date d (days(1));
    constexpr value v (d);
    static_assert(!v.is_null(), "");
    static_assert(v.is<date>(), "");
    static_assert(v.is_convertible_to<date>(), "");
    static_assert(v.to_variant() == vt(d), "");
    static_assert(v == v, "");
    static_assert(v != value(std::int64_t(0)), "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<date>(), "");
    #endif
}

BOOST_AUTO_TEST_CASE(datetime_type)
{
    constexpr datetime d (days(1));
    constexpr value v (d);
    static_assert(!v.is_null(), "");
    static_assert(v.is<datetime>(), "");
    static_assert(v.is_convertible_to<datetime>(), "");
    static_assert(v.to_variant() == vt(d), "");
    static_assert(v == v, "");
    static_assert(v != value(std::int64_t(0)), "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<datetime>(), "");
    #endif
}

BOOST_AUTO_TEST_CASE(time_type)
{
    constexpr boost::mysql::time t (1);
    constexpr value v (t);
    static_assert(!v.is_null(), "");
    static_assert(v.is<boost::mysql::time>(), "");
    static_assert(v.is_convertible_to<boost::mysql::time>(), "");
    static_assert(v.to_variant() == vt(t), "");
    static_assert(v == v, "");
    static_assert(v != value(std::int64_t(0)), "");
    static_assert(v < value(boost::mysql::time(9999)), "");
    static_assert(v <= value(boost::mysql::time(9999)), "");
    static_assert(v > value(std::int64_t(0)), "");
    static_assert(v >= value(std::int64_t(0)), "");

    #ifndef BOOST_NO_CXX17_HDR_OPTIONAL
    static_assert(v.get_std_optional<boost::mysql::time>(), "");
    #endif
}

BOOST_AUTO_TEST_SUITE_END()

#endif

max_stars_repo_name: sehe/mysql

max_issues_repo_name: sehe/mysql

max_forks_repo_name: sehe/mysql


text:
/**
 *  Copyright (c) 2019 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "config.h"
#include <boost/filesystem.hpp>
#include <iostream>

typedef char DEFAULT_KeyType[16];

void initKvsOptions(DaqDB::Options &options, const std::string &configFile) {
    options.runtime.logFunc = [](std::string msg) {
        std::cout << msg << std::endl;
    };

    /* Set default values */
    options.dht.id = 0;

    options.key.field(0, sizeof(DEFAULT_KeyType));

    if (boost::filesystem::exists(configFile)) {
        std::stringstream errorMsg;
        if (!DaqDB::readConfiguration(configFile, options, errorMsg)) {
            std::cout << errorMsg.str() << std::flush;
        }
    }
}

max_stars_repo_name: gjerecze/daqdb

max_issues_repo_name: gjerecze/daqdb

max_forks_repo_name: gjerecze/daqdb


text:
/*
 * @Description:  5刚性物体的鲁棒姿态估计  http://robot.czxy.com/docs/pcl/chapter03/registration/#_9
 * https://blog.csdn.net/zfjBIT/article/details/94566603
 * @Author: HCQ
 * @Company(School): UCAS
 * @Email: 1756260160@qq.com
 * @Date: 2020-10-22 19:29:32
 * @LastEditTime: 2020-10-23 10:36:17
 * @FilePath: /pcl-learning/14registration配准/5刚性物体的鲁棒姿态估计/alignment_prerejective.cpp
 */

#include <Eigen/Core>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/common/time.h>
#include <pcl/console/print.h>
#include <pcl/features/normal_3d_omp.h>
#include <pcl/features/fpfh_omp.h>
#include <pcl/filters/filter.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/io/pcd_io.h>
#include <pcl/registration/icp.h>
#include <pcl/registration/sample_consensus_prerejective.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/visualization/pcl_visualizer.h>

// 首先从定义类型开始，以免使代码混乱
// Types
typedef pcl::PointNormal PointNT;
typedef pcl::PointCloud<PointNT> PointCloudT;
typedef pcl::FPFHSignature33 FeatureT;
typedef pcl::FPFHEstimationOMP<PointNT,PointNT,FeatureT> FeatureEstimationT;
typedef pcl::PointCloud<FeatureT> FeatureCloudT;
typedef pcl::visualization::PointCloudColorHandlerCustom<PointNT> ColorHandlerT;

// Align a rigid object to a scene with clutter and occlusions
int
main (int argc, char **argv)
{
  // 然后，我们实例化必要的数据容器，检查输入参数并加载对象和场景点云。
  // Point clouds
  PointCloudT::Ptr object (new PointCloudT);
  PointCloudT::Ptr object_aligned (new PointCloudT);
  PointCloudT::Ptr scene (new PointCloudT);
  FeatureCloudT::Ptr object_features (new FeatureCloudT);
  FeatureCloudT::Ptr scene_features (new FeatureCloudT);

  // Get input object and scene
  if (argc != 3)
  {
    pcl::console::print_error ("Syntax is: %s object.pcd scene.pcd\n", argv[0]);
    return (1);
  }

  // Load object and scene
  pcl::console::print_highlight ("Loading point clouds...\n");
  if (pcl::io::loadPCDFile<PointNT> (argv[1], *object) < 0 ||  pcl::io::loadPCDFile<PointNT> (argv[2], *scene) < 0) // 判断加载点云文件失败
  {
    pcl::console::print_error ("Error loading object/scene file!\n");
    return (1);
  }

  // 1 Downsample 下采样：使用0.005提速分辨率对目标物体和场景点云进行空间下采样
  // 为了加快处理速度，我们使用PCL的：pcl::VoxelGrid类将对象和场景点云的采样率下采样至5 mm。
  pcl::console::print_highlight ("Downsampling...\n");
  pcl::VoxelGrid<PointNT> grid;
  const float leaf = 0.005f;
  grid.setLeafSize (leaf, leaf, leaf);
  grid.setInputCloud (object);  // 对象下采样
  grid.filter (*object);
  grid.setInputCloud (scene); // 场景下采样
  grid.filter (*scene);

  // 2 估计场景法线 Estimate normals for scene
  pcl::console::print_highlight ("Estimating scene normals...\n");
  pcl::NormalEstimationOMP<PointNT,PointNT> nest;
  nest.setRadiusSearch (0.01);
  nest.setInputCloud (scene);
  nest.compute (*scene);

  // 3 特征估计 Estimate features
  // 对于下采样点云中的每个点，我们现在使用PCL的pcl::FPFHEstimationOMP<>类来计算用于对齐过程中用于匹配的快速点特征直方图（FPFH）描述符。
  pcl::console::print_highlight ("Estimating features...\n");
  FeatureEstimationT fest;
  fest.setRadiusSearch (0.025);
  fest.setInputCloud (object);
  fest.setInputNormals (object);
  fest.compute (*object_features);
  fest.setInputCloud (scene);
  fest.setInputNormals (scene);
  fest.compute (*scene_features);

  // 4 对齐配准对象创建与配置  实施配准Perform alignment
  // SampleConsensusPrerejective 实现了有效的RANSAC姿势估计循环
  pcl::console::print_highlight ("Starting alignment...\n");
  pcl::SampleConsensusPrerejective<PointNT,PointNT,FeatureT> align; // //基于采样一致性的位姿估计
  align.setInputSource (object); // 对象
  align.setSourceFeatures (object_features); // 对象特征
  align.setInputTarget (scene);  // 场景
  align.setTargetFeatures (scene_features); //  场景特征
  align.setMaximumIterations (50000); // 采样一致性迭代次数 Number of RANSAC iterations
  align.setNumberOfSamples (3); // 在对象和场景之间进行采样的点对应数。 至少需要3个点才能计算姿势。Number of points to sample for generating/prerejecting a pose
  align.setCorrespondenceRandomness (5); //  使用的临近特征点的数目 Number of nearest features to use
  align.setSimilarityThreshold (0.9f); // 多边形边长度相似度阈值 Polygonal edge length similarity threshold
  align.setMaxCorrespondenceDistance (2.5f * leaf); // 判断是否为内点的距离阈值 Inlier threshold
  align.setInlierFraction (0.25f); //  接受位姿假设所需的内点比例 Required inlier fraction for accepting a pose hypothesis
  {
    pcl::ScopeTime t("Alignment");
    align.align (*object_aligned); // 对齐的对象存储在点云object_aligned中。
  }

  if (align.hasConverged ())
  {
    // Print results
    printf ("\n");
    Eigen::Matrix4f transformation = align.getFinalTransformation ();
    pcl::console::print_info ("    | %6.3f %6.3f %6.3f | \n", transformation (0,0), transformation (0,1), transformation (0,2));
    pcl::console::print_info ("R = | %6.3f %6.3f %6.3f | \n", transformation (1,0), transformation (1,1), transformation (1,2));
    pcl::console::print_info ("    | %6.3f %6.3f %6.3f | \n", transformation (2,0), transformation (2,1), transformation (2,2));
    pcl::console::print_info ("\n");
    pcl::console::print_info ("t = < %0.3f, %0.3f, %0.3f >\n", transformation (0,3), transformation (1,3), transformation (2,3));
    pcl::console::print_info ("\n");
    pcl::console::print_info ("Inliers: %i/%i\n", align.getInliers ().size (), object->size ()); // Inliers: 1384/3432

    // Show alignment
    pcl::visualization::PCLVisualizer visu("Alignment-刚性物体的鲁棒姿态估计");
    visu.addPointCloud (scene, ColorHandlerT (scene, 0.0, 255.0, 0.0), "scene");
    visu.addPointCloud (object_aligned, ColorHandlerT (object_aligned, 0.0, 0.0, 255.0), "object_aligned");
    visu.spin ();
  }
  else
  {
    pcl::console::print_error ("Alignment failed!\n");
    return (1);
  }

  return (0);
}


text:
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2018 Tiago de Paula Peixoto <tiago@skewed.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "graph_tool.hh"
#include "random.hh"

#include <boost/python.hpp>

#include "graph_blockmodel_overlap_util.hh"
#include "graph_blockmodel_overlap.hh"
#include "../blockmodel/graph_blockmodel_gibbs.hh"
#include "../loops/gibbs_loop.hh"

using namespace boost;
using namespace graph_tool;

GEN_DISPATCH(overlap_block_state, OverlapBlockState, OVERLAP_BLOCK_STATE_params)

template<class State>
using OverlapGibbs = Gibbs<State>;

template <class State>
GEN_DISPATCH(overlap_gibbs_block_state,
             OverlapGibbs<State>::template GibbsBlockState,
             GIBBS_BLOCK_STATE_params(State))

python::object gibbs_overlap_sweep(python::object ogibbs_state,
                                   python::object oblock_state,
                                   rng_t& rng)
{
    python::object ret;
    auto dispatch = [&](auto& block_state)
    {
        typedef typename std::remove_reference<decltype(block_state)>::type
            state_t;

        overlap_gibbs_block_state<state_t>::make_dispatch
           (ogibbs_state,
            [&](auto& s)
            {
                auto ret_ = gibbs_sweep(s, rng);
                ret = tuple_apply([&](auto&... args){ return python::make_tuple(args...); }, ret_);
            });
    };
    overlap_block_state::dispatch(oblock_state, dispatch);
    return ret;
}
class gibbs_sweep_base
{
public:
    virtual std::tuple<double, size_t, size_t> run(rng_t&) = 0;
};

template <class State>
class gibbs_sweep_dispatch : public gibbs_sweep_base
{
public:
    gibbs_sweep_dispatch(State& s) : _s(s) {}

    virtual std::tuple<double, size_t, size_t> run(rng_t& rng)
    {
        return gibbs_sweep(_s, rng);
    }
private:
    State _s;
};

python::object gibbs_overlap_sweep_parallel(python::object ogibbs_states,
                                            python::object oblock_states,
                                            rng_t& rng)
{
    std::vector<std::shared_ptr<gibbs_sweep_base>> sweeps;

    size_t N = python::len(ogibbs_states);
    for (size_t i = 0; i < N; ++ i)
    {
        auto dispatch = [&](auto& block_state)
        {
            typedef typename std::remove_reference<decltype(block_state)>::type
                state_t;

            overlap_gibbs_block_state<state_t>::make_dispatch
               (ogibbs_states[i],
                [&](auto& s)
                {
                    typedef typename std::remove_reference<decltype(s)>::type
                        s_t;
                    sweeps.push_back(std::make_shared<gibbs_sweep_dispatch<s_t>>(s));
                });
        };
        overlap_block_state::dispatch(oblock_states[i], dispatch);
    }

    std::vector<std::shared_ptr<rng_t>> rngs;
    init_rngs(rngs, rng);

    std::vector<std::tuple<double, size_t, size_t>> rets(N);

    #pragma omp parallel for schedule(runtime)
    for (size_t i = 0; i < N; ++i)
    {
        auto& rng_ = get_rng(rngs, rng);
        rets[i] = sweeps[i]->run(rng_);
    }

    python::list orets;
    for (auto& ret : rets)
        orets.append(tuple_apply([&](auto&... args){ return python::make_tuple(args...); }, ret));
    return orets;
}

void export_overlap_blockmodel_gibbs()
{
    using namespace boost::python;
    def("gibbs_overlap_sweep", &gibbs_overlap_sweep);
    def("gibbs_overlap_sweep_parallel", &gibbs_overlap_sweep_parallel);
}


text:
// This file is part of the QuickSig library.
// Copyright 2008 Stefan Dragnev.
// The library and all its files are distributed under the MIT license.
// See the full text of the license in the accompanying LICENSE.txt file or at http://www.opensource.org/licenses/mit-license.php

#include "stdafx.h"

#include "../include/Signal.h"
#include "../include/SetStorage.h"

#include <boost/test/auto_unit_test.hpp>

// also test private inheritance
struct PrioritizedCalls : private QuickSig::Trackable<>
{
	friend class QuickSig::TrackableAccess;

	int call;

	PrioritizedCalls() : call(0) {}

	void a() { BOOST_CHECK_EQUAL(call, 3); call++; }
	void b() { BOOST_CHECK_EQUAL(call, 2); call++; }
	void c() { BOOST_CHECK_EQUAL(call, 1); call++; }
	void d() { BOOST_CHECK_EQUAL(call, 0); call++; }

};

template <class Sig>
static void Test()
{
	Sig sig;
	PrioritizedCalls c;

	sig.Connect(&c, &PrioritizedCalls::d, 2);
	sig.Connect(&c, &PrioritizedCalls::b, 0);
	sig.Connect(&c, &PrioritizedCalls::c, 1);
	sig.Connect(&c, &PrioritizedCalls::a, -1);
	sig.Emit();
}

BOOST_AUTO_TEST_CASE(PrioritizedCallsTest)
{
	Test<QuickSig::Signal0<QuickSig::SortedArrayStorage>>();
	Test<QuickSig::Signal0<QuickSig::SetStorage>>();
}

max_stars_repo_name: tailsu/QuickSig

max_issues_repo_name: tailsu/QuickSig

max_forks_repo_name: tailsu/QuickSig


text:
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2009 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include <errno.h>
#include <map>
#include <memory>
#include <sstream>
#include <algorithm>
#include <boost/algorithm/string/replace.hpp>
#include "auth/KeyRing.h"
#include "common/ceph_context.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/Formatter.h"

#define dout_subsys ceph_subsys_auth

#undef dout_prefix
#define dout_prefix *_dout << "auth: "

int KeyRing::from_ceph_context(CephContext *cct)
{
  const auto& conf = cct->_conf;
  string filename;

  int ret = ceph_resolve_file_search(conf->keyring, filename);
  if (!ret) {
    ret = load(cct, filename);
    if (ret < 0)
      lderr(cct) << "failed to load " << filename
		 << ": " << cpp_strerror(ret) << dendl;
  } else {
    lderr(cct) << "unable to find a keyring on " << conf->keyring
	       << ": " << cpp_strerror(ret) << dendl;
  }

  if (!conf->key.empty()) {
    EntityAuth ea;
    try {
      ea.key.decode_base64(conf->key);
      add(conf->name, ea);
      return 0;
    }
    catch (buffer::error& e) {
      lderr(cct) << "failed to decode key '" << conf->key << "'" << dendl;
      return -EINVAL;
    }
  }

  if (!conf->keyfile.empty()) {
    bufferlist bl;
    string err;
    int r = bl.read_file(conf->keyfile.c_str(), &err);
    if (r < 0) {
      lderr(cct) << err << dendl;
      return r;
    }
    string k(bl.c_str(), bl.length());
    EntityAuth ea;
    try {
      ea.key.decode_base64(k);
      add(conf->name, ea);
    }
    catch (buffer::error& e) {
      lderr(cct) << "failed to decode key '" << k << "'" << dendl;
      return -EINVAL;
    }
    return 0;
  }

  return ret;
}

KeyRing *KeyRing::create_empty()
{
  return new KeyRing();
}

int KeyRing::set_modifier(const char *type,
			  const char *val,
			  EntityName& name,
			  map<string, bufferlist>& caps)
{
  if (!val)
    return -EINVAL;

  if (strcmp(type, "key") == 0) {
    CryptoKey key;
    string l(val);
    try {
      key.decode_base64(l);
    } catch (const buffer::error& err) {
      return -EINVAL;
    }
    set_key(name, key);
  } else if (strncmp(type, "caps ", 5) == 0) {
    const char *caps_entity = type + 5;
    if (!*caps_entity)
      return -EINVAL;
    string l(val);
    bufferlist bl;
    encode(l, bl);
    caps[caps_entity] = bl;
    set_caps(name, caps);
  } else if (strcmp(type, "auid") == 0) {
    // just ignore it so we can still decode "old" keyrings that have an auid
  } else
    return -EINVAL;

  return 0;
}

void KeyRing::encode_plaintext(bufferlist& bl)
{
  std::ostringstream os;
  print(os);
  string str = os.str();
  bl.append(str);
}

void KeyRing::encode_formatted(string label, Formatter *f, bufferlist& bl)
{
  f->open_array_section(label.c_str());
  for (map<EntityName, EntityAuth>::iterator p = keys.begin();
       p != keys.end();
       ++p) {

    f->open_object_section("auth_entities");
    f->dump_string("entity", p->first.to_str().c_str());
    std::ostringstream keyss;
    keyss << p->second.key;
    f->dump_string("key", keyss.str());
    f->open_object_section("caps");
    for (map<string, bufferlist>::iterator q = p->second.caps.begin();
	 q != p->second.caps.end();
	 ++q) {
      auto dataiter = q->second.cbegin();
      string caps;
      using ceph::decode;
      decode(caps, dataiter);
      f->dump_string(q->first.c_str(), caps);
    }
    f->close_section();	/* caps */
    f->close_section();	/* auth_entities */
  }
  f->close_section();	/* auth_dump */
  f->flush(bl);
}

void KeyRing::decode_plaintext(bufferlist::const_iterator& bli)
{
  int ret;
  bufferlist bl;
  bli.copy_all(bl);
  ConfFile cf;
  std::deque<std::string> parse_errors;

  if (cf.parse_bufferlist(&bl, &parse_errors, NULL) != 0) {
    throw buffer::malformed_input("cannot parse buffer");
  }

  for (ConfFile::const_section_iter_t s = cf.sections_begin();
	    s != cf.sections_end(); ++s) {
    string name = s->first;
    if (name == "global")
      continue;

    EntityName ename;
    map<string, bufferlist> caps;
    if (!ename.from_str(name)) {
      ostringstream oss;
      oss << "bad entity name in keyring: " << name;
      throw buffer::malformed_input(oss.str().c_str());
    }

    for (ConfSection::const_line_iter_t l = s->second.lines.begin();
	 l != s->second.lines.end(); ++l) {
      if (l->key.empty())
        continue;
      string k(l->key);
      std::replace(k.begin(), k.end(), '_', ' ');
      ret = set_modifier(k.c_str(), l->val.c_str(), ename, caps);
      if (ret < 0) {
	ostringstream oss;
	oss << "error setting modifier for [" << name << "] type=" << k
	    << " val=" << l->val;
	throw buffer::malformed_input(oss.str().c_str());
      }
    }
  }
}

void KeyRing::decode(bufferlist::const_iterator& bl) {
  uint8_t struct_v;
  auto start_pos = bl;
  try {
    using ceph::decode;
    decode(struct_v, bl);
    decode(keys, bl);
  } catch (buffer::error& err) {
    keys.clear();
    decode_plaintext(start_pos);
  }
}

int KeyRing::load(CephContext *cct, const std::string &filename)
{
  if (filename.empty())
    return -EINVAL;

  bufferlist bl;
  std::string err;
  int ret = bl.read_file(filename.c_str(), &err);
  if (ret < 0) {
    lderr(cct) << "error reading file: " << filename << ": " << err << dendl;
    return ret;
  }

  try {
    auto iter = bl.cbegin();
    decode(iter);
  }
  catch (const buffer::error& err) {
    lderr(cct) << "error parsing file " << filename << ": " << err.what() << dendl;
    return -EIO;
  }

  ldout(cct, 2) << "KeyRing::load: loaded key file " << filename << dendl;
  return 0;
}

void KeyRing::print(ostream& out)
{
  for (map<EntityName, EntityAuth>::iterator p = keys.begin();
       p != keys.end();
       ++p) {
    out << "[" << p->first << "]" << std::endl;
    out << "\tkey = " << p->second.key << std::endl;

    for (map<string, bufferlist>::iterator q = p->second.caps.begin();
	 q != p->second.caps.end();
	 ++q) {
      auto dataiter = q->second.cbegin();
      string caps;
      using ceph::decode;
      decode(caps, dataiter);
      boost::replace_all(caps, "\"", "\\\"");
      out << "\tcaps " << q->first << " = \"" << caps << '"' << std::endl;
    }
  }
}

void KeyRing::import(CephContext *cct, KeyRing& other)
{
  for (map<EntityName, EntityAuth>::iterator p = other.keys.begin();
       p != other.keys.end();
       ++p) {
    ldout(cct, 10) << " importing " << p->first << dendl;
    ldout(cct, 30) << "    " << p->second << dendl;
    keys[p->first] = p->second;
  }
}

max_stars_repo_name: liucxer/ceph-msg

max_issues_repo_name: liucxer/ceph-msg

max_forks_repo_name: liucxer/ceph-msg