程序資源



程序需要某些資源(如 CPU 和記憶體)來執行任務。現在我們將深入瞭解相關的命令和系統呼叫,以瞭解有關資源利用和監控的資訊。此外,每個程序在資源方面都有預設的某些限制,如果需要,可以增強這些限制以適應應用程式的要求。

以下是使用命令獲取的基本系統或程序資源資訊:

top 命令

$ top

top 命令持續顯示系統資源的使用情況。如果任何程序使系統處於某種掛起狀態(消耗更多 CPU 或記憶體),則可以記下程序資訊並採取適當措施(例如終止相關程序)。

ps 命令

$ ps

ps 命令提供有關所有正在執行的程序的資訊。這有助於監控和控制程序。

vmstat 命令

$ vmstat

vmstat 命令報告虛擬記憶體子系統的統計資訊。它報告程序(等待執行、休眠、可執行程序等)、記憶體(虛擬記憶體資訊,如空閒、已用等)、交換區、IO 裝置、系統資訊(中斷次數、上下文切換次數)和 CPU(使用者、系統和空閒時間)的資訊。

lsof 命令

$ lsof

lsof 命令列印所有當前正在執行的程序(包括系統程序)的開啟檔案列表。

getconf 命令

$ getconf –a

getconf 命令顯示系統配置變數資訊。

現在,讓我們看一下相關的系統呼叫。

  • getrusage() 系統呼叫,提供系統資源使用情況的資訊。

  • 與訪問和設定資源限制相關的系統呼叫,例如 getrlimit()、setrlimit()、prlimit()。

系統資源使用呼叫

#include <sys/time.h>
#include <sys/resource.h>

int getrusage(int who, struct rusage *usage);

getrusage() 系統呼叫返回系統資源使用情況的資訊。這可以包括使用標誌 RUSAGE_SELF、RUSAGE_CHILDREN、RUSAGE_THREAD(用於“who”變數)獲取自身、子程序或呼叫執行緒的資訊。呼叫後,它在 rusage 結構中返回資訊。

此呼叫在成功時返回“0”,在失敗時返回“-1”。

讓我們看一下以下示例程式。

/* 檔名:sysinfo_getrusage.c */

#include<stdio.h>
#include<sys/time.h>
#include<sys/resource.h>

void main(void) {
   struct rusage res_usage;
   int retval;
   retval = getrusage(RUSAGE_SELF, &res_usage);
   if (retval == -1) {
      perror("getrusage error");
      return;
   }
   printf("Details of getrusage:\n");
   printf("User CPU time (seconds) is %d\n", (int)res_usage.ru_utime.tv_sec);
   printf("User CPU time (micro seconds) is %d\n", (int)res_usage.ru_utime.tv_usec);
   printf("Maximum size of resident set (kb) is %ld\n", res_usage.ru_maxrss);
   printf("Soft page faults (I/O not required) is %ld\n", res_usage.ru_minflt);
   printf("Hard page faults (I/O not required) is %ld\n", res_usage.ru_majflt);
   printf("Block input operations via file system is %ld\n", res_usage.ru_inblock);
   printf("Block output operations via file system is %ld\n", res_usage.ru_oublock);
   printf("Voluntary context switches are %ld\n", res_usage.ru_nvcsw);
   printf("Involuntary context switches are %ld\n", res_usage.ru_nivcsw);
   return;
}

編譯和執行步驟

Details of getrusage:
User CPU time (seconds) is 0
User CPU time (micro seconds) is 0
Maximum size of resident set (kb) is 364
Soft page faults (I/O not required) is 137
Hard page faults (I/O not required) is 0
Block input operations via file system is 0
Block output operations via file system is 0
Voluntary context switches are 0
Involuntary context switches are 1

現在讓我們看一下與訪問和設定資源限制相關的系統呼叫。

#include <sys/time.h>
#include <sys/resource.h>

int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);
int prlimit(pid_t pid, int resource, const struct rlimit *new_limit, struct rlimit *old_limit);

getrlimit() 系統呼叫透過輸入所需資源(例如 RLIMIT_NOFILE、RLIMIT_NPROC、RLIMIT_STACK 等)在 rlimit 結構中獲取資源限制。

setrlimit() 系統呼叫在限制範圍內設定 rlimit 結構中提到的資源限制。

prlimit() 系統呼叫用於各種目的,例如檢索當前資源限制或將資源限制更新為新值。

rlimit 結構包含兩個值:

  • 軟限制 - 當前限制

  • 硬限制 - 可以擴充套件到的最大限制。

RLIMIT_NOFILE - 返回此程序可以開啟的檔案描述符的最大數量。例如,如果它返回 1024,則程序具有檔案描述符 0 到 1023。

RLIMIT_NPROC - 可以為該程序的使用者建立的程序的最大數量。

RLIMIT_STACK - 該程序的棧段的最大大小(以位元組為單位)。

所有這些呼叫在成功時返回“0”,在失敗時返回“-1”。

讓我們考慮以下示例,其中我們使用 getrlimit() 系統呼叫。

/* 檔名:sysinfo_getrlimit.c */

#include<stdio.h>
#include<sys/time.h>
#include<sys/resource.h>

void main(void) {
   struct rlimit res_limit;
   int retval;
   int resources[] = {RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_STACK};
   int max_res;
   int counter = 0;
   printf("Details of resource limits for NOFILE, NPROC, STACK are as follows: \n");
   max_res = sizeof(resources)/sizeof(int);
   while (counter < max_res) {
      retval = getrlimit(resources[counter], &res_limit);
      if (retval == -1) {
         perror("getrlimit error");
         return;
      }
      printf("Soft Limit is %ld\n", res_limit.rlim_cur);
      printf("Hard Limit (ceiling) is %ld\n", res_limit.rlim_max);
      counter++;
   }
   return;
}

編譯和執行步驟

Details of resource limits for NOFILE, NPROC, STACK are as follows: 
Soft Limit is 516
Hard Limit (ceiling) is 516
Soft Limit is 256
Hard Limit (ceiling) is 256
Soft Limit is 33554432
Hard Limit (ceiling) is 33554432

讓我們考慮另一個使用 getrlimit() 系統呼叫的示例,但現在使用 prlimit() 系統呼叫。

/* 檔名:sysinfo_prlimit.c */

#include<stdio.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/resource.h>

void main(void) {
   struct rlimit res_limit;
   int retval;
   int resources[] = {RLIMIT_NOFILE, RLIMIT_NPROC, RLIMIT_STACK};
   int max_res;
   int counter = 0;
   printf("Details of resource limits for NOFILE, NPROC, STACK using prlimit are as follows: \n");
   max_res = sizeof(resources)/sizeof(int);
   while (counter < max_res) {
      retval = prlimit(getpid(), resources[counter], NULL, &res_limit);
      if (retval == -1) {
         perror("prlimit error");
         return;
      }
      printf("Soft Limit is %ld\n", res_limit.rlim_cur);
      printf("Hard Limit (ceiling) is %ld\n", res_limit.rlim_max);
      counter++;
   }
   return;
}

編譯和執行步驟

Details of resource limits for NOFILE, NPROC, STACK using prlimit are as follows: 
Soft Limit is 516
Hard Limit (ceiling) is 516
Soft Limit is 256
Hard Limit (ceiling) is 256
Soft Limit is 33554432
Hard Limit (ceiling) is 33554432
廣告