예제 #1
0
    void performConnectionAttempts(std::wstring name, int attempts, int timeout, int instance)
    {

        std::wstring firstName(name.append(L"_First"));
        std::wstring secondName(name.append(L"_Second"));

        std::wcout << std::endl << std::endl << "Running tests instance: " << instance << ", timeout: " << timeout << "ms., attempts: " << attempts << std::endl;

        for (int attemptNo = 0; attemptNo < attempts; ++attemptNo)
        {
            try
            {
                m_firstConnection.Open(firstName,
                                  boost::lexical_cast<std::wstring>(instance),
                                  0, // Context
                                  this,
                                  &m_firstDispatcher);

                m_connectionsFirst->Tick();

            }
            catch(const Safir::Dob::NotOpenException &/*ex*/)
            {
                m_failedConnectionsFirst->Tick();
            }

            try
            {
                m_secondConnection.Open(secondName,
                                  boost::lexical_cast<std::wstring>(instance),
                                  0, // Context
                                  this,
                                  &m_secondDispatcher);

                m_connectionsSecond->Tick();
            }
            catch(const Safir::Dob::NotOpenException &/*ex*/)
            {
                m_failedConnectionsSecond->Tick();
            }

            m_firstConnection.Close();
            m_secondConnection.Close();

            boost::this_thread::sleep_for(boost::chrono::milliseconds(timeout));
        }
    }
예제 #2
0
StatusWith<PerfCounterCollector::CounterInfo> PerfCounterCollector::addCounter(StringData path) {

    PDH_HCOUNTER counter{0};

    PDH_STATUS status =
        PdhAddCounterW(_query, toNativeString(path.toString().c_str()).c_str(), NULL, &counter);

    if (status != ERROR_SUCCESS) {
        return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhAddCounterW", status)};
    }

    DWORD bufferSize = 0;

    status = PdhGetCounterInfoW(counter, false, &bufferSize, nullptr);

    if (status != PDH_MORE_DATA) {
        return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhGetCounterInfoW", status)};
    }

    auto buf = stdx::make_unique<char[]>(bufferSize);
    auto counterInfo = reinterpret_cast<PPDH_COUNTER_INFO>(buf.get());

    status = PdhGetCounterInfoW(counter, false, &bufferSize, counterInfo);

    if (status != ERROR_SUCCESS) {
        return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhGetCounterInfoW", status)};
    }

    // A full qualified path is as such:
    // "\\MYMACHINE\\Processor(0)\\% Idle Time"
    // MachineName \\ Object Name (Instance Name) \\ CounterName
    // Ex:
    //  MachineName: MYMACHINE
    //  Object Name: Processor
    //  InstanceName: 0
    //  CounterName: % Idle Time
    // We do not want to use Machine Name, but sometimes we want InstanceName
    //
    std::string firstName = str::stream() << '\\' << toUtf8String(counterInfo->szObjectName) << '\\'
                                          << toUtf8String(counterInfo->szCounterName);

    // Compute a second name
    std::string secondName(firstName);

    bool hasSecondValue = false;

    // Only include base for counters that need it
    if ((counterInfo->dwType & PERF_COUNTER_PRECISION) == PERF_COUNTER_PRECISION) {
        secondName += " Base";
        hasSecondValue = true;
    }

    // InstanceName is null for counters without instance names
    return {CounterInfo{std::move(firstName),
                        std::move(secondName),
                        hasSecondValue,
                        counterInfo->szInstanceName ? toUtf8String(counterInfo->szInstanceName)
                                                    : std::string(),
                        counterInfo->dwType,
                        counter}};
}