コード例 #1
0
ファイル: os_info.c プロジェクト: imvu-open/libstatgrab
sg_host_info *sg_get_host_info()
{
    static sg_host_info general_stat;
#ifndef WIN32
    static struct utsname os;
#endif

#ifdef HPUX
    struct pst_static *pstat_static;
    time_t currtime;
    long boottime;
#endif
#ifdef SOLARIS
    time_t boottime,curtime;
    kstat_ctl_t *kc;
    kstat_t *ksp;
    kstat_named_t *kn;
#endif
#if defined(LINUX) || defined(CYGWIN)
    FILE *f;
#endif
#ifdef ALLBSD
    int mib[2];
    struct timeval boottime;
    time_t curtime;
    size_t size;
#endif
#ifdef WIN32
    unsigned long nameln;
    char *name;
    long long result;
    OSVERSIONINFOEX osinfo;
    SYSTEM_INFO sysinfo;
    char *tmp_name;
    char tmp[10];
#endif

#ifndef WIN32 /* Trust windows to be different */
    if((uname(&os)) < 0) {
        sg_set_error_with_errno(SG_ERROR_UNAME, NULL);
        return NULL;
    }

    general_stat.os_name = os.sysname;
    general_stat.os_release = os.release;
    general_stat.os_version = os.version;
    general_stat.platform = os.machine;
    general_stat.hostname = os.nodename;
#else /* WIN32 */
    if (!runonce) {
        /* these settings are static after boot, so why get them
         * constantly? */

        /* get system name */
        nameln = MAX_COMPUTERNAME_LENGTH + 1;
        name = sg_malloc(nameln);
        if(name == NULL) {
            return NULL;
        }
        if(GetComputerName(name, &nameln) == 0) {
            free(name);
            sg_set_error(SG_ERROR_HOST, "GetComputerName");
            return NULL;
        }
        if(sg_update_string(&general_stat.hostname, name)) {
            free(name);
            return NULL;
        }
        free(name);

        /* get OS name, version and build */
        ZeroMemory(&osinfo, sizeof(OSVERSIONINFOEX));
        osinfo.dwOSVersionInfoSize = sizeof(osinfo);
        if(!GetVersionEx(&osinfo)) {
            sg_set_error(SG_ERROR_HOST, "GetVersionEx");
            return NULL;
        }

        /* Release - single number */
        if(snprintf(tmp, sizeof(tmp), "%ld", osinfo.dwBuildNumber) == -1) {
            free(tmp);
            return NULL;
        }
        if(sg_update_string(&general_stat.os_release, tmp)) {
            free(tmp);
            return NULL;
        }

        /* Version */
        /* usually a single digit . single digit, eg 5.0 */
        if(snprintf(tmp, sizeof(tmp), "%ld.%ld", osinfo.dwMajorVersion,
                    osinfo.dwMinorVersion) == -1) {
            free(tmp);
            return NULL;
        }
        if(sg_update_string(&general_stat.os_version, tmp)) {
            free(tmp);
            return NULL;
        }

        /* OS name */
        tmp_name = get_os_name(osinfo);
        if(tmp_name == NULL) {
            return NULL;
        }
        if(sg_update_string(&general_stat.os_name, tmp_name)) {
            free(tmp_name);
            return NULL;
        }
        free(tmp_name);
        runonce = 1;

        /* Platform */
        GetSystemInfo(&sysinfo);
        switch(sysinfo.wProcessorArchitecture) {
        case PROCESSOR_ARCHITECTURE_INTEL:
            if(sg_update_string(&general_stat.platform,
                                "Intel")) {
                return NULL;
            }
            break;
        case PROCESSOR_ARCHITECTURE_IA64:
            if(sg_update_string(&general_stat.platform,
                                "IA64")) {
                return NULL;
            }
            break;
        case PROCESSOR_ARCHITECTURE_AMD64:
            if(sg_update_string(&general_stat.platform,
                                "AMD64")) {
                return NULL;
            }
            break;
        default:
            if(sg_update_string(&general_stat.platform,
                                "Unknown")) {
                return NULL;
            }
            break;
        }
    }
#endif /* WIN32 */

    /* get uptime */
#ifdef HPUX
    pstat_static = sg_get_pstat_static();
    if (pstat_static == NULL) {
        return NULL;
    }

    currtime = time(NULL);

    boottime = pstat_static->boot_time;

    general_stat.uptime = currtime - boottime;
#endif
#ifdef SOLARIS
    if ((kc = kstat_open()) == NULL) {
        sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
        return NULL;
    }
    if((ksp=kstat_lookup(kc, "unix", -1, "system_misc"))==NULL) {
        sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,-1,system_misc");
        kstat_close(kc);
        return NULL;
    }
    if (kstat_read(kc, ksp, 0) == -1) {
        sg_set_error(SG_ERROR_KSTAT_READ, NULL);
        kstat_close(kc);
        return NULL;
    }
    if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL) {
        sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "boot_time");
        kstat_close(kc);
        return NULL;
    }
    boottime=(kn->value.ui32);

    kstat_close(kc);

    time(&curtime);
    general_stat.uptime = curtime - boottime;
