取得进程信息

#ifdef AIX
INT32 CProcessState::GetProcessState(const INT64 lPid, TProState* pProcessState)
{
    INT32 nRet = ZX_SYS_NO_ERROR;
    char cState = CHAR_SNONE;
    pid_t pid = lPid;
    INT32 nState = 0;
    INT64 lPageSize = 0;
    struct procsinfo pi;
    if (pProcessState == NULL)
    {
        ZX_ERROR(LOG_NORMAL,"pProcessState == NULL");
        return -1;
    }
    lPageSize = sysconf(_SC_PAGE_SIZE);
    if (lPageSize == -1)
    {
        ZX_ERROR(LOG_NORMAL,"sysconf(_SC_PAGE_SIZE)");
        return -1;
    }

    nRet = getprocs(&pi, sizeof(pi), 0, 0, &pid, 1);
    if(nRet != 1 || pi.pi_pid != lPid)
    {
        //ZX_ERROR(LOG_NORMAL,"getprocs()");
        return -1;
    }
    struct tm tmNow;
    time_t timeNow;
    time(&timeNow);
    perfstat_cpu_total_t userbuff;
    nRet = perfstat_cpu_total(NULL, &userbuff, sizeof(perfstat_cpu_total_t), 1);
    if (nRet != 1)
    {
        ZX_ERROR(LOG_NORMAL,"perfstat_cpu_total()");
        return -1;
    }

    nState = (INT32)pi.pi_state;
    switch (nState)
    {
    case SNONE:
    default:
        cState = CHAR_SNONE;
        break;
    case SIDL:
        cState = CHAR_SIDL;
        break;
    case SZOMB:
        cState = CHAR_SZOMB;
        break;
    case SSTOP:
        cState = CHAR_SSTOP;
        break;
    case SACTIVE:
        cState = CHAR_SACTIVE;
        break;
    case SSWAP:
        cState = CHAR_SSWAP;
        break;
    }
    /*打包进程信息*/
    sprintf(pProcessState->szProName,"%s",pi.pi_comm);    
    pProcessState->lPid = pi.pi_pid;
    pProcessState->dPercentCpu = (pi.pi_utime+pi.pi_stime)*100.0/(userbuff.ncpus_cfg*(timeNow-pi.pi_start));
    pProcessState->lMemUsage = pi.pi_size*lPageSize/SIZE_1K;
    pProcessState->lVMemUsage = pi.pi_size*lPageSize/SIZE_1K;
    pProcessState->nPriority = pi.pi_ppri;
    pProcessState->dPercentMem = pi.pi_prm;
    pProcessState->lRss = (pi.pi_drss+pi.pi_trss)*lPageSize/SIZE_1K;
    //CGetTime::TimeToString((time_t*)&pi.pi_start,pProcessState->szStart,"-",":");
    pProcessState->lTime = pi.pi_utime+pi.pi_stime;
    pProcessState->cStat = cState;
    return ZX_SYS_NO_ERROR;
}
#endif
#ifdef HPUX
INT32 CProcessState::GetProcessState(const INT64 lPid, TProState* pProcessState)
{
    INT32 nRet = 0;
    char cState = CHAR_SNONE;
    pid_t pid = lPid;
    INT32 nState = 0;
    INT64 lPageSize = 0;

    struct pst_static pstatic;
    struct pst_status pi;

    if (pProcessState == NULL)
    {
        ZX_ERROR(LOG_NORMAL,"pProcessState == NULL");
        return -1;
    }

    nRet = pstat_getstatic(&pstatic, sizeof(pst_static), 1, 0);
    if (nRet == -1)
    {
        ZX_ERROR(LOG_NORMAL,"pstat_getstatic()");
        return -1;
    }

    nRet = pstat_getproc(&pi,sizeof(pst_status),0,pid);
        
    if (nRet!=-1 && pi.pst_pid == lPid)
    {
        nState = (INT32)pi.pst_stat;
        switch (nState)
        {
        case PS_OTHER:
        default:
            cState = CHAR_SNONE;
            break;
        case PS_IDLE:
            cState = CHAR_SIDL;
            break;
        case PS_ZOMBIE:
            cState = CHAR_SZOMB;
            break;
        case PS_STOP:
            cState = CHAR_SSTOP;
            break;
        case PS_RUN:
            cState = CHAR_SACTIVE;
            break;
        case PS_SLEEP:
            cState = CHAR_SSWAP;
            break;
        }
        /*打包消息*/
        sprintf(pProcessState->szProName,"%s",pi.pst_ucomm);    
        pProcessState->lPid = pi.pst_pid;
        pProcessState->dPercentCpu = pi.pst_pctcpu;
        pProcessState->lMemUsage = (pi.pst_dsize+pi.pst_ssize+pi.pst_tsize)*pstatic.page_size/SIZE_1K;
        pProcessState->lVMemUsage = (pi.pst_vssize+pi.pst_vtsize+pi.pst_vdsize)*pstatic.page_size/SIZE_1K;
        pProcessState->nPriority = pi.pst_pri;
        pProcessState->dPercentMem = pi.pst_rssize/(double)pstatic.pst_maxmem;
        pProcessState->lRss = pi.pst_rssize*pstatic.page_size/SIZE_1K;
        CGetTime::TimeToString(&(pi.pst_start),pProcessState->szStart,"-",":");
        pProcessState->lTime = pi.pst_utime+pi.pst_stime;
        pProcessState->cStat = cState;
    }
    else
    {
        ZX_ERROR(LOG_NORMAL,"pstat_getproc()");
        return -1;
    }
    return ZX_SYS_NO_ERROR;
}
#endif

