stressapptest源码剖析:默认参数和参数解析
warning:
这篇文章距离上次修改已过203天,其中的内容可能已经有所变动。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#define VERSION "1.0"
#define DEFAULT_DURATION 10
#define DEFAULT_DELAY 0
#define DEFAULT_CPU_STRENGTH 2
#define DEFAULT_IO_STRENGTH 1
#define DEFAULT_VM_STRENGTH 1
#define DEFAULT_VM_LINES 256
#define DEFAULT_FILE_SIZE 1024
#define DEFAULT_MEM_PATH "/dev/mem"
#define DEFAULT_MEM_OFFSET 0
#define DEFAULT_MEM_MASK 0
#define DEFAULT_HELP_MESSAGE 0
#define DEFAULT_VERBOSE 0
// 全局变量,用于保存解析后的参数值
int duration = DEFAULT_DURATION;
int delay = DEFAULT_DELAY;
int cpu_strength = DEFAULT_CPU_STRENGTH;
int io_strength = DEFAULT_IO_STRENGTH;
int vm_strength = DEFAULT_VM_STRENGTH;
int vm_lines = DEFAULT_VM_LINES;
int file_size = DEFAULT_FILE_SIZE;
char *mem_path = DEFAULT_MEM_PATH;
unsigned long mem_offset = DEFAULT_MEM_OFFSET;
unsigned long mem_mask = DEFAULT_MEM_MASK;
int help_message = DEFAULT_HELP_MESSAGE;
int verbose = DEFAULT_VERBOSE;
// 参数解析函数
void parse_args(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "d:i:c:m:l:f:s:p:o:a:u:vhV")) != -1) {
switch (opt) {
case 'd':
duration = atoi(optarg);
break;
case 'i':
delay = atoi(optarg);
break;
case 'c':
cpu_strength = atoi(optarg);
break;
case 'm':
io_strength = atoi(optarg);
break;
case 'l':
vm_strength = atoi(optarg);
break;
case 'f':
file_size = atoi(optarg);
break;
case 's':
vm_lines = atoi(optarg);
break;
case 'p':
mem_path = optarg;
break;
case 'o':
mem_offset = strtoul(optarg, NULL, 0);
break;
case 'a':
mem_mask = strtoul(optarg, NULL, 0);
break;
case 'u':
// 此处假设有一个函数parse_units用于解析时间单位
duration = parse_units(optarg);
break;
case 'v':
verbose = 1;
break;
case
评论已关闭