Exemple #1
0
int CollectorLinux::getRawHostNetworkLoad(const char *pszFile, uint64_t *rx, uint64_t *tx)
{
    char szIfName[/*IFNAMSIZ*/ 16 + 36];

    RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/rx_bytes", pszFile);
    if (!RTLinuxSysFsExists(szIfName))
        return VERR_FILE_NOT_FOUND;

    int64_t cSize = RTLinuxSysFsReadIntFile(0, szIfName);
    if (cSize < 0)
        return VERR_ACCESS_DENIED;

    *rx = cSize;

    RTStrPrintf(szIfName, sizeof(szIfName), "/sys/class/net/%s/statistics/tx_bytes", pszFile);
    if (!RTLinuxSysFsExists(szIfName))
        return VERR_FILE_NOT_FOUND;

    cSize = RTLinuxSysFsReadIntFile(0, szIfName);
    if (cSize < 0)
        return VERR_ACCESS_DENIED;

    *tx = cSize;
    return VINF_SUCCESS;
}
Exemple #2
0
RTDECL(RTCPUID) RTMpGetOnlineCoreCount(void)
{
    RTCPUID     cMax      = rtMpLinuxMaxCpus();
    uint32_t   *paidCores = (uint32_t *)alloca(sizeof(paidCores[0]) * (cMax + 1));
    uint32_t   *paidPckgs = (uint32_t *)alloca(sizeof(paidPckgs[0]) * (cMax + 1));
    uint32_t    cCores    = 0;
    for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
    {
        if (RTMpIsCpuOnline(idCpu))
        {
            uint32_t idCore = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);
            uint32_t idPckg = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/physical_package_id", (int)idCpu);
            uint32_t i;
            for (i = 0; i < cCores; i++)
                if (   paidCores[i] == idCore
                    && paidPckgs[i] == idPckg)
                    break;
            if (i >= cCores)
            {
                paidCores[cCores] = idCore;
                paidPckgs[cCores] = idPckg;
                cCores++;
            }
        }
    }
    Assert(cCores > 0);
    return cCores;
}
Exemple #3
0
 /** Check whether the sysfs block entry is valid for a DVD device and
  * initialise the string data members for the object.  We try to get all
  * the information we need from sysfs if possible, to avoid unnecessarily
  * poking the device, and if that fails we fall back to an SCSI INQUIRY
  * command. */
 void validateAndInitForDVD()
 {
     char szVendor[128], szModel[128];
     ssize_t cchVendor, cchModel;
     int64_t type = RTLinuxSysFsReadIntFile(10, "block/%s/device/type",
                                            mpcszName);
     if (type >= 0 && type != TYPE_ROM)
         return;
     if (type == TYPE_ROM)
     {
         cchVendor = RTLinuxSysFsReadStrFile(szVendor, sizeof(szVendor),
                                             "block/%s/device/vendor",
                                             mpcszName);
         if (cchVendor >= 0)
         {
             cchModel = RTLinuxSysFsReadStrFile(szModel, sizeof(szModel),
                                                "block/%s/device/model",
                                                mpcszName);
             if (cchModel >= 0)
             {
                 misValid = true;
                 dvdCreateDeviceStrings(szVendor, szModel,
                                        mszDesc, sizeof(mszDesc),
                                        mszUdi, sizeof(mszUdi));
                 return;
             }
         }
     }
     if (!noProbe())
         probeAndInitForDVD();
 }
 /** Check whether the sysfs block entry is valid for a DVD device and
  * initialise the string data members for the object.  We try to get all
  * the information we need from sysfs if possible, to avoid unnecessarily
  * poking the device, and if that fails we fall back to an SCSI INQUIRY
  * command. */
 void validateAndInitForDVD()
 {
     char szVendor[128], szModel[128];
     int64_t type = 0;
     int rc = RTLinuxSysFsReadIntFile(10, &type, "block/%s/device/type", mpcszName);
     if (RT_SUCCESS(rc) && type != TYPE_ROM)
         return;
     if (type == TYPE_ROM)
     {
         rc = RTLinuxSysFsReadStrFile(szVendor, sizeof(szVendor), NULL,
                                      "block/%s/device/vendor", mpcszName);
         if (RT_SUCCESS(rc))
         {
             rc = RTLinuxSysFsReadStrFile(szModel, sizeof(szModel), NULL,
                                          "block/%s/device/model", mpcszName);
             if (RT_SUCCESS(rc))
             {
                 misValid = true;
                 dvdCreateDeviceStrings(szVendor, szModel,
                                        mszDesc, sizeof(mszDesc),
                                        mszUdi, sizeof(mszUdi));
                 return;
             }
         }
     }
     if (!noProbe())
         probeAndInitForDVD();
 }
