Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
    /*
     * Initialize the VBox runtime without loading
     * the support driver.
     */
    int rc = RTR3InitExe(argc, &argv, 0);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: RTR3InitExe() -> %d\n", rc);
        return 1;
    }
    if (argc > 1 && !strcmp(argv[1], "-child"))
    {
        /* We have spawned ourselves as a child process -- scratch the leg */
        RTThreadSleep(1000000);
        return 1;
    }
#ifdef RT_OS_WINDOWS
    HRESULT hRes = CoInitialize(NULL);
    /*
     * Need to initialize security to access performance enumerators.
     */
    hRes = CoInitializeSecurity(
        NULL,
        -1,
        NULL,
        NULL,
        RPC_C_AUTHN_LEVEL_NONE,
        RPC_C_IMP_LEVEL_IMPERSONATE,
        NULL, EOAC_NONE, 0);
#endif

    pm::CollectorHAL *collector = pm::createHAL();
    if (!collector)
    {
        RTPrintf("tstCollector: createMetricFactory() failed\n", rc);
        return 1;
    }
#if 1
    pm::CollectorHints hints;
    hints.collectHostCpuLoad();
    hints.collectHostRamUsage();
    hints.collectProcessCpuLoad(RTProcSelf());
    hints.collectProcessRamUsage(RTProcSelf());

    uint64_t start;

    uint64_t hostUserStart, hostKernelStart, hostIdleStart;
    uint64_t hostUserStop, hostKernelStop, hostIdleStop, hostTotal;

    uint64_t processUserStart, processKernelStart, processTotalStart;
    uint64_t processUserStop, processKernelStop, processTotalStop;

    RTPrintf("tstCollector: TESTING - CPU load, sleeping for 5 sec\n");

    rc = collector->preCollect(hints, 0);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
        return 1;
    }

    RTThreadSleep(5000); // Sleep for 5 seconds

    rc = collector->preCollect(hints, 0);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
        return 1;
    }
    hostTotal = hostUserStop - hostUserStart
        + hostKernelStop - hostKernelStart
        + hostIdleStop - hostIdleStart;
    /*printf("tstCollector: host cpu user      = %f sec\n", (hostUserStop - hostUserStart) / 10000000.);
    printf("tstCollector: host cpu kernel    = %f sec\n", (hostKernelStop - hostKernelStart) / 10000000.);
    printf("tstCollector: host cpu idle      = %f sec\n", (hostIdleStop - hostIdleStart) / 10000000.);
    printf("tstCollector: host cpu total     = %f sec\n", hostTotal / 10000000.);*/
    RTPrintf("tstCollector: host cpu user      = %u.%u %%\n",
             (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
             (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
    RTPrintf("tstCollector: host cpu kernel    = %u.%u %%\n",
             (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
             (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
    RTPrintf("tstCollector: host cpu idle      = %u.%u %%\n",
             (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
             (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
    RTPrintf("tstCollector: process cpu user   = %u.%u %%\n",
             (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
             (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
    RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
             (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
             (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));

    RTPrintf("tstCollector: TESTING - CPU load, looping for 5 sec\n");
    rc = collector->preCollect(hints, 0);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawHostCpuLoad(&hostUserStart, &hostKernelStart, &hostIdleStart);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStart, &processKernelStart, &processTotalStart);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
        return 1;
    }
    start = RTTimeMilliTS();
    while(RTTimeMilliTS() - start < 5000)
        ; // Loop for 5 seconds
    rc = collector->preCollect(hints, 0);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawHostCpuLoad(&hostUserStop, &hostKernelStop, &hostIdleStop);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawHostCpuLoad() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getRawProcessCpuLoad(RTProcSelf(), &processUserStop, &processKernelStop, &processTotalStop);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getRawProcessCpuLoad() -> %Rrc\n", rc);
        return 1;
    }
    hostTotal = hostUserStop - hostUserStart
        + hostKernelStop - hostKernelStart
        + hostIdleStop - hostIdleStart;
    RTPrintf("tstCollector: host cpu user      = %u.%u %%\n",
             (unsigned)((hostUserStop - hostUserStart) * 100 / hostTotal),
             (unsigned)((hostUserStop - hostUserStart) * 10000 / hostTotal % 100));
    RTPrintf("tstCollector: host cpu kernel    = %u.%u %%\n",
             (unsigned)((hostKernelStop - hostKernelStart) * 100 / hostTotal),
             (unsigned)((hostKernelStop - hostKernelStart) * 10000 / hostTotal % 100));
    RTPrintf("tstCollector: host cpu idle      = %u.%u %%\n",
             (unsigned)((hostIdleStop - hostIdleStart) * 100 / hostTotal),
             (unsigned)((hostIdleStop - hostIdleStart) * 10000 / hostTotal % 100));
    RTPrintf("tstCollector: process cpu user   = %u.%u %%\n",
             (unsigned)((processUserStop - processUserStart) * 100 / (processTotalStop - processTotalStart)),
             (unsigned)((processUserStop - processUserStart) * 10000 / (processTotalStop - processTotalStart) % 100));
    RTPrintf("tstCollector: process cpu kernel = %u.%u %%\n\n",
             (unsigned)((processKernelStop - processKernelStart) * 100 / (processTotalStop - processTotalStart)),
             (unsigned)((processKernelStop - processKernelStart) * 10000 / (processTotalStop - processTotalStart) % 100));

    RTPrintf("tstCollector: TESTING - Memory usage\n");

    ULONG total, used, available, processUsed;

    rc = collector->getHostMemoryUsage(&total, &used, &available);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getHostMemoryUsage() -> %Rrc\n", rc);
        return 1;
    }
    rc = collector->getProcessMemoryUsage(RTProcSelf(), &processUsed);
    if (RT_FAILURE(rc))
    {
        RTPrintf("tstCollector: getProcessMemoryUsage() -> %Rrc\n", rc);
        return 1;
    }
    RTPrintf("tstCollector: host mem total     = %lu kB\n", total);
    RTPrintf("tstCollector: host mem used      = %lu kB\n", used);
    RTPrintf("tstCollector: host mem available = %lu kB\n", available);
    RTPrintf("tstCollector: process mem used   = %lu kB\n\n", processUsed);
#endif
#if 1
    rc = testNetwork(collector);
#endif
#if 1
    rc = testFsUsage(collector);
#endif
#if 1
    rc = testDisk(collector);
#endif
#if 1
    RTPrintf("tstCollector: TESTING - Performance\n\n");

    measurePerformance(collector, argv[0], 100);
#endif

    delete collector;

    printf ("\ntstCollector FINISHED.\n");

    return rc;
}
Ejemplo n.º 2
0
int main(void)
{
    testDriveParms();
    testDisk();
    return (0);
}