#endif
#if defined(LINUX) || defined(CYGWIN)
    if ((f=fopen("/proc/uptime", "r")) == NULL) {
        sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/uptime");
        return NULL;
    }
    if((fscanf(f,"%lu %*d",&general_stat.uptime)) != 1) {
        sg_set_error(SG_ERROR_PARSE, NULL);
        return NULL;
    }
    fclose(f);
#endif
#ifdef ALLBSD
    mib[0] = CTL_KERN;
    mib[1] = KERN_BOOTTIME;
    size = sizeof boottime;
    if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
        sg_set_error_with_errno(SG_ERROR_SYSCTL,
                                "CTL_KERN.KERN_BOOTTIME");
        return NULL;
    }
    time(&curtime);
    general_stat.uptime=curtime-boottime.tv_sec;
#endif
#ifdef WIN32
    if(read_counter_large(SG_WIN32_UPTIME, &result)) {
        sg_set_error(SG_ERROR_PDHREAD, PDH_UPTIME);
        return NULL;
    }
    general_stat.uptime = (time_t) result;
#endif

    return &general_stat;

}
コード例 #2
0
int get_mem_stats(Digikam::KMemoryInfo::KMemoryInfoData* const data)
{

#ifdef Q_OS_HPUX
    struct pst_static* pstat_static = 0;
    struct pst_dynamic pstat_dynamic;
    long long          pagesize;
#endif // Q_OS_HPUX

#ifdef Q_OS_SOLARIS
    kstat_ctl_t*   kc  = 0;
    kstat_t*       ksp = 0;
    kstat_named_t* kn  = 0;
    long           totalmem;
    int            pagesize;
#endif // Q_OS_SOLARIS

#if defined(Q_OS_LINUX) || defined(Q_OS_CYGWIN)
    char*              line_ptr = 0;
    unsigned long long value;
    FILE*              f        = 0;
#endif // defined(Q_OS_LINUX) || defined(Q_OS_CYGWIN)

#if defined(Q_OS_FREEBSD) || defined(Q_OS_DFBSD)
    int    mib[2];
    u_long physmem;
    size_t size;
    u_long free_count;
    u_long cache_count;
    u_long inactive_count;
    int    pagesize;
#endif // defined(Q_OS_FREEBSD) || defined(Q_OS_DFBSD)

#if defined(Q_OS_NETBSD)
    struct uvmexp* uvm = 0;
#endif // defined(Q_OS_NETBSD)

#if defined(Q_OS_OPENBSD)
    int    mib[2];
    struct vmtotal vmtotal;
    size_t size;
    int    pagesize, page_multiplier;
#endif // defined(Q_OS_OPENBSD)

#ifdef Q_OS_WIN
    MEMORYSTATUSEX memstats;
#endif

#ifdef Q_WS_MAC
    Q_UNUSED(data);
#endif

#ifdef Q_OS_HPUX
    data->platform = QString("HPUX");

    if((pagesize = sysconf(_SC_PAGESIZE)) == -1)
    {
        //sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
        return 0;
    }

    if (pstat_getdynamic(&pstat_dynamic, sizeof(pstat_dynamic), 1, 0) == -1)
    {
        //sg_set_error_with_errno(SG_ERROR_PSTAT, "pstat_dynamic");
        return 0;
    }

    pstat_static = sg_get_pstat_static();

    if (pstat_static == NULL)
    {
        return 0;
    }

    /* FIXME Does this include swap? */
    data->totalRam = ((long long) pstat_static->physical_memory) * pagesize;
    data->freeRam  = ((long long) pstat_dynamic.psd_free)        * pagesize;
    data->usedRam  = data->totalRam - data->freeRam;

    return 1;
#endif // Q_OS_HPUX

#ifdef Q_OS_SOLARIS
    data->platform = QString("SOLARIS");

    if((pagesize = sysconf(_SC_PAGESIZE)) == -1)
    {
        //sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE");
        return 0;
    }

    if((totalmem = sysconf(_SC_PHYS_PAGES)) == -1)
    {
        //sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PHYS_PAGES");
        return 0;
    }

    if ((kc = kstat_open()) == NULL)
    {
        //sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
        return 0;
    }

    if((ksp = kstat_lookup(kc, "unix", 0, "system_pages")) == NULL)
    {
        //sg_set_error(SG_ERROR_KSTAT_LOOKUP, "unix,0,system_pages");
        return 0;
    }

    if (kstat_read(kc, ksp, 0) == -1)
    {
        //sg_set_error(SG_ERROR_KSTAT_READ, NULL);
        return 0;
    }

    if((kn = (kstat_named_t*)kstat_data_lookup(ksp, "freemem")) == NULL)
    {
        //sg_set_error(SG_ERROR_KSTAT_DATA_LOOKUP, "freemem");
        return 0;
    }

    kstat_close(kc);

    data->totalRam = (long long)totalmem * (long long)pagesize;
    data->freeRam  = ((long long)kn->value.ul) * (long long)pagesize;
    data->usedRam  = data->totalRam - data->freeRam;

    return 1;
#endif // Q_OS_SOLARIS

#if defined(Q_OS_LINUX) || defined(Q_OS_CYGWIN)
    data->platform = QString("LINUX");

    if ((f = fopen("/proc/meminfo", "r")) == NULL)
    {
        //sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo");
        return 0;
    }

    while ((line_ptr = sg_f_read_line(f, "")) != NULL)
    {
        if (sscanf(line_ptr, "%*s %llu kB", &value) != 1)
        {
            continue;
        }

        value *= 1024;

        if (strncmp(line_ptr, "MemTotal:", 9) == 0)
        {
            data->totalRam = value;
        }
        else if (strncmp(line_ptr, "MemFree:", 8) == 0)
        {
            data->freeRam = value;
        }
        else if (strncmp(line_ptr, "Cached:", 7) == 0)
        {
            data->cacheRam = value;
        }
    }

    fclose(f);
    data->usedRam = data->totalRam - data->freeRam;

    return 1;
#endif // defined(Q_OS_LINUX) || defined(Q_OS_CYGWIN)

#if defined(Q_OS_FREEBSD)
    data->platform = QString("FREEBSD");

    /* Returns bytes */
    mib[0] = CTL_HW;
    mib[1] = HW_PHYSMEM;
    size   = sizeof physmem;

    if (sysctl(mib, 2, &physmem, &size, NULL, 0) < 0)
    {
        //sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_PHYSMEM");
        return 0;
    }

    data->totalRam = physmem;

    /*returns pages*/
    size           = sizeof free_count;

    if (sysctlbyname("vm.stats.vm.v_free_count", &free_count, &size, NULL, 0) < 0)
    {
        //sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vm.stats.vm.v_free_count");
        return 0;
    }

    size = sizeof inactive_count;

    if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count , &size, NULL, 0) < 0)
    {
        //sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vm.stats.vm.v_inactive_count");
        return 0;
    }

    size = sizeof cache_count;

    if (sysctlbyname("vm.stats.vm.v_cache_count", &cache_count, &size, NULL, 0) < 0)
    {
        //sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vm.stats.vm.v_cache_count");
        return 0;
    }

    /* Because all the vm.stats returns pages, I need to get the page size.
     * After that I then need to multiple the anything that used vm.stats to
     * get the system statistics by pagesize
     */
    pagesize       = getpagesize();
    data->cacheRam = cache_count * pagesize;

    /* Of couse nothing is ever that simple :) And I have inactive pages to
     * deal with too. So I'm going to add them to free memory :)
     */
    data->freeRam  = (free_count*pagesize)+(inactive_count*pagesize);
    data->usedRam  = physmem-data->freeRam;

    return 1;