Exemple #5
0
RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu)
{
    int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu);
    if (kHz == -1)
    {
        /*
         * Check if the file isn't there - if it is there, then /proc/cpuinfo
         * would provide current frequency information, which is wrong.
         */
        if (!RTLinuxSysFsExists("devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu))
            kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
        else
            kHz = 0;
    }
    return (kHz + 999) / 1000;
}
Exemple #6
0
RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu)
{
    int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", (int)idCpu);
    if (kHz == -1)
    {
        /*
         * The file may be just unreadable - in that case use plan B, i.e.
         * /proc/cpuinfo to get the data we want. The assumption is that if
         * cpuinfo_cur_freq doesn't exist then the speed won't change, and
         * thus cur == max. If it does exist then cpuinfo contains the
         * current frequency.
         */
        kHz = rtMpLinuxGetFrequency(idCpu) * 1000;
    }
    return (kHz + 999) / 1000;
}
Exemple #7
0
RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
{
    /** @todo check if there is a simpler interface than this... */
    int i = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/online", (int)idCpu);
    if (    i == -1
        &&  RTLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu))
    {
        /** @todo Assert(!RTLinuxSysFsExists("devices/system/cpu/cpu%d/online",
         *               (int)idCpu));
         * Unfortunately, the online file wasn't always world readable (centos
         * 2.6.18-164). */
        i = 1;
    }

    AssertMsg(i == 0 || i == -1 || i == 1, ("i=%d\n", i));
    return i != 0 && i != -1;
}
Exemple #8
0
int CollectorLinux::getHostDiskSize(const char *pszFile, uint64_t *size)
{
    char *pszPath = NULL;

    RTStrAPrintf(&pszPath, "/sys/block/%s/size", pszFile);
    Assert(pszPath);

    int rc = VINF_SUCCESS;
    if (!RTLinuxSysFsExists(pszPath))
        rc = VERR_FILE_NOT_FOUND;
    else
    {
        int64_t cSize = RTLinuxSysFsReadIntFile(0, pszPath);
        if (cSize < 0)
            rc = VERR_ACCESS_DENIED;
        else
            *size = cSize * 512;
    }
    RTStrFree(pszPath);
    return rc;
}
/**
 * Returns the path of the ACPI CPU device with the given core and package ID.
 *
 * @returns VBox status code.
 * @param   ppszPath     Where to store the path.
 * @param   idCpuCore    The core ID of the CPU.
 * @param   idCpuPackage The package ID of the CPU.
 */
