Sysinfo()

From 탱이의 잡동사니
Revision as of 08:51, 25 April 2016 by Pchero (talk | contribs) (Created page with "== Overview == sysinfo() 함수 내용 설명 == Structure == === Until Linux-2.3.16 === <source lang=c> struct sysinfo { long uptime; /* Seconds since boot *...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

sysinfo() 함수 내용 설명

Structure

Until Linux-2.3.16

<source lang=c> struct sysinfo {

   long uptime;             /* Seconds since boot */
   unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
   unsigned long totalram;  /* Total usable main memory size */
   unsigned long freeram;   /* Available memory size */
   unsigned long sharedram; /* Amount of shared memory */
   unsigned long bufferram; /* Memory used by buffers */
   unsigned long totalswap; /* Total swap space size */
   unsigned long freeswap;  /* swap space still available */
   unsigned short procs;    /* Number of current processes */
   char _f[22];             /* Pads structure to 64 bytes */

}; </source>

Since Linux-2.3.23

<source lang=c> struct sysinfo {

   long uptime;             /* Seconds since boot */
   unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
   unsigned long totalram;  /* Total usable main memory size */
   unsigned long freeram;   /* Available memory size */
   unsigned long sharedram; /* Amount of shared memory */
   unsigned long bufferram; /* Memory used by buffers */
   unsigned long totalswap; /* Total swap space size */
   unsigned long freeswap;  /* swap space still available */
   unsigned short procs;    /* Number of current processes */
   unsigned long totalhigh; /* Total high memory size */
   unsigned long freehigh;  /* Available high memory size */
   unsigned int mem_unit;   /* Memory unit size in bytes */
   char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding to 64 bytes */

}; </source>

Process count

sysinfo() 함수를 이용하면 현재 프로세스의 갯수 정보를 확인할 수 있다. <source lang=c> unsigned short procs; /* Number of current processes */ </source> 얼핏보면 단순한 프로세스의 갯수 정보일 것 같지만, 사실은 프로세스가 아니라 쓰레드 갯수로 표현해야 함이 맞다. 실제로 프로그램을 작성해서 돌려보면 sysinfo() 로 확인된 프로세스의 갯수와 ps/top 으로 확인된 프로세스 갯수가 서로 맞지 않는 것을 확인할 수 있다. 쓰레드 갯수로 확인하면 갯수가 맞는 것을 확인할 수 있다.

전체 쓰레드 갯수는 아래의 명령어로 확인할 수 있다.

 $ grep -s '^Threads' /proc/[0-9]*/status | awk '{ sum += $2; } END { print sum; }' 

Sample

<source lang=c>

  1. include <stdio.h>
  2. include <sys/sysinfo.h>

int main(int argc, char** argv) {

   struct sysinfo info;
   
   sysinfo(&info);
   
   printf("uptime[%ld]\n",     info.uptime);
   printf("load_1[%lu]\n",     info.loads[0]);
   printf("load_5[%lu]\n",     info.loads[1]);
   printf("load_15[%lu]\n",    info.loads[2]);
   printf("total ram[%lu]\n",  info.totalram);
   printf("free ram[%lu]\n",   info.freeram);
   printf("shared ram[%lu]\n", info.sharedram);
   printf("buffer ram[%lu]\n", info.bufferram);
   printf("total swap[%lu]\n", info.totalswap);
   printf("free swap[%lu]\n",  info.freeswap);
   printf("procs[%d]\n",       info.procs);
   printf("total high[%lu]\n", info.totalhigh);
   printf("free high[%lu]\n",  info.freehigh);
   printf("free high[%lu]\n",  info.freehigh);
   printf("mem unit[%u]\n",   info.mem_unit);
   
   return 0;

} </source>