#endif // defined(Q_OS_FREEBSD)

#if defined(Q_OS_NETBSD)
    data->platform = QString("NETBSD");

    if ((uvm = sg_get_uvmexp()) == NULL)
    {
        return 0;
    }

    data->totalRam = uvm->pagesize * uvm->npages;
    data->cacheRam = uvm->pagesize * (uvm->filepages + uvm->execpages);
    data->freeRam  = uvm->pagesize * (uvm->free + uvm->inactive);
    data->usedRam  = data->totalRam - data->freeRam;

    return 1;
#endif // defined(Q_OS_NETBSD)

#if defined(Q_OS_OPENBSD)
    data->platform = QString("OPENBSD");

    /* The code in this section is based on the code in the OpenBSD
     * top utility, located at src/usr.bin/top/machine.c in the
     * OpenBSD source tree.
     *
     * For fun, and like OpenBSD top, we will do the multiplication
     * converting the memory stats in pages to bytes in base 2.
     */

    /* All memory stats in OpenBSD are returned as the number of pages.
     * To convert this into the number of bytes we need to know the
     * page size on this system.
     */
    pagesize = sysconf(_SC_PAGESIZE);

    /* The pagesize gives us the base 10 multiplier, so we need to work
     * out what the base 2 multiplier is. This means dividing
     * pagesize by 2 until we reach unity, and counting the number of
     * divisions required.
     */
    page_multiplier = 0;

    while (pagesize > 1)
    {
        page_multiplier++;
        pagesize >>= 1;
    }

    /* We can now ret the raw VM stats (in pages) using the
     * sysctl interface.
     */
    mib[0] = CTL_VM;
    mib[1] = VM_METER;
    size   = sizeof(vmtotal);

    if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0)
    {
        bzero(&vmtotal, sizeof(vmtotal));
        //sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_METER");
        return 0;
    }

    /* Convert the raw stats to bytes, and return these to the caller
     */
    data->usedRam  = (vmtotal.t_rm << page_multiplier);   /* total real mem in use */
    data->cacheRam = 0;                                  /* no cache stats */
    data->freeRam  = (vmtotal.t_free << page_multiplier); /* free memory pages */
    data->totalRam = (data->usedRam + data->freeRam);

    return 1;
#endif // defined(Q_OS_OPENBSD)

#ifdef Q_OS_WIN
    data->platform = QString("WINDOWS");

    memstats.dwLength = sizeof(memstats);

    if (!GlobalMemoryStatusEx(&memstats))
    {
        //sg_set_error_with_errno(SG_ERROR_MEMSTATUS, NULL);
        return 0;
    }

    data->freeRam  = memstats.ullAvailPhys;
    data->totalRam = memstats.ullTotalPhys;
    data->usedRam  = data->totalRam - data->freeRam;

    //if(read_counter_large(SG_WIN32_MEM_CACHE, &data->cacheRam)) {
        data->cacheRam = 0;
    //}

    return 1;
#endif // Q_OS_WIN

    return -1;
}