static int VBoxServiceCpuHotPlugGetACPIDevicePath(char **ppszPath, uint32_t idCpuCore, uint32_t idCpuPackage)
{
    int rc = VINF_SUCCESS;

    AssertPtrReturn(ppszPath, VERR_INVALID_PARAMETER);

    rc = VBoxServiceCpuHotPlugProbePath();
    if (RT_SUCCESS(rc))
    {
        /* Build the path from all components. */
        bool fFound = false;
        unsigned iLvlCurr = 0;
        char *pszPath = NULL;
        char *pszPathDir = NULL;
        PSYSFSCPUPATH pAcpiCpuPathLvl = &g_aAcpiCpuPath[iLvlCurr];

        /* Init everything. */
        Assert(pAcpiCpuPathLvl->uId != ACPI_CPU_PATH_NOT_PROBED);
        pszPath = RTPathJoinA(SYSFS_ACPI_CPU_PATH, pAcpiCpuPathLvl->aComponentsPossible[pAcpiCpuPathLvl->uId].pcszName);
        if (!pszPath)
            return VERR_NO_STR_MEMORY;

        pAcpiCpuPathLvl->pszPath = RTStrDup(SYSFS_ACPI_CPU_PATH);
        if (!pAcpiCpuPathLvl->pszPath)
        {
            RTStrFree(pszPath);
            return VERR_NO_STR_MEMORY;
        }

        /* Open the directory */
        rc = RTDirOpenFiltered(&pAcpiCpuPathLvl->pDir, pszPath, RTDIRFILTER_WINNT, 0);
        if (RT_SUCCESS(rc))
        {
            RTStrFree(pszPath);

            /* Search for CPU */
            while (!fFound)
            {
                /* Get the next directory. */
                RTDIRENTRY DirFolderContent;
                rc = RTDirRead(pAcpiCpuPathLvl->pDir, &DirFolderContent, NULL);
                if (RT_SUCCESS(rc))
                {
                    /* Create the new path. */
                    char *pszPathCurr = RTPathJoinA(pAcpiCpuPathLvl->pszPath, DirFolderContent.szName);
                    if (!pszPathCurr)
                    {
                        rc = VERR_NO_STR_MEMORY;
                        break;
                    }

                    /* If this is the last level check for the given core and package id. */
                    if (iLvlCurr == RT_ELEMENTS(g_aAcpiCpuPath) - 1)
                    {
                        /* Get the sysdev */
                        uint32_t idCore    = RTLinuxSysFsReadIntFile(10, "%s/sysdev/topology/core_id",
                                                                     pszPathCurr);
                        uint32_t idPackage = RTLinuxSysFsReadIntFile(10, "%s/sysdev/topology/physical_package_id",
                                                                     pszPathCurr);
                        if (   idCore    == idCpuCore
                            && idPackage == idCpuPackage)
                        {
                            /* Return the path */
                            pszPath = pszPathCurr;
                            fFound = true;
                            VBoxServiceVerbose(3, "CPU found\n");
                            break;
                        }
                        else
                        {
                            /* Get the next directory. */
                            RTStrFree(pszPathCurr);
                            VBoxServiceVerbose(3, "CPU doesn't match, next directory\n");
                        }
                    }
                    else
                    {
                        /* Go deeper */
                        iLvlCurr++;

                        VBoxServiceVerbose(3, "Going deeper (iLvlCurr=%u)\n", iLvlCurr);

                        pAcpiCpuPathLvl = &g_aAcpiCpuPath[iLvlCurr];

                        Assert(!pAcpiCpuPathLvl->pDir);
                        Assert(!pAcpiCpuPathLvl->pszPath);
                        pAcpiCpuPathLvl->pszPath = pszPathCurr;
                        PCSYSFSCPUPATHCOMP pPathComponent = &pAcpiCpuPathLvl->aComponentsPossible[pAcpiCpuPathLvl->uId];

                        Assert(pAcpiCpuPathLvl->uId != ACPI_CPU_PATH_NOT_PROBED);

                        pszPathDir = RTPathJoinA(pszPathCurr, pPathComponent->pcszName);
                        if (!pszPathDir)
                        {
                            rc = VERR_NO_STR_MEMORY;
                            break;
                        }

                        VBoxServiceVerbose(3, "New path %s\n", pszPathDir);

                        /* Open the directory */
                        rc = RTDirOpenFiltered(&pAcpiCpuPathLvl->pDir, pszPathDir, RTDIRFILTER_WINNT, 0);
                        if (RT_FAILURE(rc))
                            break;
                    }
                }
                else
                {
                    /* Go back one level and try to get the next entry. */
                    Assert(iLvlCurr > 0);

                    RTDirClose(pAcpiCpuPathLvl->pDir);
                    RTStrFree(pAcpiCpuPathLvl->pszPath);
                    pAcpiCpuPathLvl->pDir = NULL;
                    pAcpiCpuPathLvl->pszPath = NULL;

                    iLvlCurr--;
                    pAcpiCpuPathLvl = &g_aAcpiCpuPath[iLvlCurr];
                    VBoxServiceVerbose(3, "Directory not found, going back (iLvlCurr=%u)\n", iLvlCurr);
                }
            } /* while not found */
        } /* Successful init */

        /* Cleanup */
        for (unsigned i = 0; i < RT_ELEMENTS(g_aAcpiCpuPath); i++)
        {
            if (g_aAcpiCpuPath[i].pDir)
                RTDirClose(g_aAcpiCpuPath[i].pDir);
            if (g_aAcpiCpuPath[i].pszPath)
                RTStrFree(g_aAcpiCpuPath[i].pszPath);
            g_aAcpiCpuPath[i].pDir = NULL;
            g_aAcpiCpuPath[i].pszPath = NULL;
        }
        if (pszPathDir)
            RTStrFree(pszPathDir);
        if (RT_FAILURE(rc) && pszPath)
            RTStrFree(pszPath);

        if (RT_SUCCESS(rc))
            *ppszPath = pszPath;
    }

    return rc;
}