void Monitor::Run()

in nodemanager/core/Monitor.cpp [513:629]


void Monitor::Run()
{
    uint64_t cpuLast = 0, idleLast = 0;
    std::map<std::string, uint64_t> networkLast;
    int collectCount = 0;

    while (true)
    {
        time_t t;
        time(&t);

        uint64_t cpuCurrent = cpuLast + 1, idleCurrent = idleLast;

        System::CPUUsage(cpuCurrent, idleCurrent);
        uint64_t totalDiff = cpuCurrent - cpuLast;
        uint64_t idleDiff = idleCurrent - idleLast;
        float cpuUsage = (float)(100.0f * (totalDiff - idleDiff) / totalDiff);
        cpuLast = cpuCurrent;
        idleLast = idleCurrent;

        uint64_t available, total;
        System::Memory(available, total);
        float availableMemoryMb = (float)available / 1024.0f;
        float totalMemoryMb = (float)total / 1024.0f;

        float freeSpacePercent = 0.0f, queueLength = 0.0f, pagesPerSec = 0.0f, contextSwitchesPerSec = 0.0f, bytesPerSecond = 0.0f;
        System::FreeSpace(freeSpacePercent);
        System::IostatX(queueLength);
        System::Vmstat(pagesPerSec, contextSwitchesPerSec);
        System::Iostat(bytesPerSecond);

        // network usage
        auto networkUsage = System::GetNetworkUsage();
        for (const auto & pair : networkUsage)
        {
            auto networkName = pair.first;
            auto networkCurrent = pair.second;
            if (networkLast.find(networkName) == networkLast.end())
            {
                networkLast[networkName] = 0;
            }

            networkUsage[networkName] = (networkCurrent - networkLast[networkName]) / this->intervalSeconds;
            networkLast[networkName] = networkCurrent;
        }

        // ip address;
        std::string ipAddress = System::GetIpAddress(IpAddressVersion::V4, this->networkName);

        // cpu type;
        int cores, sockets;
        System::CPU(cores, sockets);

        // distro;
        const std::string& distro = System::GetDistroInfo();

        // networks;
        auto netInfo = System::GetNetworkInfo();

        // GPU
        System::GpuInfoList gpuInfo;
        if (this->gpuInitRet == 0)
        {
            this->gpuInitRet = System::QueryGpuInfo(gpuInfo);
        }

        auto queryMetadata = collectCount % 30 == 0;
        std::string metaData = "";
        if (queryMetadata)
        {
            metaData = this->QueryAzureInstanceMetadata();
        }

        {
            WriterLock writerLock(&this->lock);

            this->metricTime = ctime(&t);

            this->cpuUsage = cpuUsage;
            this->availableMemoryMb = availableMemoryMb;
            this->networkUsage = std::move(networkUsage);

            this->totalMemoryMb = totalMemoryMb;
            this->ipAddress = ipAddress;
            this->coreCount = cores;
            this->socketCount = sockets;
            this->distroInfo = distro;
            this->networkInfo = std::move(netInfo);

            this->freeSpacePercent = freeSpacePercent;
            this->queueLength = queueLength;
            this->pagesPerSec = pagesPerSec;
            this->contextSwitchesPerSec = contextSwitchesPerSec;
            this->bytesPerSecond = bytesPerSecond;

            if (this->gpuInitRet == 0)
            {
                if (NodeManagerConfig::GetDebug())
                {
                    Logger::Debug("Saving Gpu Info ret {0}, info count {1}", this->gpuInitRet, gpuInfo.GpuInfos.size());
                }
                
                this->gpuInfo = std::move(gpuInfo);
            }

            if (queryMetadata)
            {
                this->azureInstanceMetadata = metaData;
            }
        }

        this->isCollected = true;

        sleep(this->intervalSeconds);
        collectCount++;
    }
}