#ifdef LINUX
INT32 CProcessState::GetProcessState(const INT64 lPid, TProState* pProcessState)
{
    INT32 nRet = ZX_SYS_NO_ERROR;
    char szCommand[SIZE_1K] = "";
    char szTemp[SIZE_1K] = "";
    char szbuf[SIZE_1K] = "";
    char *pszTemp = NULL;
    char *pszSearch = " ";
    INT32 nPos = 0;
    FILE *fp = NULL;
    sprintf(szCommand,"ps -eo comm,pid,pcpu,pmem,size,vsize,rss,s,pri,cputime,lstart|grep %"PRId64 ,lPid);
    //TL_DEBUG("%s\n",szCommand);
    if (pProcessState == NULL)
    {
        ZX_ERROR(LOG_NORMAL,"pProcessState == NULL");
        return -1;
    }
    fp = popen(szCommand,"r");
    if (fp == NULL)
    {
        ZX_ERROR(LOG_NORMAL,szCommand);
        pszSearch = NULL;
        pszTemp = NULL;
        fp = NULL;
        return -1;
    }
    if(fgets(szbuf,sizeof(szbuf),fp))
    {
        pszTemp = strtok(szbuf,pszSearch);
        if(pszTemp != NULL)
        {
            sprintf(pProcessState->szProName,"%s",pszTemp);
        }
        for(int i = 0;i<10; i++)
        {
            if (i == 9)
            {
                pszSearch = "\n";
            }
            pszTemp = strtok(NULL,pszSearch);
            if(pszTemp != NULL)
            {
                switch (i)
                {
                case 0:
                    pProcessState->lPid = atol(pszTemp);
                    break;
                case 1:
                    pProcessState->dPercentCpu = atof(pszTemp);
                    break;
                case 2:
                    pProcessState->dPercentMem = atof(pszTemp);
                    break;
                case 3:
                    pProcessState->lMemUsage = atol(pszTemp);
                    break;
                case 4:
                    pProcessState->lVMemUsage = atol(pszTemp);
                    break;
                case 5:
                    pProcessState->lRss = atol(pszTemp);
                    break;
                case 6:
                    pProcessState->cStat = pszTemp[0];                    
                    break;
                case 7:
                    pProcessState->nPriority = atoi(pszTemp);
                    break;
                case 8:
                    pProcessState->lTime = 0;
                    break;
                case 9:
                    sprintf(pProcessState->szStart,"%s",pszTemp);
                    break;
                default:
                    break;
                }
            }
        }        
    }
    pclose(fp);
    pszSearch = NULL;
    pszTemp = NULL;
    fp = NULL;
    return ZX_SYS_NO_ERROR;
}
#endif

 

posted on 2013-05-24 00:04  山本二十八  阅读(378)  评论(0编辑  收藏  举报

导航